generated from sethcg/c-vcpkg-sdl3-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (135 loc) · 5.03 KB
/
main.cpp
File metadata and controls
165 lines (135 loc) · 5.03 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
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
#define SDL_MAIN_USE_CALLBACKS 1 // USE CALLBACKS INSTEAD OF THE "main()" FUNCTION
#include <stdlib.h>
#include <time.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <imgui.h>
#include <imgui_impl_sdl3.h>
#include <imgui_impl_sdlrenderer3.h>
extern "C" {
#include <Grid.h>
#include <WilsonMazeGeneration.h>
#include <AStarPathfinding.h>
}
#define WINDOW_WIDTH 800 + (GRID_PADDING_SIZE * 2)
#define WINDOW_HEIGHT 800 + (GRID_PADDING_SIZE * 2)
typedef struct AppContext {
SDL_Window* window;
SDL_Renderer* renderer;
ImDrawData *data;
bool isRunning;
bool hasMaze;
MazeContext* mazeContext;
bool showPath;
bool showGradient;
PathContext* pathContext;
cell* cells;
} AppContext;
// THIS FUNCTION RUNS ONCE AT STARTUP
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
srand(time(NULL));
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Error %s", SDL_GetError());
return SDL_APP_FAILURE;
}
// SETUP APP STATE
AppContext* appContext = (AppContext*) SDL_malloc(sizeof(AppContext));
appContext->cells = (cell*) malloc(GRID_ARRAY_SIZE * sizeof(cell));
appContext->isRunning = false;
appContext->hasMaze = false;
appContext->showPath = true;
appContext->showGradient = false;
if (appContext == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Error %s", SDL_GetError());
return SDL_APP_FAILURE;
}
*appstate = appContext;
if (!SDL_CreateWindowAndRenderer("Maze Generation", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &appContext->window, &appContext->renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Error %s", SDL_GetError());
return SDL_APP_FAILURE;
}
// SETUP IMGUI
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
io.IniFilename = NULL;
io.LogFilename = NULL;
ImGui::StyleColorsDark();
ImGui_ImplSDL3_InitForSDLRenderer(appContext->window, appContext->renderer);
ImGui_ImplSDLRenderer3_Init(appContext->renderer);
// CREATE/DRAW GRID
CreateGrid(appContext->renderer, appContext->cells);
return SDL_APP_CONTINUE;
}
// THIS FUNCTION RUNS WHEN A NEW EVENT OCCURS (MOUSE INPUT, KEY PRESS, ETC.)
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event* event) {
AppContext* appContext = (AppContext*) appstate;
switch (event->type) {
case SDL_EVENT_QUIT:
return SDL_APP_SUCCESS;
case SDL_EVENT_KEY_DOWN:
switch(event->key.key) {
case SDLK_ESCAPE:
return SDL_APP_SUCCESS;
case SDLK_KP_BACKSPACE:
case SDLK_BACKSPACE:
appContext->mazeContext = Init_WilsonMaze(appContext->cells);
appContext->isRunning = true;
case SDLK_KP_ENTER:
case SDLK_RETURN:
if(appContext->hasMaze && !appContext->isRunning) {
appContext->pathContext = Init_AStarPathfinding(0, (GRID_ARRAY_SIZE - 1), appContext->cells);
}
}
}
ImGui_ImplSDL3_ProcessEvent(event);
return SDL_APP_CONTINUE;
}
// THIS FUNCTION RUNS ONCE PER FRAME
SDL_AppResult SDL_AppIterate(void *appstate) {
AppContext* appContext = (AppContext*) appstate;
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
ImGui::SetNextWindowSize(ImVec2(136, 0));
ImGui::Begin("Control Panel", NULL, ImGuiWindowFlags_NoCollapse);
if (ImGui::Button("Generate", ImVec2(120, 20))) {
if(!appContext->isRunning) {
appContext->mazeContext = Init_WilsonMaze(appContext->cells);
appContext->isRunning = true;
}
}
if (ImGui::Button("Solve", ImVec2(120, 20))) {
if(appContext->hasMaze && !appContext->isRunning) {
appContext->pathContext = Init_AStarPathfinding(0, (GRID_ARRAY_SIZE - 1), appContext->cells);
}
}
ImGui::Checkbox("Path", &appContext->showPath);
ImGui::Checkbox("Gradient", &appContext->showGradient);
ImGui::End();
// RENDERING EACH WILSON'S ALGORITHM WALK ITERATION
if(appContext->isRunning) {
if(!Continue_WilsonMaze(appContext->mazeContext, appContext->cells)) {
appContext->isRunning = false;
appContext->hasMaze = true;
}
}
// RENDERING
ImGui::Render();
DrawGrid(appContext->renderer, appContext->cells, appContext->showPath, appContext->showGradient);
appContext->data = ImGui::GetDrawData();
ImGui_ImplSDLRenderer3_RenderDrawData(appContext->data, appContext->renderer);
SDL_RenderPresent(appContext->renderer);
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
AppContext* appContext = (AppContext*) appstate;
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(appContext->renderer);
SDL_DestroyWindow(appContext->window);
SDL_Quit();
SDL_free(appContext);
}