This commit is contained in:
Tobias P.L Wennberg 2025-08-22 15:29:53 +02:00
commit dbdccc90e2
12 changed files with 228 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.o
.cache/
imgui.ini

53
Makefile Normal file
View File

@ -0,0 +1,53 @@
# Makefile for ImGui SDL2 + OpenGL3 project
CXX = g++
CXXFLAGS = -std=c++26 -Wall -g
# ImGui paths
IMGUI_DIR = imgui
IMGUI_BACKENDS = $(IMGUI_DIR)/backends
IMGUI_SOURCES = \
$(IMGUI_DIR)/imgui.cpp \
$(IMGUI_DIR)/imgui_draw.cpp \
$(IMGUI_DIR)/imgui_widgets.cpp \
$(IMGUI_DIR)/imgui_tables.cpp \
$(IMGUI_DIR)/imgui_demo.cpp \
$(IMGUI_BACKENDS)/imgui_impl_sdl2.cpp \
$(IMGUI_BACKENDS)/imgui_impl_opengl3.cpp
INCLUDES = -I$(IMGUI_DIR) -I$(IMGUI_BACKENDS)
# SDL2 and OpenGL flags
UNAME_S := $(shell uname -s)
SDL_CFLAGS := $(shell sdl2-config --cflags)
SDL_LIBS := $(shell sdl2-config --libs)
GL_LIBS = -lGL
LIBS = $(SDL_LIBS) $(GL_LIBS) -ldl
CXXFLAGS += $(SDL_CFLAGS) $(INCLUDES)
# Targets and source files
EXE = app
SOURCES = main.cpp $(IMGUI_SOURCES)
# Convert source files to object files (e.g. imgui/imgui.cpp -> imgui/imgui.o)
OBJECTS = $(SOURCES:.cpp=.o)
.PHONY: all build run clean
all: build
build: $(EXE)
$(EXE): $(OBJECTS)
$(CXX) -o $@ $^ $(LIBS)
# Ensure object files are built from correct paths
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
run: build
./$(EXE)
clean:
rm -f $(OBJECTS) $(EXE)

0
OSI/README.md Normal file
View File

0
OSI/datalink/README.md Normal file
View File

0
OSI/network/README.md Normal file
View File

0
OSI/physical/README.md Normal file
View File

0
OSI/transport/README.md Normal file
View File

0
README.md Normal file
View File

BIN
app Executable file

Binary file not shown.

22
compile_commands.json Normal file
View File

@ -0,0 +1,22 @@
[
{
"arguments": [
"/usr/bin/g++",
"-std=c++26",
"-Wall",
"-g",
"-I/usr/include/SDL2",
"-D_GNU_SOURCE=1",
"-D_REENTRANT",
"-Iimgui",
"-Iimgui/backends",
"-c",
"-o",
"main.o",
"main.cpp"
],
"directory": "/home/t/org/dev/simulated_network/cpp",
"file": "/home/t/org/dev/simulated_network/cpp/main.cpp",
"output": "/home/t/org/dev/simulated_network/cpp/main.o"
}
]

1
imgui Submodule

@ -0,0 +1 @@
Subproject commit 1f7f1f54af38b0350d8c0008b096a9af6de299c7

149
main.cpp Normal file
View File

@ -0,0 +1,149 @@
#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <print>
int main() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
std::println("Error: %s\n", SDL_GetError());
return -1;
}
const char* glsl_version = "#version 130";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
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, 0);
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
// Create window with graphics context
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
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, (int)(1280 * main_scale), (int)(720 * main_scale), window_flags);
if (window == nullptr)
{
std::println("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return -1;
}
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
if (gl_context == nullptr)
{
std::println("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError());
return -1;
}
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
//io.ConfigViewportsNoAutoMerge = true;
//io.ConfigViewportsNoTaskBarIcon = true;
// Setup Dear ImGui style
//ImGui::StyleColorsDark();
ImGui::StyleColorsLight();
// Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
// Our state
bool show_demo_window = true;
bool show_another_window = true;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
// Main loop
bool done = false;
while (!done)
{
/***************/
/**IMGUI SETUP**/
/***************/
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;
}
if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
{
SDL_Delay(10);
continue;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
/***********/
/**MY CODE**/
/***********/
/*******************/
/**IMGUI 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());
// Update and Render additional Platform Windows
// (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
// For this specific demo app we could also call SDL_GL_MakeCurrent(window, gl_context) directly)
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
}
SDL_GL_SwapWindow(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}