summaryrefslogtreecommitdiff
path: root/display.c
blob: 42235a2d6351014c454f485a9c351442da2cc062 (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
#include "SDL2/SDL.h"
#include "SDL2/SDL2_gfxPrimitives.h"
#include <SDL2/SDL_ttf.h>

#include "GenBkBasB.h"
#include "display.h"

#define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h))

static SDL_Window *screen;
static SDL_Renderer *renderer;
static int g_width, g_height;
static TTF_Font *font = NULL;

void
display_init(int width, int height)
{
        SDL_RWops *font_file;

        g_width = width;
        g_height = 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, width, 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(GenBkBasB_ttf, GenBkBasB_ttf_len);
        font = TTF_OpenFontRW(font_file, 1, 28);
}

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

void
display_circle(int x, int y, int w)
{
        if (x >= 0 && x < g_width && y >= 0 && y < g_height)
                display_circle_color(x, y, w, 0xffffffff);
}

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

void
display_line(int x0, int y0, int x1, int y1)
{
        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)
{
        SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255);
        SDL_RenderDrawLine(renderer, x0, y0, x1, y1);
}

void
display_redraw()
{
        SDL_RenderPresent(renderer);
}

void
display_text(char *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_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int color)
{
        int w, h, min_y, item_height;

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

        //boxColor(screen, min_x, min_y, max_x, min_y + item_height, color);
        //rectangleColor(screen, min_x, min_y, max_x, min_y + item_height, 0xffffffff);
        display_measure_text(text, &w, &h);
        display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff);
}

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

/*
  display_measure_text( "#", &w, &h );
  item_height = 3 * h / 2;
  min_y = ( g_height - item_height * max_pos ) / 2;
  if( y < min_y ) return -1;
  if( y > min_y + item_height * max_pos ) return -1;
  return ( y - min_y ) / item_height;
*/
        return 0;
}