oop
This commit is contained in:
parent
015f406eba
commit
e47f497f8c
19
src/UsefulException.cpp
Normal file
19
src/UsefulException.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "UsefulException.hpp"
|
||||
#include <sstream>
|
||||
|
||||
namespace GraphC {
|
||||
|
||||
UsefulException_::UsefulException_(std::string _message, std::string _file, std::string _func, int _line_n)
|
||||
: message(_message), file(_file), function(_func), line_n(_line_n)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss<<message<<'\n';
|
||||
ss<<file<<':'<<_line_n<<" in "<<function;
|
||||
complete_text = ss.str();
|
||||
}
|
||||
|
||||
const char* UsefulException_::what() {
|
||||
return complete_text.c_str();
|
||||
}
|
||||
|
||||
}
|
||||
23
src/UsefulException.hpp
Normal file
23
src/UsefulException.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace GraphC {
|
||||
|
||||
#define UsefulException(MSG) UsefulException_(MSG, __FILE__, __func__, __LINE__)
|
||||
|
||||
class UsefulException_ : public std::exception {
|
||||
std::string message;
|
||||
std::string file;
|
||||
std::string function;
|
||||
int line_n;
|
||||
std::string complete_text;
|
||||
|
||||
public:
|
||||
UsefulException_(std::string msg, std::string _file, std::string _func, int line_n);
|
||||
|
||||
const char* what();
|
||||
};
|
||||
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
#include "gui_internal.hpp"
|
||||
#include "gui.hpp"
|
||||
#include <algorithm>
|
||||
#include "NodeEditor.hpp"
|
||||
|
||||
@ -92,7 +92,6 @@ void NodeEditor::draw(){
|
||||
id_t out_attr_id;
|
||||
if (ImNodes::IsLinkCreated(&in_attr_id, &out_attr_id))
|
||||
{
|
||||
kprintf("new link id: %i\n", next_id);
|
||||
NodeAttributeLink link(next_id++, in_attr_id, out_attr_id);
|
||||
links.push_back(link);
|
||||
}
|
||||
@ -101,15 +100,12 @@ void NodeEditor::draw(){
|
||||
id_t link_id;
|
||||
if (ImNodes::IsLinkDestroyed(&link_id))
|
||||
{
|
||||
kprintf("link destroyed %i\n", link_id);
|
||||
auto iter = std::find_if(links.begin(), links.end(),
|
||||
[link_id](const NodeAttributeLink& link) -> bool {
|
||||
kprintf("destroyed link found\n");
|
||||
return link.id == link_id;
|
||||
});
|
||||
assert(iter != links.end());
|
||||
links.erase(iter);
|
||||
kprintf("destroyed link erased\n");
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
|
||||
@ -1,36 +0,0 @@
|
||||
#include "gui_internal.hpp"
|
||||
|
||||
void draw_bg_window(){
|
||||
const ImGuiDockNodeFlags dockspace_flags =
|
||||
ImGuiDockNodeFlags_PassthruCentralNode;
|
||||
const ImGuiWindowFlags window_flags =
|
||||
ImGuiWindowFlags_MenuBar |
|
||||
ImGuiWindowFlags_NoDocking |
|
||||
ImGuiWindowFlags_NoScrollbar |
|
||||
ImGuiWindowFlags_NoScrollWithMouse |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus |
|
||||
ImGuiWindowFlags_NoFocusOnAppearing |
|
||||
ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoCollapse |
|
||||
ImGuiWindowFlags_NoTitleBar |
|
||||
ImGuiWindowFlags_NoBackground;
|
||||
// not dockable window that always bound to viewport
|
||||
ImGuiViewport* viewport = ImGui::GetWindowViewport();
|
||||
ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always);
|
||||
ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
||||
ImGui::Begin("bg_window", nullptr, window_flags);
|
||||
ImGui::PopStyleVar(3);
|
||||
|
||||
// DockSpace
|
||||
ImGuiID dockspace_id = ImGui::GetID("bg_dockspace");
|
||||
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
|
||||
|
||||
// MenuBar
|
||||
//TODO
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
#include "gui_internal.hpp"
|
||||
|
||||
bool show_demo_window = false;
|
||||
bool show_metrics_window = false;
|
||||
|
||||
void draw_debug_window(ImGuiIO& io, bool* main_loop_wait_for_input){
|
||||
ImGui::Begin("Debug Options");
|
||||
ImGui::ColorEdit3("clear_color", (float*)&clear_color);
|
||||
ImGui::Checkbox("main_loop_wait_for_input", main_loop_wait_for_input);
|
||||
ImGui::Text("Application average %.3f ms/frame (%.2f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||
ImGui::Checkbox("Demo Window", &show_demo_window);
|
||||
ImGui::Checkbox("Metrics/Debug Window", &show_metrics_window);
|
||||
ImGui::End();
|
||||
|
||||
if (show_demo_window)
|
||||
ImGui::ShowDemoWindow(&show_demo_window);
|
||||
|
||||
if (show_metrics_window)
|
||||
ImGui::ShowMetricsWindow(&show_metrics_window);
|
||||
}
|
||||
12
src/gui/exceptions.cpp
Normal file
12
src/gui/exceptions.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
#include "exceptions.hpp"
|
||||
#include "../../dependencies/SDL2/include/SDL.h"
|
||||
|
||||
namespace GraphC::gui {
|
||||
|
||||
SDLException_::SDLException_(std::string _file, std::string _func, int _line_n)
|
||||
: UsefulException_(SDL_GetError(), _file, _func, _line_n)
|
||||
{
|
||||
SDL_ClearError();
|
||||
}
|
||||
|
||||
}
|
||||
17
src/gui/exceptions.hpp
Normal file
17
src/gui/exceptions.hpp
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "../UsefulException.hpp"
|
||||
|
||||
namespace GraphC::gui {
|
||||
|
||||
#define SDLException() SDLException_(__FILE__, __func__, __LINE__)
|
||||
|
||||
class SDLException_ : UsefulException_ {
|
||||
public:
|
||||
SDLException_(std::string _file, std::string _func, int line_n);
|
||||
};
|
||||
|
||||
#define SDL_TRY_ZERO(FUNC_CALL) if(FUNC_CALL != 0) throw SDLException();
|
||||
#define SDL_TRY_ONE(FUNC_CALL) if(FUNC_CALL != 1) throw SDLException();
|
||||
|
||||
}
|
||||
@ -1,15 +1,19 @@
|
||||
#include "gui_internal.hpp"
|
||||
#include "gui.hpp"
|
||||
|
||||
ImFont* ImFont_LoadFromFile(const char* file_path, f32 font_size){
|
||||
namespace GraphC::gui::fonts {
|
||||
|
||||
ImFont* ImFont_LoadFromFile(const char* file_path, f32 font_size, f32 dpi){
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
font_size *= getMainWindowDPI();
|
||||
font_size *= dpi;
|
||||
return io.Fonts->AddFontFromFileTTF(file_path, font_size);
|
||||
}
|
||||
|
||||
ImFont* _ImFont_LoadEmbedded(const void* data, int data_size, const char* font_name, f32 font_size){
|
||||
ImFont* _ImFont_LoadEmbedded(const void* data, int data_size, const char* font_name, f32 font_size, f32 dpi){
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
font_size *= getMainWindowDPI();
|
||||
font_size *= dpi;
|
||||
ImFontConfig font_cfg = ImFontConfig();
|
||||
sprintf_s(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", font_name, font_size);
|
||||
return io.Fonts->AddFontFromMemoryCompressedTTF(data, data_size, font_size, &font_cfg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
23
src/gui/fonts.hpp
Normal file
23
src/gui/fonts.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../dependencies/imgui/imgui.h"
|
||||
#include "../generated/fonts_embedded.h"
|
||||
|
||||
namespace GraphC::gui::fonts {
|
||||
|
||||
#define __CAT3(A,B,C) A##B##C
|
||||
#define embedded_font_data(FONT) __CAT3(font_,FONT,_compressed_data)
|
||||
#define embedded_font_data_size(FONT) __CAT3(font_,FONT,_compressed_size)
|
||||
#define embedded_font_name(FONT) #FONT
|
||||
|
||||
|
||||
ImFont* ImFont_LoadFromFile(const char* file_path, f32 font_size, f32 dpi);
|
||||
|
||||
#define ImFont_LoadEmbedded(FONT, FONT_SIZE, DPI) _ImFont_LoadEmbedded( \
|
||||
embedded_font_data(FONT), \
|
||||
embedded_font_data_size(FONT), \
|
||||
embedded_font_name(FONT), \
|
||||
FONT_SIZE, DPI)
|
||||
ImFont* _ImFont_LoadEmbedded(const void* data, int data_size, const char* font_name, f32 font_size, f32 dpi);
|
||||
|
||||
}
|
||||
@ -1,17 +1,10 @@
|
||||
#include "gui_internal.hpp"
|
||||
#include "NodeEditor.hpp"
|
||||
using namespace GraphC::gui;
|
||||
#include "gui.hpp"
|
||||
#include "../../dependencies/imgui/backends/imgui_impl_sdl2.h"
|
||||
#include "../../dependencies/imgui/backends/imgui_impl_opengl3.h"
|
||||
|
||||
#define default_font_name font_DroidSans
|
||||
const f32 default_font_size=14.0f;
|
||||
ImVec4 clear_color = RGBAHexToF(35,35,50,255);
|
||||
bool loop_running=false;
|
||||
SDL_Window* sdl_window;
|
||||
SDL_GLContext gl_context;
|
||||
bool main_loop_wait_for_input=true;
|
||||
NodeEditor node_editor("new editor");
|
||||
namespace GraphC::gui {
|
||||
|
||||
f32 getMainWindowDPI(){
|
||||
f32 GUI::getDPI(){
|
||||
int w=0, h=0;
|
||||
SDL_GL_GetDrawableSize(sdl_window, &w, &h);
|
||||
int sim_w=0, sim_h=0;
|
||||
@ -22,7 +15,7 @@ f32 getMainWindowDPI(){
|
||||
return dpi;
|
||||
}
|
||||
|
||||
Maybe main_window_open(const char* window_title){
|
||||
void GUI::init(const char* window_title){
|
||||
SDL_TRY_ZERO(SDL_Init(SDL_INIT_VIDEO));
|
||||
SDL_version v;
|
||||
SDL_GetVersion(&v);
|
||||
@ -69,53 +62,53 @@ Maybe main_window_open(const char* window_title){
|
||||
|
||||
// Setup Platform/Renderer backends
|
||||
if(ImGui_ImplSDL2_InitForOpenGL(sdl_window, gl_context) != true)
|
||||
SDL_ERROR_SAFETHROW();
|
||||
throw SDLException();
|
||||
if(ImGui_ImplOpenGL3_Init(glsl_version) != true)
|
||||
SDL_ERROR_SAFETHROW();
|
||||
throw SDLException();
|
||||
|
||||
// Setup Dear ImGui style
|
||||
ImGui::StyleColorsDark();
|
||||
io.FontDefault=ImFont_LoadEmbedded(default_font_name, default_font_size);
|
||||
ImFont_LoadEmbedded(font_Cousine_Regular, default_font_size);
|
||||
f32 dpi = getDPI();
|
||||
io.FontDefault=fonts:: ImFont_LoadEmbedded(default_font, default_font_size, dpi);
|
||||
fonts:: ImFont_LoadEmbedded(font_Cousine_Regular, default_font_size, dpi);
|
||||
|
||||
ImNodes::CreateContext();
|
||||
ImNodes::StyleColorsDark();
|
||||
ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
|
||||
node_editor=NodeEditor("node editor");
|
||||
return MaybeNull;
|
||||
node_editor.show();
|
||||
}
|
||||
|
||||
// Wait, poll and handle events (inputs, window resize, etc.)
|
||||
Maybe poll_events(u16& frame_updates_requested, bool wait){
|
||||
void GUI::poll_events(u16& frame_updates_requested, bool wait){
|
||||
SDL_Event event;
|
||||
if(wait){
|
||||
// waits for first event in cpu-efficient way
|
||||
SDL_TRY_ONE(SDL_WaitEvent(&event));
|
||||
}
|
||||
// doesnt wait for event
|
||||
// dont wait for event
|
||||
else if(!SDL_PollEvent(&event))
|
||||
return MaybeNull;
|
||||
return;
|
||||
|
||||
do {
|
||||
if(ImGui_ImplSDL2_ProcessEvent(&event))
|
||||
frame_updates_requested=2;
|
||||
switch(event.type){
|
||||
case SDL_QUIT: {
|
||||
try_cpp(main_window_close(),_9914,;);
|
||||
close();
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT: {
|
||||
if(event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(sdl_window)){
|
||||
try_cpp(main_window_close(),_9915,;);
|
||||
close();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (SDL_PollEvent(&event)); // if there are more events, handles them
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
Maybe draw_frame(){
|
||||
void GUI::draw_frame(){
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
@ -125,7 +118,6 @@ Maybe draw_frame(){
|
||||
// Draw UI
|
||||
draw_bg_window();
|
||||
draw_debug_window(io, &main_loop_wait_for_input);
|
||||
node_editor.show();
|
||||
node_editor.draw();
|
||||
|
||||
// Rendering
|
||||
@ -149,15 +141,14 @@ Maybe draw_frame(){
|
||||
|
||||
SDL_GL_SwapWindow(sdl_window);
|
||||
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
Maybe main_window_loop_start(){
|
||||
void GUI::startAndWait(){
|
||||
if(loop_running)
|
||||
safethrow_msg("loop is already running",;);
|
||||
throw UsefulException("loop is already running");
|
||||
|
||||
// draw first frame
|
||||
try_cpp(draw_frame(),_2175,;);
|
||||
draw_frame();
|
||||
|
||||
u16 frame_updates_requested=1;
|
||||
u64 prev_update_time_ms=SDL_GetTicks64();
|
||||
@ -165,20 +156,20 @@ Maybe main_window_loop_start(){
|
||||
// main loop
|
||||
while(loop_running){
|
||||
// waits for events
|
||||
try_cpp(poll_events(frame_updates_requested, main_loop_wait_for_input),_55415,;);
|
||||
poll_events(frame_updates_requested, main_loop_wait_for_input);
|
||||
|
||||
if(frame_updates_requested==0)
|
||||
{
|
||||
u64 update_time_ms=SDL_GetTicks64();
|
||||
if(update_time_ms >= prev_update_time_ms + 1000/frame_rate_min){
|
||||
// if frame rate < frame_rate_min then requests frame draw
|
||||
if(update_time_ms >= prev_update_time_ms + 1000/fps_min){
|
||||
// if frame rate < fps_min then requests frame draw
|
||||
// works only if main_loop_wait_for_input = false
|
||||
frame_updates_requested=1;
|
||||
prev_update_time_ms=update_time_ms;
|
||||
}
|
||||
else {
|
||||
// skips frame rendering and waits to limit fps
|
||||
u32 frame_delay_ms=1000/frame_rate_max;
|
||||
u32 frame_delay_ms=1000/fps_max;
|
||||
SDL_Delay(frame_delay_ms);
|
||||
continue;
|
||||
}
|
||||
@ -186,24 +177,20 @@ Maybe main_window_loop_start(){
|
||||
|
||||
// deaws requested number of frames
|
||||
while(frame_updates_requested>0) {
|
||||
try_cpp(draw_frame(),_2175,;);
|
||||
draw_frame();
|
||||
frame_updates_requested--;
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
main_window_destroy();
|
||||
return MaybeNull;
|
||||
GUI::destroy();
|
||||
}
|
||||
|
||||
Maybe main_window_close(){
|
||||
if(!loop_running)
|
||||
return MaybeNull;
|
||||
void GUI::close(){
|
||||
loop_running=false;
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
void main_window_destroy(){
|
||||
void GUI::destroy(){
|
||||
ImNodes::DestroyContext();
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
@ -212,3 +199,56 @@ void main_window_destroy(){
|
||||
SDL_DestroyWindow(sdl_window);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void GUI::draw_bg_window(){
|
||||
const ImGuiDockNodeFlags dockspace_flags =
|
||||
ImGuiDockNodeFlags_PassthruCentralNode;
|
||||
const ImGuiWindowFlags window_flags =
|
||||
ImGuiWindowFlags_MenuBar |
|
||||
ImGuiWindowFlags_NoDocking |
|
||||
ImGuiWindowFlags_NoScrollbar |
|
||||
ImGuiWindowFlags_NoScrollWithMouse |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus |
|
||||
ImGuiWindowFlags_NoFocusOnAppearing |
|
||||
ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoCollapse |
|
||||
ImGuiWindowFlags_NoTitleBar |
|
||||
ImGuiWindowFlags_NoBackground;
|
||||
// not dockable window that always bound to viewport
|
||||
ImGuiViewport* viewport = ImGui::GetWindowViewport();
|
||||
ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always);
|
||||
ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
||||
ImGui::Begin("bg_window", nullptr, window_flags);
|
||||
ImGui::PopStyleVar(3);
|
||||
|
||||
// DockSpace
|
||||
ImGuiID dockspace_id = ImGui::GetID("bg_dockspace");
|
||||
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
|
||||
|
||||
// MenuBar
|
||||
//TODO
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
void GUI::draw_debug_window(ImGuiIO& io, bool* main_loop_wait_for_input){
|
||||
ImGui::Begin("Debug Options");
|
||||
ImGui::ColorEdit3("clear_color", (float*)&clear_color);
|
||||
ImGui::Checkbox("main_loop_wait_for_input", main_loop_wait_for_input);
|
||||
ImGui::Text("Application average %.3f ms/frame (%.2f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||
ImGui::Checkbox("Demo Window", &show_demo_window);
|
||||
ImGui::Checkbox("Metrics/Debug Window", &show_metrics_window);
|
||||
ImGui::End();
|
||||
|
||||
if (show_demo_window)
|
||||
ImGui::ShowDemoWindow(&show_demo_window);
|
||||
|
||||
if (show_metrics_window)
|
||||
ImGui::ShowMetricsWindow(&show_metrics_window);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../../dependencies/kerep/src/base/base.h"
|
||||
|
||||
Maybe main_window_open(const char* window_title);
|
||||
Maybe main_window_loop_start();
|
||||
Maybe main_window_close();
|
||||
|
||||
/// converts hex color to float vector
|
||||
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
||||
/// converts float vector to hex color
|
||||
#define RGBAFToHex(VEC4) {(u8)(VEC4.x*255), (u8)(VEC4.y*255), (u8)(VEC4.z*255), (u8)(VEC4.w*255)}
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
51
src/gui/gui.hpp
Normal file
51
src/gui/gui.hpp
Normal file
@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../dependencies/kerep/src/base/base.h"
|
||||
#include "../../dependencies/SDL2/include/SDL.h"
|
||||
#include "../../dependencies/SDL2/include/SDL_opengl.h"
|
||||
#include "../../dependencies/imgui/imgui.h"
|
||||
#include "NodeEditor.hpp"
|
||||
#include "imgui_extensions.hpp"
|
||||
#include "fonts.hpp"
|
||||
#include "exceptions.hpp"
|
||||
|
||||
/// converts hex color to float vector
|
||||
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
||||
/// converts float vector to hex color
|
||||
#define RGBAFToHex(VEC4) {(u8)(VEC4.x*255), (u8)(VEC4.y*255), (u8)(VEC4.z*255), (u8)(VEC4.w*255)}
|
||||
|
||||
namespace GraphC::gui {
|
||||
|
||||
#define default_font font_DroidSans
|
||||
|
||||
class GUI {
|
||||
public:
|
||||
ImVec4 clear_color = RGBAHexToF(35,35,50,255);
|
||||
f32 default_font_size = 14.0f;
|
||||
u8 fps_min = 30;
|
||||
u8 fps_max = 60;
|
||||
|
||||
private:
|
||||
bool loop_running=false;
|
||||
bool main_loop_wait_for_input=true;
|
||||
bool show_demo_window = false;
|
||||
bool show_metrics_window = false;
|
||||
SDL_Window* sdl_window;
|
||||
SDL_GLContext gl_context;
|
||||
NodeEditor node_editor = NodeEditor("new editor");
|
||||
|
||||
public:
|
||||
void init(const char* window_title);
|
||||
void startAndWait();
|
||||
void close();
|
||||
f32 getDPI();
|
||||
|
||||
private:
|
||||
void destroy();
|
||||
void poll_events(u16& frame_updates_requested, bool wait);
|
||||
void draw_frame();
|
||||
void draw_debug_window(ImGuiIO &io, bool *main_loop_wait_for_input);
|
||||
void draw_bg_window();
|
||||
};
|
||||
|
||||
}
|
||||
@ -1,65 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "gui.h"
|
||||
#include "../../dependencies/SDL2/include/SDL.h"
|
||||
#include "../../dependencies/SDL2/include/SDL_opengl.h"
|
||||
#include "../../dependencies/imgui/imgui.h"
|
||||
#include "../../dependencies/imgui/backends/imgui_impl_sdl2.h"
|
||||
#include "../../dependencies/imgui/backends/imgui_impl_opengl3.h"
|
||||
#include "../../dependencies/kerep/src/Filesystem/filesystem.h"
|
||||
|
||||
//////////////////////////////////////
|
||||
// Fonts //
|
||||
//////////////////////////////////////
|
||||
#include "../generated/fonts_embedded.h"
|
||||
|
||||
#define __CAT3(A,B,C) A##B##C
|
||||
#define embedded_font_data(FONT) __CAT3(font_,FONT,_compressed_data)
|
||||
#define embedded_font_size(FONT) __CAT3(font_,FONT,_compressed_size)
|
||||
#define embedded_font_name(FONT) #FONT
|
||||
|
||||
f32 getMainWindowDPI();
|
||||
ImFont* ImFont_LoadFromFile(const char* file_path, f32 font_size);
|
||||
ImFont* _ImFont_LoadEmbedded(const void* data, int data_size, const char* font_name, f32 font_size);
|
||||
|
||||
#define ImFont_LoadEmbedded(FONT, FONT_SIZE) _ImFont_LoadEmbedded( \
|
||||
embedded_font_data(FONT), \
|
||||
embedded_font_size(FONT), \
|
||||
embedded_font_name(FONT), \
|
||||
FONT_SIZE)
|
||||
|
||||
//////////////////////////////////////
|
||||
// Variables and constants //
|
||||
//////////////////////////////////////
|
||||
const u8 frame_rate_min=30; // frames per second
|
||||
const u8 frame_rate_max=60; // frames per second
|
||||
extern ImVec4 clear_color; // background color for main window
|
||||
|
||||
//////////////////////////////////////
|
||||
// UI Functions //
|
||||
//////////////////////////////////////
|
||||
/// @brief frees all allocated resources
|
||||
void main_window_destroy();
|
||||
void draw_debug_window(ImGuiIO& io, bool* main_loop_wait_for_input);
|
||||
void draw_bg_window();
|
||||
|
||||
//////////////////////////////////////
|
||||
// Macros //
|
||||
//////////////////////////////////////
|
||||
#define SDL_ERROR_SAFETHROW() { \
|
||||
const char* sdl_error=SDL_GetError(); \
|
||||
safethrow_msg(cptr_concat("SDL Error: ", sdl_error),;); \
|
||||
SDL_ClearError(); \
|
||||
}
|
||||
#define SDL_TRY_ZERO(FUNC_CALL) if(FUNC_CALL != 0) SDL_ERROR_SAFETHROW();
|
||||
#define SDL_TRY_ONE(FUNC_CALL) if(FUNC_CALL != 1) SDL_ERROR_SAFETHROW();
|
||||
|
||||
|
||||
//////////////////////////////////////
|
||||
// ImGui extensions //
|
||||
//////////////////////////////////////
|
||||
namespace ImGui {
|
||||
void SetWindowSizeMin(f32 window_width_min, f32 window_height_min);
|
||||
}
|
||||
|
||||
ImVec2 ImVec2Add(ImVec2 a, ImVec2 b);
|
||||
@ -1,8 +1,8 @@
|
||||
#include "gui_internal.hpp"
|
||||
#include "gui.hpp"
|
||||
|
||||
namespace ImGui {
|
||||
|
||||
void SetWindowSizeMin(f32 window_width_min, f32 window_height_min){
|
||||
void SetWindowSizeMin(float window_width_min, float window_height_min){
|
||||
ImVec2 window_size=ImGui::GetWindowSize();
|
||||
ImVec2 new_size=window_size;
|
||||
bool changed=false;
|
||||
@ -18,11 +18,10 @@ void SetWindowSizeMin(f32 window_width_min, f32 window_height_min){
|
||||
ImGui::SetWindowSize(new_size);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
ImVec2 ImVec2Add(ImVec2 a, ImVec2 b) {
|
||||
a.x+=b.x;
|
||||
a.y+=b.y;
|
||||
return a;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
11
src/gui/imgui_extensions.hpp
Normal file
11
src/gui/imgui_extensions.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../dependencies/imgui/imgui.h"
|
||||
|
||||
namespace ImGui {
|
||||
|
||||
void SetWindowSizeMin(float window_width_min, float window_height_min);
|
||||
|
||||
ImVec2 ImVec2Add(ImVec2 a, ImVec2 b);
|
||||
|
||||
}
|
||||
28
src/main.c
28
src/main.c
@ -1,28 +0,0 @@
|
||||
#include "../dependencies/kerep/src/base/base.h"
|
||||
#include "gui/gui.h"
|
||||
|
||||
void kt_initGraphCTypes(){
|
||||
|
||||
}
|
||||
|
||||
int main(const int argc, const char* const* argv){
|
||||
if(setlocale(LC_CTYPE, "C.UTF-8")!=0)
|
||||
kprintf("\e[93msetlocale failed!\n");
|
||||
|
||||
kt_beginInit(true);
|
||||
kt_initKerepTypes();
|
||||
kt_initGraphCTypes();
|
||||
kt_endInit();
|
||||
|
||||
kprintf("\e[37margs:");
|
||||
for(i32 i=0; i<argc; i++)
|
||||
kprintf(" %s", argv[i]);
|
||||
kprintf("\n");
|
||||
|
||||
tryLast(main_window_open("GraphC"),_1, ;);
|
||||
tryLast(main_window_loop_start(),_2, ;);
|
||||
tryLast(main_window_close(),_3, ;);
|
||||
|
||||
kt_free();
|
||||
return 0;
|
||||
}
|
||||
41
src/main.cpp
Normal file
41
src/main.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include "gui/gui.hpp"
|
||||
#include <iostream>
|
||||
|
||||
// SDL for some reason redefains main
|
||||
#ifdef main
|
||||
#undef main
|
||||
#endif
|
||||
|
||||
int main(const int argc, const char* const* argv){
|
||||
if(setlocale(LC_CTYPE, "C.UTF-8")!=0)
|
||||
kprintf("\e[93msetlocale failed!\n");
|
||||
|
||||
kt_beginInit(true);
|
||||
kt_initKerepTypes();
|
||||
kt_endInit();
|
||||
|
||||
try {
|
||||
GraphC::gui::GUI gui;
|
||||
gui.init("GraphC");
|
||||
gui.startAndWait();
|
||||
}
|
||||
catch(const std::exception& e){
|
||||
std::cerr<<"Catched exception: "<<e.what()<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch(const char* msg){
|
||||
std::cerr<<"Catched exception message: "<<msg<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch(std::string& msg){
|
||||
std::cerr<<"Catched exception message: "<<msg<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch(...){
|
||||
std::cerr<<"Catched unknown exception"<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
kt_free();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user