This commit is contained in:
Alfie King 2025-07-10 02:14:01 +01:00
commit b467feb015
6 changed files with 296 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vscode
build
imgui.ini

4
.gitmodules vendored Normal file
View File

@ -0,0 +1,4 @@
[submodule "imgui"]
path = imgui
url = https://github.com/ocornut/imgui.git
branch = docking

1
imgui Submodule

@ -0,0 +1 @@
Subproject commit 44aa9a4b3a6f27d09a4eb5770d095cbd376dfc4b

44
include/main.hpp Normal file
View File

@ -0,0 +1,44 @@
#ifndef MAIN_HPP
#define MAIN_HPP
// ImGui
#include "imgui.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_opengl3.h"
// SDL3 and OpenGL
#include <SDL3/SDL.h>
#include <SDL3/SDL_opengl.h>
// C++ Standard Library
#include <iostream>
#include <string>
#include <vector>
// Structs
// Variables
int window_width = 1920;
int window_height = 1080;
float main_scale;
SDL_WindowFlags window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
SDL_Window* window;
bool running;
SDL_Event event;
Uint32 last_time;
Uint32 current_time;
float frame_time;
float fps;
float frame_time_graph[100] = {0.0f};
// Functions
int main(int argc, char* argv[]);
int initialize();
void cleanup();
#endif

74
makefile Normal file
View File

@ -0,0 +1,74 @@
# Directories
INCLUDE_DIR = include
SRC_DIR = src
BUILD_DIR = build
# ImGui directory (only set if ImGui is used)
IMGUI_DIR = imgui
# Compiler
CXX = g++
# Libs
LIBS = -lGL `pkg-config --libs sdl3`
# Compiler and linker settings
CXXFLAGS = `pkg-config --cflags sdl3` -std=c++17 -I $(INCLUDE_DIR)
# If ImGui directory is set, include its source files
ifdef IMGUI_DIR
CXXFLAGS += -I $(IMGUI_DIR) -I $(IMGUI_DIR)/backends
endif
# Source and object files
SRC = $(wildcard $(SRC_DIR)/*.cpp)
# If ImGui is used, add its source files to the source list
ifdef IMGUI_DIR
SRC += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
SRC += $(IMGUI_DIR)/backends/imgui_impl_sdl3.cpp $(IMGUI_DIR)/backends/imgui_impl_opengl3.cpp
endif
# Target executable
UNAME := $(shell uname -s)
BUILD_DIR := $(BUILD_DIR)/$(UNAME)
OBJ_DIR := $(BUILD_DIR)/objs
BIN = $(BUILD_DIR)/main
# Object files corresponding to the source files (now in obj directory)
OBJS = $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(basename $(notdir $(SRC)))))
# development target with debugging
dev: CXXFLAGS += -g -Wall -Wformat
dev: all
# Release target
release: CXXFLAGS += -O3
release: all
# Create directories for build output
dirs:
@mkdir -p $(BUILD_DIR)
@mkdir -p $(OBJ_DIR)
# Pattern rule for source files in src directory
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
# Pattern rule for ImGui source files
ifdef IMGUI_DIR
$(OBJ_DIR)/%.o: $(IMGUI_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
$(OBJ_DIR)/%.o: $(IMGUI_DIR)/backends/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
endif
all: dirs $(BIN)
@echo Build complete
$(BIN): $(OBJS)
$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)
clean:
rm -rf $(BUILD_DIR)

170
src/main.cpp Normal file
View File

@ -0,0 +1,170 @@
#include "main.hpp"
// Init
int initialize() {
// Initialize SDL
if (!SDL_Init(SDL_INIT_VIDEO)) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1; // Initialization failed
}
// Create a window with OpenGL context
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
window = SDL_CreateWindow("Untitled", (int)(window_width * main_scale), (int)(window_height * main_scale), window_flags);
if (!window) {
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1; // Window creation failed
}
// Initialize OpenGL context
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
if (!gl_context) {
std::cerr << "OpenGL context could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1; // OpenGL context creation failed
}
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable VSync
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
SDL_ShowWindow(window);
// Initialize ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// Setup scale
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(main_scale);
style.FontScaleDpi = main_scale;
// Setup Platform/Renderer bindings
if (!ImGui_ImplSDL3_InitForOpenGL(window, gl_context)) {
std::cerr << "ImGui_ImplSDL3_InitForOpenGL failed!" << std::endl;
SDL_GL_DestroyContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 1; // Initialization failed
}
if (!ImGui_ImplOpenGL3_Init("#version 330")) {
std::cerr << "ImGui_ImplOpenGL3_Init failed!" << std::endl;
ImGui_ImplSDL3_Shutdown();
SDL_GL_DestroyContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 1; // Initialization failed
}
return 0; // Initialization successful
}
// Cleanup
void cleanup() {
// Shutdown ImGui
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
// Destroy OpenGL context
SDL_GLContext gl_context = SDL_GL_GetCurrentContext();
if (gl_context) {
SDL_GL_MakeCurrent(NULL, NULL);
SDL_GL_DestroyContext(gl_context);
}
// Destroy window
if (window) {
SDL_DestroyWindow(window);
window = nullptr;
}
// Quit SDL
SDL_Quit();
std::cout << "Cleanup successful." << std::endl;
}
// Main
int main(int argc, char* argv[]) {
// Initialize
if (initialize() != 0) {
std::cerr << "Initialization failed." << std::endl;
return -1; // Initialization failed
}
// Main loop
running = true;
last_time = SDL_GetTicks();
while (running) {
// Handle events
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_QUIT) {
running = false; // Exit loop on quit event
}
}
// Start the ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
// Show a simple ImGui window
ImGui::Begin("Hello, World!");
ImGui::Text("This is a simple ImGui window.");
if (ImGui::Button("Close")) {
running = false; // Close button clicked
}
ImGui::End();
// Display FPS in the ImGui window
ImGui::Begin("FPS Counter");
ImGui::Text("Current FPS: %.2f", fps);
// Frame time graph
ImGui::PlotLines("##FrameTime", frame_time_graph, 100, 0, "Frame Time (ms)", 0.0f, 0.1f, ImVec2(200, 80));
ImGui::Text("Current Time: %u ms", current_time);
ImGui::End();
// Render
ImGui::Render();
glViewport(0, 0, (int)(window_width * main_scale), (int)(window_height * main_scale));
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
// Calculate FPS
current_time = SDL_GetTicks();
frame_time = (current_time - last_time) / 1000.0f; // Convert milliseconds to seconds
fps = 1.0f / frame_time;
last_time = current_time;
// Update frame time graph
for (int i = 99; i > 0; --i) {
frame_time_graph[i] = frame_time_graph[i - 1]; // Shift old values
}
frame_time_graph[0] = frame_time; // Add new frame time
}
// Cleanup
cleanup();
}