optimisation
This commit is contained in:
@@ -7,40 +7,30 @@ const int directions[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
|
|||||||
const uint32_t colors[2] = {BLACK, WHITE};
|
const uint32_t colors[2] = {BLACK, WHITE};
|
||||||
|
|
||||||
Board init_board(size_t width, size_t height, int random) {
|
Board init_board(size_t width, size_t height, int random) {
|
||||||
Board board = {NULL, width, height};
|
Board board = {NULL, NULL, width, height};
|
||||||
board.pixels = malloc(height * sizeof(uint32_t*));
|
board.pixels = malloc(width * height * sizeof(uint32_t));
|
||||||
|
board.next_pixels = malloc(width * height * sizeof(uint32_t));
|
||||||
for (size_t y = 0; y < height; y++) {
|
for (size_t y = 0; y < height; y++) {
|
||||||
board.pixels[y] = malloc(width * sizeof(uint32_t));
|
|
||||||
for (size_t x = 0; x < width; x++) {
|
for (size_t x = 0; x < width; x++) {
|
||||||
if (random)
|
if (random)
|
||||||
board.pixels[y][x] = colors[rand() % 2];
|
board.pixels[y * width + x] = colors[rand() % 2];
|
||||||
else
|
else
|
||||||
board.pixels[y][x] = BLACK;
|
board.pixels[y * width + x] = BLACK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return board;
|
return board;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_board(Board board) {
|
void free_board(Board board) {
|
||||||
for (size_t y = 0; y < board.height; y++)
|
|
||||||
free(board.pixels[y]);
|
|
||||||
free(board.pixels);
|
free(board.pixels);
|
||||||
}
|
free(board.next_pixels);
|
||||||
|
|
||||||
Board copy_board(Board board) {
|
|
||||||
Board copy = init_board(board.width, board.height, 0);
|
|
||||||
for (size_t x = 0; x < board.width; x++) {
|
|
||||||
for (size_t y = 0; y < board.height; y++) {
|
|
||||||
copy.pixels[y][x] = board.pixels[y][x];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return copy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void display_board(Board board) {
|
void display_board(Board board) {
|
||||||
for (size_t y = 0; y < board.height; y++) {
|
for (size_t y = 0; y < board.height; y++) {
|
||||||
for (size_t x = 0; x < board.width; x++) {
|
for (size_t x = 0; x < board.width; x++) {
|
||||||
printf("%c", board.pixels[y][x] == BLACK ? ' ' : 'X');
|
printf("%c",
|
||||||
|
board.pixels[y * board.width + x] == BLACK ? ' ' : 'X');
|
||||||
}
|
}
|
||||||
puts("");
|
puts("");
|
||||||
}
|
}
|
||||||
@@ -51,31 +41,35 @@ int count_adjacent(Board board, size_t x, size_t y) {
|
|||||||
int w, h;
|
int w, h;
|
||||||
uint8_t count = 0;
|
uint8_t count = 0;
|
||||||
for (i = 0; i < 8; i++) {
|
for (i = 0; i < 8; i++) {
|
||||||
w = (int)x + directions[i][0];
|
w = ((int)x + directions[i][0] + board.width) % board.width;
|
||||||
h = (int)y + directions[i][1];
|
h = ((int)y + directions[i][1] + board.height) % board.height;
|
||||||
if (!((w < 0 || w >= (int)board.width) ||
|
if (!((w < 0 || w >= (int)board.width) ||
|
||||||
(h < 0 || h >= (int)board.height)))
|
(h < 0 || h >= (int)board.height)))
|
||||||
if (board.pixels[h][w] == WHITE)
|
if (board.pixels[h * board.width + w] == WHITE)
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
Board process_game(Board board) {
|
Board process_game(Board board) {
|
||||||
Board new_board = copy_board(board);
|
|
||||||
size_t x, y;
|
size_t x, y;
|
||||||
uint8_t count = 0;
|
uint8_t count = 0;
|
||||||
for (y = 0; y < board.height; y++) {
|
for (y = 0; y < board.height; y++) {
|
||||||
for (x = 0; x < board.width; x++) {
|
for (x = 0; x < board.width; x++) {
|
||||||
count = count_adjacent(board, x, y);
|
count = count_adjacent(board, x, y);
|
||||||
if (board.pixels[y][x] == BLACK && count == 3) {
|
if (board.pixels[y * board.width + x] == BLACK && count == 3) {
|
||||||
new_board.pixels[y][x] = WHITE;
|
board.next_pixels[y * board.width + x] = WHITE;
|
||||||
} else if (board.pixels[y][x] == WHITE && count != 2 &&
|
} else if (board.pixels[y * board.width + x] == WHITE &&
|
||||||
count != 3) {
|
count != 2 && count != 3) {
|
||||||
new_board.pixels[y][x] = BLACK;
|
board.next_pixels[y * board.width + x] = BLACK;
|
||||||
|
} else {
|
||||||
|
board.next_pixels[y * board.width + x] =
|
||||||
|
board.pixels[y * board.width + x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free_board(board);
|
uint32_t* tmp = board.next_pixels;
|
||||||
return new_board;
|
board.next_pixels = board.pixels;
|
||||||
|
board.pixels = tmp;
|
||||||
|
return board;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
#define WHITE (0xFFFFFFFF)
|
#define WHITE (0xFFFFFFFF)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t **pixels;
|
uint32_t* pixels;
|
||||||
|
uint32_t* next_pixels;
|
||||||
size_t width;
|
size_t width;
|
||||||
size_t height;
|
size_t height;
|
||||||
} Board;
|
} Board;
|
||||||
|
|||||||
33
src/main.c
33
src/main.c
@@ -1,4 +1,5 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -39,7 +40,12 @@ int main(int argc, char* argv[]) {
|
|||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
if (flags.graphical) {
|
if (flags.graphical) {
|
||||||
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 0);
|
||||||
|
b.pixels[0 * SIM_WIDTH + 1] = WHITE;
|
||||||
|
b.pixels[1 * SIM_WIDTH + 2] = WHITE;
|
||||||
|
b.pixels[2 * SIM_WIDTH + 0] = WHITE;
|
||||||
|
b.pixels[2 * SIM_WIDTH + 1] = WHITE;
|
||||||
|
b.pixels[2 * SIM_WIDTH + 2] = WHITE;
|
||||||
|
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
|
||||||
@@ -53,23 +59,25 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
int running = 1;
|
int running = 1;
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
|
int pitch;
|
||||||
|
void* tex_pixels;
|
||||||
while (running) {
|
while (running) {
|
||||||
while (SDL_PollEvent(&e))
|
while (SDL_PollEvent(&e))
|
||||||
if (e.type == SDL_QUIT)
|
if (e.type == SDL_QUIT)
|
||||||
running = 0;
|
running = 0;
|
||||||
|
|
||||||
uint32_t flat[SIM_HEIGHT * SIM_WIDTH];
|
|
||||||
for (size_t y = 0; y < SIM_HEIGHT; y++)
|
|
||||||
for (size_t x = 0; x < SIM_WIDTH; x++)
|
|
||||||
flat[y * SIM_WIDTH + x] = b.pixels[y][x];
|
|
||||||
|
|
||||||
SDL_Rect dst = {0, 0, WIDTH, HEIGHT};
|
SDL_Rect dst = {0, 0, WIDTH, HEIGHT};
|
||||||
SDL_UpdateTexture(tex, NULL, flat, SIM_WIDTH * sizeof(uint32_t));
|
SDL_LockTexture(tex, NULL, &tex_pixels, &pitch);
|
||||||
SDL_RenderClear(ren);
|
memcpy(tex_pixels, b.pixels,
|
||||||
|
SIM_WIDTH * SIM_HEIGHT * sizeof(uint32_t));
|
||||||
|
SDL_UnlockTexture(tex);
|
||||||
|
// SDL_UpdateTexture(tex, NULL, b.pixels,SIM_WIDTH *
|
||||||
|
// sizeof(uint32_t));
|
||||||
|
// SDL_RenderClear(ren);
|
||||||
SDL_RenderCopy(ren, tex, NULL, &dst);
|
SDL_RenderCopy(ren, tex, NULL, &dst);
|
||||||
SDL_RenderPresent(ren);
|
SDL_RenderPresent(ren);
|
||||||
b = process_game(b);
|
b = process_game(b);
|
||||||
SDL_Delay(50);
|
// SDL_Delay(100);
|
||||||
}
|
}
|
||||||
SDL_DestroyTexture(tex);
|
SDL_DestroyTexture(tex);
|
||||||
SDL_DestroyRenderer(ren);
|
SDL_DestroyRenderer(ren);
|
||||||
@@ -78,12 +86,7 @@ int main(int argc, char* argv[]) {
|
|||||||
free_board(b);
|
free_board(b);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Board b = init_board(6, 6, 0);
|
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
||||||
b.pixels[0][1] = WHITE;
|
|
||||||
b.pixels[1][2] = WHITE;
|
|
||||||
b.pixels[2][0] = WHITE;
|
|
||||||
b.pixels[2][1] = WHITE;
|
|
||||||
b.pixels[2][2] = WHITE;
|
|
||||||
display_board(b);
|
display_board(b);
|
||||||
puts("----------");
|
puts("----------");
|
||||||
b = process_game(b);
|
b = process_game(b);
|
||||||
|
|||||||
Reference in New Issue
Block a user