-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameState.cpp
138 lines (121 loc) · 3.13 KB
/
GameState.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
#include "GameState.h"
#include <iostream>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <cmath>
GameState::GameState()
{
}
GameState::GameState(SDL_Renderer *gRenderer)
{
this->Renderer = gRenderer;
}
GameState::~GameState()
{
}
TextureWithVariables GameState::loadTexture(std::string path, SDL_Renderer *R, bool to_clip, RGB rgb) {
TextureWithVariables twv = { NULL, 0, 0 };
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL)
{
std::cout << "Unable to load image! SDL_image Error: " << IMG_GetError() << std::endl;
}
else
{
if (to_clip) {
//Color key image
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, rgb.r, rgb.g, rgb.b));
}
//Create texture from surface pixels
twv.texture = SDL_CreateTextureFromSurface(R, loadedSurface);
if (twv.texture == NULL)
{
std::cout << "Unable to create texture! SDL Error: " << SDL_GetError() << std::endl;
}
else {
twv.height = loadedSurface->h;
twv.width = loadedSurface->w;
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
return twv;
}
TextureWithVariables GameState::loadFont(SDL_Renderer *R, std::string fontName, int fontSize, std::string textureText, SDL_Color textColor)
{
TextureWithVariables twv = { NULL,0,0 };
TTF_Font *font = TTF_OpenFont(fontName.c_str(), fontSize);
SDL_Surface *textSurface = TTF_RenderUTF8_Blended(font, textureText.c_str(), textColor);
if (textSurface == NULL)
{
std::cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << std::endl;
}
else
{
//Create texture from surface pixels
twv.texture = SDL_CreateTextureFromSurface(R, textSurface);
if (twv.texture == NULL)
{
std::cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << std::endl;
}
else {
twv.height = textSurface->h;
twv.width = textSurface->w;
}
}
SDL_FreeSurface(textSurface);
TTF_CloseFont(font);
font = NULL;
return twv;
}
StartingScreen::StartingScreen()
{
}
StartingScreen::~StartingScreen()
{
}
StartingScreen::StartingScreen(SDL_Renderer *gRenderer, int delay)
{
this->Renderer = gRenderer;
fadeDelay = delay;
}
void StartingScreen::init()
{
startTime = SDL_GetTicks();
//Load texture
screen = loadTexture("startingmenuscreen.jpg",Renderer);
SDL_SetTextureBlendMode(screen.texture, SDL_BLENDMODE_BLEND);
initted = true;
}
void StartingScreen::uninit()
{
if (initted == true) {
SDL_DestroyTexture(screen.texture);
initted = false;
}
}
Instruction StartingScreen::process(SDL_Event e, Instruction nextInstruction)
{
if (!initted) {
init();
std::cout << "In starting screen" << std::endl;
}
else
{
int currentTick = SDL_GetTicks();
//Render texture to screen
SDL_SetTextureAlphaMod(screen.texture,(Uint8) (255.0 * (((float)currentTick - (float)startTime) / (float)fadeDelay)));
SDL_RenderCopyEx(Renderer, screen.texture, NULL, NULL, 0, NULL, SDL_FLIP_NONE);
if (!(currentTick - startTime < fadeDelay))
{
uninit();
instruction.nextState = enums::MUSIC_SELECTION;
}
else
{
instruction.nextState = enums::STARTING_SCREEN;
}
}
return instruction;
}