-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleRender.cpp
116 lines (93 loc) · 2.22 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
#include "Globals.h"
#include "Application.h"
#include "ModuleRender.h"
ModuleRender::ModuleRender(Application* app, bool start_enabled) : Module(app, start_enabled)
{
renderer = NULL;
camera.x = camera.y = 0;
camera.w = SCREEN_WIDTH;
camera.h = SCREEN_HEIGHT;
}
// Destructor
ModuleRender::~ModuleRender()
{}
// Called before render is available
bool ModuleRender::Init()
{
LOG("Creating Renderer context");
bool ret = true;
Uint32 flags = 0;
if(VSYNC == true)
{
flags |= SDL_RENDERER_PRESENTVSYNC;
}
renderer = SDL_CreateRenderer(App->window->window, -1, flags);
if(renderer == NULL)
{
LOG("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
return ret;
}
// PreUpdate: clear buffer
update_status ModuleRender::PreUpdate()
{
SDL_RenderClear(renderer);
return UPDATE_CONTINUE;
}
// Update: debug camera
update_status ModuleRender::Update()
{
int speed = 3;
if(App->input->GetKey(SDL_SCANCODE_UP) == KEY_REPEAT)
App->renderer->camera.y += speed;
if(App->input->GetKey(SDL_SCANCODE_DOWN) == KEY_REPEAT)
App->renderer->camera.y -= speed;
if(App->input->GetKey(SDL_SCANCODE_LEFT) == KEY_REPEAT)
App->renderer->camera.x += speed;
if(App->input->GetKey(SDL_SCANCODE_RIGHT) == KEY_REPEAT)
App->renderer->camera.x -= speed;
return UPDATE_CONTINUE;
}
// PostUpdate present buffer to screen
update_status ModuleRender::PostUpdate()
{
SDL_RenderPresent(renderer);
return UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleRender::CleanUp()
{
LOG("Destroying renderer");
//Destroy window
if(renderer != NULL)
{
SDL_DestroyRenderer(renderer);
}
return true;
}
// Blit to screen
bool ModuleRender::Blit(SDL_Texture* texture, int x, int y, SDL_Rect* section, float speed)
{
bool ret = true;
SDL_Rect rect;
rect.x = (int) (camera.x * speed) + x * SCREEN_SIZE;
rect.y = (int) (camera.y * speed) + y * SCREEN_SIZE;
if(section != NULL)
{
rect.w = section->w;
rect.h = section->h;
}
else
{
SDL_QueryTexture(texture, NULL, NULL, &rect.w, &rect.h);
}
rect.w *= SCREEN_SIZE;
rect.h *= SCREEN_SIZE;
if(SDL_RenderCopy(renderer, texture, section, &rect) != 0)
{
LOG("Cannot blit to screen. SDL_RenderCopy error: %s", SDL_GetError());
ret = false;
}
return ret;
}