diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 90 |
1 files changed, 90 insertions, 0 deletions
@@ -0,0 +1,90 @@ +#include <SDL2/SDL.h> +#include <SDL2/SDL_render.h> +#include <SDL2/SDL_video.h> +#include <SDL2/SDL_image.h> +#include "microui.h" +#include "ui.h" +#include "font.h" +#include "text_size.h" +#include "render.h" + +SDL_Window *window = NULL; +SDL_Renderer *renderer = NULL; +mu_Context *mu_ctx = NULL; + +static int init(void); +static void deinit(void); + +int main(int argc, char **argv) +{ + if (init()) { + deinit(); + return 1; + } + + for (;;) { + SDL_Event e; + while (SDL_PollEvent(&e)) switch (e.type) { + case SDL_QUIT: + return 0; + default: + break; + } + + r_draw_set_color(0, 0, 0, 1); + SDL_RenderClear(renderer); + ui_draw(); + SDL_RenderPresent(renderer); + } + return 0; +} + +static int init(void) +{ + if (SDL_Init(SDL_INIT_VIDEO)) { + fprintf(stderr, "%s\n", SDL_GetError()); + return 1; + } + if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) { + fprintf(stderr, "%s\n", IMG_GetError()); + return 1; + } + if (SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_RESIZABLE, + &window, &renderer)) { + fprintf(stderr, "%s\n", SDL_GetError()); + return 1; + } + if (font_init("res/font.png")) + return 1; + if ((mu_ctx = malloc(sizeof(mu_Context))) == NULL) { + perror("init"); + return 1; + } + mu_init(mu_ctx); + mu_ctx->text_width = text_width; + mu_ctx->text_height = text_height; + if (atexit(deinit)) { + perror("init"); + return 1; + } + return 0; +} + +static void deinit(void) +{ + if (mu_ctx != NULL) { + free(mu_ctx); + mu_ctx = NULL; + } + font_deinit(); + if (renderer != NULL) { + SDL_DestroyRenderer(renderer); + renderer = NULL; + } + if (window != NULL) { + SDL_DestroyWindow(window); + window = NULL; + } + IMG_Quit(); + SDL_Quit(); +} |