forked from jdanecki/FractalCL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.c
155 lines (129 loc) · 4 KB
/
gui.c
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
/*
Copyright (C) 2018-2019 Jacek Danecki <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gui.h"
#ifdef OPENCL_SUPPORT
#include "fractal_ocl.h"
#endif
const char* font_file = "FreeMono.ttf";
TTF_Font* font;
SDL_Renderer* main_window;
SDL_Texture* texture;
void* texture_pixels;
void init_font()
{
char p[256];
if (TTF_Init() == -1)
{
printf("TTF_Init: %s\n", TTF_GetError());
exit(0);
}
sprintf(p, "%s/%s", STRING_MACRO(DATA_PATH), font_file);
font = TTF_OpenFont(p, FONT_SIZE);
if (font == NULL)
{
printf("TTF_OpenFont(%s) : %s\n", p, TTF_GetError());
exit(0);
}
}
void write_text(const char* t, int x, int y)
{
SDL_Surface* temp;
SDL_Texture* text_box;
SDL_Rect dest;
SDL_Color text_color;
SDL_Color shade_color;
text_color.r = 255;
text_color.b = 255;
text_color.g = 255;
shade_color.r = 0;
shade_color.g = 0;
shade_color.b = 60;
temp = TTF_RenderUTF8_Shaded(font, t, text_color, shade_color);
if (!temp)
{
printf("TTF_RenderUTF8_Solid error\n");
exit(0);
}
text_box = SDL_CreateTextureFromSurface(main_window, temp);
SDL_FreeSurface(temp);
dest.x = x;
dest.y = y;
dest.w = temp->w;
dest.h = temp->h;
SDL_RenderCopy(main_window, text_box, NULL, &dest);
SDL_DestroyTexture(text_box);
}
int init_window()
{
SDL_Window* app_window;
if (SDL_Init(SDL_INIT_VIDEO) < 0) return 1;
#ifdef SDL_ACCELERATED
app_window = SDL_CreateWindow("FractalCL", SDL_WINDOWPOS_CENTERED | SDL_WINDOW_OPENGL, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, HEIGHT, 0);
main_window = SDL_CreateRenderer(app_window, -1, SDL_RENDERER_ACCELERATED);
#else
app_window = SDL_CreateWindow("FractalCL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, HEIGHT, 0);
main_window = SDL_CreateRenderer(app_window, -1, SDL_RENDERER_SOFTWARE);
#endif
texture = SDL_CreateTexture(main_window, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
init_font();
draw_box(WIDTH, 0, RIGTH_PANEL_WIDTH, HEIGHT, 0, 0, 60);
return 0;
}
void draw_box(int x, int y, int w, int h, int r, int g, int b)
{
SDL_Rect dst;
dst.w = w;
dst.h = h;
dst.x = x;
dst.y = y;
SDL_SetRenderDrawColor(main_window, r, g, b, 255);
SDL_RenderFillRect(main_window, &dst);
}
void clear_window() { draw_box(0, 0, WIDTH, HEIGHT, 0, 0, 0); }
void draw_double(int y, char* txt, double val)
{
char buf[256];
sprintf(buf, "%s=%2.10e ", txt, val);
write_text(buf, WIDTH, FONT_SIZE * y + 2);
}
void draw_int(int y, char* txt, int val)
{
char buf[256];
sprintf(buf, "%s=%d ", txt, val);
write_text(buf, WIDTH, FONT_SIZE * y + 2);
}
void draw_long(int y, char* txt, unsigned long val)
{
char buf[256];
sprintf(buf, "%s=%lu ", txt, val);
write_text(buf, WIDTH, FONT_SIZE * y + 2);
}
void draw_2long(int y, char* txt1, unsigned long val1, char* txt2, unsigned long val2)
{
char buf[256];
sprintf(buf, "%s=%lu %s=%lu ", txt1, val1, txt2, val2);
write_text(buf, WIDTH, FONT_SIZE * y + 2);
}
void draw_hex(int y, char* txt, int val)
{
char buf[256];
sprintf(buf, "%s=%-8x", txt, val);
write_text(buf, WIDTH, FONT_SIZE * y + 2);
}
void draw_string(int y, char* txt, char* val)
{
char buf[256];
sprintf(buf, "%s=%s", txt, val);
write_text(buf, WIDTH, FONT_SIZE * y + 2);
}