summaryrefslogtreecommitdiffhomepage
path: root/src/window/rodeo_window.c
blob: e92382e1ddc85194487ed412d0d6a8932c83735c (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

// -- internal --
// public
#include "rodeo/window.h"
#include "rodeo/log.h"
// private
#include "window/irodeo_window.h"

// -- external --
#include "SDL.h"
#include "stc/cstr.h"

static irodeo_window_state_t irodeo_window_state = {0};

void
rodeo_window_init(
	uint16_t screen_height,
	uint16_t screen_width,
	cstr title
)
{
	irodeo_window_state.window = NULL;
	irodeo_window_state.screen_surface = NULL;
	irodeo_window_state.screen_height = screen_height;
	irodeo_window_state.screen_width = screen_width;

	rodeo_log(
		rodeo_logLevel_info,
		"Initializing SDL..."
	);

	{
		uint32_t init_flags_sdl = 0;
		init_flags_sdl = init_flags_sdl | SDL_INIT_VIDEO;
		init_flags_sdl = init_flags_sdl | SDL_INIT_AUDIO;
		init_flags_sdl = init_flags_sdl | SDL_INIT_GAMECONTROLLER;
	
		if(SDL_Init(init_flags_sdl) < 0)
		{
			rodeo_log(
				rodeo_logLevel_error,
				"Failed to initialize SDL. SDL_Error: %s",
				SDL_GetError()
			);
			exit(EXIT_FAILURE);
		}
		rodeo_log(
			rodeo_logLevel_info,
			"Success initializing SDL"
		);
	}

	rodeo_log(
		rodeo_logLevel_info,
		"Initializing SDL window..."
	);
	irodeo_window_state.window = SDL_CreateWindow(
			cstr_str(&title),
			SDL_WINDOWPOS_UNDEFINED,
			SDL_WINDOWPOS_UNDEFINED,
			screen_width,
			screen_height,
			SDL_WINDOW_SHOWN //| SDL_WINDOW_RESIZABLE
			);
	if(irodeo_window_state.window == NULL)
	{
		rodeo_log(
			rodeo_logLevel_error,
			"Failed creating SDL window. SDL_Error: %s",
			SDL_GetError()
		);
		exit(EXIT_FAILURE);
	}
	rodeo_log(
		rodeo_logLevel_info,
		"Success initializing SDL window"
	);

#if !__EMSCRIPTEN__
	rodeo_log(
		rodeo_logLevel_info,
		"SDL setting up driver specific information..."
	);
	SDL_VERSION(&irodeo_window_state.wmi.version);
	if(
		!SDL_GetWindowWMInfo(
			irodeo_window_state.window,
			&irodeo_window_state.wmi  
		)
	)
	{
		rodeo_log(
			rodeo_logLevel_error,
			"SDL failed to get driver specific information. SDL Error: %s", SDL_GetError()
		);
		exit(EXIT_FAILURE);
	}
	rodeo_log(
		rodeo_logLevel_info,
		"Success getting driver specific information"
	);
#endif
}

void
rodeo_window_deinit(void)
{
	SDL_DestroyWindow(irodeo_window_state.window);
	SDL_Quit();
}

void
irodeo_window_update_screen_size(void)
{
	int32_t width = 0;
	int32_t height = 0;
	SDL_GetWindowSize(
		irodeo_window_state.window,
		&width,
		&height
	);
	irodeo_window_state.screen_width = (uint32_t)width;
	irodeo_window_state.screen_height = (uint32_t)height;
}

uint32_t
rodeo_window_screen_width_get(void)
{
	irodeo_window_update_screen_size();
	return irodeo_window_state.screen_width;
}

uint32_t
rodeo_window_screen_height_get(void)
{
	irodeo_window_update_screen_size();
	return irodeo_window_state.screen_height;
}

void
irodeo_window_screen_width_setVar(uint16_t width)
{
	irodeo_window_state.screen_width = width;
}

void
irodeo_window_screen_height_setVar(uint16_t height)
{
	irodeo_window_state.screen_height = height;
}

bool
rodeo_window_shouldQuit(void)
{
	return irodeo_window_state.quit;
}

void
rodeo_window_quit(void)
{
	irodeo_window_state.quit = true;
}

SDL_Window *
irodeo_window_get(void)
{
	return irodeo_window_state.window;
}

SDL_Surface *
irodeo_window_surface_get(void)
{
	return irodeo_window_state.screen_surface;
}

SDL_SysWMinfo
irodeo_window_wmi_get(void)
{
	return irodeo_window_state.wmi;
}