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
|
#include "TZR.h"
#include "TZR_globals.h"
#include "sdl_error.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_mouse.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) < 0)
return _sdl_error();
if (IMG_Init(IMG_INIT_PNG * ___tzr_config.png_loading) < 0)
return _sdl_error();
if (___tzr_config.mixer == TZR_MIXER_FLAC &&
Mix_Init(MIX_INIT_FLAC) != MIX_INIT_FLAC)
{
SDL_Log("%s", Mix_GetError());
___tzr_config.mixer = TZR_MIXER_OFF;
}
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.scale,
___tzr_config.height * ___tzr_config.scale,
SDL_WINDOW_RESIZABLE);
if (___tzr_window == NULL)
return _sdl_error();
#ifdef __EMSCRIPTEN__
___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1,
SDL_RENDERER_SOFTWARE);
#else
___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1,
SDL_RENDERER_ACCELERATED);
#endif
if (___tzr_renderer == NULL)
return _sdl_error();
if (___tzr_config.hd_render) {
if (SDL_RenderSetLogicalSize(___tzr_renderer,
___tzr_config.width,
___tzr_config.height) < 0)
return _sdl_error();
} else {
___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(SDL_FALSE);
___tzr_mouse_x = ___tzr_config.width / 2;
___tzr_mouse_y = ___tzr_config.height / 2;
/* Setup audio. */
if (___tzr_config.mixer) {
if (Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 8, 1024) < 0) {
sdl_error(0);
Mix_Quit();
___tzr_config.mixer = false;
}
}
return 0;
}
|