-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.cpp
More file actions
113 lines (101 loc) · 2.64 KB
/
Game.cpp
File metadata and controls
113 lines (101 loc) · 2.64 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
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include "Game.h"
//Initializes SDL and starts game loop
Game::Game(){
#ifdef DEBUG
std::cout<<"Initializing SDL"<<std::endl;
#endif
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init( IMG_INIT_PNG ) ;
TTF_Init();
gameLoop();
}
//Quits SDL and subsystems
Game::~Game(){
#ifdef DEBUG
std::cout<<"Quiting SDL"<<std::endl;
#endif
fmap.freeSprites();
player.freeSprites();
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
//Game loop. Handles all events and drawing and updating of elements
void Game::gameLoop(){
#ifdef DEBUG
std::cout<<"Starting Game Loop"<<std::endl;
#endif
Graphics graphics;
SDL_Event e;
log.clearLog();
fmap = FloorMap(graphics);
fmap.setFloor(log,1);
fmap.genMap(graphics);
Vector2 playPos = fmap.putPlayer();
player.init(graphics);
player.setPosition(playPos);
currentKey = 0;
currentFrame = -1;
bool running = true;
float LAST_UPDATE;
while(running){
LAST_UPDATE = SDL_GetTicks();
graphics.clear();
while(SDL_PollEvent(&e) != 0){ //Handle all inputs
if( e.type == SDL_QUIT ){
running = false;
}
if( e.type == SDL_KEYDOWN && e.key.repeat == 0 ){
switch(e.key.keysym.sym){
case SDLK_w:case SDLK_a:case SDLK_s:case SDLK_d:case SDLK_UP:
case SDLK_LEFT:case SDLK_DOWN:case SDLK_RIGHT:
currentKey = e.key.keysym.sym;
currentFrame = 0;
}
}
else if( e.type == SDL_KEYUP ){
if(currentKey == e.key.keysym.sym){
currentKey = 0;
currentFrame = -1;
}
}
}
this->update(currentKey,currentFrame,graphics); //Update frame
if(currentFrame >= 0){ //Increment frame counter
currentFrame = (currentFrame+1)%FRAME_PER_MOVE;
}
if( fmap.currentEnemyProcess == 1 ){ //If enemies were not processed fully
currentKey = 0; //Stop movement
currentFrame = -1;
}
draw(graphics); //Draw graphics
float CURRENT_TIME = SDL_GetTicks();
if(CURRENT_TIME - LAST_UPDATE < FRAME_TIME){ //Wait to maintain frame rate
SDL_Delay(FRAME_TIME - CURRENT_TIME + LAST_UPDATE);
}
}
}
//Draws all the graphics
void Game::draw(Graphics &graphics){
graphics.defaultViewPort();
graphics.clear();
graphics.setViewPort(GAME_VIEWPORT);
fmap.drawMap(graphics);
fmap.drawViewCone(graphics);
fmap.drawItems(graphics);
player.draw(graphics);
fmap.drawEnemy(graphics);
graphics.setViewPort(LOG_VIEWPORT);
log.render(graphics);
graphics.setViewPort(STAT_VIEWPORT);
player.renderStat(graphics);
graphics.flip();
}
//Updates map and player
void Game::update(SDL_Keycode key, int currentFrame, Graphics &graphics){
fmap.handleMove(player,log,key,currentFrame,graphics);
player.setPosition(fmap.getPlayerPos());
}