aboutsummaryrefslogtreecommitdiff
path: root/screen.c
blob: 2176cb485822023e603465bd5fec5d2f3984d009 (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
/*
 * screen.c -- Abstracción sobre SDL para la entrada y salida gráfica.
 * Copyright (C) 2018 Juan Marín Noguera
 *
 * 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 <https://www.gnu.org/licenses/>.
 */
#include <malloc.h>
#include <SDL2/SDL.h>
#include <string.h>
#include "error.h"
#include "screen.h"

#define TEXT_X 3
#define TEXT_Y 460
#define TEXT_FILE "56929.bmp"
#define TEXT_H 8
#define TEXT_W 8
#define TEXT_PER_ROW 16

struct Picture {
	SDL_Surface *s;
	SDL_Surface *f;
};

struct Screen {
	SDL_Window *window;
	SDL_Renderer *renderer;
	SDL_Surface *surface;
};

Picture screen_text_bmp = NULL;

static void screen_init()
{
	if (!SDL_WasInit(SDL_INIT_VIDEO) && SDL_Init(SDL_INIT_VIDEO) < 0)
		error_sdl_exit();
}

Screen screen_create(const char *caption, int w, int h)
{
	Screen scr;

	if (w <= 0 || h <= 0)
		error_exit(ERR_OUT_OF_RANGE);

	scr = malloc(sizeof(struct Screen));
	if (scr == NULL)
		error_libc_exit();

	screen_init();

	scr->window = SDL_CreateWindow(
		caption,
		SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED,
		w,
		h,
		0);
	if (scr->window == NULL)
		error_sdl_exit();

	scr->surface = SDL_GetWindowSurface(scr->window);
	if (scr->surface == NULL)
		error_sdl_exit();

	scr->renderer = SDL_CreateSoftwareRenderer(scr->surface);
	if (scr->renderer == NULL)
		error_sdl_exit();

	if (SDL_SetRenderDrawBlendMode(scr->renderer, SDL_BLENDMODE_BLEND) ==
		-1)
		error_sdl_exit();

	return scr;
}

void screen_free(Screen scr)
{
	SDL_DestroyWindow(scr->window);
	SDL_DestroyRenderer(scr->renderer);
}

void screen_end()
{
	if (screen_text_bmp != NULL)
		screen_free_picture(screen_text_bmp);
	SDL_Quit();
}


int screen_width(Screen scr)
{
	return scr->surface->w;
}

int screen_height(Screen scr)
{
	return scr->surface->h;
}

void screen_wait(int ms)
{
	SDL_Delay(ms);
}

void screen_update(Screen scr)
{
	if (SDL_UpdateWindowSurface(scr->window) == -1)
		error_sdl_exit();
}

int screen_get_keyboard_event(SDL_Scancode *sc)
{
	SDL_Event event;
	while (SDL_PollEvent(&event))
		switch (event.type) {
		case SDL_KEYDOWN:
			if (event.key.repeat)
				break;
			if (sc != NULL)
				*sc = event.key.keysym.scancode;
			return SCREEN_KEYDOWN;
		case SDL_KEYUP:
			if (event.key.repeat)
				break;
			if (sc != NULL)
				*sc = event.key.keysym.scancode;
			return SCREEN_KEYUP;
		case SDL_QUIT:
			exit(0);
		}

	return 0;
}

int screen_key_pressed(SDL_Scancode scancode)
{
	int len;
	const uint8_t *state;

	SDL_PumpEvents();
	state = SDL_GetKeyboardState(&len);
	if (len <= scancode || scancode < 0)
		error_exit(ERR_INVALID_SCANCODE);
	return state[scancode];
}

void screen_fill(Screen scr, int r, int g, int b, int a)
{
	if (SDL_SetRenderDrawColor(scr->renderer, r, g, b, a) == -1)
		error_sdl_exit();
	if (SDL_RenderClear(scr->renderer) == -1)
		error_sdl_exit();
}

void screen_place(Screen scr, Picture pict, int x, int y, int sx, int sy,
			int w, int h, int flipped)
{
	SDL_Rect src, dst;
	int total_width = screen_width(scr), total_height = screen_height(scr);
	SDL_Surface *surface;

	if (flipped) {
		sx = pict->f->w - sx - w;
		surface = pict->f;
	} else {
		surface = pict->s;
	}

	if (x > total_width || y > total_height || x + w <= 0 || y + h <= 0)
		return; // Nothing to do here

	src.x = x < 0 ? sx - x : sx;
	src.y = y < 0 ? sy - y : sy;
	src.w = x < 0 ? w + x :
		x + w > total_width ? total_width - x :
		w;
	src.h = y < 0 ? h + y :
		y + h > total_height ? total_height - y:
		h;
	dst.x = x < 0 ? 0 : x;
	dst.y = y < 0 ? 0 : y;
	dst.w = src.w;
	dst.h = src.h;

	if (SDL_BlitSurface(surface, &src, scr->surface, &dst) == -1)
		error_sdl_exit();
}

Picture screen_get_picture(const char *file)
{
	SDL_Surface *raw, *converted, *flipped;
	SDL_PixelFormat *pxf;
	Picture pict = malloc(sizeof(struct Picture));

	if (pict == NULL)
		error_libc_exit();

	raw = SDL_LoadBMP(file);
	if (raw == NULL)
		error_sdl_exit();

	pxf = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888);
	if (pxf == NULL)
		error_sdl_exit();

	converted = SDL_ConvertSurface(raw, pxf, 0);
	if (converted == NULL)
		error_sdl_exit();

	SDL_FreeFormat(pxf);
	SDL_FreeSurface(raw);

	flipped = SDL_CreateRGBSurface(0, raw->w, raw->h, 32,
			pxf->Rmask, pxf->Gmask, pxf->Bmask, pxf->Amask);
	if (flipped == NULL)
		error_sdl_exit();
	for (int y = 0; y < raw->h; y++)
		for (int x = 0; x < raw->w; x++)
			((int*)flipped->pixels)[(y + 1) * raw->w - x - 1] =
				((int*)converted->pixels)[y * raw->w + x];
	pict->s = converted;
	pict->f = flipped;
	return pict;
}

void screen_free_picture(Picture pict)
{
	SDL_FreeSurface(pict->f);
	SDL_FreeSurface(pict->s);
	free(pict);
}

void screen_print_char(Screen scr, int x, int y, char c)
{
	int i;
	if (c >= '0' && c <= '9')
		i = c - '0';
	else if (c >= 'a' && c <= 'z')
		i = c - 'a' + 10;
	else if (c >= 'A' && c <= 'Z')
		i = c - 'A' + 10;
	else if (c == '.')
		i = 36; // This is the copyright mark.
	else if (c == '-')
		i = 40;
	else if (c == '*')
		i = 41;
	else if (c == '!')
		i = 43;
	else
		return;
	screen_place(scr, screen_text_bmp, x, y,
			TEXT_X + (i % TEXT_PER_ROW) * TEXT_W,
			TEXT_Y + (i / TEXT_PER_ROW) * TEXT_H,
			TEXT_W,
			TEXT_H,
			0);
}

void screen_text_print(Screen scr, int x, int y, int w, const char *str)
{
	if (screen_text_bmp == NULL)
		screen_text_bmp = screen_get_picture(TEXT_FILE);

	for (int i = 0; str[i] != '\0'; i++)
		screen_print_char(
			scr,
			x + (w != 0 ? i % w : i) * TEXT_W,
			y + (w != 0 ? i / w : 0) * TEXT_H,
			str[i]);
}

void screen_text_centered(Screen scr, int x, int y, const char *str)
{
	int len = strlen(str);
	screen_text_print(scr, x - len * TEXT_W / 2, y, 0, str);
}