Initial setup with SDL window

This commit is contained in:
macmacmac 2024-08-30 08:15:44 -04:00
commit baad5b4555
5 changed files with 133 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.cache/
build/

14
CMakeLists.txt Normal file
View File

@ -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)

30
include/vulkanapp.hh Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include "SDL_video.h"
#include "SDL_events.h"
#include <cstdint>
#include <vulkan/vulkan.h>
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();
};

17
src/main.cc Normal file
View File

@ -0,0 +1,17 @@
#include "vulkanapp.hh"
#include <exception>
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;
}

69
src/vulkanapp.cc Normal file
View File

@ -0,0 +1,69 @@
#include "vulkanapp.hh"
#include "SDL.h"
#include "SDL_vulkan.h"
#include <cstdio>
#include <stdexcept>
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();
}