1 | #include "vulkan-game.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | #include "consts.hpp"
|
---|
6 | #include "logger.hpp"
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | VulkanGame::VulkanGame() {
|
---|
11 | gui = nullptr;
|
---|
12 | window = nullptr;
|
---|
13 | }
|
---|
14 |
|
---|
15 | VulkanGame::~VulkanGame() {
|
---|
16 | }
|
---|
17 |
|
---|
18 | void VulkanGame::run(int width, int height, unsigned char guiFlags) {
|
---|
19 | cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl;
|
---|
20 |
|
---|
21 | cout << "Vulkan Game" << endl;
|
---|
22 |
|
---|
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 |
|
---|
42 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
43 | return;
|
---|
44 | }
|
---|
45 |
|
---|
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 |
|
---|
53 | initVulkan();
|
---|
54 | mainLoop();
|
---|
55 | cleanup();
|
---|
56 |
|
---|
57 | close_log();
|
---|
58 | }
|
---|
59 |
|
---|
60 | // TODO: Make some more initi functions, or call this initUI if the
|
---|
61 | // amount of things initialized here keeps growing
|
---|
62 | bool VulkanGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
63 | // TODO: Put all fonts, textures, and images in the assets folder
|
---|
64 | gui = new GameGui_SDL();
|
---|
65 |
|
---|
66 | if (gui->init() == RTWO_ERROR) {
|
---|
67 | // TODO: Also print these sorts of errors to the log
|
---|
68 | cout << "UI library could not be initialized!" << endl;
|
---|
69 | cout << gui->getError() << endl;
|
---|
70 | return RTWO_ERROR;
|
---|
71 | }
|
---|
72 |
|
---|
73 | window = (SDL_Window*) gui->createWindow("Vulkan Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
74 | if (window == nullptr) {
|
---|
75 | cout << "Window could not be created!" << endl;
|
---|
76 | cout << gui->getError() << endl;
|
---|
77 | return RTWO_ERROR;
|
---|
78 | }
|
---|
79 |
|
---|
80 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
81 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
---|
82 |
|
---|
83 | return RTWO_SUCCESS;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void VulkanGame::initVulkan() {
|
---|
87 | }
|
---|
88 |
|
---|
89 | void VulkanGame::mainLoop() {
|
---|
90 | UIEvent e;
|
---|
91 | bool quit = false;
|
---|
92 |
|
---|
93 | while (!quit) {
|
---|
94 | gui->processEvents();
|
---|
95 |
|
---|
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;
|
---|
121 | default:
|
---|
122 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | void VulkanGame::cleanup() {
|
---|
129 | gui->destroyWindow();
|
---|
130 | gui->shutdown();
|
---|
131 | delete gui;
|
---|
132 | }
|
---|