#include "lib.h" static int actual_tile_size, actual_grid_height, actual_grid_width; static bool was_init = false; static SDL_Window * window; static SDL_Surface * win_surface; static SDL_Surface * grid_sfc; void init(void) { if (was_init) { fprintf(stderr, "Cannot re-initialize.\n"); abort(); // Life is tough, you do not deserve to handle this. // Actually, crashing is the better behaviour in this caseā€¦ } was_init = true; actual_tile_size = TILE_SIZE; actual_grid_height = GRID_HEIGHT; actual_grid_width = GRID_WIDTH; if (SDL_Init( SDL_INIT_VIDEO ) < 0) { fprintf(stderr, "SDL_Init error: %s\n", SDL_GetError()); abort(); } grid = malloc(actual_grid_width * sizeof(color_t *)); for (int i = 0; i < actual_grid_width; i++) { grid[i] = malloc(actual_grid_height * sizeof(color_t)); for (int j = 0; j < actual_grid_height; j++) grid[i][j] = (color_t) {0,0,0}; } // TODO: Maybe add margins? int winh = actual_tile_size * actual_grid_height; int winw = actual_tile_size * actual_grid_width; window = SDL_CreateWindow("Snake?", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, winw, winh, SDL_WINDOW_SHOWN); if (window == NULL) { fprintf(stderr, "Window broken: %s\n", SDL_GetError()); abort(); } win_surface = SDL_GetWindowSurface(window); grid_sfc = SDL_CreateRGBSurface(0, winw, winh, 24, 0,0,0,0); // Ensure something is shown. redraw(); } void redraw(void) { for (int x = 0; x < actual_grid_width; x++) { for (int y = 0; y < actual_grid_height; y++) { color_t c = grid[x][y]; uint32_t color = (c.r << 24) + (c.g << 16) + (c.b << 8) + 0xFF /* alpha */; SDL_Rect rect = {.x = x * actual_tile_size, .y = y * actual_tile_size, .h = actual_tile_size, .w = actual_tile_size}; SDL_FillRect(grid_sfc, &rect, color); } } } char ** getkeys(int count) { } /* The simple stuff */ void grid_set(int x, int y, color_t c) { // FIXME: bounds checks! grid[x][y] = c; } color_t grid_get(int x, int y) { return grid[x][y]; } void wait(int msec) { SDL_Delay(msec); }