A modern C++ demonstration project showcasing SDL3 graphics programming with proper coordinate system transformations and callback-based application architecture. USE vibe coding)
hello_triangle/
├── CMakeLists.txt # Build configuration
├── main.cpp # Main application source
└── README.md # This file
- SDL3 (latest development version)
- GLM (OpenGL Mathematics library)
- C++17 compatible compiler
- CMake 3.16+
The core of the coordinate conversion is handled by this function:
void update_transform_matrix(AppState* state) {
state->transform = glm::mat4(1.0f);
// 1. First, shift (0,0) to the center of the window
state->transform = glm::translate(state->transform, glm::vec3(
state->window_width / 2.0f, // X shift by half width
state->window_height / 2.0f, // Y shift by half height
0.0f
));
// 2. Then scale to window dimensions
state->transform = glm::scale(state->transform, glm::vec3(
state->window_width / 2.0f, // X scale
-state->window_height / 2.0f, // Y scale (negative for Y inversion)
1.0f
));
}The triangle vertices are defined in OpenGL normalized device coordinates:
glm::vec2 opengl_vertices[3] = {
glm::vec2(0.0f, 0.5f), // Top vertex
glm::vec2(0.5f, -0.5f), // Bottom-right vertex
glm::vec2(-0.5f, -0.5f) // Bottom-left vertex
};Each vertex has an associated color for gradient rendering:
SDL_FColor vertex_colors[3] = {
{ 1.0f, 0.0f, 0.0f, 1.0f }, // Red - Top vertex
{ 0.0f, 1.0f, 0.0f, 1.0f }, // Green - Bottom-right vertex
{ 0.0f, 0.0f, 1.0f, 1.0f } // Blue - Bottom-left vertex
};- SDL3 Integration: Utilizes the latest SDL3 API with callback-driven application lifecycle
- OpenGL-style Coordinates: Implements a coordinate system similar to OpenGL with center at (0,0) and normalized ranges
- Hardware-Accelerated Rendering: Uses SDL_RenderGeometry for efficient triangle rendering
- Delta Time Calculation: Real-time frame timing for smooth animations and performance monitoring
- Cross-Platform: Compatible with Windows, macOS, and Linux
- Modern C++: Uses contemporary C++ features and smart memory management
mkdir build && cd build
cmake ..
make
./HelloTriangleSDL3mkdir build && cd build
cmake -G "Visual Studio 16 2019" ..
cmake --build . --config Releasesudo apt-get install libsdl3-dev libglm-dev cmake build-essentialbrew install sdl3 glm cmakevcpkg install sdl3 glmThe project demonstrates SDL3's callback-based architecture:
- SDL_AppInit: Initializes application state, window, and renderer
- SDL_AppEvent: Handles user input and window events
- SDL_AppIterate: Main rendering loop with delta time calculation
- SDL_AppQuit: Cleanup and resource management
- Window Management: Resizable window with automatic coordinate system updates
- Coordinate Transformation: Mathematical conversion between OpenGL and SDL coordinate systems
- Renderer Information: Automatic detection and logging of graphics backend
- Input Handling: Keyboard (ESC to quit) and mouse input with coordinate conversion
- Performance Monitoring: Real-time FPS and delta time calculation
- Memory Management: Proper resource cleanup using RAII principles
- ESC: Exit application
- Window Resize: Automatically adjusts coordinate transformation matrix
- Mouse Click: Logs coordinates in both window space and relative to center
The application includes built-in performance monitoring:
- Real-time FPS counter
- Delta time calculation for frame-independent animations
- Efficient rendering using SDL's geometry API
- Automatic performance logging every second
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.