forked from GuillemFP/DoubleDragon3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleRender.cpp
271 lines (227 loc) · 6.69 KB
/
ModuleRender.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "Globals.h"
#include "Application.h"
#include "ModuleInput.h"
#include "ModuleWindow.h"
#include "ModuleRender.h"
#include "ModuleFonts.h"
#include "ModuleEntities.h"
#include "Player.h"
#include "SDL/include/SDL.h"
#include "JsonHandler.h"
ModuleRender::ModuleRender() : Module(MODULE_RENDER)
{
}
ModuleRender::~ModuleRender()
{}
bool ModuleRender::Init()
{
LOG("Creating Renderer context");
bool ret = true;
Uint32 flags = 0;
if (ConstantConfig() == false)
{
LOG("Problem retrieving value from configuration file");
ret = false;
}
camera.w = App->window->GetScreenWidth() * iSCREENSIZE;
camera.h = App->window->GetScreenHeight() * iSCREENSIZE;
if (bVSYNC)
flags |= SDL_RENDERER_PRESENTVSYNC;
renderer = SDL_CreateRenderer(App->window->window, -1, flags);
if (renderer == nullptr)
{
LOG("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
return ret;
}
update_status ModuleRender::PreUpdate()
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
return UPDATE_CONTINUE;
}
update_status ModuleRender::PostUpdate()
{
SDL_RenderPresent(renderer);
return UPDATE_CONTINUE;
}
bool ModuleRender::CleanUp()
{
LOG("Destroying renderer");
//Destroy window
if (renderer != nullptr)
{
SDL_DestroyRenderer(renderer);
}
return true;
}
bool ModuleRender::BlitScreenCentered(SDL_Texture* texture, SDL_Rect* section, float speed)
{
iPoint draw_origin;
if (section != nullptr)
{
draw_origin.x = (iSCREENWIDTH - section->w) / 2;
draw_origin.y = (iSCREENHEIGHT - section->h) / 2;
}
else
{
SDL_QueryTexture(texture, NULL, NULL, &draw_origin.x, &draw_origin.y);
draw_origin.x = (iSCREENWIDTH - draw_origin.x) / 2;
draw_origin.y = (iSCREENHEIGHT - draw_origin.y) / 2;
}
return Blit(texture, draw_origin, section, speed);
}
bool ModuleRender::BlitScreenXCentered(SDL_Texture * texture, int y, SDL_Rect * section, float speed)
{
iPoint draw_origin;
if (section != nullptr)
{
draw_origin.x = (iSCREENWIDTH - section->w) / 2;
draw_origin.y = y;
}
else
{
SDL_QueryTexture(texture, NULL, NULL, &draw_origin.x, &draw_origin.y);
draw_origin.x = (iSCREENWIDTH - draw_origin.x) / 2;
draw_origin.y = y;
}
return Blit(texture, draw_origin, section, speed);
}
bool ModuleRender::Blit(SDL_Texture* texture, const iPoint& position, SDL_Rect* section, float speed, bool inverse)
{
bool ret = true;
SDL_Rect rect;
rect.x = position.x * iSCREENSIZE + (int)(camera.x * speed);
rect.y = position.y * iSCREENSIZE + (int)(camera.y * speed);
if (section != nullptr)
{
rect.w = section->w;
rect.h = section->h;
}
else
SDL_QueryTexture(texture, NULL, NULL, &rect.w, &rect.h);
rect.w *= iSCREENSIZE;
rect.h *= iSCREENSIZE;
if (inverse == false)
{
if (SDL_RenderCopy(renderer, texture, section, &rect) != 0)
{
LOG("Cannot blit to screen. SDL_RenderCopy error: %s", SDL_GetError());
ret = false;
}
}
else
{
if (SDL_RenderCopyEx(renderer, texture, section, &rect, 0.0, NULL, SDL_FLIP_HORIZONTAL) != 0)
{
LOG("Cannot blit to screen. SDL_RenderCopy error: %s", SDL_GetError());
ret = false;
}
}
return ret;
}
bool ModuleRender::DrawQuad(const SDL_Rect& rect, Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool use_camera)
{
bool ret = true;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_Rect rec(rect);
if (use_camera == true)
{
rec.x = (int)(camera.x + rect.x * iSCREENSIZE);
rec.y = (int)(camera.y + rect.y * iSCREENSIZE);
rec.w *= iSCREENSIZE;
rec.h *= iSCREENSIZE;
}
if (SDL_RenderFillRect(renderer,&rec) != 0)
{
LOG("Cannot draw quad to screen. SDL_RenderFillRect error: %s", SDL_GetError());
ret = false;
}
return ret;
}
void ModuleRender::CameraInsideScene(int room_x, int room_width, int room_y)
{
if (bCenterCamera)
{
int active_players = App->entities->GetNumberActivePlayers();
int num_players = App->entities->GetNumberPlayers();
if (active_players == 0)
{
Player* player_one = App->entities->GetPlayerByNumber(0);
int center_player = center_player = player_one->position.x + player_one->dimensions.x / 2;
if (center_player*iSCREENSIZE + camera.x < (int)((1.0f - fCAMERA_MARGIN) * iSCREENWIDTH * iSCREENSIZE))
camera.x = ((int)((1.0f - fCAMERA_MARGIN) * iSCREENWIDTH) - center_player)*iSCREENSIZE;
else if (center_player*iSCREENSIZE + camera.x >(int)(fCAMERA_MARGIN * iSCREENWIDTH * iSCREENSIZE))
camera.x = ((int)(fCAMERA_MARGIN * iSCREENWIDTH) - center_player)*iSCREENSIZE;
}
else if (active_players == 1)
{
Player* player_one;
int center_player = 0;
for (int i = 0; i < num_players; i++)
{
player_one = App->entities->GetPlayerByNumber(i);
if (player_one->active == true)
{
center_player = player_one->position.x + player_one->dimensions.x / 2;
break;
}
}
if (center_player*iSCREENSIZE + camera.x < (int)((1.0f - fCAMERA_MARGIN) * iSCREENWIDTH * iSCREENSIZE))
camera.x = ((int)((1.0f - fCAMERA_MARGIN) * iSCREENWIDTH) - center_player)*iSCREENSIZE;
else if (center_player*iSCREENSIZE + camera.x >(int)(fCAMERA_MARGIN * iSCREENWIDTH * iSCREENSIZE))
camera.x = ((int)(fCAMERA_MARGIN * iSCREENWIDTH) - center_player)*iSCREENSIZE;
}
else
{
int center_player = 0;
for (int i = 0; i < num_players; i++)
{
Player* player = App->entities->GetPlayerByNumber(i);
if (player->active == true)
center_player += player->position.x + player->dimensions.x / 2;
}
center_player /= active_players;
camera.x = (iSCREENWIDTH / 2 - center_player)*iSCREENSIZE;
}
if (camera.x < (room_x - room_width) * iSCREENSIZE + camera.w)
camera.x = (room_x - room_width) * iSCREENSIZE + camera.w;
else if (camera.x > room_x)
camera.x = room_x;
camera.y = room_y;
}
}
bool ModuleRender::ConstantConfig()
{
bool ret = true;
if (App->parser->LoadObject(RENDER_SECTION) == true)
{
bVSYNC = App->parser->GetBoolMandatory("Vsync");
fDEFAULT_SPEED = App->parser->GetFloat("DefaultBlitSpeed");
fCAMERA_MARGIN = App->parser->GetFloat("CameraMargin");
ret = App->parser->UnloadObject();
}
else
ret = false;
if (fCAMERA_MARGIN < 0.5f)
fCAMERA_MARGIN = 0.5f;
iSCREENSIZE = App->window->GetScreenSize();
iSCREENWIDTH = App->window->GetScreenWidth();
iSCREENHEIGHT = App->window->GetScreenHeight();
return ret;
}
void ModuleRender::DebugCamera()
{
// debug camera
int speed = 5;
if (App->input->GetKey(SDL_SCANCODE_KP_8) == KEY_REPEAT)
App->renderer->camera.y += speed;
if (App->input->GetKey(SDL_SCANCODE_KP_2) == KEY_REPEAT)
App->renderer->camera.y -= speed;
if (App->input->GetKey(SDL_SCANCODE_KP_4) == KEY_REPEAT)
App->renderer->camera.x += speed;
if (App->input->GetKey(SDL_SCANCODE_KP_6) == KEY_REPEAT)
App->renderer->camera.x -= speed;
}