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
|
#include "config.h"
#include "mandel.h"
#define MANDEL_FONT_PATH "font/00_starmap.ttf"
#define MANDEL_FONT_SIZE 12
bool state_init(State *state)
{
SDL_CALL(SDL_Init(SDL_INIT_VIDEO));
TTF_CALL(TTF_Init());
SDL_CALL(state->window = SDL_CreateWindow(
MANDEL_WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
MANDEL_WINDOW_WIDTH,
MANDEL_WINDOW_HEIGHT,
SDL_WINDOW_RESIZABLE
));
SDL_CALL(state->renderer = SDL_CreateRenderer(state->window, -1, 0));
SDL_CALL(state->texture = SDL_CreateTexture(
state->renderer,
SDL_PIXELFORMAT_RGB888,
SDL_TEXTUREACCESS_STREAMING,
MANDEL_WINDOW_WIDTH,
MANDEL_WINDOW_HEIGHT
));
TTF_CALL(state->font = TTF_OpenFont(MANDEL_FONT_PATH, MANDEL_FONT_SIZE));
state->iterations = MANDEL_ITERATIONS;
state->texture_iterations = text_texture_new(state, "iterations: %d", state->iterations);
state->texture_time = text_texture_new(state, "time: 0ms");
state->texture_center = text_texture_new(state, "center: 0i + 0");
state->palette = color_palette_new(NULL, MANDEL_ITERATIONS);
if (state->palette == NULL)
return false;
state->real_start = -2.0;
state->real_end = 2.0;
state->imag_start = -2.0;
state->imag_end = 2.0;
state->running = true;
state->info = false;
return true;
}
void state_run(State *state)
{
while (state->running)
{
event_handle(state);
double real_step;
double imag_step;
double real;
double imag;
void *pixels;
int pitch;
int width;
int height;
SDL_CALL(SDL_LockTexture(state->texture, NULL, &pixels, &pitch));
SDL_CALL(SDL_QueryTexture(state->texture, NULL, NULL, &width, &height));
real_step = (state->real_end - state->real_start) / width;
imag_step = (state->imag_end - state->imag_start) / height;
imag = state->imag_start;
uint32_t render_start_time = SDL_GetTicks();
for (int i = 0; i < height; i++)
{
real = state->real_start;
for (int j = 0; j < width; j++)
{
int n = mandelbrot(real, imag, state->iterations);
((Color*)pixels)[i * width + j] = state->palette[n];
real += real_step;
}
imag += imag_step;
}
uint32_t render_end_time = SDL_GetTicks();
SDL_UnlockTexture(state->texture);
SDL_CALL(SDL_RenderCopy(state->renderer, state->texture, NULL, NULL));
if (state->info)
{
TEXT_TEXTURE_UPDATE(state, state->texture_time, "time: %u", render_end_time - render_start_time);
text_render(state, state->texture_center, 10, 10, 200, 12);
text_render(state, state->texture_iterations, 10, 30, 90, 12);
text_render(state, state->texture_time, 10, 50, 45, 12);
}
SDL_RenderPresent(state->renderer);
SDL_Delay(3);
}
}
void state_quit(State *state)
{
free(state->palette);
TTF_CloseFont(state->font);
SDL_DestroyTexture(state->texture_iterations);
SDL_DestroyTexture(state->texture_center);
SDL_DestroyTexture(state->texture_time);
SDL_DestroyTexture(state->texture);
SDL_DestroyRenderer(state->renderer);
SDL_DestroyWindow(state->window);
TTF_Quit();
SDL_Quit();
}
|