add an imgui demo app + cmake cleanup
This commit is contained in:
parent
28489b9ee8
commit
9700ada079
2 changed files with 89 additions and 6 deletions
|
@ -1,23 +1,22 @@
|
||||||
|
set(DP_WARN_FLAGS -Wall -Wextra -Wpedantic)
|
||||||
|
|
||||||
# Source: log.c by rxi (https://github.com/rxi/log.c)
|
# Source: log.c by rxi (https://github.com/rxi/log.c)
|
||||||
add_library(logc log.c)
|
add_library(logc log.c)
|
||||||
target_compile_features(logc PRIVATE c_std_11)
|
target_compile_features(logc PRIVATE c_std_11)
|
||||||
target_compile_definitions(logc PRIVATE -DLOG_USE_COLOR)
|
target_compile_definitions(logc PRIVATE -DLOG_USE_COLOR)
|
||||||
target_compile_options(logc INTERFACE "-ffile-prefix-map=${CMAKE_SOURCE_DIR}=." PRIVATE "-Wall" "-Wextra")
|
target_compile_options(logc INTERFACE -ffile-prefix-map=${CMAKE_SOURCE_DIR}=. PRIVATE ${DP_WARN_FLAGS})
|
||||||
|
|
||||||
find_package(Threads)
|
find_package(Threads)
|
||||||
|
|
||||||
|
|
||||||
add_library(dp_common dp_common.c)
|
add_library(dp_common dp_common.c)
|
||||||
target_compile_features(dp_common PRIVATE c_std_11)
|
target_compile_features(dp_common PRIVATE c_std_11)
|
||||||
target_compile_options(dp_common PUBLIC "-Wall" "-Wextra")
|
target_compile_options(dp_common PUBLIC ${DP_WARN_FLAGS}) # spread warning flags
|
||||||
target_link_libraries(dp_common
|
target_link_libraries(dp_common
|
||||||
PUBLIC logc
|
PUBLIC logc
|
||||||
PUBLIC nng
|
PUBLIC nng
|
||||||
PUBLIC Threads::Threads
|
PUBLIC Threads::Threads
|
||||||
)
|
)
|
||||||
|
|
||||||
find_package(SDL2 REQUIRED)
|
|
||||||
|
|
||||||
add_executable(doompanning doompanning.cc)
|
add_executable(doompanning doompanning.cc)
|
||||||
target_compile_features(doompanning PRIVATE cxx_std_17)
|
target_compile_features(doompanning PRIVATE cxx_std_17)
|
||||||
target_link_libraries(doompanning
|
target_link_libraries(doompanning
|
||||||
|
@ -25,8 +24,15 @@ target_link_libraries(doompanning
|
||||||
PRIVATE imgui
|
PRIVATE imgui
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(dp_imgui_demo dp_imgui_demo.cc)
|
||||||
|
target_compile_features(dp_imgui_demo PRIVATE cxx_std_17)
|
||||||
|
target_link_libraries(dp_imgui_demo
|
||||||
|
PRIVATE dp_common
|
||||||
|
PRIVATE imgui
|
||||||
|
)
|
||||||
|
|
||||||
add_executable(doomsim doomsim.cc)
|
add_executable(doomsim doomsim.cc)
|
||||||
target_compile_features(doomsim PRIVATE cxx_std_17)
|
target_compile_features(doomsim PRIVATE cxx_std_17)
|
||||||
target_link_libraries(doomsim
|
target_link_libraries(doomsim
|
||||||
PRIVATE dp_common
|
PRIVATE dp_common
|
||||||
)
|
)
|
77
src/dp_imgui_demo.cc
Normal file
77
src/dp_imgui_demo.cc
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
#include <imgui.h>
|
||||||
|
#include <backends/imgui_impl_sdl.h>
|
||||||
|
#include <backends/imgui_impl_sdlrenderer.h>
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
|
#include "dp_common.h"
|
||||||
|
|
||||||
|
void dp_sdl_fatal(const char *const msg)
|
||||||
|
{
|
||||||
|
log_fatal("%s: %s", msg, SDL_GetError());
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
(void) argc;
|
||||||
|
(void) argv;
|
||||||
|
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
|
||||||
|
dp_sdl_fatal("SDL_Init");
|
||||||
|
|
||||||
|
#ifdef SDL_HINT_IME_SHOW_UI
|
||||||
|
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const auto windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
|
||||||
|
auto window = SDL_CreateWindow("doompanning imgui demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, windowFlags);
|
||||||
|
if (!window)
|
||||||
|
dp_sdl_fatal("SDL_CreateWindow");
|
||||||
|
|
||||||
|
auto renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
|
||||||
|
|
||||||
|
if (!renderer)
|
||||||
|
dp_sdl_fatal("SDL_CreateRenderer");
|
||||||
|
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
ImGui::CreateContext();
|
||||||
|
ImGui::StyleColorsDark();
|
||||||
|
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
|
||||||
|
ImGui_ImplSDLRenderer_Init(renderer);
|
||||||
|
|
||||||
|
bool show_demo_window = true;
|
||||||
|
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||||
|
|
||||||
|
bool done = false;
|
||||||
|
|
||||||
|
while (!done)
|
||||||
|
{
|
||||||
|
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_ImplSDLRenderer_NewFrame();
|
||||||
|
ImGui_ImplSDL2_NewFrame();
|
||||||
|
ImGui::NewFrame();
|
||||||
|
|
||||||
|
if (show_demo_window)
|
||||||
|
ImGui::ShowDemoWindow(&show_demo_window);
|
||||||
|
|
||||||
|
// Rendering
|
||||||
|
ImGui::Render();
|
||||||
|
SDL_SetRenderDrawColor(renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue