Compare commits

...

3 Commits

Author SHA1 Message Date
2c240ad7e3 isloate sdl logic to window class 2026-04-12 16:41:27 -05:00
779c51c2f8 fix sdl3 linking + comments 2026-04-12 14:18:55 -05:00
f032f152a9 bugfix: debug mask 2026-04-12 11:08:32 -05:00
10 changed files with 94 additions and 36 deletions

View File

@@ -20,8 +20,7 @@ set(Vulkan_INCLUDE_DIR "${VULKAN_PATH}/Include")
set(Vulkan_LIBRARY "${VULKAN_PATH}/Lib/vulkan-1.lib")
find_package(Vulkan REQUIRED)
# TODO: use SDL in the vulkanSDK if available
set(SDL3_DIR "${SDL3_PATH}/cmake")
set(SDL3_DIR "${VULKAN_PATH}/cmake")
find_package(SDL3 REQUIRED)
# TODO: cascade cmakelists.txt
@@ -35,10 +34,11 @@ add_executable(ouros
target_include_directories(ouros PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/"
"${CMAKE_CURRENT_SOURCE_DIR}/src"
"${VULKAN_PATH}/Include"
)
if(WIN32)
set(SDL3_DLL "${SDL3_PATH}/lib/x64/SDL3.dll") # assuming youre not arm or 32 bit
set(SDL3_DLL "${VULKAN_PATH}/Bin/SDL3.dll")
add_custom_command(TARGET ouros POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${SDL3_DLL}"

View File

@@ -43,10 +43,14 @@ Below is beyond Sascha's guide, but available elsewhere on his github. These are
- CMake (im using 4.0.0)
- C++20 compatible compiler (I use g++12 or MSVC++17) (probably works with mingw but I haven't tested it myself)
- Vulkan compatible gpu drivers (tested with an RTX3070 and an R9700)
- Vulkan SDK (https://vulkan.lunarg.com/sdk/home) <- the installer provides the option to install SDL and VOC as well as verifies GPU drivers
- SDL3 (im using version 3.4.4)
- Vulkan SDK (https://vulkan.lunarg.com/sdk/home)
- SDL3*
- VOC*
- GLM*
- Will add more as the project grows
*packaged with the Vulkan SDK
### Clone respository:
```bash
$ git clone https://git.vxbard.net/homeburger/ouros.git --recursive
@@ -54,8 +58,7 @@ $ git clone https://git.vxbard.net/homeburger/ouros.git --recursive
### Configure and build project:
```bash
$ cmake -S . -B build \
-VULKAN_PATH="${VULKAN_INSTALL_PATH}" \ # either set these variables or substitute them in
-DSDL3_PATH="${SDL3_INSTALL_PATH}" # optional, dont need if youre using the sdl in the vulkan sdk
-DVULKAN_PATH="${VULKAN_INSTALL_PATH}" # either set this variable or substitute it in
$ cmake --build build -j
```
### Execute application:
@@ -66,10 +69,11 @@ $ ./build/ouros
```
For control on debugging:
```bash
$ ./build/ouros --debug 0b00111111
$ ./build/ouros --debug=0b00111111
```
where each bit in the mask corresponds to a debug level, in order: unused, unused, fatal, trace, error, warning, notice, and info. `0b1111111` enables all debug messages.
## Additional Resources
- https://howtovulkan.com/
- https://howtovulkan.com/, primary guide, most up-to-date (2026, Vulkan v1.4)
- https://vkguide.dev/, Vulkan v1.3, but provides a lot of guidance on a more robustly architectured Vulkan app
- Will add more as I use them

View File

@@ -1,6 +1,9 @@
#include "App.hpp"
#include <thread>
#include <chrono>
#include "utils/utils.hpp"
App::App() {
@@ -24,24 +27,25 @@ int App::run() {
utils::debugPrint(__FUNCTION__, __LINE__, "Run app.", utils::DebugLevel::Trace);
engine_->exec();
bool quit = false;
while (!quit) {
// app loop for as long as the window is open
// other threads might be able to change quit to true to auto close
SDL_Event event;
while(SDL_PollEvent(&event)) { // TODO: pass event handling to window
if(event.type == SDL_EVENT_QUIT ) {
quit = true;
}
while (window_->open()) {
// app loop for as long as the window is open
// pass events to the window
while(SDL_PollEvent(&event) != 0) {
window_->handleEvent(event);
}
// pass vulkan handling to engine
// call engine.render() or something
// engine has a pointer to window so can handle pushing to the screen
if(window_->rendering()) {
engine_->draw();
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // wait until the window is visible again
}
}
// SDL teardown handled in window destructor

View File

@@ -11,7 +11,7 @@ public:
App();
~App() = default;
// excecute, called from main()
// excecute, called from main(). returns after window closes
int run();
private:
@@ -22,4 +22,6 @@ private:
Window* window_;
Engine* engine_;
bool rendering_ = true;
};

View File

@@ -1,6 +1,8 @@
#include "Window.hpp"
#include <SDL3/SDL_events.h>
Window::Window() {
(void)init();
@@ -16,8 +18,27 @@ Window::~Window() {
int Window::init() {
sdlWindow_ = SDL_CreateWindow("How to Vulkan", 1280u, 720u, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
sdlWindow_ = SDL_CreateWindow("Ouros: Vulkan", 1280u, 720u, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
return (sdlWindow_ == nullptr);
if(sdlWindow_ != nullptr) {
rendering_ = true;
open_ = true;
return 0;
} else {
return -1;
}
}
void Window::handleEvent(SDL_Event& event) {
if(event.type == SDL_EVENT_QUIT ) {
open_ = false;
}
if(event.type == SDL_EVENT_WINDOW_MINIMIZED) {
rendering_ = false;
} else if(event.type == SDL_EVENT_WINDOW_RESTORED) {
rendering_ = true;
}
}

View File

@@ -2,7 +2,7 @@
#pragma once
#include "SDL3/SDL.h"
#include "SDL3/SDL_Vulkan.h"
#include "SDL3/SDL_vulkan.h"
// reference: https://wiki.libsdl.org/SDL3/SDL_CreateWindow
@@ -13,10 +13,22 @@ public:
Window();
~Window();
void handleEvent(SDL_Event& event);
bool rendering() { return rendering_; }
bool open() { return open_; }
private:
// launches window
int init();
// this window class will eventually hold mouse, keyboard, audio, etc. interfaces, like an SDL3 hub
// app will be able to attach callbacks for mouse and keyboard events
SDL_Window* sdlWindow_;
bool rendering_ = false;
bool open_ = false;
};

View File

@@ -13,10 +13,6 @@ Engine::Engine(Window* window): window_(window) {
void Engine::init() {
}
void Engine::exec() {
VkApplicationInfo appInfo {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "How to Vulkan",
@@ -34,11 +30,21 @@ void Engine::exec() {
};
VkInstance instance;
VkResult result = vkCreateInstance(&instanceCI, nullptr, &instance);
if(result == VK_SUCCESS) {
if(vkCreateInstance(&instanceCI, nullptr, &instance) == VK_SUCCESS) {
utils::debugPrint(__FUNCTION__, __LINE__, "Vulkan instance successfully created.", utils::DebugLevel::Info);
} else {
utils::debugPrint(__FUNCTION__, __LINE__, "Error creating Vulkan instance", utils::DebugLevel::Error);
}
// next steps:
// device selection and setup
// queue creation
// vulkan memory allocator
// create vulkan surface
// attach surface to window
}
void Engine::draw() {
}

View File

@@ -12,11 +12,14 @@ public:
Engine(Window* window);
~Engine() = default;
void exec();
void init();
// draw is called every render iteration in that while loop
void draw();
private:
void init();
// might get rid of this
// TODO: smart pointers probably would be smart
Window* window_;

View File

@@ -22,6 +22,7 @@ int main(int argc, char *argv[]) {
// maybe do some exceptions here
App app = App();
// executes for as long as the window is open
return app.run();
}

View File

@@ -5,9 +5,10 @@
#include <iomanip>
#include <string>
// TODO: might move a lot of this into a debug.h file
// TODO: might move a lot of this into a debug.hpp file
// TODO: logging ? but i dont care enough
// do i classify this up? global static functions kinda scare me but i mean otrherwise itll be a global singleton so idk
namespace utils {
enum DebugLevel : size_t {
@@ -31,13 +32,14 @@ namespace utils {
"Extra1",
"Extra2"
};
// the idea is to have a debug bitmask, maybe 8 bits wide, where the bits can be set via compiler flags
// stores the input that determines which debug statements to filter
inline uint8_t debugMask;
// print to standard output in a normalized format, includes the trace and debug level
static void debugPrint(const char* function, int line, std::string message, size_t debugLevel) {
if(!(debugMask >> debugLevel)) return; // then ignore this debug level
if(!((debugMask >> debugLevel) & 1)) return; // then ignore this debug level
std::cout << "[ " << std::left << std::setw(16) << function << ": " << std::right << std::setw(4) << line << ", " << std::left << std::setw(8) << debugTypeStrings[debugLevel] << " ] " << message << std::endl;
// TODO: add timestamps
@@ -45,14 +47,17 @@ namespace utils {
return;
}
// process the input from the executable (0bxxxxxxxx)
static void parseDebugMaskString(std::string& debugMaskString) {
std::string value = debugMaskString;
if(value.rfind("0b", 0) == 0) {
if(value.rfind("0b", 0) == 0) { // may allow other inputs in the future, this is just a binary byte
value = value.substr(2); // strip the binary literal identifier
debugMask = static_cast<uint8_t>(std::stoul(value, nullptr, 2)); // interpret a string as a uint
} else {
std::cout << "Unsupported debug key." << std::endl;
}
debugMask = static_cast<uint8_t>(std::stoul(value, nullptr, 2)); // interpret a string as a uint
debugMask = 0; // disable debug statements
}
}
}