summaryrefslogtreecommitdiff
path: root/display.c
blob: 27b96e55a5906ce339f317ae2aec6a7d4ec93d2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "SDL2/SDL.h"
#include "SDL2/SDL2_gfxPrimitives.h"
#include <SDL2/SDL_ttf.h>

#include "display.h"

#define FONT_NAME(suffix) SourceCodePro_Semibold ## suffix
extern unsigned char FONT_NAME(_ttf[]);
extern unsigned int FONT_NAME(_ttf_len);

#define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h))
enum { FONT_SIZE = 28 };

static SDL_Window *screen;
static SDL_Renderer *renderer;
static int g_screen_width, g_screen_height;
static int g_harfe_width, g_harfe_height;
double g_scale_factor;
static TTF_Font *font = NULL;

void
display_init(int screen_width, int screen_height, int harfe_width, int harfe_height)
{
        SDL_RWops *font_file;

        g_screen_width  = screen_width;
        g_screen_height = screen_height;
        g_harfe_width   = harfe_width;
        g_harfe_height  = harfe_height;

        if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
                fprintf(stderr, "Can't initialize SDL.\n");
                exit(1);
        }
        screen = SDL_CreateWindow("Laserharfe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_OPENGL);
        if (!screen) {
                fprintf(stderr, "Can't set video mode.\n");
                exit(1);
        }
        renderer = SDL_CreateRenderer(screen, -1, 0);

        SDL_RenderClear(renderer);

        TTF_Init();
        font_file = SDL_RWFromConstMem( FONT_NAME(_ttf), FONT_NAME(_ttf_len) );
        font = TTF_OpenFontRW(font_file, 1, FONT_SIZE);

        g_scale_factor = (double)harfe_height / (double)screen_height;
        if( (double)harfe_width / (double)screen_width < g_scale_factor )
            g_scale_factor = (double)harfe_width / (double)screen_width;
        printf( "Using scale factor of %lf\n", g_scale_factor );
}

void
display_getdimensions(int *width, int *height, int *fontsize)
{
    *width = g_screen_width;
    *height = g_screen_height;
    *fontsize = FONT_SIZE;
}

int
display_scale_harfe_to_screen(int coord)
{
    return (int)((double)coord / g_scale_factor);
}

int
display_scale_screen_to_harfe(int coord)
{
    return (int)((double)coord * g_scale_factor);
}

void
display_clear()
{
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);
}

void
display_circle(int x, int y, int w)
{
        y = g_screen_height - y;
        if (x >= 0 && x <= g_screen_width && y >= 0 && y <= g_screen_height)
                filledCircleColor(renderer, x, y, w, 0xffffffff);
}

void
display_circle_color(int x, int y, int w, int color)
{
        y = g_screen_height - y;
        filledCircleColor(renderer, x, y, w, color);
}

void
display_line(int x0, int y0, int x1, int y1)
{
        y1 = g_screen_height - y1;
        y0 = g_screen_height - y0;
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        SDL_RenderDrawLine(renderer, x0, y0, x1, y1);
}

void
display_line_color(int x0, int y0, int x1, int y1, int color)
{
        y1 = g_screen_height - y1;
        y0 = g_screen_height - y0;
        SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255);
        SDL_RenderDrawLine(renderer, x0, y0, x1, y1);
}

static void
display_rect_color_static(int x, int y, int width, int height, int color)
{
        SDL_Rect r;
        r.x = x;
        r.y = y;
        r.w = width;
        r.h = height;
        SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255);
        SDL_RenderFillRect(renderer, &r);
}
void
display_rect_color(int x, int y, int width, int height, int color)
{
    display_rect_color_static(x, g_screen_height - y, width, height, color);
}

void
display_redraw()
{
        SDL_RenderPresent(renderer);
}

static void
display_text_static(char const *text, int x, int y, int color)
{
        SDL_Color s_color = { 255 & (color>>24), 255 & (color>>16), 255 & (color>>8) };
        SDL_Surface *sText = TTF_RenderText_Solid(font, text, s_color);
        SDL_Rect rect = {x, y, sText->w, sText->h};
        SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, sText);

        SDL_RenderCopy(renderer, texture, NULL, &rect);
        SDL_DestroyTexture(texture);
        SDL_FreeSurface(sText);
}
void
display_text(char const *text, int x, int y, int color)
{
    display_text_static(text, x, g_screen_height - y, color);
}

void
display_textbox(int min_x, int pos, int max_x, int max_pos, char const *text, int color)
{
        int w, h, min_y, item_height;

        display_measure_text("#", &w, &h);
        item_height = 3 * h / 2;
        min_y = pos * item_height + 4;

        display_rect_color_static(min_x, min_y, max_x - min_x, item_height - 4, color);

        display_measure_text(text, &w, &h);
        // display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff);
        display_text_static(text, min_x + 8, min_y + (item_height - h) / 2, 0xffffffff);
}

int
display_test_menu_click(int y)
{
    int w, h, min_y, item_height;

    y -= 4;
    if (y<0)
        return -1;

    display_measure_text( "#", &w, &h );
    item_height = 3 * h / 2;

    return y / item_height;
}

void
display_messagebox(char *title, char *info) {
        SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_INFORMATION, title, info, screen);
}

int
display_messagebox_yesno(char *title, char *info) {
    const SDL_MessageBoxButtonData buttons[] = {
        { /* .flags, .buttonid, .text */        0, 0, "Cancel" },
        { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "OK" },
    };
    const SDL_MessageBoxData messageboxdata = {
        SDL_MESSAGEBOX_INFORMATION, screen, title, info, SDL_arraysize(buttons), buttons, NULL
    };
    int buttonid;
    SDL_ShowMessageBox(&messageboxdata, &buttonid);
    return buttonid == 1;
}