diff options
| -rw-r--r-- | main.c | 2 | ||||
| -rw-r--r-- | renderer.c | 58 | ||||
| -rw-r--r-- | renderer.h | 3 | ||||
| -rw-r--r-- | ui.c | 23 | ||||
| -rw-r--r-- | ui.h | 2 |
5 files changed, 36 insertions, 52 deletions
@@ -4,8 +4,6 @@ #include <stdio.h> #include <stdlib.h> -#include <SDL2/SDL.h> - #include "microui.h" #include "renderer.h" #include "unit.h" @@ -4,6 +4,7 @@ #include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> +#include <SDL2/SDL_image.h> #include "microui.h" #include "renderer.h" @@ -15,6 +16,8 @@ enum window { RENDERFLAGS = SDL_RENDERER_PRESENTVSYNC, }; +enum { ICONLIST_SIZE = 32 }; + static const char FONT[] = "font/P052-Roman.ttf"; enum font { FONTSIZE = 14, }; @@ -37,6 +40,9 @@ static const char key_map[256] = { [SDLK_BACKSPACE & 0xff] = MU_KEY_BACKSPACE, }; +mu_stack(SDL_Surface *, ICONLIST_SIZE) icon_list; + + static void print_info(void); static int text_width(mu_Font mufont, const char *str, int len); static int text_height(mu_Font mufont); @@ -54,6 +60,8 @@ static SDL_Renderer *renderer = NULL; /* Initialize the window and renderer. Returns non-zero on error. */ int r_init(mu_Context *ctx, const char *title) { + icon_list.idx = 0; + if (SDL_Init(SDL_INIT_VIDEO) != 0) { fprintf(stderr, "%s\n", SDL_GetError()); return 1; @@ -178,30 +186,6 @@ r_render(mu_Context *ctx) { SDL_RenderPresent(renderer); } -/* Render an image. Returns non-zero on error. */ -int -r_image(SDL_Surface *img, mu_Rect r) { - SDL_Texture *texture; - SDL_Rect rect; - - texture = SDL_CreateTextureFromSurface(renderer, img); - if (!texture) { - fprintf(stderr, "%s\n", SDL_GetError()); - return 1; - } - - rect = (SDL_Rect) {r.x, r.y, r.w, r.h}; - if (SDL_RenderCopy(renderer, texture, NULL, &rect) != 0) { - fprintf(stderr, "%s\n", SDL_GetError()); - SDL_DestroyTexture(texture); - return 1; - } - - SDL_DestroyTexture(texture); - - return 0; -} - static void clear(void) { if (SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, bg.a) != 0) { @@ -282,3 +266,29 @@ r_get_window_size(int *w, int*h) { fprintf(stderr, "%s\n", SDL_GetError()); } } + +int +r_push_icon(const char *path) { + SDL_Surface **p; + + if (icon_list.idx >= ICONLIST_SIZE) { + fprintf(stderr, "icon list overflow\n"); + return 1; + } + p = icon_list.items + icon_list.idx; + *p = IMG_Load(path); + if (*p == NULL) { + fprintf(stderr, "failed to load %s: %s\n", path, SDL_GetError()); + return 1; + } + icon_list.idx++; + return 0; +} + +void +r_pop_icon(void) { + if (icon_list.idx >= 0) { + return; + } + SDL_FreeSurface(icon_list.items[--icon_list.idx]); +} @@ -2,4 +2,5 @@ int r_init(mu_Context *ctx, const char *title); void r_input(mu_Context *ctx); void r_render(mu_Context *ctx); void r_get_window_size(int *w, int *h); -int r_image(SDL_Surface *img, mu_Rect r); +int r_push_icon(const char *path); +void r_pop_icon(void); @@ -4,9 +4,6 @@ #include <stdlib.h> #include <string.h> -#include <SDL2/SDL.h> -#include <SDL2/SDL_image.h> - #include "microui.h" #include "unit.h" #include "compressor.h" @@ -43,7 +40,6 @@ static void init_volume_flow_rate(UI *ui); static void init_mass_flow_rate(UI *ui); static void init_mass_flow_rate_corrected(UI *ui); static int init_comps(UI *ui); -static int init_comp_img(UI *ui); static void compute_pressure_ratio(UI *ui, int idx); static void compute_comp_outlet_temperature(UI *ui, int idx); static void compute_manifold_temperature(UI *ui, int idx); @@ -80,11 +76,6 @@ init_ui(UI *ui) { return 1; } - if (init_comp_img(ui) != 0) { - free_ui(ui); - return 1; - } - compute(ui, 0); return 0; @@ -94,7 +85,6 @@ void free_ui(UI *ui) { w_free_select_compressor(&ui->comp_select); free(ui->comps); - SDL_FreeSurface(ui->comp_img); } static void @@ -254,19 +244,6 @@ init_comps(UI *ui) { return 0; } -static int -init_comp_img(UI *ui) { - const Compressor *comp; - - comp = &ui->comps[ui->comp_select.idx]; - ui->comp_img = IMG_Load(comp->imgfile); - if (ui->comp_img == NULL) { - weprintf("failed to load %s", comp->imgfile); - return 1; - } - return 0; -} - void set_displacement(UI *ui) { int idx, i; @@ -47,8 +47,6 @@ typedef struct { Compressor *comps; int ncomps; w_Select_Compressor comp_select; - - SDL_Surface *comp_img; /* image of compressor map. */ } UI; int init_ui(UI *ui); |