123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <SDL2/SDL.h>
- typedef struct {
- SDL_Window *win;
- SDL_Renderer *ren;
- SDL_Event *ev;
- bool is_initialized;
- int flags;
- bool running;
- } env;
- typedef struct {
- int x;
- int y;
- } vec2d;
- bool init_env(
- env *env,
- int sdl_flags,
- char* win_name,
- vec2d win_size,
- int renderer_flags)
- {
- if (env->is_initialized)
- return false;
- SDL_Init(sdl_flags);
- env->flags = sdl_flags;
- env->win = SDL_CreateWindow(win_name,
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- win_size.x,
- win_size.y,
- SDL_WINDOW_OPENGL);
- if (!env->win) {
- fprintf(stderr, "could not create window: %s\n", SDL_GetError());
- return false;
- }
- env->ren = SDL_CreateRenderer(env->win,
- -1,
- renderer_flags);
-
- if (!env->win) {
- fprintf(stderr, "could not create renderer: %s\n", SDL_GetError());
- return false;
- }
- env->running = false;
- return true;
- }
- void process_event(env *env){
- switch(env->ev->type)
- {
- case SDL_QUIT:
- env->running = false;
- break;
- }
- }
- void run(env *env)
- {
- if (!env->is_initialized) {
- fprintf(stderr, "event not initialized.");
- return;
- }
- env->running = true;
- while (env->running) {
- while(SDL_PollEvent(env->ev))
- process_event(env);
- }
- }
- void destroy_env(env *env)
- {
- env->is_initialized = false;
- SDL_DestroyRenderer(env->ren);
- SDL_DestroyWindow(env->win);
- env->flags = 0;
- }
- int main(int argc, char** argv)
- {
- env e;
- e.is_initialized = false;
- vec2d win_size = { .x = 800, .y = 600 };
- if (init_env(&e,
- SDL_INIT_VIDEO,
- "Wander",
- win_size,
- SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)){
- fprintf(stdout, "cannot initialize, exiting\n");
- return 1;
- }
-
- run(&e);
- destroy_env(&e);
- }
|