Rewrite of main function to use SDL3 app main callbacks
This commit is contained in:
parent
eeb0dd8024
commit
969a3285e9
5
.gitignore
vendored
5
.gitignore
vendored
@ -103,4 +103,7 @@ CMakeUserPresets.json
|
||||
# Other
|
||||
.cache/
|
||||
.vscode/
|
||||
build/
|
||||
build/
|
||||
|
||||
.kdev4/
|
||||
*.kdev4
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(vksdlproj VERSION 0.1.0 LANGUAGES C CXX)
|
||||
project(vksdlproj VERSION 0.2.0 LANGUAGES C CXX)
|
||||
|
||||
SET(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
|
||||
set(EXE_NAME project)
|
||||
|
||||
set(PROGRAM_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/vulkanapp.cc)
|
||||
set(PROGRAM_SOURCES )#${CMAKE_CURRENT_SOURCE_DIR}/src/vulkanapp.cc)
|
||||
|
||||
option(SDL3_NONSYSTEM "Use SDL3 from folder in source tree" ON)
|
||||
|
||||
@ -69,4 +69,4 @@ endif()
|
||||
# Linking
|
||||
target_link_libraries(${EXE_NAME} PRIVATE SDL3::SDL3)
|
||||
target_link_libraries(${EXE_NAME} PRIVATE Vulkan::Vulkan)
|
||||
target_link_libraries(${EXE_NAME} PRIVATE vk-bootstrap::vk-bootstrap)
|
||||
target_link_libraries(${EXE_NAME} PRIVATE vk-bootstrap::vk-bootstrap)
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
const std::vector<const char*> validationLayers = {
|
||||
@ -138,4 +140,4 @@ private:
|
||||
|
||||
return VK_FALSE;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
69
src/main.cc
69
src/main.cc
@ -1,19 +1,62 @@
|
||||
#include "vulkanapp.hh"
|
||||
#define SDL_MAIN_USE_CALLBACKS
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <SDL3/SDL_init.h>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <exception>
|
||||
constexpr uint32_t winInitWidth = 512;
|
||||
constexpr uint32_t winInitHeight = 512;
|
||||
|
||||
int main() {
|
||||
VulkanApp app(1024, 1024);
|
||||
struct AppContext {
|
||||
SDL_Window *window;
|
||||
SDL_AppResult appQuit = SDL_APP_CONTINUE;
|
||||
};
|
||||
|
||||
try {
|
||||
app.init();
|
||||
app.loop();
|
||||
app.cleanup();
|
||||
} catch(const std::exception& except) {
|
||||
fprintf(stderr, "Error! %s\n", except.what());
|
||||
SDL_AppResult SDL_Fail() {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error: %s", SDL_GetError());
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
return 1;
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
|
||||
if(!SDL_Init(SDL_INIT_VIDEO))
|
||||
return SDL_Fail();
|
||||
|
||||
SDL_Window *win = SDL_CreateWindow("SDL3 Vulkan", winInitWidth, winInitHeight, SDL_WINDOW_RESIZABLE | SDL_WINDOW_VULKAN);
|
||||
|
||||
if(!win)
|
||||
return SDL_Fail();
|
||||
|
||||
*appstate = new AppContext {
|
||||
.window = win,
|
||||
};
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
||||
AppContext* app = static_cast<AppContext*>(appstate);
|
||||
|
||||
switch(event->type) {
|
||||
case SDL_EVENT_QUIT:
|
||||
app->appQuit = SDL_APP_SUCCESS;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||
AppContext* app = static_cast<AppContext*>(appstate);
|
||||
|
||||
return app->appQuit;
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||
AppContext* app = static_cast<AppContext*>(appstate);
|
||||
|
||||
if(app) {
|
||||
SDL_DestroyWindow(app->window);
|
||||
|
||||
delete app;
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user