commit baad5b45550964e3dbefb7174c2c21b8cdd9fded Author: macmacmac Date: Fri Aug 30 08:15:44 2024 -0400 Initial setup with SDL window diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b594ca7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.cache/ +build/ + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..db54fe1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.10) +project(vksdlproj VERSION 0.1.0 LANGUAGES C CXX) + +SET(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE) +set(EXE_NAME project) + +find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2) + +set(PROGRAM_SOURCES ${CMAKE_SOURCE_DIR}/src/vulkanapp.cc) + +add_executable(${EXE_NAME} ${CMAKE_SOURCE_DIR}/src/main.cc ${PROGRAM_SOURCES}) +target_include_directories(${EXE_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include) + +target_link_libraries(${EXE_NAME} PRIVATE SDL2::SDL2) \ No newline at end of file diff --git a/include/vulkanapp.hh b/include/vulkanapp.hh new file mode 100644 index 0000000..995e819 --- /dev/null +++ b/include/vulkanapp.hh @@ -0,0 +1,30 @@ +#pragma once + +#include "SDL_video.h" +#include "SDL_events.h" + +#include + +#include + +class VulkanApp { +public: + VulkanApp(const uint32_t& _w, const uint32_t& _h) : + mWidth(_w), mHeight(_h), mWin(nullptr), mActive(false) {} + + void run() { + init(); + loop(); + cleanup(); + } + + SDL_Window *mWin; + SDL_Event mEvent; + const uint32_t mWidth; + const uint32_t mHeight; + bool mActive; + + void init(); + void loop(); + void cleanup(); +}; \ No newline at end of file diff --git a/src/main.cc b/src/main.cc new file mode 100644 index 0000000..4cff868 --- /dev/null +++ b/src/main.cc @@ -0,0 +1,17 @@ +#include "vulkanapp.hh" + +#include + +int main() { + VulkanApp app(640, 480); + + try { + app.run(); + } catch(const std::exception& except) { + fprintf(stderr, "Error! %s\n", except.what()); + + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/src/vulkanapp.cc b/src/vulkanapp.cc new file mode 100644 index 0000000..231cd4f --- /dev/null +++ b/src/vulkanapp.cc @@ -0,0 +1,69 @@ +#include "vulkanapp.hh" + +#include "SDL.h" +#include "SDL_vulkan.h" + +#include +#include + +void VulkanApp::init() { + SDL_Init(SDL_INIT_EVERYTHING); + SDL_Vulkan_LoadLibrary(nullptr); + + // Create the window + mWin = SDL_CreateWindow("Vulkan+SDL2 Application", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + mWidth, + mHeight, + SDL_WINDOW_SHOWN | SDL_WINDOW_VULKAN + ); + + if(mWin == nullptr) { + std::string err = "Could not create window "; + err += SDL_GetError(); + err += "\n"; + throw std::runtime_error(err); + } +} + +void VulkanApp::loop() { + if(mWin == nullptr) { + std::string err = "Could not find window "; + err += SDL_GetError(); + err += "\n"; + throw std::runtime_error(err); + } + + mActive = true; + + //SDL_Renderer *rend = SDL_CreateRenderer(mWin, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + + // Main loop + while(mActive) { + while(SDL_PollEvent(&mEvent)) + if(mEvent.type == SDL_QUIT) { + mActive = false; + break; + } + + //SDL_SetRenderDrawColor(rend, 255, 0, 0, 255); + //SDL_RenderClear(rend); + + //SDL_RenderPresent(rend); + + SDL_ShowWindow(mWin); + } + + //SDL_DestroyRenderer(rend); +} + +void VulkanApp::cleanup() { + if(mWin != nullptr) { + SDL_DestroyWindow(mWin); + mWin = nullptr; + } + + SDL_Vulkan_UnloadLibrary(); + SDL_Quit(); +} \ No newline at end of file