added imgui and sdl

This commit is contained in:
Timerix22 2023-03-29 21:37:36 +06:00
parent 9edf287034
commit 22ae745aa2
11 changed files with 232 additions and 20 deletions

8
.gitmodules vendored
View File

@ -4,3 +4,11 @@
[submodule "kerep"]
path = kerep
url = https://github.com/Timerix22/kerep.git
[submodule "imgui"]
path = imgui
url = https://github.com/Timerix22/imgui.git
branch = master_cbuild
[submodule "SDL2"]
path = SDL2
url = https://github.com/Timerix22/SDL.git
branch = SDL2

View File

@ -5,7 +5,7 @@
all: build_exec_dbg
# creates executable using profile info generated by profile
build_exec: profile
build_exec: rebuild_kerep rebuild_imgui profile
@cbuild/call_task.sh build_exec 2>&1 | tee -a make_raw.log
# creates executable with debug info and no optimizations
@ -63,3 +63,7 @@ fix_log:
# recompile kerep.a in the next build task
rebuild_kerep:
@cbuild/call_task.sh rebuild_kerep
# recompile imgui.a in the next build task
rebuild_imgui:
@cbuild/call_task.sh rebuild_imgui

2
cbuild

@ -1 +1 @@
Subproject commit d64b1f3e9b41f6843c501ed28df6adbfdb384217
Subproject commit d72aa0c8098fe2a427e50355250f473d70886502

View File

@ -1,6 +1,6 @@
#!/bin/bash
CBUILD_VERSION=6
CONFIG_VERSION=1
CONFIG_VERSION=2
PROJECT="GraphC"
CMP_C="gcc"
@ -29,6 +29,8 @@ case "$OS" in
;;
LINUX)
EXEC_FILE="$PROJECT"
INCLUDE="-I./imgui -I/usr/include/SDL2"
LINKER_SHARED_LIBS="-lSDL2 -lGL"
;;
*)
error "operating system $OS has no configuration variants"
@ -40,6 +42,9 @@ case "$TASK" in
rebuild_kerep)
TASK_SCRIPT=tasks/rebuild_kerep.sh
;;
rebuild_imgui)
TASK_SCRIPT=tasks/rebuild_imgui.sh
;;
# creates executable using profile info generated by build_profile
build_exec)
# -flto applies more optimizations across object files
@ -47,7 +52,7 @@ case "$TASK" in
# -fuse-linker-plugin is required to use static libs with lto, it strips away all
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_SHARED_LIBS"
PRE_TASK_SCRIPT=tasks/pre_build.sh
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
KEREP_BUILD_TASK=build_static_lib
@ -56,7 +61,7 @@ case "$TASK" in
build_exec_dbg)
C_ARGS="-O0 -g"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_SHARED_LIBS"
PRE_TASK_SCRIPT=tasks/pre_build.sh
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
KEREP_BUILD_TASK=build_static_lib_dbg

2
kerep

@ -1 +1 @@
Subproject commit a2906e2c3aba5d3f94f9eea45bc3e6faed293a7b
Subproject commit 20537813ecbd08041318e832d645a982c09ac7a1

15
src/gui/gui.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../../kerep/src/base/base.h"
Maybe main_window_open();
Maybe main_window_loop_start();
Maybe main_window_close();
#if __cplusplus
}
#endif

8
src/gui/gui_internal.hpp Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include "gui.h"
#include "SDL2/SDL.h"
#include "SDL2/SDL_opengl.h"
#include "../../imgui/imgui.h"
#include "../../imgui/backends/imgui_impl_sdl2.h"
#include "../../imgui/backends/imgui_impl_opengl3.h"

157
src/gui/main_window.cpp Normal file
View File

@ -0,0 +1,157 @@
#include "gui_internal.hpp"
const char* window_title="GraphC";
bool loop_running=false;
SDL_Window* sdl_window;
SDL_GLContext gl_context;
#define SDL_ERROR_SAFETHROW() { \
const char* sdl_error=SDL_GetError(); \
throw_msg(cptr_concat("SDL Error: ", sdl_error)); \
SDL_ClearError(); \
}
#define SDL_TRY_ZERO(FUNC_CALL) if(FUNC_CALL != 0) SDL_ERROR_SAFETHROW();
Maybe main_window_open(){
SDL_TRY_ZERO(SDL_Init(SDL_INIT_VIDEO));
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0));
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE));
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3));
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0));
// From 2.0.18: Enable native IME.
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif
// Create window with graphics context
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1));
SDL_TRY_ZERO( SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24));
SDL_TRY_ZERO( 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 = SDL_CreateWindow(window_title,
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1280, 720, window_flags);
gl_context = SDL_GL_CreateContext(sdl_window);
SDL_TRY_ZERO( SDL_GL_MakeCurrent(sdl_window, gl_context));
SDL_TRY_ZERO( SDL_GL_SetSwapInterval(1)); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// Setup Platform/Renderer backends
if(ImGui_ImplSDL2_InitForOpenGL(sdl_window, gl_context) != true)
SDL_ERROR_SAFETHROW();
if(ImGui_ImplOpenGL3_Init(glsl_version) != true)
SDL_ERROR_SAFETHROW();
return MaybeNull;
}
Maybe main_window_loop_start(){
if(loop_running)
safethrow_msg("loop is already running",;);
loop_running=true;
// Our state
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
// main loop
while(loop_running){
// 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)
loop_running = false;
if (event.type==SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(sdl_window))
loop_running = false;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
ImGuiIO& io = ImGui::GetIO(); (void)io;
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
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;
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::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);
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)
{
ImGui::Begin("Another Window", &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::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(sdl_window);
}
try_cpp(main_window_close(),_999,;);
return MaybeNull;
}
Maybe main_window_close(){
if(!loop_running)
return MaybeNull;
loop_running=false;
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(sdl_window);
SDL_Quit();
return MaybeNull;
}

View File

@ -1,4 +1,5 @@
#include "../kerep/src/base/base.h"
#include "gui/gui.h"
void kt_initGraphCTypes(){
@ -18,6 +19,10 @@ int main(const int argc, const char* const* argv){
kprintf(" %s", argv[i]);
kprintf("\n");
tryLast(main_window_open(),_1);
tryLast(main_window_loop_start(),_2);
tryLast(main_window_close(),_3);
kt_free();
return 0;
}

View File

@ -1,17 +1,24 @@
#!/bin/bash
# check if kerep static lib exists or kerep_rebuild task was executed
if [ ! -f "$OBJDIR/libs/kerep.a" ] || [ -f .rebuild_kerep.tmp ]; then
[[ -z "$KEREP_BUILD_TASK" ]] && error "KEREP_BUILD_TASK is empty"
myprint "${BLUE}making kerep task <$KEREP_BUILD_TASK>"
# if $lib_project.a doesn't exist or rebuild_* task was executed, builds static lib
function handle_static_dependency {
local lib_project="$1"
local lib_build_task="$2"
if [ ! -f "$OBJDIR/libs/$lib_project.a" ] || [ -f .rebuild_$lib_project.tmp ]; then
[[ -z "$lib_build_task" ]] && error "lib_build_task is empty"
myprint "${BLUE}making $lib_project task <$lib_build_task>"
cd kerep
if ! make "$KEREP_BUILD_TASK"; then
cd $lib_project
if ! make "$lib_build_task"; then
exit 1
fi
cd ..
cp kerep/bin/kerep.a $OBJDIR/libs/
myprint "${GREEN}copied ${CYAN}kerep.a"
rm -f .rebuild_kerep.tmp
cp $lib_project/bin/$lib_project.a $OBJDIR/libs/
myprint "${GREEN}copied ${CYAN}$lib_project.a"
rm -f .rebuild_$lib_project.tmp
fi
}
handle_static_dependency kerep $KEREP_BUILD_TASK
handle_static_dependency imgui $KEREP_BUILD_TASK

3
tasks/rebuild_imgui.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
touch .rebuild_imgui.tmp
myprint "${YELLOW}imgui.a will be rebuilt in the next build task"