mirror of
https://github.com/fredrikr79/SnakeDL3.git
synced 2026-02-13 11:08:12 +01:00
feat: add dracula color macros
This commit is contained in:
119
src/main.c
119
src/main.c
@@ -7,77 +7,73 @@
|
|||||||
static SDL_Window *window = NULL;
|
static SDL_Window *window = NULL;
|
||||||
static SDL_Renderer *renderer = NULL;
|
static SDL_Renderer *renderer = NULL;
|
||||||
|
|
||||||
static int window_width = 640;
|
#define WINDOW_WIDTH 640
|
||||||
static int window_height = 480;
|
#define WINDOW_HEIGHT 480
|
||||||
static int grid_width = 32;
|
#define GRID_PADDING 2
|
||||||
static int grid_height = 24;
|
#define GRID_WIDTH 20
|
||||||
|
#define GRID_HEIGHT 20
|
||||||
|
|
||||||
static Uint64 last;
|
enum GRID_TILES { EMPTY, SNAKE, FRUIT };
|
||||||
|
enum SNAKE_PARTS { TAIL, BODY, HEAD };
|
||||||
|
|
||||||
enum TILES { EMPTY, SNAKE, FRUIT };
|
#define COLOR_SNAKE 80, 250, 123, SDL_ALPHA_OPAQUE
|
||||||
enum PARTS { TAIL, HEAD, BODY };
|
#define COLOR_FRUIT 255, 85, 85, SDL_ALPHA_OPAQUE
|
||||||
|
#define COLOR_EMPTY 40, 42, 54, SDL_ALPHA_OPAQUE
|
||||||
|
#define COLOR_BG 68, 71, 90, SDL_ALPHA_OPAQUE
|
||||||
|
|
||||||
typedef enum DIRECTION { LEFT, RIGHT, UP, DOWN } DIRECTION;
|
typedef enum DIRECTION { LEFT, RIGHT, UP, DOWN } DIRECTION;
|
||||||
|
|
||||||
|
typedef struct Snake {
|
||||||
|
DIRECTION direction;
|
||||||
|
int length;
|
||||||
|
} Snake;
|
||||||
|
|
||||||
typedef struct State {
|
typedef struct State {
|
||||||
int *grid;
|
int grid[GRID_HEIGHT * GRID_WIDTH];
|
||||||
DIRECTION player_face;
|
Snake *player;
|
||||||
SDL_FRect *r;
|
|
||||||
} State;
|
} State;
|
||||||
|
|
||||||
|
#define SET_GRID_AT(grid, row, col, value) grid[row * GRID_WIDTH + col] = value
|
||||||
|
#define GET_GRID_AT(grid, row, col) grid[row * GRID_WIDTH + col]
|
||||||
|
|
||||||
void log_game_grid(int *game_grid) {
|
void log_game_grid(int *game_grid) {
|
||||||
char *buf = SDL_malloc(sizeof(char) * grid_width * 2);
|
char buf[GRID_WIDTH * 2];
|
||||||
for (int i = 0; i < grid_height; i++) {
|
for (int i = 0; i < GRID_HEIGHT; i++) {
|
||||||
for (int j = 0; j < grid_width; j++) {
|
for (int j = 0; j < GRID_WIDTH; j++) {
|
||||||
buf[2 * j] = '0' + game_grid[i * grid_width + j];
|
buf[2 * j] = '0' + GET_GRID_AT(game_grid, i, j);
|
||||||
buf[2 * j + 1] = ' ';
|
buf[2 * j + 1] = ' ';
|
||||||
}
|
}
|
||||||
buf[2 * grid_width] = '\0';
|
buf[2 * GRID_WIDTH - 1] = '\0';
|
||||||
SDL_Log("%s", buf);
|
SDL_Log("%s", buf);
|
||||||
}
|
}
|
||||||
SDL_free(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_grid_at(int *grid, int row, int col, int value) { grid[row * grid_width + col] = value; }
|
|
||||||
|
|
||||||
int get_grid_at(int *grid, int row, int col) { return grid[row * grid_width + col]; }
|
|
||||||
|
|
||||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
|
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
|
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
|
||||||
return SDL_APP_FAILURE;
|
return SDL_APP_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/clear", window_width, window_height, 0,
|
if (!SDL_CreateWindowAndRenderer("SnakeDL3", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window,
|
||||||
&window, &renderer)) {
|
&renderer)) {
|
||||||
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
|
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
|
||||||
return SDL_APP_FAILURE;
|
return SDL_APP_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int *game_grid = SDL_malloc(sizeof(int) * grid_width * grid_height);
|
|
||||||
for (int i = 0; i < grid_height; i++) {
|
|
||||||
for (int j = 0; j < grid_width; j++) {
|
|
||||||
set_grid_at(game_grid, i, j, EMPTY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
set_grid_at(game_grid, 12, 16, SNAKE);
|
|
||||||
set_grid_at(game_grid, 4, 12, FRUIT);
|
|
||||||
set_grid_at(game_grid, 18, 5, FRUIT);
|
|
||||||
|
|
||||||
SDL_FRect *rect2 = SDL_malloc(sizeof(SDL_FRect));
|
|
||||||
rect2->y = 240;
|
|
||||||
rect2->x = 0;
|
|
||||||
rect2->w = 100;
|
|
||||||
rect2->h = 100;
|
|
||||||
|
|
||||||
State *state = SDL_malloc(sizeof(State));
|
State *state = SDL_malloc(sizeof(State));
|
||||||
state->grid = game_grid;
|
if (!state) {
|
||||||
state->player_face = UP;
|
SDL_Log("Failed to allocate state");
|
||||||
state->r = rect2;
|
return SDL_APP_FAILURE;
|
||||||
*appstate = state;
|
}
|
||||||
|
SDL_memset(state->grid, EMPTY, sizeof(state->grid));
|
||||||
|
|
||||||
last = SDL_GetTicksNS();
|
SET_GRID_AT(state->grid, GRID_WIDTH / 2, GRID_HEIGHT / 2, SNAKE);
|
||||||
|
SET_GRID_AT(state->grid, 4, 12, FRUIT);
|
||||||
|
SET_GRID_AT(state->grid, 18, 5, FRUIT);
|
||||||
|
|
||||||
|
log_game_grid(state->grid);
|
||||||
|
|
||||||
|
*appstate = state;
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
@@ -93,47 +89,40 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
|||||||
SDL_AppResult SDL_AppIterate(void *appstate) {
|
SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||||
State *stateptr = appstate;
|
State *stateptr = appstate;
|
||||||
int *game_grid = stateptr->grid;
|
int *game_grid = stateptr->grid;
|
||||||
Uint64 delta = SDL_GetTicksNS() - last;
|
|
||||||
double duration = (double)delta / (FPS * 1000);
|
|
||||||
|
|
||||||
SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
SDL_SetRenderDrawColor(renderer, COLOR_BG);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
SDL_FRect rect;
|
SDL_FRect rect;
|
||||||
rect.w = (float)window_width / grid_width;
|
rect.w = (float)(WINDOW_WIDTH - 2 * GRID_PADDING) / GRID_WIDTH;
|
||||||
rect.h = (float)window_height / grid_height;
|
rect.h = (float)(WINDOW_HEIGHT - 2 * GRID_PADDING) / GRID_HEIGHT;
|
||||||
for (int i = 0; i < grid_height; i++) {
|
for (int i = 0; i < GRID_HEIGHT; i++) {
|
||||||
for (int j = 0; j < grid_width; j++) {
|
for (int j = 0; j < GRID_WIDTH; j++) {
|
||||||
switch (get_grid_at(game_grid, i, j)) {
|
switch (GET_GRID_AT(game_grid, i, j)) {
|
||||||
case EMPTY:
|
case EMPTY:
|
||||||
SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
SDL_SetRenderDrawColor(renderer, COLOR_EMPTY);
|
||||||
break;
|
break;
|
||||||
case SNAKE:
|
case SNAKE:
|
||||||
SDL_SetRenderDrawColorFloat(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE_FLOAT);
|
SDL_SetRenderDrawColor(renderer, COLOR_SNAKE);
|
||||||
break;
|
break;
|
||||||
case FRUIT:
|
case FRUIT:
|
||||||
SDL_SetRenderDrawColorFloat(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
SDL_SetRenderDrawColor(renderer, COLOR_FRUIT);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
rect.x = j * rect.w;
|
rect.x = j * rect.w + GRID_PADDING;
|
||||||
rect.y = i * rect.h;
|
rect.y = i * rect.h + GRID_PADDING;
|
||||||
|
// SDL_Log("x: %f, y: %f, w: %f, h: %f", rect.x, rect.y, rect.w, rect.h);
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// SDL_Log("---");
|
||||||
SDL_FRect *rect2 = stateptr->r;
|
|
||||||
rect2->x += 5 * (double)delta / 1000000;
|
|
||||||
SDL_Log("%f %f %f %f", rect2->x, rect2->y, rect2->w, rect2->h);
|
|
||||||
SDL_SetRenderDrawColorFloat(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
|
||||||
SDL_RenderFillRect(renderer, rect2);
|
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
last = SDL_GetTicksNS();
|
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||||
/* SDL will clean up the window/renderer for us. */
|
/* SDL will clean up the window/renderer for us. */
|
||||||
|
SDL_free(appstate);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user