-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainGame.cpp
More file actions
155 lines (120 loc) · 3.19 KB
/
MainGame.cpp
File metadata and controls
155 lines (120 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "MainGame.h"
#include "Errors.h"
// For debugging
#include <iostream>
#define _DEBUG_MODE
MainGame::MainGame() :
_window(nullptr),
_screenWidth(1024),
_screenHeight(768),
_gameState(GameState::PLAY),
_time(0)
{
}
MainGame::~MainGame()
{
}
void MainGame::run()
{
initSystems();
// Temporary test code
_testSprite.init(-1.0f, -1.0f, 2.0f, 2.0f, "../../Assets/uwu.png");
// When this returns game is ready to quit
gameLoop();
// Cleanup and exit program
SDL_DestroyWindow(_window);
SDL_Quit();
exit(EXIT_SUCCESS);
}
void MainGame::initSystems()
{
// Initialize all SDL capabilities
// May eventually only use a subset of them for performance reasons
SDL_Init(SDL_INIT_EVERYTHING);
// Create OpenGL context with debug messages enabled
#ifdef _DEBUG_MODE
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
#endif
_window = SDL_CreateWindow("GameEngine",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
_screenWidth, _screenHeight,
SDL_WINDOW_OPENGL);
if (_window == nullptr)
{
fatalError("Could not create SDL Window.");
}
SDL_GLContext glCtx = SDL_GL_CreateContext(_window);
if (!glCtx)
{
fatalError("Could not create OpenGL Context.");
}
// Fixes some weird issues
glewExperimental = true;
GLenum glError = glewInit();
if (glError)
{
fatalError("Could not initialize GLEW.");
}
// Set debug callback for OpenGL errors
#ifdef _DEBUG_MODE
glDebugMessageCallback(errorCallback, nullptr);
#endif
// Having 2 frame buffers helps prevent flickering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Set background color to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Compile and link shaders
initShaders();
}
void MainGame::initShaders()
{
_colorProgram.compileShaders("../../Shaders/colorShader.vert",
"../../Shaders/colorShader.frag");
_colorProgram.linkShaders();
}
void MainGame::gameLoop()
{
while (_gameState != GameState::EXIT)
{
processInput();
_time = SDL_GetTicks() / 1000.0f;
drawGame();
}
}
void MainGame::processInput()
{
SDL_Event evnt;
while (SDL_PollEvent(&evnt))
{
switch (evnt.type)
{
case SDL_QUIT:
_gameState = GameState::EXIT;
return;
}
}
}
void MainGame::drawGame()
{
// Clear stuff
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use color shaders
_colorProgram.use();
// Set Uniform values on the GPU
setUniforms();
glActiveTexture(GL_TEXTURE0);
// Temporary test code
_testSprite.draw();
// Unbind stuff
_colorProgram.unuse();
// Show stuff on the screen
SDL_GL_SwapWindow(_window);
}
void MainGame::setUniforms()
{
GLint timeLocation = _colorProgram.getUniformLocation("time");
if (timeLocation) glUniform1f(timeLocation, _time);
GLint textureLocation = _colorProgram.getUniformLocation("textureSampler");
if (textureLocation) glUniform1i(textureLocation, 0);
}