forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainGame.cpp
More file actions
206 lines (163 loc) · 5.54 KB
/
MainGame.cpp
File metadata and controls
206 lines (163 loc) · 5.54 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "MainGame.h"
#include <Bengine/BengineErrors.h>
#include <Bengine/ResourceManager.h>
#include <iostream>
#include <string>
//Constructor, just initializes private member variables
MainGame::MainGame() :
_screenWidth(1024),
_screenHeight(768),
_time(0.0f),
_gameState(GameState::PLAY),
_maxFPS(60.0f)
{
_camera.init(_screenWidth, _screenHeight);
}
//Destructor
MainGame::~MainGame()
{
}
//This runs the game
void MainGame::run() {
initSystems();
//This only returns when the game ends
gameLoop();
}
//Initialize SDL and Opengl and whatever else we need
void MainGame::initSystems() {
Bengine::init();
_window.create("Game Engine", _screenWidth, _screenHeight, 0);
initShaders();
_spriteBatch.init();
_fpsLimiter.init(_maxFPS);
}
void MainGame::initShaders() {
_colorProgram.compileShaders("Shaders/colorShading.vert", "Shaders/colorShading.frag");
_colorProgram.addAttribute("vertexPosition");
_colorProgram.addAttribute("vertexColor");
_colorProgram.addAttribute("vertexUV");
_colorProgram.linkShaders();
}
//This is the main game loop for our program
void MainGame::gameLoop() {
//Will loop until we set _gameState to EXIT
while (_gameState != GameState::EXIT) {
_fpsLimiter.begin();
processInput();
_time += 0.1;
_camera.update();
// Update all bullets
for (size_t i = 0; i < _bullets.size();) {
if (_bullets[i].update() == true) {
_bullets[i] = _bullets.back();
_bullets.pop_back();
} else {
i++;
}
}
drawGame();
_fps = _fpsLimiter.end();
//print only once every 10 frames
static int frameCounter = 0;
frameCounter++;
if (frameCounter == 10) {
std::cout << _fps << std::endl;
frameCounter = 0;
}
}
}
//Processes input with SDL
void MainGame::processInput() {
SDL_Event evnt;
const float CAMERA_SPEED = 2.0f;
const float SCALE_SPEED = 0.1f;
//Will keep looping until there are no more events to process
while (SDL_PollEvent(&evnt)) {
switch (evnt.type) {
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
_inputManager.setMouseCoords((float)evnt.motion.x, (float)evnt.motion.y);
break;
case SDL_KEYDOWN:
_inputManager.pressKey(evnt.key.keysym.sym);
break;
case SDL_KEYUP:
_inputManager.releaseKey(evnt.key.keysym.sym);
break;
case SDL_MOUSEBUTTONDOWN:
_inputManager.pressKey(evnt.button.button);
break;
case SDL_MOUSEBUTTONUP:
_inputManager.releaseKey(evnt.button.button);
break;
}
}
if (_inputManager.isKeyDown(SDLK_w)) {
_camera.setPosition(_camera.getPosition() + glm::vec2(0.0f, CAMERA_SPEED));
}
if (_inputManager.isKeyDown(SDLK_s)) {
_camera.setPosition(_camera.getPosition() + glm::vec2(0.0f, -CAMERA_SPEED));
}
if (_inputManager.isKeyDown(SDLK_a)) {
_camera.setPosition(_camera.getPosition() + glm::vec2(-CAMERA_SPEED, 0.0f));
}
if (_inputManager.isKeyDown(SDLK_d)) {
_camera.setPosition(_camera.getPosition() + glm::vec2(CAMERA_SPEED, 0.0f));
}
if (_inputManager.isKeyDown(SDLK_q)) {
_camera.setScale(_camera.getScale() + SCALE_SPEED);
}
if (_inputManager.isKeyDown(SDLK_e)) {
_camera.setScale(_camera.getScale() - SCALE_SPEED);
}
if (_inputManager.isKeyDown(SDL_BUTTON_LEFT)) {
glm::vec2 mouseCoords = _inputManager.getMouseCoords();
mouseCoords = _camera.convertScreenToWorld(mouseCoords);
glm::vec2 playerPosition(0.0f);
glm::vec2 direction = mouseCoords - playerPosition;
direction = glm::normalize(direction);
_bullets.emplace_back(playerPosition, direction, 5.00f, 1000);
}
}
//Draws the game using the almighty OpenGL
void MainGame::drawGame() {
//Set the base depth to 1.0
glClearDepth(1.0);
//Clear the color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Enable the shader
_colorProgram.use();
//We are using texture unit 0
glActiveTexture(GL_TEXTURE0);
//Get the uniform location
GLint textureLocation = _colorProgram.getUniformLocation("mySampler");
//Tell the shader that the texture is in texture unit 0
glUniform1i(textureLocation, 0);
//Set the camera matrix
GLint pLocation = _colorProgram.getUniformLocation("P");
glm::mat4 cameraMatrix = _camera.getCameraMatrix();
glUniformMatrix4fv(pLocation, 1, GL_FALSE, &(cameraMatrix[0][0]));
_spriteBatch.begin();
glm::vec4 pos(0.0f, 0.0f, 50.0f, 50.0f);
glm::vec4 uv(0.0f, 0.0f, 1.0f, 1.0f);
static Bengine::GLTexture texture = Bengine::ResourceManager::getTexture("Textures/jimmyJump_pack/PNG/CharacterRight_Standing.png");
Bengine::ColorRGBA8 color;
color.r = 255;
color.g = 255;
color.b = 255;
color.a = 255;
_spriteBatch.draw(pos, uv, texture.id, 0.0f, color);
for (size_t i = 0; i < _bullets.size(); i++) {
_bullets[i].draw(_spriteBatch);
}
_spriteBatch.end();
_spriteBatch.renderBatch();
//unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);
//disable the shader
_colorProgram.unuse();
//Swap our buffer and draw everything to the screen!
_window.swapBuffer();
}