Rewrite of main function to use SDL3 app main callbacks
This commit is contained in:
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user