aboutsummaryrefslogtreecommitdiff
path: root/sources/TZR_Init.c
blob: 7f809784fcf186c998e50026184c7884068d75ad (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
#include "TZR.h"
#include "TZR_globals.h"
#include "sdl_error.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <unistd.h>

static int
_sdl_error(void)
{
	sdl_error(-1);
	TZR_Quit();
	return -1;
}

int
_TZR_Init(const TZR_Config *config)
{
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
		return _sdl_error();
	if (IMG_Init(IMG_INIT_PNG) < 0)
		return _sdl_error();

	memcpy(&___tzr_config, config, sizeof(TZR_Config));
	char *const basepath = SDL_GetBasePath();
	if (basepath == NULL)
		return _sdl_error();
	const int chdir_rv = chdir(basepath);
	SDL_free(basepath);
	if (chdir_rv < 0)
		return perror("TZR_Init"), -1;

	___tzr_window = SDL_CreateWindow("TZR", SDL_WINDOWPOS_UNDEFINED,
	                                 SDL_WINDOWPOS_UNDEFINED,
	                                 ___tzr_config.width,
	                                 ___tzr_config.height,
	                                 SDL_WINDOW_RESIZABLE);
	if (___tzr_window == NULL)
		return _sdl_error();

	___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1,
	                                     SDL_RENDERER_ACCELERATED);
	if (___tzr_renderer == NULL)
		return _sdl_error();

	if (SDL_SetRenderDrawBlendMode(___tzr_renderer, SDL_BLENDMODE_BLEND))
		return _sdl_error();

	___tzr_target = SDL_CreateTexture(___tzr_renderer,
	                                  SDL_PIXELFORMAT_RGB888,
	                                  SDL_TEXTUREACCESS_TARGET,
	                                  ___tzr_config.width,
	                                  ___tzr_config.height);
	if (___tzr_target == NULL)
		return _sdl_error();

	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
	___tzr_tick = 0;

	if (___tzr_config.target_fps > 0) {
		___tzr_min_dt = 1000 / ___tzr_config.target_fps;
		___tzr_next_time = SDL_GetTicks64();
	}

	if (!___tzr_config.show_cursor)
		SDL_ShowCursor(0);
	return 0;
}