-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (100 loc) · 4.69 KB
/
main.cpp
File metadata and controls
115 lines (100 loc) · 4.69 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
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_main.h>
#include <engine/engine.hpp>
#include <engine/logger.hpp>
// Initialises subsystems and initialises appState to be used by all other main
// functions.
SDL_AppResult SDL_AppInit(void **appState, int argc, char *argv[]) {
SPDLOG_INFO("Initializing application.");
Engine::Logging::Init();
Engine::Engine *gameEngine = &Engine::Engine::Instance();
SDL_SetAppMetadata("GameEngine", "0.0.1",
"org.acm.pesuecc.aiep.game-engine");
SPDLOG_INFO("Initializing game engine.");
if (!gameEngine->Init()) {
SPDLOG_CRITICAL("Failed to initialize game engine.");
return SDL_APP_FAILURE;
}
SPDLOG_INFO("Game engine initialized successfully.");
SPDLOG_INFO("Setting window dimensions to 1000x800.");
gameEngine->GetWindow().SetDimensions(1000, 800);
SPDLOG_INFO("Registering event callbacks.");
gameEngine->GetEvents().RegisterCallback(
Engine::EventType::WindowResize, [gameEngine](Engine::EventData data) {
SPDLOG_INFO("Window resized to {} x {}", data.window.width,
data.window.height);
gameEngine->GetWindow().SetDimensions(data.window.width,
data.window.height);
});
gameEngine->GetEvents().RegisterCallback(
Engine::EventType::KeyDown, [](Engine::EventData data) {
SPDLOG_INFO("Key pressed: {} (keycode)", data.keyboard.keycode);
SPDLOG_INFO("Key pressed: {} (scancode)",
static_cast<unsigned int>(data.keyboard.scancode));
SPDLOG_INFO("Key pressed modifiers: {}", data.keyboard.modifiers);
});
gameEngine->GetEvents().RegisterCallback(
Engine::EventType::KeyUp, [](Engine::EventData data) {
SPDLOG_INFO("Key released: {} (keycode)", data.keyboard.keycode);
SPDLOG_INFO("Key released: {} (scancode)",
static_cast<unsigned int>(data.keyboard.scancode));
SPDLOG_INFO("Key released modifiers: {}", data.keyboard.modifiers);
});
gameEngine->GetEvents().RegisterCallback(
Engine::EventType::MouseButtonDown,
[](Engine::EventData data) { SPDLOG_INFO("Mouse buttons pressed"); });
gameEngine->GetEvents().RegisterCallback(
Engine::EventType::MouseWheel, [](Engine::EventData data) {
SPDLOG_INFO("Mouse wheel scrolled: {:.0f}", data.mouse.y);
});
gameEngine->GetEvents().RegisterCallback(
Engine::EventType::MouseButtonUp,
[](Engine::EventData data) { SPDLOG_INFO("Mouse buttons released."); });
gameEngine->GetEvents().RegisterCallback(
Engine::EventType::MouseMove, [](Engine::EventData data) {
SPDLOG_INFO("Mouse moved to: ({:.0f}, {:.0f})", data.mouse.x,
data.mouse.y);
});
SPDLOG_INFO("Application initialized successfully.");
*appState = gameEngine;
return SDL_APP_CONTINUE;
}
// The event handling function called by SDL.
// TODO: Try and make a genericsed function that is called from here to allow
// custom event handling to be added by a user.
SDL_AppResult SDL_AppEvent(void *appState, SDL_Event *event) {
Engine::Engine *gameEngine = static_cast<Engine::Engine *>(appState);
if (event->type == SDL_EVENT_QUIT || event->type == SDL_EVENT_TERMINATING) {
SPDLOG_INFO("Received SDL_EVENT_QUIT or SDL_EVENT_TERMINATING. Exiting "
"application.");
return SDL_APP_SUCCESS;
} else {
SPDLOG_DEBUG("Processing SDL event of type: {}", event->type);
gameEngine->GetEvents().ProcessEvent(event);
}
return SDL_APP_CONTINUE;
}
// The "main loop" of the window.
SDL_AppResult SDL_AppIterate(void *appState) {
Engine::Engine *gameEngine = static_cast<Engine::Engine *>(appState);
SPDLOG_TRACE("Starting main loop iteration.");
SPDLOG_DEBUG("Clearing renderer with color: Black.");
gameEngine->GetRenderer().Clear(Engine::Color::Black());
SPDLOG_DEBUG("Rendering all objects.");
gameEngine->GetRenderManager().RenderAll(gameEngine->GetRenderer());
SPDLOG_DEBUG("Presenting rendered frame.");
gameEngine->GetRenderer().Present();
SPDLOG_TRACE("Main loop iteration completed.");
return SDL_APP_CONTINUE;
}
// Cleans up the initialised subsystems.
void SDL_AppQuit(void *appState, SDL_AppResult result) {
Engine::Engine *gameEngine = static_cast<Engine::Engine *>(appState);
if (gameEngine != nullptr) {
SPDLOG_INFO("Shutting down game engine.");
gameEngine->Shutdown();
SPDLOG_INFO("Game engine shutdown completed.");
} else {
SPDLOG_WARN("Game engine was null during shutdown.");
}
}