summaryrefslogtreecommitdiff
path: root/display.c
diff options
context:
space:
mode:
authorerdgeist <erdgeist@bauklotz.fritz.box>2017-04-08 14:21:36 +0200
committererdgeist <erdgeist@bauklotz.fritz.box>2017-04-08 14:21:36 +0200
commitf2683a4b707cd714b7f540ebf6482563df83d51e (patch)
treede4941add99f0eb1642aa57c6af180b4ee70119a /display.c
parent78d309a97b782bd6ab2716fa7595bb3f409479e3 (diff)
Near complete rewrite.
Diffstat (limited to 'display.c')
-rw-r--r--display.c88
1 files changed, 78 insertions, 10 deletions
diff --git a/display.c b/display.c
index 42235a2..4263e52 100644
--- a/display.c
+++ b/display.c
@@ -6,25 +6,30 @@
6#include "display.h" 6#include "display.h"
7 7
8#define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h)) 8#define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h))
9enum { FONT_SIZE = 28 };
9 10
10static SDL_Window *screen; 11static SDL_Window *screen;
11static SDL_Renderer *renderer; 12static SDL_Renderer *renderer;
12static int g_width, g_height; 13static int g_screen_width, g_screen_height;
14static int g_harfe_width, g_harfe_height;
15double g_scale_factor;
13static TTF_Font *font = NULL; 16static TTF_Font *font = NULL;
14 17
15void 18void
16display_init(int width, int height) 19display_init(int screen_width, int screen_height, int harfe_width, int harfe_height)
17{ 20{
18 SDL_RWops *font_file; 21 SDL_RWops *font_file;
19 22
20 g_width = width; 23 g_screen_width = screen_width;
21 g_height = height; 24 g_screen_height = screen_height;
25 g_harfe_width = harfe_width;
26 g_harfe_height = harfe_height;
22 27
23 if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { 28 if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
24 fprintf(stderr, "Can't initialize SDL.\n"); 29 fprintf(stderr, "Can't initialize SDL.\n");
25 exit(1); 30 exit(1);
26 } 31 }
27 screen = SDL_CreateWindow("Laserharfe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL); 32 screen = SDL_CreateWindow("Laserharfe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_OPENGL);
28 if (!screen) { 33 if (!screen) {
29 fprintf(stderr, "Can't set video mode.\n"); 34 fprintf(stderr, "Can't set video mode.\n");
30 exit(1); 35 exit(1);
@@ -35,7 +40,32 @@ display_init(int width, int height)
35 40
36 TTF_Init(); 41 TTF_Init();
37 font_file = SDL_RWFromConstMem(GenBkBasB_ttf, GenBkBasB_ttf_len); 42 font_file = SDL_RWFromConstMem(GenBkBasB_ttf, GenBkBasB_ttf_len);
38 font = TTF_OpenFontRW(font_file, 1, 28); 43 font = TTF_OpenFontRW(font_file, 1, FONT_SIZE);
44
45 g_scale_factor = (double)harfe_height / (double)screen_height;
46 if( (double)harfe_width / (double)screen_width < g_scale_factor )
47 g_scale_factor = (double)harfe_width / (double)screen_width;
48 printf( "Using scale factor of %lf\n", g_scale_factor );
49}
50
51void
52display_getdimensions(int *width, int *height, int *fontsize)
53{
54 *width = g_screen_width;
55 *height = g_screen_height;
56 *fontsize = FONT_SIZE;
57}
58
59int
60display_scale_harfe_to_screen(int coord)
61{
62 return (int)((double)coord / g_scale_factor);
63}
64
65int
66display_scale_screen_to_harfe(int coord)
67{
68 return (int)((double)coord * g_scale_factor);
39} 69}
40 70
41void 71void
@@ -48,19 +78,23 @@ display_clear()
48void 78void
49display_circle(int x, int y, int w) 79display_circle(int x, int y, int w)
50{ 80{
51 if (x >= 0 && x < g_width && y >= 0 && y < g_height) 81 y = g_screen_height - y;
52 display_circle_color(x, y, w, 0xffffffff); 82 if (x >= 0 && x <= g_screen_width && y >= 0 && y <= g_screen_height)
83 filledCircleColor(renderer, x, y, w, 0xffffffff);
53} 84}
54 85
55void 86void
56display_circle_color(int x, int y, int w, int color) 87display_circle_color(int x, int y, int w, int color)
57{ 88{
89 y = g_screen_height - y;
58 filledCircleColor(renderer, x, y, w, color); 90 filledCircleColor(renderer, x, y, w, color);
59} 91}
60 92
61void 93void
62display_line(int x0, int y0, int x1, int y1) 94display_line(int x0, int y0, int x1, int y1)
63{ 95{
96 y1 = g_screen_height - y1;
97 y0 = g_screen_height - y0;
64 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 98 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
65 SDL_RenderDrawLine(renderer, x0, y0, x1, y1); 99 SDL_RenderDrawLine(renderer, x0, y0, x1, y1);
66} 100}
@@ -68,11 +102,25 @@ display_line(int x0, int y0, int x1, int y1)
68void 102void
69display_line_color(int x0, int y0, int x1, int y1, int color) 103display_line_color(int x0, int y0, int x1, int y1, int color)
70{ 104{
105 y1 = g_screen_height - y1;
106 y0 = g_screen_height - y0;
71 SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255); 107 SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255);
72 SDL_RenderDrawLine(renderer, x0, y0, x1, y1); 108 SDL_RenderDrawLine(renderer, x0, y0, x1, y1);
73} 109}
74 110
75void 111void
112display_rect_color(int x, int y, int width, int height, int color)
113{
114 SDL_Rect r;
115 r.x = x;
116 r.y = g_screen_height - y;
117 r.w = width;
118 r.h = height;
119 SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255);
120 SDL_RenderFillRect(renderer, &r);
121}
122
123void
76display_redraw() 124display_redraw()
77{ 125{
78 SDL_RenderPresent(renderer); 126 SDL_RenderPresent(renderer);
@@ -81,6 +129,7 @@ display_redraw()
81void 129void
82display_text(char *text, int x, int y, int color) 130display_text(char *text, int x, int y, int color)
83{ 131{
132 y = g_screen_height - y;
84 SDL_Color s_color = { 255 & (color>>24), 255 & (color>>16), 255 & (color>>8) }; 133 SDL_Color s_color = { 255 & (color>>24), 255 & (color>>16), 255 & (color>>8) };
85 SDL_Surface *sText = TTF_RenderText_Solid(font, text, s_color); 134 SDL_Surface *sText = TTF_RenderText_Solid(font, text, s_color);
86 SDL_Rect rect = {x, y, sText->w, sText->h}; 135 SDL_Rect rect = {x, y, sText->w, sText->h};
@@ -98,7 +147,7 @@ display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int colo
98 147
99 display_measure_text("#", &w, &h); 148 display_measure_text("#", &w, &h);
100 item_height = 3 * h / 2; 149 item_height = 3 * h / 2;
101 min_y = (g_height - item_height * max_pos) / 2 + pos * item_height; 150 min_y = (g_screen_height - item_height * max_pos) / 2 + pos * item_height;
102 151
103 //boxColor(screen, min_x, min_y, max_x, min_y + item_height, color); 152 //boxColor(screen, min_x, min_y, max_x, min_y + item_height, color);
104 //rectangleColor(screen, min_x, min_y, max_x, min_y + item_height, 0xffffffff); 153 //rectangleColor(screen, min_x, min_y, max_x, min_y + item_height, 0xffffffff);
@@ -106,6 +155,25 @@ display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int colo
106 display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff); 155 display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff);
107} 156}
108 157
158void
159display_messagebox(char *title, char *info) {
160 SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_INFORMATION, title, info, screen);
161}
162
163int
164display_messagebox_yesno(char *title, char *info) {
165 const SDL_MessageBoxButtonData buttons[] = {
166 { /* .flags, .buttonid, .text */ 0, 0, "Cancel" },
167 { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "OK" },
168 };
169 const SDL_MessageBoxData messageboxdata = {
170 SDL_MESSAGEBOX_INFORMATION, screen, title, info, SDL_arraysize(buttons), buttons, NULL
171 };
172 int buttonid;
173 SDL_ShowMessageBox(&messageboxdata, &buttonid);
174 return buttonid == 1;
175}
176
109int 177int
110display_test_menu_click(int y, int max_pos) 178display_test_menu_click(int y, int max_pos)
111{ 179{
@@ -114,7 +182,7 @@ display_test_menu_click(int y, int max_pos)
114/* 182/*
115 display_measure_text( "#", &w, &h ); 183 display_measure_text( "#", &w, &h );
116 item_height = 3 * h / 2; 184 item_height = 3 * h / 2;
117 min_y = ( g_height - item_height * max_pos ) / 2; 185 min_y = ( g_screen_height - item_height * max_pos ) / 2;
118 if( y < min_y ) return -1; 186 if( y < min_y ) return -1;
119 if( y > min_y + item_height * max_pos ) return -1; 187 if( y > min_y + item_height * max_pos ) return -1;
120 return ( y - min_y ) / item_height; 188 return ( y - min_y ) / item_height;