summaryrefslogtreecommitdiff
path: root/src/sdl_fb.c
blob: cb1d6a2c952bb7979c11b2ed5ca14147c5a45f8a (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#define SDL_MAIN_HANDLED
#include <assert.h>
#include <SDL.h>
#include <stdlib.h>
#include <errno.h>

#include "til_fb.h"
#include "til_settings.h"
#include "til_video_setup.h"


/* sdl fb backend, everything sdl-specific in rototiller resides here. */

typedef struct sdl_fb_setup_t {
	til_video_setup_t	til_video_setup;
	unsigned		fullscreen:1;
	unsigned		vsync:1;
	unsigned		width, height;
} sdl_fb_setup_t;

typedef struct sdl_fb_page_t sdl_fb_page_t;

struct sdl_fb_page_t {
	sdl_fb_page_t	*next_spare;
	SDL_Surface	*surface;
};

typedef struct sdl_fb_t {
	const char	*title;
	unsigned	width, height;
	Uint32		window_flags;
	Uint32		renderer_flags;

	SDL_Window	*window;
	SDL_Renderer	*renderer;
	SDL_Texture	*texture;

	sdl_fb_page_t	*spare_pages;
} sdl_fb_t;


static int sdl_err_to_errno(int err)
{
	switch (err) {
	case SDL_ENOMEM:
		return ENOMEM;
	case SDL_EFREAD:
	case SDL_EFWRITE:
	case SDL_EFSEEK:
		return EIO;
	case SDL_UNSUPPORTED:
		return ENOTSUP;
	default:
		return EINVAL;
	}
}

static int sdl_fb_init(const char *title, const til_video_setup_t *setup, void **res_context)
{
	sdl_fb_setup_t	*s = (sdl_fb_setup_t *)setup;
	sdl_fb_t	*c;
	int		r;

	assert(title);
	assert(setup);
	assert(res_context);

	c = calloc(1, sizeof(sdl_fb_t));
	if (!c)
		return -ENOMEM;

	if (s->fullscreen) {
		if (s->width && s->height)
			c->window_flags = SDL_WINDOW_FULLSCREEN;
		else
			c->window_flags = SDL_WINDOW_FULLSCREEN_DESKTOP;
	}

	if (s->vsync)
		c->renderer_flags = SDL_RENDERER_PRESENTVSYNC;

	c->title = strdup(title);
	if (!c->title) {
		free(c);
		return -ENOMEM;
	}

	c->width = s->width;
	c->height = s->height;

	SDL_SetMainReady();
	r = SDL_Init(SDL_INIT_VIDEO);
	if (r < 0) {
		free(c);
		return -sdl_err_to_errno(r);
	}

	if (c->window_flags == SDL_WINDOW_FULLSCREEN_DESKTOP) {
		SDL_DisplayMode	mode;

		r = SDL_GetDesktopDisplayMode(0, &mode);
		if (r != 0) {
			SDL_Quit();
			free(c);
			return -sdl_err_to_errno(r);
		}

		c->width = mode.w;
		c->height = mode.h;
	}

	*res_context = c;

	return 0;
}


static void sdl_fb_shutdown(til_fb_t *fb, void *context)
{
	sdl_fb_t	*c = context;
	sdl_fb_page_t	*p;

	while ((p = c->spare_pages)) {
		c->spare_pages = p->next_spare;

		SDL_FreeSurface(p->surface);
		free(p);
	}
	free((void *)c->title);
	SDL_Quit();
	free(c);
}


static int sdl_fb_acquire(til_fb_t *fb, void *context, void *page)
{
	sdl_fb_t	*c = context;

	c->window = SDL_CreateWindow(c->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, c->width, c->height, c->window_flags);
	if (!c->window)
		return -1;

	c->renderer = SDL_CreateRenderer(c->window, -1, c->renderer_flags);
	if (!c->renderer)
		return -1;

	c->texture = SDL_CreateTexture(c->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, c->width, c->height);
	if (!c->texture)
		return -1;

	if (c->window_flags & SDL_WINDOW_FULLSCREEN || c->window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP)
		SDL_ShowCursor(SDL_DISABLE);

	return 0;
}


static void sdl_fb_release(til_fb_t *fb, void *context)
{
	sdl_fb_t	*c = context;

	SDL_DestroyTexture(c->texture);
	SDL_DestroyRenderer(c->renderer);
	SDL_DestroyWindow(c->window);
}


static void * sdl_fb_page_alloc(til_fb_t *fb, void *context, til_fb_fragment_t *res_fragment)
{
	sdl_fb_t	*c = context;
	sdl_fb_page_t	*p = NULL;

	if (c->spare_pages) {
		p = c->spare_pages;
		c->spare_pages = p->next_spare;
	}

	if (!p) {
		p = calloc(1, sizeof(sdl_fb_page_t));
		if (!p)
			return NULL;

		p->surface = SDL_CreateRGBSurface(0, c->width, c->height, 32, 0, 0, 0, 0); /* TODO: errors! */

		/* rototiller wants to assume all pixels to be 32-bit aligned, so prevent unaligning pitches */
		assert(!(p->surface->pitch & 0x3));
	}

	*res_fragment =	(til_fb_fragment_t){
				.buf = p->surface->pixels,
				.width = c->width,
				.frame_width = c->width,
				.height = c->height,
				.frame_height = c->height,
				.pitch = p->surface->pitch >> 2,
				.stride = (p->surface->pitch >> 2) - c->width,
			};

	return p;
}


static int sdl_fb_page_free(til_fb_t *fb, void *context, void *page)
{
	sdl_fb_t	*c = context;
	sdl_fb_page_t	*p = page;

	if (p->surface->w != c->width || p->surface->h != c->height) {
		/* TODO FIXME: there seems to be a lack of resizing consideration altogether in this fb backend */
		SDL_FreeSurface(p->surface);
		free(p);
	} else {
		p->next_spare = c->spare_pages;
		c->spare_pages = p;
	}

	return 0;
}


static int sdl_ready()
{
	SDL_Event	ev;

	/* It's important on Windows in particular to
	 * drain the event queue vs. just SDL_QuitRequested()
	 */
	while (SDL_PollEvent(&ev)) {
		if (ev.type == SDL_QUIT)
			return -EPIPE;

		if (ev.type == SDL_KEYDOWN &&
		    ev.key.keysym.sym == SDL_GetKeyFromScancode(SDL_SCANCODE_ESCAPE))
			return -EPIPE;
	}

	return 0;
}


static int sdl_fb_page_flip(til_fb_t *fb, void *context, void *page)
{
	sdl_fb_t	*c = context;
	sdl_fb_page_t	*p = page;
	int		r;

	r = sdl_ready();
	if (r < 0)
		return r;

	if (SDL_UpdateTexture(c->texture, NULL, p->surface->pixels, p->surface->pitch) < 0)
		return -1;

	if (SDL_RenderClear(c->renderer) < 0)
		return -1;

	if (SDL_RenderCopy(c->renderer, c->texture, NULL, NULL) < 0)
		return -1;

	SDL_RenderPresent(c->renderer);

	return 0;
}


static int sdl_fb_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);


til_fb_ops_t sdl_fb_ops = {
	.setup = sdl_fb_setup,
	.init = sdl_fb_init,
	.shutdown = sdl_fb_shutdown,
	.acquire = sdl_fb_acquire,
	.release = sdl_fb_release,
	.page_alloc = sdl_fb_page_alloc,
	.page_free = sdl_fb_page_free,
	.page_flip = sdl_fb_page_flip
};


static int sdl_fb_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
	const char	*bool_values[] = {
				"off",
				"on",
				NULL
			};
	til_setting_t	*fullscreen;
	til_setting_t	*vsync;
	til_setting_t	*size;
	int		r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "SDL fullscreen mode",
							.key = "fullscreen",
							.regex = NULL,
							.preferred = bool_values[0],
							.values = bool_values,
							.annotations = NULL
						},
						&fullscreen,
						res_setting,
						res_desc);
	if (r)
		return r;

	r = til_settings_get_and_describe_setting(settings,
						&(til_setting_spec_t){
							.name = "SDL synchronize present with refresh rate",
							.key = "vsync",
							.regex = NULL,
							.preferred = bool_values[1],
							.values = bool_values,
							.annotations = NULL
						},
						&vsync,
						res_setting,
						res_desc);
	if (r)
		return r;

	if (!strcasecmp(fullscreen->value, "off")) {
		r = til_settings_get_and_describe_setting(settings,
							&(til_setting_spec_t){
								.name = "SDL window size",
								.key = "size",
								.regex = "[1-9][0-9]*[xX][1-9][0-9]*",
								.preferred = "640x480",
								.values = NULL,
								.annotations = NULL
							},
							&size,
							res_setting,
							res_desc);
		if (r)
			return r;
	} else if ((size = til_settings_get_setting_by_key(settings, "size", res_setting)) && !size->desc) {
		/* if fullscreen=on AND size=WxH is specified, we'll do a more legacy style SDL fullscreen
		 * where it tries to change the video mode.  But if size is unspecified, it'll be a desktop
		 * style fullscreen where it just uses a fullscreen window in the existing video mode, and
		 * we won't forcibly require a size= be specified.
		 */
		 /* FIXME TODO: this is all copy-n-pasta grossness that wouldn't need to exist if
		  * til_settings_get_and_describe_value() just supported optional settings we only
		  * describe when they're already present.  It just needs something like an optional flag,
		  * to be added in a future commit which will remove this hack.
		  */
		 r = til_setting_desc_new(	settings,
						&(til_setting_spec_t){
							.name = "SDL window size",
							.key = "size",
							.regex = "[1-9][0-9]*[xX][1-9][0-9]*",
							.preferred = "640x480",
							.values = NULL,
							.annotations = NULL
						}, res_desc);
		if (r < 0)
			return r;

		return 1;
	}

	if (res_setup) {
		sdl_fb_setup_t	*setup;

		setup = til_setup_new(settings, sizeof(*setup), NULL, &sdl_fb_ops);
		if (!setup)
			return -ENOMEM;

		if (!strcasecmp(fullscreen->value, "on"))
			setup->fullscreen = 1;

		if (!strcasecmp(vsync->value, "on"))
			setup->vsync = 1;

		if (size && sscanf(size->value, "%u%*[xX]%u", &setup->width, &setup->height) != 2)
			return til_setup_free_with_failed_setting_ret_err(&setup->til_video_setup.til_setup, size, res_setting, -EINVAL);

		*res_setup = &setup->til_video_setup.til_setup;
	}

	return 0;
}
© All Rights Reserved