source: opengl-game/vulkan-game.cpp@ c559904

feature/imgui-sdl points-test
Last change on this file since c559904 was c559904, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Start using the logger class to output basic debugging info to a file in both openglgame and vulkangame

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[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]8using namespace std;
9
[99d44b2]10VulkanGame::VulkanGame() {
[0df3c9a]11 gui = nullptr;
12 window = nullptr;
13}
14
[99d44b2]15VulkanGame::~VulkanGame() {
[0df3c9a]16}
17
[b6e60b4]18void 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]62bool 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 int actualWidth, actualHeight;
81 gui->getWindowSize(&actualWidth, &actualHeight);
82
83 cout << "Target window size: (" << width << ", " << height << ")" << endl;
84 cout << "Actual window size: (" << actualWidth << ", " << actualHeight << ")" << endl;
85
[0df3c9a]86 return RTWO_SUCCESS;
87}
88
[99d44b2]89void VulkanGame::initVulkan() {
[0df3c9a]90}
91
[99d44b2]92void VulkanGame::mainLoop() {
[f6521fb]93 UIEvent e;
[0df3c9a]94 bool quit = false;
95
96 while (!quit) {
[27c40ce]97 gui->processEvents();
98
[f6521fb]99 while (gui->pollEvent(&e)) {
100 switch(e.type) {
101 case UI_EVENT_QUIT:
102 cout << "Quit event detected" << endl;
103 quit = true;
104 break;
105 case UI_EVENT_WINDOW:
106 cout << "Window event detected" << endl;
107 // Currently unused
108 break;
109 case UI_EVENT_KEY:
110 if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
111 quit = true;
112 } else {
113 cout << "Key event detected" << endl;
114 }
115 break;
116 case UI_EVENT_MOUSEBUTTONDOWN:
117 cout << "Mouse button down event detected" << endl;
118 break;
119 case UI_EVENT_MOUSEBUTTONUP:
120 cout << "Mouse button up event detected" << endl;
121 break;
122 case UI_EVENT_MOUSEMOTION:
123 break;
[c61323a]124 default:
125 cout << "Unhandled UI event: " << e.type << endl;
[0df3c9a]126 }
127 }
128 }
129}
130
[99d44b2]131void VulkanGame::cleanup() {
[b6e60b4]132 gui->destroyWindow();
133 gui->shutdown();
[0df3c9a]134 delete gui;
[850e84c]135}
Note: See TracBrowser for help on using the repository browser.