-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
349 lines (295 loc) · 9.86 KB
/
main.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "main.h" // globals
#include "render.h"
#include "input.h"
#include "sprite.h"
#include "addons.h"
#include "audio.h"
#include "png.h"
#include <iostream>
#include <fstream>
#include <vector>
enum class MenuState {
MAIN_MENU,
OPTIONS,
EXIT,
};
// System definitions
MenuState currentMenu = MenuState::MAIN_MENU;
double delta;
// Debug definitions
WindowDimensions dims;
// Resource definitions
Mix_Chunk *bell;
Mix_Music *bgm;
// Color definitions
Uint32 gPink;
Uint32 gRed;
Uint32 gBeige;
Uint32 gBlue;
Uint32 gDarkblue;
Uint32 gDarkgreen;
// edge padding 10px
// inner padding 5px
// buttons 60x40
// sprite wide 125x40
// for window size see int main
// infoframe 200x295
// Bracketed coordinates are the drawing x,y, where w,h, are only used with DrawScaled.
// Braced coordinates are the raw location and size on the spritesheet: x,y,w,h
Sprite spriteBg(0, 0, 0, 0, "assets/spritesheet.png", {0,280,480,320});
Sprite spriteBorder(0, 0, 0, 0, "assets/spritesheet.png", {480,280,480,320});
Sprite spriteFrame(240, 10, 0, 0, "assets/spritesheet.png", {960,280,200,295});
Sprite spriteDrop(10, 10, 80, 140, "assets/spritesheet.png", {0,80,120,198});
Sprite spriteMute(10, 180, 0, 0, "assets/spritesheet.png", {0,0,60,40});
Sprite spritePause(75, 180, 0, 0, "assets/spritesheet.png", {60,0,60,40});
Sprite spriteExit(10, 270, 0, 0, "assets/spritesheet.png", {120,0,60,40});
Sprite spriteTests(10, 225, 0, 0, "assets/spritesheet.png", {180,0,60,40});
void initSprites(){
spriteMute.SetToggleCallback([](bool toggled){
if(toggled){
Mix_VolumeMusic(0);
std::cout << "Mixer muted." << std::endl;
//std::cout << "mouse clicked at xy pos: " << mouse.point.x << "," << mouse.point.y
// << " toggled was: " << spriteMute.toggled << std::endl;
//std::cout << "got spriteMute wh bounds as: " << spriteMute.w << "," << spriteMute.h << std::endl;
} else {
Mix_VolumeMusic(20);
std::cout << "Mixer unmuted." << std::endl;
}
});
spritePause.SetToggleCallback([](bool toggled){
if(toggled){
Mix_PauseMusic();
std::cout << "Mixer paused." << std::endl;
} else {
Mix_ResumeMusic();
std::cout << "Mixer resumed." << std::endl;
}
});
}
void ReadTextChunks(png_structp png, png_infop info){
int num_text = 0;
png_textp text_data = NULL;
// Retrieve the text chunks (tEXt, zTXt, iTXt)
num_text = png_get_text(png, info, &text_data, &num_text);
if (!num_text){
std::cout << "No text metadata found in the png file." << std::endl;
return;
}
//Loop through each text chunk
for (int i = 0; i < num_text; ++i){
std::cout << "Chunk key: " << text_data[i].key << std::endl;
std::cout << "Text: " << text_data[i].text << std::endl;
//Determine chunk type
switch (text_data[i].compression){
case PNG_TEXT_COMPRESSION_NONE:
std::cout << "Chunk type: tEXt (uncompressed)" << std::endl;
break;
case PNG_TEXT_COMPRESSION_zTXt:
std::cout << "Chunk type: TXt (compressed)" << std::endl;
break;
case PNG_ITXT_COMPRESSION_NONE:
case PNG_ITXT_COMPRESSION_zTXt:
std::cout << "Chunk type: zTXt (international text)" << std::endl;
break;
default:
std::cout << "Chunk type: unknown" << std::endl;
}
std::cout << "-----------------------------------" << std::endl;
}
}
void PrintPNGInfo(const char* filename){
// std::ifstream automatically closes the file when it goes out of scope.
// So no need to fclose(file) repeatedly.
// Open the PNG file using std::ifstream
std::ifstream file(filename, std::ios::binary);
if (!file){
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
// Read the file content into a vector
std::vector<unsigned char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
// Init libpng structs
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png){
std::cerr << "png_create_read_struct failed.\n" << std::endl;
return;
}
png_infop info = png_create_info_struct(png);
if (!info){
std::cerr << "png_create_info_struct failed.\n" << std::endl;
png_destroy_read_struct(&png, NULL, NULL);
return;
}
if (setjmp(png_jmpbuf(png))){
std::cerr << "libpng error!\n" << std::endl;
png_destroy_read_struct(&png, &info, NULL);
return;
}
/**
* @brief Process the input buffer for libpng.
* @details Read PNG data directly from the std::vector with a custom callback function
* @param &buffer is the raw pointer that was passed to libpng; buf is a reference to the
* dereferenced pointer retrieved from png_get_io_ptr(png).
*/
png_set_read_fn(png, &buffer, [](png_structp png, png_bytep data, png_size_t length){
auto &buf = *static_cast<std::vector<unsigned char>*>(png_get_io_ptr(png));
std::copy(buf.begin(), buf.begin() + length, data);
buf.erase(buf.begin(), buf.begin() + length);
});
// Read the PNG header
png_read_info(png, info);
// Print text metadata
ReadTextChunks(png, info);
// Get generics of the PNG
png_uint_32 width, height;
int bit_depth, color_type;
png_get_IHDR(png, info, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
std::cout << "PNG Info " << "Width: " << width << ", Height: " << height
<< ", Bit Depth: " << bit_depth << std::endl;
//Cleanup
png_destroy_read_struct(&png, &info, NULL);
}
void DetectIntersections(Mouse &mouse){
// Interactable sprites need this, sets sprite.hasintersection accordingly
spriteMute.DetectIntersections(mouse);
spritePause.DetectIntersections(mouse);
spriteTests.DetectIntersections(mouse);
spriteExit.DetectIntersections(mouse);
}
void UpdateInteractions(Mouse &mouse, SDL_Event &e){
// MOUSE: Update the getters mouse coords
mouse.GetXY();
// MOUSE: Update sprite-mouse intersections
DetectIntersections(mouse);
}
void renderMainMenuState(RendererBase &ren, Mouse &mouse, SDL_Event &e){
UpdateInteractions(mouse, e);
ren.Draw(mouse,spriteExit,spriteTests,spriteDrop,spriteMute,
spritePause,spriteBorder,spriteFrame,
spriteBg);
ren.Present();
}
void renderStates(RendererBase &ren, Mouse &mouse, SDL_Event &e){
switch (currentMenu) {
case MenuState::MAIN_MENU:
renderMainMenuState(ren,mouse,e);
break;
case MenuState::OPTIONS:
//TODO renderOptionMenuState(...);
break;
case MenuState::EXIT:
break;
default:
break;
}
}
void EventHandlerMainMenu(RendererBase &ren, Mouse &mouse, const SDL_Event &e) {
switch (e.type){
case SDL_WINDOWEVENT:
switch (e.window.event){
case SDL_WINDOWEVENT_RESIZED:
//std::cout << "resize detected: " << e.window.data1 << "x" << e.window.data2 << std::endl;
// Reacquire window surface if it becomes invalid
gScreen = SDL_GetWindowSurface(gWindow);
if (!gScreen){
std::cerr << "SDL_GetWindowSurface failed during resize: "
<< SDL_GetError() << std::endl;
}
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONUP:
switch (e.button.button){
case SDL_BUTTON_LEFT:
if (spriteExit.hasintersection){
currentMenu = MenuState::EXIT;
} else if (spriteMute.hasintersection){
spriteMute.Toggle();
} else if (spritePause.hasintersection){
spritePause.Toggle();
}
break;
default:
break;
}
break;
case SDL_KEYDOWN:
switch (e.key.keysym.sym){
case SDLK_ESCAPE:
currentMenu = MenuState::EXIT;
break;
case SDLK_m:
Mix_VolumeMusic(0);
std::cout << "Mixer muted via m." << std::endl; //TODO needs to be toggleable
break;
}
break;
case SDL_DROPFILE: {
char* libpng_droppedfile = e.drop.file;
if (libpng_droppedfile && libpng_droppedfile[0] != '\0') {
std::cout << "File dropped: " << libpng_droppedfile << std::endl;
PrintPNGInfo(libpng_droppedfile);
SDL_free(libpng_droppedfile);
}else{
std::cerr << "Error: Dropped file was null or empty." << std::endl;
}
break;
}
}
}
void EventHandlerGlobal(RendererBase &ren, Mouse &mouse, SDL_Event &e){
// Global event handling for the windows X sprite
if (e.type == SDL_QUIT){
currentMenu = MenuState::EXIT;
return;
}
// Global event handling for Ctrl+q
if (e.type == SDL_KEYDOWN){
if ((e.key.keysym.sym == SDLK_q || e.key.keysym.sym == SDLK_q) &&
(e.key.keysym.mod & KMOD_CTRL)) {
currentMenu = MenuState::EXIT;
return;
}
}
// State-specific event handling
switch (currentMenu) {
case MenuState::MAIN_MENU:
EventHandlerMainMenu(ren,mouse,e);
break;
case MenuState::OPTIONS:
//TODO EventHandlerOptionsMenu(...);
std::cout << "Not implemented, exiting..." << std::endl;
currentMenu = MenuState::EXIT;
break;
case MenuState::EXIT:
break;
default:
break;
}
}
int main (int argc, char *argv[]){
RendererBase ren;
Audio audio;
ren.initVideo(480, 320);
ren.initColors(gScreen);
initSprites();
audio.initMixer();
Uint32 starting_tick;
currentMenu = MenuState::MAIN_MENU;
Mouse mouse(24, 24, "assets/mouse00.png"); // TODO moving this out of main next to the other resources doesnt apply SDL_ShowCursor(false)
while (currentMenu != MenuState::EXIT){
starting_tick = SDL_GetTicks();
SDL_Event e;
while (SDL_PollEvent(&e)){
EventHandlerGlobal(ren,mouse,e); // handle events for the current state
}
renderStates(ren,mouse,e); // render the current state
ren.cap_framerate(starting_tick);
}
audio.Shutdown(bell, bgm);
ren.Shutdown(gWindow, dims);
return 0;
}