Last active
September 11, 2023 13:15
-
-
Save cacharle/12627a0cb18f89bdb3e03f089ba9943e to your computer and use it in GitHub Desktop.
SDL boilerplate files for basic application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "sdl_boilerplate.h" | |
int main(void) | |
{ | |
GState *gstate = graphics_init(400, 400); | |
graphics_run(gstate); | |
graphics_quit(gstate); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdbool.h> | |
#include <SDL2/SDL.h> | |
#define WINDOW_TITLE "Title" | |
#define WINDOW_X 20 | |
#define WINDOW_Y 20 | |
static void update(GState *state); | |
static void event_handler(GState *state); | |
static void destroy_state(GState *state); | |
static void error_exit_state(GState *state, const char *msg); | |
static void error_exit(const char *msg); | |
GState *graphics_init(int width, int height) | |
{ | |
GState *state = (GState*)malloc(sizeof(GState)); | |
if (state == NULL) | |
return NULL; | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) | |
error_exit("unable to init SDL"); | |
state->window = SDL_CreateWindow(WINDOW_TITLE, WINDOW_X, WINDOW_Y, | |
width, height, 0); | |
if (state->window == NULL) | |
error_exit("unable to create window"); | |
state->renderer = SDL_CreateRenderer(state->window, -1, 0); | |
if (state->renderer == NULL) | |
error_exit_state(state, "unable to create renderer"); | |
state->running = true; | |
return state; | |
} | |
void graphics_quit(GState *state) | |
{ | |
destroy_state(state); | |
SDL_Quit(); | |
} | |
void graphics_run(GState *state) | |
{ | |
while (state->running) | |
{ | |
event_handler(state); | |
update(state); | |
SDL_Delay(10); | |
} | |
} | |
static void update(GState *state) | |
{ | |
// do stuff | |
} | |
static void event_handler(GState *state) | |
{ | |
SDL_Event e; | |
while (SDL_PollEvent(&e)) | |
{ | |
switch (e.type) | |
{ | |
case SDL_QUIT: | |
state->running = false; | |
break; | |
} | |
} | |
} | |
static void destroy_state(GState *state) | |
{ | |
if (state == NULL) | |
return; | |
SDL_DestroyRenderer(state->renderer); | |
SDL_DestroyWindow(state->window); | |
free(state); | |
} | |
static void error_exit_state(GState *state, const char *msg) | |
{ | |
destroy_state(state); | |
error_exit(msg); | |
} | |
static void error_exit(const char *msg) | |
{ | |
SDL_Log("ERROR: %s: %s", SDL_GetError(), msg); | |
SDL_Quit(); | |
exit(EXIT_FAILURE); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HEADER_H | |
# define HEADER_H | |
# include <stdbool.h> | |
# include <SDL2/SDL.h> | |
typedef struct | |
{ | |
SDL_Window *window; | |
SDL_Renderer *renderer; | |
bool running; | |
} GState; | |
// protitypes | |
GState *graphics_init(int width, int height); | |
void graphics_quit(GState *state); | |
void graphics_run(GState *state); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment