summaryrefslogtreecommitdiff
path: root/display.c
diff options
context:
space:
mode:
authorerdgeist <erdgeist@bauklotz.fritz.box>2016-08-12 14:46:51 +0200
committererdgeist <erdgeist@bauklotz.fritz.box>2016-08-12 14:46:51 +0200
commita8be0d3d20f07d4561826b01f566ca307eb23526 (patch)
treeb2c5c6d513ae3a84aba8e4eea94ec32e46d352fa /display.c
commit as a backup
Diffstat (limited to 'display.c')
-rw-r--r--display.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/display.c b/display.c
new file mode 100644
index 0000000..42235a2
--- /dev/null
+++ b/display.c
@@ -0,0 +1,123 @@
1#include "SDL2/SDL.h"
2#include "SDL2/SDL2_gfxPrimitives.h"
3#include <SDL2/SDL_ttf.h>
4
5#include "GenBkBasB.h"
6#include "display.h"
7
8#define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h))
9
10static SDL_Window *screen;
11static SDL_Renderer *renderer;
12static int g_width, g_height;
13static TTF_Font *font = NULL;
14
15void
16display_init(int width, int height)
17{
18 SDL_RWops *font_file;
19
20 g_width = width;
21 g_height = height;
22
23 if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
24 fprintf(stderr, "Can't initialize SDL.\n");
25 exit(1);
26 }
27 screen = SDL_CreateWindow("Laserharfe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
28 if (!screen) {
29 fprintf(stderr, "Can't set video mode.\n");
30 exit(1);
31 }
32 renderer = SDL_CreateRenderer(screen, -1, 0);
33
34 SDL_RenderClear(renderer);
35
36 TTF_Init();
37 font_file = SDL_RWFromConstMem(GenBkBasB_ttf, GenBkBasB_ttf_len);
38 font = TTF_OpenFontRW(font_file, 1, 28);
39}
40
41void
42display_clear()
43{
44 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
45 SDL_RenderClear(renderer);
46}
47
48void
49display_circle(int x, int y, int w)
50{
51 if (x >= 0 && x < g_width && y >= 0 && y < g_height)
52 display_circle_color(x, y, w, 0xffffffff);
53}
54
55void
56display_circle_color(int x, int y, int w, int color)
57{
58 filledCircleColor(renderer, x, y, w, color);
59}
60
61void
62display_line(int x0, int y0, int x1, int y1)
63{
64 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
65 SDL_RenderDrawLine(renderer, x0, y0, x1, y1);
66}
67
68void
69display_line_color(int x0, int y0, int x1, int y1, int color)
70{
71 SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255);
72 SDL_RenderDrawLine(renderer, x0, y0, x1, y1);
73}
74
75void
76display_redraw()
77{
78 SDL_RenderPresent(renderer);
79}
80
81void
82display_text(char *text, int x, int y, int color)
83{
84 SDL_Color s_color = { 255 & (color>>24), 255 & (color>>16), 255 & (color>>8) };
85 SDL_Surface *sText = TTF_RenderText_Solid(font, text, s_color);
86 SDL_Rect rect = {x, y, sText->w, sText->h};
87 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, sText);
88
89 SDL_RenderCopy(renderer, texture, NULL, &rect);
90 SDL_DestroyTexture(texture);
91 SDL_FreeSurface(sText);
92}
93
94void
95display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int color)
96{
97 int w, h, min_y, item_height;
98
99 display_measure_text("#", &w, &h);
100 item_height = 3 * h / 2;
101 min_y = (g_height - item_height * max_pos) / 2 + pos * item_height;
102
103 //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);
105 display_measure_text(text, &w, &h);
106 display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff);
107}
108
109int
110display_test_menu_click(int y, int max_pos)
111{
112 int w, h, min_y, item_height;
113
114/*
115 display_measure_text( "#", &w, &h );
116 item_height = 3 * h / 2;
117 min_y = ( g_height - item_height * max_pos ) / 2;
118 if( y < min_y ) return -1;
119 if( y > min_y + item_height * max_pos ) return -1;
120 return ( y - min_y ) / item_height;
121*/
122 return 0;
123}