mirror of
https://github.com/fredrikr79/SnakeDL3.git
synced 2026-03-28 13:14:02 +01:00
style: format in 2 spaces instead
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# Common settings
|
# Common settings
|
||||||
BasedOnStyle: llvm # WebKit
|
BasedOnStyle: llvm # WebKit
|
||||||
TabWidth: 4
|
TabWidth: 2
|
||||||
IndentWidth: 4
|
IndentWidth: 2
|
||||||
UseTab: Never
|
UseTab: Never
|
||||||
ColumnLimit: 80
|
ColumnLimit: 100
|
||||||
|
|||||||
183
src/main.c
183
src/main.c
@@ -7,140 +7,133 @@
|
|||||||
static SDL_Window *window = NULL;
|
static SDL_Window *window = NULL;
|
||||||
static SDL_Renderer *renderer = NULL;
|
static SDL_Renderer *renderer = NULL;
|
||||||
|
|
||||||
static int width = 640;
|
static int window_width = 640;
|
||||||
static int height = 480;
|
static int window_height = 480;
|
||||||
static int grid_width = 32;
|
static int grid_width = 32;
|
||||||
static int grid_height = 24;
|
static int grid_height = 24;
|
||||||
|
|
||||||
static Uint64 last;
|
static Uint64 last;
|
||||||
|
|
||||||
enum TILES { EMPTY, SNAKE, FRUIT };
|
enum TILES { EMPTY, SNAKE, FRUIT };
|
||||||
|
enum PARTS { TAIL, HEAD, BODY };
|
||||||
|
|
||||||
typedef enum DIRECTION { LEFT, RIGHT, UP, DOWN } DIRECTION;
|
typedef enum DIRECTION { LEFT, RIGHT, UP, DOWN } DIRECTION;
|
||||||
|
|
||||||
typedef struct State {
|
typedef struct State {
|
||||||
int *grid;
|
int *grid;
|
||||||
DIRECTION player_face;
|
DIRECTION player_face;
|
||||||
SDL_FRect *r;
|
SDL_FRect *r;
|
||||||
} State;
|
} State;
|
||||||
|
|
||||||
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 = SDL_malloc(sizeof(char) * 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' + game_grid[i * grid_width + j];
|
||||||
buf[2 * j + 1] = ' ';
|
buf[2 * j + 1] = ' ';
|
||||||
}
|
|
||||||
buf[2 * grid_width] = '\0';
|
|
||||||
SDL_Log("%s", buf);
|
|
||||||
}
|
}
|
||||||
SDL_free(buf);
|
buf[2 * grid_width] = '\0';
|
||||||
|
SDL_Log("%s", buf);
|
||||||
|
}
|
||||||
|
SDL_free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_grid_at(int *grid, int row, int col, int value) {
|
void set_grid_at(int *grid, int row, int col, int value) { grid[row * grid_width + col] = value; }
|
||||||
grid[row * grid_width + col] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_grid_at(int *grid, int row, int col) {
|
int get_grid_at(int *grid, int row, int col) { return grid[row * grid_width + 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,
|
||||||
|
&window, &renderer)) {
|
||||||
|
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/clear", width, height,
|
set_grid_at(game_grid, 12, 16, SNAKE);
|
||||||
0, &window, &renderer)) {
|
set_grid_at(game_grid, 4, 12, FRUIT);
|
||||||
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
|
set_grid_at(game_grid, 18, 5, FRUIT);
|
||||||
return SDL_APP_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
int *game_grid = SDL_malloc(sizeof(int) * grid_width * grid_height);
|
SDL_FRect *rect2 = SDL_malloc(sizeof(SDL_FRect));
|
||||||
for (int i = 0; i < grid_height; i++) {
|
rect2->y = 240;
|
||||||
for (int j = 0; j < grid_width; j++) {
|
rect2->x = 0;
|
||||||
set_grid_at(game_grid, i, j, EMPTY);
|
rect2->w = 100;
|
||||||
}
|
rect2->h = 100;
|
||||||
}
|
|
||||||
|
|
||||||
set_grid_at(game_grid, 12, 16, SNAKE);
|
State *state = SDL_malloc(sizeof(State));
|
||||||
set_grid_at(game_grid, 4, 12, FRUIT);
|
state->grid = game_grid;
|
||||||
set_grid_at(game_grid, 18, 5, FRUIT);
|
state->player_face = UP;
|
||||||
|
state->r = rect2;
|
||||||
|
*appstate = state;
|
||||||
|
|
||||||
SDL_FRect *rect2 = SDL_malloc(sizeof(SDL_FRect));
|
last = SDL_GetTicksNS();
|
||||||
rect2->y = 240;
|
|
||||||
rect2->x = 0;
|
|
||||||
rect2->w = 100;
|
|
||||||
rect2->h = 100;
|
|
||||||
|
|
||||||
State *state = SDL_malloc(sizeof(State));
|
return SDL_APP_CONTINUE;
|
||||||
state->grid = game_grid;
|
|
||||||
state->player_face = UP;
|
|
||||||
state->r = rect2;
|
|
||||||
*appstate = state;
|
|
||||||
|
|
||||||
last = SDL_GetTicksNS();
|
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
||||||
if ((event->type == SDL_EVENT_KEY_UP &&
|
if ((event->type == SDL_EVENT_KEY_UP && event->key.scancode == SDL_SCANCODE_ESCAPE) ||
|
||||||
event->key.scancode == SDL_SCANCODE_ESCAPE) ||
|
event->type == SDL_EVENT_QUIT) {
|
||||||
event->type == SDL_EVENT_QUIT) {
|
return SDL_APP_SUCCESS;
|
||||||
return SDL_APP_SUCCESS;
|
}
|
||||||
}
|
return SDL_APP_CONTINUE;
|
||||||
return SDL_APP_CONTINUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
Uint64 delta = SDL_GetTicksNS() - last;
|
||||||
double duration = (double)delta / (FPS * 1000);
|
double duration = (double)delta / (FPS * 1000);
|
||||||
|
|
||||||
SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
SDL_FRect rect;
|
SDL_FRect rect;
|
||||||
rect.w = (float)width / grid_width;
|
rect.w = (float)window_width / grid_width;
|
||||||
rect.h = (float)height / grid_height;
|
rect.h = (float)window_height / 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_SetRenderDrawColorFloat(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
||||||
SDL_ALPHA_OPAQUE_FLOAT);
|
break;
|
||||||
break;
|
case SNAKE:
|
||||||
case SNAKE:
|
SDL_SetRenderDrawColorFloat(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE_FLOAT);
|
||||||
SDL_SetRenderDrawColorFloat(renderer, 255, 255, 255,
|
break;
|
||||||
SDL_ALPHA_OPAQUE_FLOAT);
|
case FRUIT:
|
||||||
break;
|
SDL_SetRenderDrawColorFloat(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
||||||
case FRUIT:
|
break;
|
||||||
SDL_SetRenderDrawColorFloat(renderer, 255, 0, 0,
|
}
|
||||||
SDL_ALPHA_OPAQUE_FLOAT);
|
rect.x = j * rect.w;
|
||||||
break;
|
rect.y = i * rect.h;
|
||||||
}
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
rect.x = j * rect.w;
|
|
||||||
rect.y = i * rect.h;
|
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SDL_FRect *rect2 = stateptr->r;
|
SDL_FRect *rect2 = stateptr->r;
|
||||||
rect2->x += 5 * (double)delta / 1000000;
|
rect2->x += 5 * (double)delta / 1000000;
|
||||||
SDL_Log("%f %f %f %f", rect2->x, rect2->y, rect2->w, rect2->h);
|
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_SetRenderDrawColorFloat(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE_FLOAT);
|
||||||
SDL_RenderFillRect(renderer, rect2);
|
SDL_RenderFillRect(renderer, rect2);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
last = SDL_GetTicksNS();
|
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. */
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user