Skip to content

Commit 6a106a7

Browse files
Switch to ImGui, change font
1 parent 2d58cfe commit 6a106a7

File tree

5 files changed

+71
-33
lines changed

5 files changed

+71
-33
lines changed

CMakeLists.txt

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.31)
1+
cmake_minimum_required(VERSION 3.28)
22
project(AlchemyPlusPlus)
33

44
# Create an option to switch between a system sdl library and a vendored sdl library
@@ -24,20 +24,33 @@ endif()
2424
# Find dependencies
2525
find_package(SDL2_image REQUIRED)
2626
find_package(SDL2_ttf REQUIRED)
27-
find_package(GLEW REQUIRED)
28-
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})
27+
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS})
2928

30-
# Create your game executable target as usual
31-
add_subdirectory(src/JSON)
29+
# Include source directories and add executable
3230
include_directories(src/JSON)
33-
add_subdirectory(src/GFX)
31+
add_subdirectory(src/JSON)
3432
include_directories(src/GFX)
35-
add_subdirectory(src/Game)
33+
add_subdirectory(src/GFX)
3634
include_directories(src/Game)
37-
add_subdirectory(src/Misc)
35+
add_subdirectory(src/Game)
3836
include_directories(src/Misc)
39-
add_subdirectory(src/Menu)
37+
add_subdirectory(src/Misc)
4038
include_directories(src/Menu)
39+
add_subdirectory(src/Menu)
40+
# ImGui linking
41+
include_directories(src/include/imgui)
42+
include_directories(src/include/imgui/backends)
43+
add_library(ImGui
44+
src/include/imgui/imgui.cpp
45+
src/include/imgui/imgui_demo.cpp
46+
src/include/imgui/imgui_draw.cpp
47+
src/include/imgui/imgui_tables.cpp
48+
src/include/imgui/imgui_widgets.cpp
49+
src/include/imgui/backends/imgui_impl_sdl2.cpp
50+
src/include/imgui/backends/imgui_impl_sdlrenderer2.cpp
51+
src/include/imgui/backends/imgui_impl_opengl2.cpp
52+
)
53+
4154
add_executable(AlchemyPlusPlus src/main.cpp) #Main file
4255

4356
if(WIN32)
@@ -50,7 +63,7 @@ if(TARGET SDL2::SDL2main)
5063
# It has an implicit dependency on SDL2 functions, so it MUST be added before SDL2::SDL2 (or SDL2::SDL2-static)
5164
target_link_libraries(AlchemyPlusPlus SDL2::SDL2main)
5265
endif()
53-
target_link_libraries(AlchemyPlusPlus GLEW::GLEW)
66+
target_link_libraries(AlchemyPlusPlus ImGui)
5467
target_link_libraries(AlchemyPlusPlus JSON)
5568
target_link_libraries(AlchemyPlusPlus GFX)
5669
target_link_libraries(AlchemyPlusPlus Game)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The following build instructions should work on Linux and Windows provided the a
1111
- CMake
1212

1313
```
14-
git clone https://github.com/palaceswitcher/AlchemyPlusPlus
14+
git clone --recursive https://github.com/palaceswitcher/AlchemyPlusPlus
1515
cd AlchemyPlusPlus
1616
mkdir build && cd build
1717
cmake ..

src/GFX/SDL_FontCache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ THE SOFTWARE.
3333
#ifndef _SDL_FONTCACHE_H__
3434
#define _SDL_FONTCACHE_H__
3535

36-
#include <SDL2/SDL.h>
37-
#include <SDL2/SDL_ttf.h>
36+
#include "SDL.h"
37+
#include "SDL_ttf.h"
3838

3939
#ifdef FC_USE_SDL_GPU
40-
#include <SDL2/SDL_gpu.h>
40+
#include "SDL_gpu.h"
4141
#endif
4242

4343

src/Game/Element.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ bool compareZIndexRaw(DraggableElement* d1, DraggableElement* d2) { return d1->z
2020

2121
// Constructor
2222
DraggableElement::DraggableElement(std::string elemID, int mX, int mY) {
23-
FC_Font* font = FC_CreateFont();
2423
SDL_Rect* newElemBox = (SDL_Rect*) malloc(sizeof(struct SDL_Rect)); //Allocate space for new element's rectangle
2524

2625
box = newElemBox; //Add box to new element

src/main.cpp

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#include <SDL2/SDL.h>
33
#include <SDL2/SDL_image.h>
44
#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"
78
#include <iostream>
89
#include <stdio.h>
910
#include <stdlib.h>
1011
#include <stdbool.h>
11-
#include <fstream>
1212
#include <string>
1313
#include <vector>
1414
#include <map>
@@ -19,21 +19,22 @@
1919
#include "GFX/Animation.hpp"
2020
#include "GFX/SDL_FontCache.h"
2121
#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+
2227
typedef std::chrono::high_resolution_clock Clock;
2328

2429
int main(int argc, char* argv[])
2530
{
26-
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); //Always use OpenGL
27-
SDL_GLContext glContext;
28-
SDL_Event e;
29-
3031
// SDL Init
3132
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
3233
fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
3334
return EXIT_FAILURE;
3435
}
3536
// 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);
3738
if (win == NULL) {
3839
fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError());
3940
return EXIT_FAILURE;
@@ -43,9 +44,8 @@ int main(int argc, char* argv[])
4344
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
4445
}
4546
}
46-
glContext = SDL_GL_CreateContext(win);
4747

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);
4949
if (ren == NULL) {
5050
fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError());
5151
if (win != NULL) {
@@ -55,6 +55,16 @@ int main(int argc, char* argv[])
5555
return EXIT_FAILURE;
5656
}
5757

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+
5868
SDL_Texture* tex = IMG_LoadTexture(ren, "gamedata/default/textures/backgrounds/emptyuniverse.png");
5969
SDL_Texture* addBtn = IMG_LoadTexture(ren, "gamedata/default/textures/buttons/addBtn.png");
6070
if (tex == NULL) {
@@ -67,7 +77,9 @@ int main(int argc, char* argv[])
6777
// Initialize font
6878
TTF_Init();
6979
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));
7183

7284
elem::JSONInit(); //Initialize JSON
7385
Text::loadAll("en-us"); //Load game text
@@ -79,6 +91,7 @@ int main(int argc, char* argv[])
7991

8092
elem::spawnDraggable(draggables, 288, 208, "air");
8193
elem::spawnDraggable(draggables, 50, 50, "fire");
94+
8295
DraggableElement* selectedElem = NULL; //Currently selected draggable
8396

8497
bool leftClickDown = false; //Left click state, used to track drag and drop
@@ -98,6 +111,8 @@ int main(int argc, char* argv[])
98111

99112
//deltaTime = endTick-startTick;
100113

114+
SDL_Event e;
115+
ImGui_ImplSDL2_ProcessEvent(&e);
101116
while (SDL_PollEvent(&e)) {
102117
switch (e.type) {
103118
case SDL_QUIT:
@@ -158,11 +173,9 @@ int main(int argc, char* argv[])
158173
}
159174
leftClickTick = SDL_GetTicks64(); //Get next click tick
160175
}
176+
161177
if (!rightClickDown && e.button.button == SDL_BUTTON_RIGHT) {
162178
rightClickDown = true;
163-
if (selectedElem != NULL) {
164-
std::cout << "Selected: " << selectedElem->id << std::endl;
165-
}
166179
}
167180
break;
168181
}
@@ -197,23 +210,29 @@ int main(int argc, char* argv[])
197210
elem::secondParentElem = NULL;
198211
}
199212
}
200-
// Load textures
213+
214+
// Load draggable element textures
201215
for (auto &d : draggables) {
202216
elem::loadTexture(ren, d.get());
203217
}
204218
SDL_RenderClear(ren);
205219

220+
// Start the Dear ImGui frame
221+
ImGui_ImplSDLRenderer2_NewFrame();
222+
ImGui_ImplSDL2_NewFrame();
223+
ImGui::NewFrame();
224+
206225
SDL_RenderCopy(ren, tex, NULL, NULL); //Render background
207226
SDL_Rect r{winWidth/2-32, winHeight-80, 64, 64};
208227
SDL_RenderCopy(ren, addBtn, NULL, &r); //Render add button
209228

210229
//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");
212231

213232
FC_Draw(font, ren, 20, winHeight-20, "elems: %d", draggables.size());
214233

215234
// Render every draggable element
216-
for (auto const& d : draggables) {
235+
for (auto &d : draggables) {
217236
if ((int)d->scale != 1) {
218237
SDL_Rect* scaledRect = anim::applyScale(d.get()); //Get scaled rect
219238
SDL_RenderCopy(ren, d->texture, NULL, scaledRect);
@@ -232,12 +251,19 @@ int main(int argc, char* argv[])
232251

233252
FC_Draw(font, ren, winWidth-170, 10, "FPS: %f", 1000/deltaTime);
234253

254+
ImGui::Render(); //Render ImGui stuff
255+
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), ren);
235256
SDL_RenderPresent(ren);
257+
236258
endTick = Clock::now();
237259
deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(endTick - startTick).count() / 1000.0; //Get the time the frame took in ms
238260
}
239261

240-
SDL_GL_DeleteContext(glContext);
262+
// Cleanup
263+
ImGui_ImplSDLRenderer2_Shutdown();
264+
ImGui_ImplSDL2_Shutdown();
265+
ImGui::DestroyContext();
266+
241267
SDL_DestroyTexture(tex);
242268
SDL_DestroyRenderer(ren);
243269
SDL_DestroyWindow(win);

0 commit comments

Comments
 (0)