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) 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(SDL2 REQUIRED)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
@ -24,27 +26,24 @@ add_library(imgui STATIC
ext/imgui/backends/imgui_impl_opengl3.h ext/imgui/backends/imgui_impl_opengl3.h
ext/imgui/misc/freetype/imgui_freetype.cpp ext/imgui/misc/freetype/imgui_freetype.cpp
ext/imgui/misc/freetype/imgui_freetype.h ext/imgui/misc/freetype/imgui_freetype.h)
)
add_library(imgui::imgui ALIAS imgui) add_library(imgui::imgui ALIAS imgui)
target_link_libraries(imgui target_link_libraries(imgui
PUBLIC PUBLIC
OpenGL::GL OpenGL::GL
$<TARGET_NAME_IF_EXISTS:SDL2::SDL2main> $<TARGET_NAME_IF_EXISTS:SDL2::SDL2main>
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static> $<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
Freetype::Freetype Freetype::Freetype)
)
target_include_directories(imgui PUBLIC target_include_directories(imgui PUBLIC
ext/imgui/ ext/imgui/
ext/imgui/backends/ ext/imgui/backends/
ext/imgui/misc/freetype ext/imgui/misc/freetype
${FREETYPE_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS})
)
include_directories(ext/) include_directories(ext/ ${SDL2_INCLUDE_DIRS})
add_executable(kge add_executable(kge
kge.cc kge.cc
) )
target_link_libraries(kge imgui) target_link_libraries(kge imgui)

82
kge.cc
View File

@ -5,13 +5,15 @@
#include <imgui/backends/imgui_impl_sdl2.h> #include <imgui/backends/imgui_impl_sdl2.h>
#include <imgui/backends/imgui_impl_opengl3.h> #include <imgui/backends/imgui_impl_opengl3.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#if defined(IMGUI_IMPL_OPENGL_ES2) #if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL2/SDL_opengles2.h> #include <SDL2/SDL_opengles2.h>
#else #else
#include <SDL2/SDL_opengl.h> #include <SDL2/SDL_opengl.h>
#endif #endif
#include "fonts/b612_mono.h"
#include "fonts/brassmono.h" #include "fonts/brassmono.h"
@ -22,7 +24,7 @@ int
main() main()
{ {
// Should this use the SDL_EVENTS subsystem? // 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 << "kge: failed to initialize SDL" << std::endl;
std::cerr << "\t" << SDL_GetError() << std::endl; std::cerr << "\t" << SDL_GetError() << std::endl;
return -1; return -1;
@ -38,9 +40,11 @@ main()
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(__APPLE__) #elif defined(__APPLE__)
// GL 3.2 Core + GLSL 150 // GL 3.2 Core + GLSL 150
const char* glsl_version = "#version 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_FLAGS,
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); 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_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#else #else
@ -61,8 +65,13 @@ main()
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); 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_WindowFlags window_flags = (SDL_WindowFlags) (SDL_WINDOW_OPENGL |
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); 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_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context); SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync SDL_GL_SetSwapInterval(1); // Enable vsync
@ -70,7 +79,8 @@ main()
// Setup Dear ImGui context // Setup Dear ImGui context
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io; ImGuiIO &io = ImGui::GetIO();
(void) io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// Setup Dear ImGui style // Setup Dear ImGui style
@ -81,18 +91,10 @@ main()
ImGui_ImplSDL2_InitForOpenGL(window, gl_context); ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version); ImGui_ImplOpenGL3_Init(glsl_version);
for (auto pixelSize : fontPixelSizes) { for (auto pixelSize: fontPixelSizes) {
io.Fonts->AddFontFromMemoryCompressedTTF(BrassMono_Regular_compressed_data, BrassMono_Regular_compressed_size, pixelSize); io.Fonts->AddFontFromMemoryCompressedTTF(
io.Fonts->AddFontFromMemoryCompressedTTF(B612Mono_Regular_compressed_data, B612Mono_Regular_compressed_size, pixelSize); BrassMono_Regular_compressed_data,
BrassMono_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);
} }
bool done = false; bool done = false;
@ -107,12 +109,13 @@ main()
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard 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. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event)) {
{
ImGui_ImplSDL2_ProcessEvent(&event); ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT) if (event.type == SDL_QUIT)
done = true; done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window))
done = true; done = true;
} }
@ -129,27 +132,34 @@ main()
static float f = 0.0f; static float f = 0.0f;
static int counter = 0; 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::Text(
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state "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::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::SliderFloat("float", &f, 0.0f,
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color 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) if (ImGui::Button(
"Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++; counter++;
ImGui::SameLine(); ImGui::SameLine();
ImGui::Text("counter = %d", counter); ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); ImGui::Text(
"Application average %.3f ms/frame (%.1f FPS)",
1000.0f / io.Framerate, io.Framerate);
ImGui::End(); ImGui::End();
} }
// 3. Show another simple window. // 3. Show another simple window.
if (show_another_window) if (show_another_window) {
{
// Pass a pointer to our bool variable (the window // Pass a pointer to our bool variable (the window
// will have a closing button that will clear the bool // will have a closing button that will clear the bool
// when clicked) // when clicked)
@ -163,8 +173,11 @@ main()
// Rendering // Rendering
ImGui::Render(); ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); glViewport(0, 0, (int) io.DisplaySize.x,
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); (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); glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
@ -179,7 +192,6 @@ main()
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
return 0; return 0;
} }