diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-08-27 17:24:45 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-08-27 17:24:45 +0200 |
| commit | 7c48d434f0a68ac47ebe1bd66daa2c86842979c7 (patch) | |
| tree | 9cf10fa6fe88a68038eebd5ff09cc550a7fb15b7 | |
| parent | 60adb6e2f051ab72fb66541a8f48ef195317d403 (diff) | |
| download | mandelbrot_cpu-7c48d434f0a68ac47ebe1bd66daa2c86842979c7.tar.gz mandelbrot_cpu-7c48d434f0a68ac47ebe1bd66daa2c86842979c7.tar.bz2 mandelbrot_cpu-7c48d434f0a68ac47ebe1bd66daa2c86842979c7.zip | |
SDL boilerplate window
init, quit and run functions.
Error handling.
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | graphics.c | 80 | ||||
| -rw-r--r-- | header.h | 25 | ||||
| -rw-r--r-- | main.c | 9 |
4 files changed, 112 insertions, 4 deletions
@@ -1,7 +1,7 @@ NAME = mandel CC = gcc CCFLAGS = -Wall -Wextra -LDFLAGS = -lm # $(shell sdl2-config --libs --cflags) +LDFLAGS = -lm $(shell sdl2-config --libs --cflags) HEADER = header.h SRC = main.c graphics.c mandelbrot.c @@ -0,0 +1,80 @@ +#include "header.h" + +#define WINDOW_TITLE "Mandelbrot" +#define WINDOW_X 20 +#define WINDOW_Y 20 + +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(GConf *conf) +{ + 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, + conf->window_width, conf->window_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); + + SDL_Delay(10); + } +} + +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); +} @@ -1,9 +1,30 @@ #ifndef HEADER_H # define HEADER_H -#include <complex.h> +# include <stdbool.h> +# include <complex.h> +# include <SDL2/SDL.h> -double mandelbrot_in_set(double complex c); +typedef struct +{ + SDL_Window *window; + SDL_Renderer *renderer; + bool running; +} GState; + +typedef struct +{ + int window_width; + int window_height; +} GConf; + +// mandelbrot.c +double mandelbrot_in_set(double _Complex c); void mandelbrot_print(void); +// graphics.c +GState *graphics_init(GConf *conf); +void graphics_quit(GState *state); +void graphics_run(GState *state); + #endif @@ -2,6 +2,13 @@ int main(void) { - mandelbrot_print(); + /* mandelbrot_print(); */ + GConf gconf = { + .window_width = 300, + .window_height = 300 + }; + GState *gstate = graphics_init(&gconf); + graphics_run(gstate); + graphics_quit(gstate); return 0; } |
