2
2
#include < SDL2/SDL.h>
3
3
#include < SDL2/SDL_image.h>
4
4
#include < SDL2/SDL_ttf.h>
5
- #define GLEW_STATIC
6
- #include < GL/glew.h>
5
+ #include " imgui.h"
6
+ #include " imgui_impl_sdl2.h"
7
+ #include " imgui_impl_sdlrenderer2.h"
7
8
#include < iostream>
8
9
#include < stdio.h>
9
10
#include < stdlib.h>
10
11
#include < stdbool.h>
11
- #include < fstream>
12
12
#include < string>
13
13
#include < vector>
14
14
#include < map>
19
19
#include " GFX/Animation.hpp"
20
20
#include " GFX/SDL_FontCache.h"
21
21
#include " Menu/Lang.hpp"
22
+
23
+ #if !SDL_VERSION_ATLEAST(2,0,17)
24
+ #error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function
25
+ #endif
26
+
22
27
typedef std::chrono::high_resolution_clock Clock;
23
28
24
29
int main (int argc, char * argv[])
25
30
{
26
- SDL_SetHint (SDL_HINT_RENDER_DRIVER, " opengl" ); // Always use OpenGL
27
- SDL_GLContext glContext;
28
- SDL_Event e;
29
-
30
31
// SDL Init
31
32
if (SDL_Init (SDL_INIT_EVERYTHING) != 0 ) {
32
33
fprintf (stderr, " SDL_Init Error: %s\n " , SDL_GetError ());
33
34
return EXIT_FAILURE;
34
35
}
35
36
// Initialize window
36
- SDL_Window* win = SDL_CreateWindow (" Alchemy++ alpha v0.1. 2" , 64 , 64 , 800 , 600 , SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL |SDL_WINDOW_RESIZABLE);
37
+ SDL_Window* win = SDL_CreateWindow (" Alchemy++ alpha v0.2" , 64 , 64 , 800 , 600 , SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
37
38
if (win == NULL ) {
38
39
fprintf (stderr, " SDL_CreateWindow Error: %s\n " , SDL_GetError ());
39
40
return EXIT_FAILURE;
@@ -43,9 +44,8 @@ int main(int argc, char* argv[])
43
44
printf (" SDL_image could not initialize! SDL_image Error: %s\n " , IMG_GetError ());
44
45
}
45
46
}
46
- glContext = SDL_GL_CreateContext (win);
47
47
48
- SDL_Renderer* ren = SDL_CreateRenderer (win, -1 , SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC );
48
+ SDL_Renderer* ren = SDL_CreateRenderer (win, -1 , SDL_RENDERER_ACCELERATED);
49
49
if (ren == NULL ) {
50
50
fprintf (stderr, " SDL_CreateRenderer Error: %s\n " , SDL_GetError ());
51
51
if (win != NULL ) {
@@ -55,6 +55,16 @@ int main(int argc, char* argv[])
55
55
return EXIT_FAILURE;
56
56
}
57
57
58
+ // Set up ImGui
59
+ IMGUI_CHECKVERSION ();
60
+ ImGui::CreateContext ();
61
+ ImGuiIO& io = ImGui::GetIO (); (void )io;
62
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
63
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
64
+ ImGui::StyleColorsDark (); // Use dark mode by default
65
+ ImGui_ImplSDL2_InitForSDLRenderer (win, ren);
66
+ ImGui_ImplSDLRenderer2_Init (ren); // Init for SDL renderer
67
+
58
68
SDL_Texture* tex = IMG_LoadTexture (ren, " gamedata/default/textures/backgrounds/emptyuniverse.png" );
59
69
SDL_Texture* addBtn = IMG_LoadTexture (ren, " gamedata/default/textures/buttons/addBtn.png" );
60
70
if (tex == NULL ) {
@@ -67,7 +77,9 @@ int main(int argc, char* argv[])
67
77
// Initialize font
68
78
TTF_Init ();
69
79
FC_Font* font = FC_CreateFont ();
70
- FC_LoadFont (font, ren, " gamedata/default/font/Droid-Sans.ttf" , 12 , FC_MakeColor (255 ,255 ,255 ,255 ), TTF_STYLE_NORMAL);
80
+ FC_LoadFont (font, ren, " gamedata/default/font/Open-Sans.ttf" , 12 , FC_MakeColor (255 ,255 ,255 ,255 ), TTF_STYLE_NORMAL);
81
+
82
+ // FC_LoadFontFromTTF(font, ren, ttfFont, FC_MakeColor(255,255,255,255));
71
83
72
84
elem::JSONInit (); // Initialize JSON
73
85
Text::loadAll (" en-us" ); // Load game text
@@ -79,6 +91,7 @@ int main(int argc, char* argv[])
79
91
80
92
elem::spawnDraggable (draggables, 288 , 208 , " air" );
81
93
elem::spawnDraggable (draggables, 50 , 50 , " fire" );
94
+
82
95
DraggableElement* selectedElem = NULL ; // Currently selected draggable
83
96
84
97
bool leftClickDown = false ; // Left click state, used to track drag and drop
@@ -98,6 +111,8 @@ int main(int argc, char* argv[])
98
111
99
112
// deltaTime = endTick-startTick;
100
113
114
+ SDL_Event e;
115
+ ImGui_ImplSDL2_ProcessEvent (&e);
101
116
while (SDL_PollEvent (&e)) {
102
117
switch (e.type ) {
103
118
case SDL_QUIT:
@@ -158,11 +173,9 @@ int main(int argc, char* argv[])
158
173
}
159
174
leftClickTick = SDL_GetTicks64 (); // Get next click tick
160
175
}
176
+
161
177
if (!rightClickDown && e.button .button == SDL_BUTTON_RIGHT) {
162
178
rightClickDown = true ;
163
- if (selectedElem != NULL ) {
164
- std::cout << " Selected: " << selectedElem->id << std::endl;
165
- }
166
179
}
167
180
break ;
168
181
}
@@ -197,23 +210,29 @@ int main(int argc, char* argv[])
197
210
elem::secondParentElem = NULL ;
198
211
}
199
212
}
200
- // Load textures
213
+
214
+ // Load draggable element textures
201
215
for (auto &d : draggables) {
202
216
elem::loadTexture (ren, d.get ());
203
217
}
204
218
SDL_RenderClear (ren);
205
219
220
+ // Start the Dear ImGui frame
221
+ ImGui_ImplSDLRenderer2_NewFrame ();
222
+ ImGui_ImplSDL2_NewFrame ();
223
+ ImGui::NewFrame ();
224
+
206
225
SDL_RenderCopy (ren, tex, NULL , NULL ); // Render background
207
226
SDL_Rect r{winWidth/2 -32 , winHeight-80 , 64 , 64 };
208
227
SDL_RenderCopy (ren, addBtn, NULL , &r); // Render add button
209
228
210
229
// Render text
211
- FC_Draw (font, ren, 0 , 0 , " Alchemy++ alpha v0.1. 2" );
230
+ FC_Draw (font, ren, 0 , 0 , " Alchemy++ alpha v0.2" );
212
231
213
232
FC_Draw (font, ren, 20 , winHeight-20 , " elems: %d" , draggables.size ());
214
233
215
234
// Render every draggable element
216
- for (auto const & d : draggables) {
235
+ for (auto & d : draggables) {
217
236
if ((int )d->scale != 1 ) {
218
237
SDL_Rect* scaledRect = anim::applyScale (d.get ()); // Get scaled rect
219
238
SDL_RenderCopy (ren, d->texture , NULL , scaledRect);
@@ -232,12 +251,19 @@ int main(int argc, char* argv[])
232
251
233
252
FC_Draw (font, ren, winWidth-170 , 10 , " FPS: %f" , 1000 /deltaTime);
234
253
254
+ ImGui::Render (); // Render ImGui stuff
255
+ ImGui_ImplSDLRenderer2_RenderDrawData (ImGui::GetDrawData (), ren);
235
256
SDL_RenderPresent (ren);
257
+
236
258
endTick = Clock::now ();
237
259
deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(endTick - startTick).count () / 1000.0 ; // Get the time the frame took in ms
238
260
}
239
261
240
- SDL_GL_DeleteContext (glContext);
262
+ // Cleanup
263
+ ImGui_ImplSDLRenderer2_Shutdown ();
264
+ ImGui_ImplSDL2_Shutdown ();
265
+ ImGui::DestroyContext ();
266
+
241
267
SDL_DestroyTexture (tex);
242
268
SDL_DestroyRenderer (ren);
243
269
SDL_DestroyWindow (win);
0 commit comments