summaryrefslogtreecommitdiff
path: root/sdl.cpp
blob: dcc2bb96a6d49d2697b5dd235b4b717a42dcb571 (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
#include <iostream>
#include <iomanip>
#include <sstream>
#include <SDL.h>
#include "mandel.h"

using namespace std;


#if 1
int main() {
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		cerr << "SDL init error: " << SDL_GetError() << endl;
		return 1;
	}

	SDL_Window *window = SDL_CreateWindow("Mandel", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 960, 640, SDL_WINDOW_SHOWN);
	if (!window) {
		cerr << "SDL window creation error: " << SDL_GetError() << endl;
		return 1;
	}

	int w, h;
	SDL_GetWindowSize(window, &w, &h);
	Mandel *ctx = mandel_init(w, h);
	Params par = mandel_default_params();
	uint8_t *imgdata = new uint8_t[3 * w * h];
	mandel_render(imgdata, ctx, &par);

	SDL_Surface *mandelsurf = SDL_CreateRGBSurfaceFrom(imgdata, w, h, 24, 3 * w, 0x0000000ff, 0x0000ff00, 0x00ff0000, 0);

	SDL_Surface *winsurf = SDL_GetWindowSurface(window);

	SDL_BlitSurface(mandelsurf, nullptr, winsurf, nullptr);

	SDL_UpdateWindowSurface(window);

	while (true) {
		const double min_img_size = min(par.imgw, mandel_imgh(ctx, &par));
		const double scroll_dist = min_img_size * 0.1;

		bool need_redraw = false;

		{
			stringstream ss;
			ss << "Mandel "
			   << setprecision(17)
			   << " x=" << par.cx
			   << " y=" << par.cy
			   << " w=" << par.imgw
			   << " i=" << par.maxit;
			SDL_SetWindowTitle(window, ss.str().data());
		}

		SDL_Event e;
		if (SDL_WaitEvent(&e) == 0) {
			cerr << "SDL event wait error: " << SDL_GetError() << endl;
			break;
		}

		winsurf = SDL_GetWindowSurface(window);

		switch (e.type) {
			case SDL_QUIT:
				break;

			case SDL_KEYDOWN:
				switch (e.key.keysym.sym) {
					case SDLK_UP:
						par.cy += scroll_dist;
						need_redraw = true;
						break;

					case SDLK_DOWN:
						par.cy -= scroll_dist;
						need_redraw = true;
						break;

					case SDLK_RIGHT:
						par.cx += scroll_dist;
						need_redraw = true;
						break;

					case SDLK_LEFT:
						par.cx -= scroll_dist;
						need_redraw = true;
						break;

					case SDLK_KP_PLUS:
					case SDLK_PLUS:
					case SDLK_EQUALS:
						par.imgw *= 0.8;
						need_redraw = true;
						break;

					case SDLK_KP_MINUS:
					case SDLK_MINUS:
						par.imgw *= 1.25;
						need_redraw = true;
						break;

					case SDLK_PAGEUP:
						par.maxit += 128;
						need_redraw = true;
						break;

					case SDLK_PAGEDOWN:
						if (par.maxit > 256) {
							par.maxit -= 128;
							need_redraw = true;
						}
						break;

					case SDLK_q:
						goto cleanup;
				}
				break;

			case SDL_WINDOWEVENT:
				switch (e.window.event) {
					case SDL_WINDOWEVENT_FOCUS_GAINED:
						SDL_UpdateWindowSurface(window);
						break;

					case SDL_WINDOWEVENT_SIZE_CHANGED:
						mandel_free(ctx);
						delete[] imgdata;
						SDL_FreeSurface(mandelsurf);

						winsurf = SDL_GetWindowSurface(window);
						SDL_GetWindowSize(window, &w, &h);
						cerr << "w=" << w << " h=" << h << endl;
						cerr << "surface: w=" << winsurf->w << " h=" << winsurf->h << endl;
						imgdata = new uint8_t[3 * w * h];
						mandelsurf = SDL_CreateRGBSurfaceFrom(imgdata, w, h, 24, 3 * w, 0x0000000ff, 0x0000ff00, 0x00ff0000, 0);
						ctx = mandel_init(w, h);
						need_redraw = true;
						break;
				}
				break;
		}

		if (need_redraw) {
			SDL_GetWindowSize(window, &w, &h);
			cerr << "w=" << w << " h=" << h << endl;
			cerr << "winsurf: w=" << winsurf->w << " h=" << winsurf->h << endl;
			cerr << "mandelsurf: w=" << mandelsurf->w << " h=" << mandelsurf->h << endl;
			mandel_render(imgdata, ctx, &par);

			if (SDL_MUSTLOCK(winsurf)) SDL_LockSurface(winsurf);
			SDL_BlitSurface(mandelsurf, nullptr, winsurf, nullptr);
			if (SDL_MUSTLOCK(winsurf)) SDL_UnlockSurface(winsurf);

			SDL_UpdateWindowSurface(window);
		}
	}

cleanup:
	SDL_DestroyWindow(window);
	SDL_Quit();
}
#endif