Set C++ standard; code formatting.

On MacOS, clang will default to C++2003 if this isn't explicitly set.
This commit is contained in:
Kyle Isom 2023-10-09 01:27:53 -07:00
parent 47bba754f4
commit 72e3bf77a7
2 changed files with 107 additions and 96 deletions

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.22)
project(kge)
project(kge LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)
@ -23,28 +25,25 @@ add_library(imgui STATIC
ext/imgui/backends/imgui_impl_opengl3.cpp
ext/imgui/backends/imgui_impl_opengl3.h
ext/imgui/misc/freetype/imgui_freetype.cpp
ext/imgui/misc/freetype/imgui_freetype.h
)
ext/imgui/misc/freetype/imgui_freetype.cpp
ext/imgui/misc/freetype/imgui_freetype.h)
add_library(imgui::imgui ALIAS imgui)
target_link_libraries(imgui
PUBLIC
OpenGL::GL
$<TARGET_NAME_IF_EXISTS:SDL2::SDL2main>
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
Freetype::Freetype
)
PUBLIC
OpenGL::GL
$<TARGET_NAME_IF_EXISTS:SDL2::SDL2main>
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
Freetype::Freetype)
target_include_directories(imgui PUBLIC
ext/imgui/
ext/imgui/backends/
ext/imgui/misc/freetype
${FREETYPE_INCLUDE_DIRS}
)
ext/imgui/
ext/imgui/backends/
ext/imgui/misc/freetype
${FREETYPE_INCLUDE_DIRS})
include_directories(ext/)
include_directories(ext/ ${SDL2_INCLUDE_DIRS})
add_executable(kge
kge.cc
)
kge.cc
)
target_link_libraries(kge imgui)

168
kge.cc
View File

@ -5,24 +5,26 @@
#include <imgui/backends/imgui_impl_sdl2.h>
#include <imgui/backends/imgui_impl_opengl3.h>
#include <SDL2/SDL.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL2/SDL_opengles2.h>
#else
#include <SDL2/SDL_opengl.h>
#endif
#include "fonts/b612_mono.h"
#include "fonts/brassmono.h"
static const float fontPixelSizes[] = {16, 10, 12, 14, 18};
static const float fontPixelSizes[] = {16, 10, 12, 14, 18};
int
main()
{
// Should this use the SDL_EVENTS subsystem?
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
std::cerr << "kge: failed to initialize SDL" << std::endl;
std::cerr << "\t" << SDL_GetError() << std::endl;
return -1;
@ -38,9 +40,11 @@ main()
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(__APPLE__)
// GL 3.2 Core + GLSL 150
const char* glsl_version = "#version 150";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
const char *glsl_version = "#version 150";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS,
SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#else
@ -61,8 +65,13 @@ main()
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
SDL_WindowFlags window_flags = (SDL_WindowFlags) (SDL_WINDOW_OPENGL |
SDL_WINDOW_RESIZABLE |
SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window *window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 1280, 720,
window_flags);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
@ -70,7 +79,8 @@ main()
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGuiIO &io = ImGui::GetIO();
(void) io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// Setup Dear ImGui style
@ -81,18 +91,10 @@ main()
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
for (auto pixelSize : fontPixelSizes) {
io.Fonts->AddFontFromMemoryCompressedTTF(BrassMono_Regular_compressed_data, BrassMono_Regular_compressed_size, pixelSize);
io.Fonts->AddFontFromMemoryCompressedTTF(B612Mono_Regular_compressed_data, B612Mono_Regular_compressed_size, pixelSize);
io.Fonts->AddFontFromMemoryCompressedTTF(B612Mono_Bold_compressed_data, B612Mono_Bold_compressed_size, pixelSize);
io.Fonts->AddFontFromMemoryCompressedTTF(BrassMono_Bold_compressed_data, BrassMono_Bold_compressed_size, pixelSize);
io.Fonts->AddFontFromMemoryCompressedTTF(B612Mono_BoldItalic_compressed_data, B612Mono_BoldItalic_compressed_size, pixelSize);
io.Fonts->AddFontFromMemoryCompressedTTF(BrassMono_BoldItalic_compressed_data, BrassMono_BoldItalic_compressed_size, pixelSize);
io.Fonts->AddFontFromMemoryCompressedTTF(B612Mono_Italic_compressed_data, B612Mono_Italic_compressed_size, pixelSize);
io.Fonts->AddFontFromMemoryCompressedTTF(BrassMono_Italic_compressed_data, BrassMono_Italic_compressed_size, pixelSize);
for (auto pixelSize: fontPixelSizes) {
io.Fonts->AddFontFromMemoryCompressedTTF(
BrassMono_Regular_compressed_data,
BrassMono_Regular_compressed_size, pixelSize);
}
bool done = false;
@ -101,73 +103,84 @@ main()
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
while (!done) {
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
done = true;
}
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window))
done = true;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
{
static float f = 0.0f;
static int counter = 0;
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Begin(
"Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::Text(
"This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window",
&show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
ImGui::SliderFloat("float", &f, 0.0f,
1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color",
(float *) &clear_color); // Edit 3 floats representing a color
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
if (ImGui::Button(
"Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
ImGui::Text(
"Application average %.3f ms/frame (%.1f FPS)",
1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
// 3. Show another simple window.
if (show_another_window)
{
// Pass a pointer to our bool variable (the window
// will have a closing button that will clear the bool
// when clicked)
ImGui::Begin("Another Window", &show_another_window);
// 3. Show another simple window.
if (show_another_window) {
// Pass a pointer to our bool variable (the window
// will have a closing button that will clear the bool
// when clicked)
ImGui::Begin("Another Window", &show_another_window);
ImGui::Text("Hello from another window!");
if (ImGui::Button("Close Me"))
show_another_window = false;
ImGui::End();
}
ImGui::Text("Hello from another window!");
if (ImGui::Button("Close Me"))
show_another_window = false;
ImGui::End();
}
// Rendering
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
// Rendering
ImGui::Render();
glViewport(0, 0, (int) io.DisplaySize.x,
(int) io.DisplaySize.y);
glClearColor(clear_color.x * clear_color.w,
clear_color.y * clear_color.w,
clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
}
// Cleanup
@ -179,7 +192,6 @@ main()
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}