-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
146 lines (117 loc) · 3.4 KB
/
Game.cpp
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
#include "Game.h"
#include "MusicSelection.h"
#include "MainGame.h"
#include <iostream>
#include <SDL_image.h>
#include <bass.h>
Game::Game(InitVariables var)
{
initVariables = var;
this->SCREEN_WIDTH = initVariables.screen_width;
this->SCREEN_HEIGHT = initVariables.screen_height;
this->SCREEN_FPS = initVariables.screen_fps;
}
Game::~Game()
{
}
bool Game::init()
{
this->gRenderer = NULL;
this->gWindow = NULL;
bool success = true;
msPerFrame =(1000.0f / ((float)SCREEN_FPS));
prevFrameTime = SDL_GetTicks();
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "SDL could not initialize! SDL Error: " << SDL_GetError() << std::endl;
success = false;
}
//Set texture filtering to linear
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
std::cout << "Warning: Linear texture filtering not enabled!" << std::endl;
}
//Create window
gWindow = SDL_CreateWindow("Rhythm Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
std::cout << "Window could not be created! SDL Error: " << SDL_GetError() << std::endl;
success = false;
}
//Create renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer == NULL)
{
std::cout << "Renderer could not be created! SDL Error: " << SDL_GetError() << std::endl;
success = false;
}
//Initialize renderer color
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG| IMG_INIT_JPG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
success = false;
}
//Initialize TTF loading
if (TTF_Init() == -1)
{
std::cout << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << std::endl;
success = false;
}
//Initialize BASS module
if (!BASS_Init(-1, 44100, BASS_DEVICE_STEREO, 0, NULL)) {
std::cout << "BASS initialization failed" << std::endl;
}
//Load Game States
states[enums::STARTING_SCREEN] = new StartingScreen(gRenderer, initVariables.startup_screen_delay);
states[enums::MUSIC_SELECTION] = new MusicSelection(gRenderer, initVariables);
states[enums::MAIN_GAME] = new MainGame(gRenderer, initVariables);
currentState = enums::STARTING_SCREEN;
return success;
}
void Game::uninit()
{
//Don't forgget to free loaded images!
//SDL_DestroyTexture(gTexture);
//gTexture = NULL;
//Destroy window
SDL_DestroyRenderer(this->gRenderer);
SDL_DestroyWindow(this->gWindow);
this->gWindow = NULL;
this->gRenderer = NULL;
uninitializeAllStates();
BASS_Free();
//Quit SDL subsystems
IMG_Quit();
TTF_Quit();
SDL_Quit();
}
void Game::uninitializeAllStates()
{
for (int i = 0; i < enums::TOTAL_STATES; i++)
{
states[i]->uninit();
delete states[i];
}
}
bool Game::newFrame(SDL_Event e)
{
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
//Process and get instructions
nextInstruction = states[currentState]->process(e, previousInstruction);
currentState = nextInstruction.nextState;
previousInstruction = nextInstruction;
//Update screen
SDL_RenderPresent(gRenderer);
currentFrameTime = SDL_GetTicks();
if (currentFrameTime - prevFrameTime < msPerFrame)
{
SDL_Delay(((Uint32)msPerFrame) - (currentFrameTime - prevFrameTime));
}
prevFrameTime = currentFrameTime;
return nextInstruction.quit;
}