summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@bauklotz.fritz.box>2018-04-02 01:38:07 +0200
committererdgeist <erdgeist@bauklotz.fritz.box>2018-04-02 01:38:07 +0200
commit57bd5755394aeb965f3339e4bd523edbe6986be1 (patch)
tree7c1dc465a098c7a78af3d7298a7d0a020e5ad318
parentfa0cb86eec43978054f9db295a77449d6b83ba43 (diff)
Rework display framework
-rw-r--r--display.c69
-rw-r--r--display.h6
2 files changed, 44 insertions, 31 deletions
diff --git a/display.c b/display.c
index 4263e52..27b96e5 100644
--- a/display.c
+++ b/display.c
@@ -2,9 +2,12 @@
2#include "SDL2/SDL2_gfxPrimitives.h" 2#include "SDL2/SDL2_gfxPrimitives.h"
3#include <SDL2/SDL_ttf.h> 3#include <SDL2/SDL_ttf.h>
4 4
5#include "GenBkBasB.h"
6#include "display.h" 5#include "display.h"
7 6
7#define FONT_NAME(suffix) SourceCodePro_Semibold ## suffix
8extern unsigned char FONT_NAME(_ttf[]);
9extern unsigned int FONT_NAME(_ttf_len);
10
8#define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h)) 11#define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h))
9enum { FONT_SIZE = 28 }; 12enum { FONT_SIZE = 28 };
10 13
@@ -39,7 +42,7 @@ display_init(int screen_width, int screen_height, int harfe_width, int harfe_hei
39 SDL_RenderClear(renderer); 42 SDL_RenderClear(renderer);
40 43
41 TTF_Init(); 44 TTF_Init();
42 font_file = SDL_RWFromConstMem(GenBkBasB_ttf, GenBkBasB_ttf_len); 45 font_file = SDL_RWFromConstMem( FONT_NAME(_ttf), FONT_NAME(_ttf_len) );
43 font = TTF_OpenFontRW(font_file, 1, FONT_SIZE); 46 font = TTF_OpenFontRW(font_file, 1, FONT_SIZE);
44 47
45 g_scale_factor = (double)harfe_height / (double)screen_height; 48 g_scale_factor = (double)harfe_height / (double)screen_height;
@@ -108,17 +111,22 @@ display_line_color(int x0, int y0, int x1, int y1, int color)
108 SDL_RenderDrawLine(renderer, x0, y0, x1, y1); 111 SDL_RenderDrawLine(renderer, x0, y0, x1, y1);
109} 112}
110 113
111void 114static void
112display_rect_color(int x, int y, int width, int height, int color) 115display_rect_color_static(int x, int y, int width, int height, int color)
113{ 116{
114 SDL_Rect r; 117 SDL_Rect r;
115 r.x = x; 118 r.x = x;
116 r.y = g_screen_height - y; 119 r.y = y;
117 r.w = width; 120 r.w = width;
118 r.h = height; 121 r.h = height;
119 SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255); 122 SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255);
120 SDL_RenderFillRect(renderer, &r); 123 SDL_RenderFillRect(renderer, &r);
121} 124}
125void
126display_rect_color(int x, int y, int width, int height, int color)
127{
128 display_rect_color_static(x, g_screen_height - y, width, height, color);
129}
122 130
123void 131void
124display_redraw() 132display_redraw()
@@ -126,10 +134,9 @@ display_redraw()
126 SDL_RenderPresent(renderer); 134 SDL_RenderPresent(renderer);
127} 135}
128 136
129void 137static void
130display_text(char *text, int x, int y, int color) 138display_text_static(char const *text, int x, int y, int color)
131{ 139{
132 y = g_screen_height - y;
133 SDL_Color s_color = { 255 & (color>>24), 255 & (color>>16), 255 & (color>>8) }; 140 SDL_Color s_color = { 255 & (color>>24), 255 & (color>>16), 255 & (color>>8) };
134 SDL_Surface *sText = TTF_RenderText_Solid(font, text, s_color); 141 SDL_Surface *sText = TTF_RenderText_Solid(font, text, s_color);
135 SDL_Rect rect = {x, y, sText->w, sText->h}; 142 SDL_Rect rect = {x, y, sText->w, sText->h};
@@ -139,20 +146,41 @@ display_text(char *text, int x, int y, int color)
139 SDL_DestroyTexture(texture); 146 SDL_DestroyTexture(texture);
140 SDL_FreeSurface(sText); 147 SDL_FreeSurface(sText);
141} 148}
149void
150display_text(char const *text, int x, int y, int color)
151{
152 display_text_static(text, x, g_screen_height - y, color);
153}
142 154
143void 155void
144display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int color) 156display_textbox(int min_x, int pos, int max_x, int max_pos, char const *text, int color)
145{ 157{
146 int w, h, min_y, item_height; 158 int w, h, min_y, item_height;
147 159
148 display_measure_text("#", &w, &h); 160 display_measure_text("#", &w, &h);
149 item_height = 3 * h / 2; 161 item_height = 3 * h / 2;
150 min_y = (g_screen_height - item_height * max_pos) / 2 + pos * item_height; 162 min_y = pos * item_height + 4;
163
164 display_rect_color_static(min_x, min_y, max_x - min_x, item_height - 4, color);
151 165
152 //boxColor(screen, min_x, min_y, max_x, min_y + item_height, color);
153 //rectangleColor(screen, min_x, min_y, max_x, min_y + item_height, 0xffffffff);
154 display_measure_text(text, &w, &h); 166 display_measure_text(text, &w, &h);
155 display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff); 167 // display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff);
168 display_text_static(text, min_x + 8, min_y + (item_height - h) / 2, 0xffffffff);
169}
170
171int
172display_test_menu_click(int y)
173{
174 int w, h, min_y, item_height;
175
176 y -= 4;
177 if (y<0)
178 return -1;
179
180 display_measure_text( "#", &w, &h );
181 item_height = 3 * h / 2;
182
183 return y / item_height;
156} 184}
157 185
158void 186void
@@ -174,18 +202,3 @@ display_messagebox_yesno(char *title, char *info) {
174 return buttonid == 1; 202 return buttonid == 1;
175} 203}
176 204
177int
178display_test_menu_click(int y, int max_pos)
179{
180 int w, h, min_y, item_height;
181
182/*
183 display_measure_text( "#", &w, &h );
184 item_height = 3 * h / 2;
185 min_y = ( g_screen_height - item_height * max_pos ) / 2;
186 if( y < min_y ) return -1;
187 if( y > min_y + item_height * max_pos ) return -1;
188 return ( y - min_y ) / item_height;
189*/
190 return 0;
191}
diff --git a/display.h b/display.h
index c7003fd..da2f7fa 100644
--- a/display.h
+++ b/display.h
@@ -13,9 +13,9 @@ void display_circle(int x, int y, int w);
13void display_circle_color(int x, int y, int w, int color); 13void display_circle_color(int x, int y, int w, int color);
14void display_rect_color(int x, int y, int width, int height, int color); 14void display_rect_color(int x, int y, int width, int height, int color);
15 15
16void display_text(char *text, int x, int y, int color); 16void display_text(char const *text, int x, int y, int color);
17void display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int color); 17void display_textbox(int min_x, int pos, int max_x, int max_pos, char const *text, int color);
18int display_test_menu_click(int y, int max_pos); 18int display_test_menu_click(int y);
19void display_messagebox(char *title, char *info); 19void display_messagebox(char *title, char *info);
20int display_messagebox_yesno(char *title, char *info); 20int display_messagebox_yesno(char *title, char *info);
21 21