-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·136 lines (109 loc) · 3.41 KB
/
main.cpp
File metadata and controls
executable file
·136 lines (109 loc) · 3.41 KB
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
#define ENET_IMPLEMENTATION
#define SDL_MAIN_USE_CALLBACKS
#include "src/managers/cursormanager.h"
#include "src/managers/gamemanager.h"
#include "src/managers/keyboardmanager.h"
#include "src/managers/animationmanager.h"
#include "src/managers/ecsmanager.h"
#include "src/managers/fontmanager.h"
#include "src/managers/netmanager.h"
#include "src/panes/home.h"
#include "src/shared/enet.h"
#include "src/utility/renderwindow.h"
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_main.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <cstdio>
struct AppState
{
SDL_Window *window{nullptr};
SDL_Renderer *renderer{nullptr};
bool running{true};
uint64_t current_tick{0};
uint64_t last_tick{0};
};
SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv)
{
*appstate = new AppState();
AppState& state = *static_cast<AppState*>(*appstate);
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize SDL: %s\n", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("Table Tennis", RenderWindow::SCREEN_WIDTH / 2, RenderWindow::SCREEN_HEIGHT / 2,
SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_OPENGL, &state.window, &state.renderer))
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't create window and renderer: %s\n", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetWindowPosition(state.window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
if (!MIX_Init())
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize Mixer: %s\n", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!TTF_Init())
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize TTF: %s\n", SDL_GetError());
return SDL_APP_FAILURE;
}
if (enet_initialize() < 0)
{
fprintf(stderr, "Couldn't initialize ENet.\n");
exit(EXIT_FAILURE);
}
CursorManager::load_cursors();
FontManager::init();
GameManager::switch_scene(nullptr, new HomePane(state.renderer));
state.current_tick = SDL_GetPerformanceCounter();
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
const auto *state = static_cast<AppState*>(appstate);
switch (event->type)
{
case SDL_EVENT_QUIT: {
return SDL_APP_SUCCESS;
}
default:
KeyboardManager::pre_event(event);
GameManager::current_pane->on_event(event);
EcsManager::event(event);
KeyboardManager::post_event(state->window);
}
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppIterate(void *appstate)
{
auto *state = static_cast<AppState*>(appstate);
state->last_tick = state->current_tick;
state->current_tick = SDL_GetPerformanceCounter();
const double delta_time = (double)(state->current_tick - state->last_tick) / (double)SDL_GetPerformanceFrequency();
if (SDL_GetWindowFlags(state->window) & SDL_WINDOW_OCCLUDED)
{
SDL_Delay(100);
}
EcsManager::sort();
CursorManager::force_set_cursor(CursorManager::arrow_cursor);
SDL_SetRenderDrawColor(state->renderer, 203, 211, 235, 255);
SDL_RenderClear(state->renderer);
NetManager::update(delta_time);
EcsManager::update(delta_time);
GameManager::current_pane->on_gui(delta_time);
AnimationManager::update(delta_time);
CursorManager::update();
SDL_RenderPresent(state->renderer);
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
const auto *state = static_cast<AppState*>(appstate);
delete state;
FontManager::close();
SDL_Quit();
MIX_Quit();
TTF_Quit();
enet_deinitialize();
}