[99d44b2] | 1 | #include "vulkan-game.hpp"
|
---|
[850e84c] | 2 |
|
---|
[0df3c9a] | 3 | #include <iostream>
|
---|
| 4 |
|
---|
[5edbd58] | 5 | #include "consts.hpp"
|
---|
[c559904] | 6 | #include "logger.hpp"
|
---|
[5edbd58] | 7 |
|
---|
[0df3c9a] | 8 | using namespace std;
|
---|
| 9 |
|
---|
[99d44b2] | 10 | VulkanGame::VulkanGame() {
|
---|
[0df3c9a] | 11 | gui = nullptr;
|
---|
| 12 | window = nullptr;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
[99d44b2] | 15 | VulkanGame::~VulkanGame() {
|
---|
[0df3c9a] | 16 | }
|
---|
| 17 |
|
---|
[b6e60b4] | 18 | void VulkanGame::run(int width, int height, unsigned char guiFlags) {
|
---|
[2e77b3f] | 19 | cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
|
---|
| 20 |
|
---|
| 21 | cout << "Vulkan Game" << endl;
|
---|
| 22 |
|
---|
[c559904] | 23 | // This gets the runtime version, use SDL_VERSION() for the comppile-time version
|
---|
| 24 | // TODO: Create a game-gui function to get the gui version and retrieve it that way
|
---|
| 25 | SDL_GetVersion(&sdlVersion);
|
---|
| 26 |
|
---|
| 27 | // TODO: Refactor the logger api to be more flexible,
|
---|
| 28 | // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
|
---|
| 29 | restart_gl_log();
|
---|
| 30 | gl_log("starting SDL\n%s.%s.%s",
|
---|
| 31 | to_string(sdlVersion.major).c_str(),
|
---|
| 32 | to_string(sdlVersion.minor).c_str(),
|
---|
| 33 | to_string(sdlVersion.patch).c_str());
|
---|
| 34 |
|
---|
| 35 | open_log();
|
---|
| 36 | get_log() << "starting SDL" << endl;
|
---|
| 37 | get_log() <<
|
---|
| 38 | (int)sdlVersion.major << "." <<
|
---|
| 39 | (int)sdlVersion.minor << "." <<
|
---|
| 40 | (int)sdlVersion.patch << endl;
|
---|
| 41 |
|
---|
[5edbd58] | 42 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
[0df3c9a] | 43 | return;
|
---|
| 44 | }
|
---|
[b6e60b4] | 45 |
|
---|
[27c40ce] | 46 | SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
|
---|
| 47 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
---|
| 48 |
|
---|
| 49 | SDL_RenderClear(renderer);
|
---|
| 50 |
|
---|
| 51 | SDL_RenderPresent(renderer);
|
---|
| 52 |
|
---|
[0df3c9a] | 53 | initVulkan();
|
---|
| 54 | mainLoop();
|
---|
| 55 | cleanup();
|
---|
[c559904] | 56 |
|
---|
| 57 | close_log();
|
---|
[0df3c9a] | 58 | }
|
---|
| 59 |
|
---|
[c559904] | 60 | // TODO: Make some more initi functions, or call this initUI if the
|
---|
| 61 | // amount of things initialized here keeps growing
|
---|
[b6e60b4] | 62 | bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
[c559904] | 63 | // TODO: Put all fonts, textures, and images in the assets folder
|
---|
[0df3c9a] | 64 | gui = new GameGui_SDL();
|
---|
| 65 |
|
---|
[b6e60b4] | 66 | if (gui->init() == RTWO_ERROR) {
|
---|
[c559904] | 67 | // TODO: Also print these sorts of errors to the log
|
---|
[0df3c9a] | 68 | cout << "UI library could not be initialized!" << endl;
|
---|
[b6e60b4] | 69 | cout << gui->getError() << endl;
|
---|
[0df3c9a] | 70 | return RTWO_ERROR;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[b6e60b4] | 73 | window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
[0df3c9a] | 74 | if (window == nullptr) {
|
---|
| 75 | cout << "Window could not be created!" << endl;
|
---|
[ed7c953] | 76 | cout << gui->getError() << endl;
|
---|
[0df3c9a] | 77 | return RTWO_ERROR;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[b6e60b4] | 80 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
[a6f6833] | 81 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
---|
[b6e60b4] | 82 |
|
---|
[0df3c9a] | 83 | return RTWO_SUCCESS;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[99d44b2] | 86 | void VulkanGame::initVulkan() {
|
---|
[0df3c9a] | 87 | }
|
---|
| 88 |
|
---|
[99d44b2] | 89 | void VulkanGame::mainLoop() {
|
---|
[f6521fb] | 90 | UIEvent e;
|
---|
[0df3c9a] | 91 | bool quit = false;
|
---|
| 92 |
|
---|
| 93 | while (!quit) {
|
---|
[27c40ce] | 94 | gui->processEvents();
|
---|
| 95 |
|
---|
[f6521fb] | 96 | while (gui->pollEvent(&e)) {
|
---|
| 97 | switch(e.type) {
|
---|
| 98 | case UI_EVENT_QUIT:
|
---|
| 99 | cout << "Quit event detected" << endl;
|
---|
| 100 | quit = true;
|
---|
| 101 | break;
|
---|
| 102 | case UI_EVENT_WINDOW:
|
---|
| 103 | cout << "Window event detected" << endl;
|
---|
| 104 | // Currently unused
|
---|
| 105 | break;
|
---|
| 106 | case UI_EVENT_KEY:
|
---|
| 107 | if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
|
---|
| 108 | quit = true;
|
---|
| 109 | } else {
|
---|
| 110 | cout << "Key event detected" << endl;
|
---|
| 111 | }
|
---|
| 112 | break;
|
---|
| 113 | case UI_EVENT_MOUSEBUTTONDOWN:
|
---|
| 114 | cout << "Mouse button down event detected" << endl;
|
---|
| 115 | break;
|
---|
| 116 | case UI_EVENT_MOUSEBUTTONUP:
|
---|
| 117 | cout << "Mouse button up event detected" << endl;
|
---|
| 118 | break;
|
---|
| 119 | case UI_EVENT_MOUSEMOTION:
|
---|
| 120 | break;
|
---|
[c61323a] | 121 | default:
|
---|
| 122 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
[0df3c9a] | 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[99d44b2] | 128 | void VulkanGame::cleanup() {
|
---|
[b6e60b4] | 129 | gui->destroyWindow();
|
---|
| 130 | gui->shutdown();
|
---|
[0df3c9a] | 131 | delete gui;
|
---|
[850e84c] | 132 | }
|
---|