aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.c2
-rw-r--r--renderer.c58
-rw-r--r--renderer.h3
-rw-r--r--ui.c23
-rw-r--r--ui.h2
5 files changed, 36 insertions, 52 deletions
diff --git a/main.c b/main.c
index 2d3bb18..e2d8f95 100644
--- a/main.c
+++ b/main.c
@@ -4,8 +4,6 @@
#include <stdio.h>
#include <stdlib.h>
-#include <SDL2/SDL.h>
-
#include "microui.h"
#include "renderer.h"
#include "unit.h"
diff --git a/renderer.c b/renderer.c
index d508526..7adc8a9 100644
--- a/renderer.c
+++ b/renderer.c
@@ -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]);
+}
diff --git a/renderer.h b/renderer.h
index 0f8cd41..85cefc0 100644
--- a/renderer.h
+++ b/renderer.h
@@ -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);
diff --git a/ui.c b/ui.c
index 5c2afd5..9dfbf87 100644
--- a/ui.c
+++ b/ui.c
@@ -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;
diff --git a/ui.h b/ui.h
index e259133..b5c2635 100644
--- a/ui.h
+++ b/ui.h
@@ -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);