aboutsummaryrefslogtreecommitdiff
path: root/sources/TZR_Init.c
blob: 586b2cf2fff37c69b5df8a10bb99cf6b06178362 (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
#include "TZR.h"
#include "TZR_globals.h"
#include "sdl_error.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_mouse.h>
#include <errno.h>
#include <unistd.h>

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

int
_TZR_Init(const TZR_Config *config)
{
	memcpy(&___tzr_config, config, sizeof(TZR_Config));

	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
		return _sdl_error();
	if (___tzr_config.mixer == TZR_MIXER_FLAC &&
	    Mix_Init(MIX_INIT_FLAC) != MIX_INIT_FLAC)
	{
		TZR_Log("%s", Mix_GetError());
		___tzr_config.mixer = TZR_MIXER_OFF;
	}

	int chdir_rv;
	if (___tzr_config.basepath == NULL) {
		char *const basepath = SDL_GetBasePath();
		if (basepath == NULL)
			return _sdl_error();
		chdir_rv = chdir(basepath);
		SDL_free(basepath);
	} else
		chdir_rv = chdir(___tzr_config.basepath);
	if (chdir_rv < 0)
		return TZR_Log("%s", strerror(errno)), TZR_Quit(), -1;

	___tzr_window =
	    SDL_CreateWindow(config->title,
	                     SDL_WINDOWPOS_UNDEFINED,
	                     SDL_WINDOWPOS_UNDEFINED,
	                     ___tzr_config.width * ___tzr_config.ratio
	                                         * ___tzr_config.scale,
	                     ___tzr_config.height * ___tzr_config.scale,
	                     SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
	if (___tzr_window == NULL) {
		___tzr_window =
		            SDL_CreateWindow(config->title,
		                             SDL_WINDOWPOS_UNDEFINED,
		                             SDL_WINDOWPOS_UNDEFINED,
		                             ___tzr_config.width * ___tzr_config.ratio
		                                                 * ___tzr_config.scale,
		                             ___tzr_config.height * ___tzr_config.scale,
		                             SDL_WINDOW_RESIZABLE );
	}
	if (___tzr_window == NULL)
		return _sdl_error();

#ifdef TZR_OPENGL
	___tzr_gl_ctx = SDL_GL_CreateContext(___tzr_window);
	if (___tzr_gl_ctx == NULL) {
		TZR_Log("%s", SDL_GetError());
		___tzr_config.mipmaps = false;
	} else if (___tzr_config.mipmaps) do {
		const char *gl_version = (const char *)glGetString(GL_VERSION);
		if (gl_version == NULL || strlen(gl_version) < 3) {
			___tzr_config.mipmaps = false;
			break;
		}
		const int major = atoi(gl_version);
		const int minor = atoi(gl_version + 2);
		if (major < 3)
			___tzr_config.mipmaps = false;
	} while (0);
#endif /* TZR_OPENGL */

#ifdef __EMSCRIPTEN__
	/* TODO: verify than this is unneeded */
	___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1,
	                                     SDL_RENDERER_SOFTWARE);
#else
	___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1,
	                                     SDL_RENDERER_ACCELERATED);
	if (___tzr_renderer == NULL) {
		TZR_Log("%s", SDL_GetError());
		___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1,
		                                     SDL_RENDERER_SOFTWARE);
	}
#endif /* __EMSCRIPTEN__ */
	if (___tzr_renderer == NULL)
		return _sdl_error();

	if (TZR_SetViewportSize(___tzr_config.width, ___tzr_config.height)) {
		TZR_Quit();
		return -1;
	}

	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,
	            ___tzr_config.scale_linear ? "1" : "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(SDL_FALSE);
	___tzr_mouse_x = ___tzr_config.width / 2;
	___tzr_mouse_y = ___tzr_config.height / 2;

	/* Setup audio. */
	do if (___tzr_config.mixer) {
		if (Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 8, 1024) < 0) {
			sdl_error(0);
			Mix_Quit();
			___tzr_config.mixer = false;
			break;
		}

#ifdef TZR_SOLOUD
		___tzr_soloud = Soloud_create();
		if (___tzr_soloud == NULL) {
			TZR_Log("Soloud_create failed");
			Mix_Quit();
			___tzr_config.mixer = false;
			break;
		}

		if (Soloud_init(___tzr_soloud)) {
			TZR_Log("Soloud_init failed");
			Soloud_destroy(___tzr_soloud);
			___tzr_soloud = NULL;
			Mix_Quit();
			___tzr_config.mixer = false;
			break;
		}
#endif /* TZR_SOLOUD */
	} while (0);

	return 0;
}