add much-needed optimizations and enhance the snake example game
This commit is contained in:
@@ -65,6 +65,11 @@ engine_sdl_init_renderer(
|
||||
return RCODE_FAILURE;
|
||||
}
|
||||
|
||||
// enable VSync
|
||||
if (!SDL_SetRenderVSync(state->renderer, 1)) {
|
||||
eprintf("SDL_SetRenderVSync failed: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
return RCODE_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -127,9 +132,16 @@ engine_init(
|
||||
) {
|
||||
internal_state = (engine_internal_state_t *)
|
||||
malloc(sizeof(engine_internal_state_t));
|
||||
if (internal_state == NULL) {
|
||||
return RCODE_FAILURE;
|
||||
}
|
||||
|
||||
game_state = (game_state_t *)
|
||||
malloc(sizeof(game_state_t));
|
||||
if (game_state == NULL) {
|
||||
free(internal_state);
|
||||
return RCODE_FAILURE;
|
||||
}
|
||||
|
||||
internal_state->engine_poll_events
|
||||
= engine_poll_events;
|
||||
@@ -138,11 +150,22 @@ engine_init(
|
||||
= engine_update_framebuffer;
|
||||
|
||||
internal_state->sdl_state = engine_sdl_init();
|
||||
if (internal_state->sdl_state == NULL) {
|
||||
free(internal_state);
|
||||
free(game_state);
|
||||
return RCODE_FAILURE;
|
||||
}
|
||||
|
||||
game_state->is_running = true;
|
||||
game_state->framebuffer_dirty = true;
|
||||
game_state->dirty_rect = (SDL_Rect) {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.w = game_state->width,
|
||||
.h = game_state->height
|
||||
};
|
||||
game_state->width = internal_state->sdl_state->width;
|
||||
game_state->height = internal_state->sdl_state->height;
|
||||
|
||||
return (game_state == NULL || internal_state == NULL)
|
||||
? RCODE_FAILURE : RCODE_SUCCESS;
|
||||
return RCODE_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user