forked from Trevorstrobel/advent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cpp
142 lines (119 loc) · 2.62 KB
/
Engine.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
#include "Engine.h"
#include "Player.h"
#include "Input.h"
#include "Projectile.h"
int Engine::screenWidth = 0;
int Engine::screenHeight = 0;
bool Engine::isRunning = true;
Engine::Engine()
{
// gameAudio;
renderer = nullptr;
window = nullptr;
// if you turn this off you can watch it do ugly antialiasing on rotation
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
}
Engine::~Engine()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
}
void Engine::Run(int w, int h)
{
screenWidth = w;
screenHeight = h;
if (Init() == false)
{
printf("Failed to init the Engine. SDL Error: %s\n", SDL_GetError());
}
else
{
gameLoop();
}
}
bool Engine::Init()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0)
{
printf("SDL failed to init SDL. SDL Error: %s\n", SDL_GetError());
return false;
}
window = SDL_CreateWindow("Advent", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
if (window == nullptr)
{
printf("SDL Failed to create a window. SDL Error: %s\n", SDL_GetError());
return false;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr)
{
printf("SDL failed to create a renderer. SDL Error: %s\n", SDL_GetError());
return false;
}
gameAudio->LoadAudioMedia();
/*gameBGMusic = gameAudio->bgMusic;
if (gameBGMusic == nullptr) {
printf("you dun goofed. no bg music");
}*/
background.LoadFromFile(renderer, "resources/space_bg.png");
return true;
}
//Engine::gameLoop() Kappa.
void Engine::gameLoop()
{
player = new Player(renderer);
crosshair = new Crosshair(renderer);
input = new Input();
float dt = 1 / 60.0f;
float currentTime = SDL_GetTicks() / 1000.0f;
while (isRunning)
{
processInputs();
//update stuff
float newTime = SDL_GetTicks() / 1000.0f;
float frameTime = newTime - currentTime;
currentTime = newTime;
while (frameTime > 0.0f)
{
float deltaTime = std::min(frameTime, dt);
frameTime -= deltaTime;
}
update();
}
}
void Engine::processInputs()
{
input->ProcessInput();
crosshair->HandleInput(input);
player->HandleInput(input);
}
void Engine::update()
{
if (!player->IsDead)
{
player->Update();
draw();
}
}
void Engine::draw()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background.GetTexture(), NULL, NULL);
player->Render(renderer);
crosshair->Render(renderer);
SDL_RenderPresent(renderer);
}
int Engine::GetScreenWidth()
{
return screenWidth;
}
int Engine::GetScreenHeight()
{
return screenHeight;
}
void Engine::Quit()
{
isRunning = false;
}