-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
193 lines (158 loc) · 5.23 KB
/
Renderer.cpp
File metadata and controls
193 lines (158 loc) · 5.23 KB
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
#include "Renderer.h"
Camera::Camera(int x_, int y_, float zoom_)
{
x = x_;
y = y_;
zoom = zoom_;
transitionDuration = 1;
transitionRemaining = 0;
transitionOffset = {0, 0};
}
void Camera::transition(std::pair<int, int> offset, float duration)
{
transitionRemaining = duration;
transitionOffset = offset;
if (duration <= 0) // Don't divide by zero silly!
duration = 1;
transitionDuration = duration;
}
Renderer::Renderer(int _windowWidth, int _windowHeight)
{
SDL_Init(SDL_INIT_EVERYTHING);
windowWidth = _windowWidth;
windowHeight = _windowHeight;
window = SDL_CreateWindow("Poppy's Scuttle Slugging!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, SDL_WINDOW_SHOWN);
sdlRenderer = SDL_CreateRenderer(window, -1, 0);
images = new ImageCache();
camera = Camera(0, 0, 1);
initTime = prevFrameTime = currentFrameTime = clock();
}
Renderer::~Renderer()
{
delete images;
SDL_DestroyRenderer(sdlRenderer);
SDL_Quit();
}
ImageCache *Renderer::getImageCache()
{
return images;
}
Camera &Renderer::getCamera()
{
return camera;
}
float Renderer::deltaTime()
{
// FPS (Frames Per Second) = 1.0f / deltaTime()
return ((float)currentFrameTime - float(prevFrameTime)) / CLOCKS_PER_SEC;
}
int Renderer::getWindowWidth()
{
return windowWidth;
}
int Renderer::getWindowHeight()
{
return windowHeight;
}
std::pair<int, int> Renderer::getCameraPos()
{
return std::pair<int, int>{camera.x, camera.y};
}
void Renderer::addSprite(Sprite *sprite)
{
// todo: here I should put if framerect is negative, dont add to list
sprites.push_back(sprite);
}
void Renderer::addSprites(std::vector<Sprite *> sprites)
{
for (Sprite *s : sprites)
addSprite(s);
}
void Renderer::flushSprites()
{
while (sprites.size() > 0)
{
delete sprites.at(0);
sprites.erase(sprites.begin());
}
}
void Renderer::renderSprite(Sprite *sprite)
{
SDL_Rect destination;
SDL_Rect *frameRect_ = &sprite->frameRect;
// Display entire image if size of frameRect is less than or equal to zero
if (frameRect_->w <= 0 || frameRect_->h <= 0)
frameRect_ = NULL;
destination.w = sprite->width * camera.zoom;
destination.h = sprite->height * camera.zoom;
destination.x = windowWidth / 2 + (sprite->x - camera.x) * camera.zoom - destination.w * 0.5; // Center the sprites at their origin
destination.y = windowHeight / 2 + (sprite->y - camera.y) * camera.zoom - destination.h * 0.5;
SDL_RenderCopy(sdlRenderer, sprite->getTexture(sdlRenderer, images), frameRect_, &destination); //&sprite->frameRect
}
void Renderer::renderSprites()
{
for (Sprite *s : sprites)
renderSprite(s);
}
/**
* Render all sprites to the screen for the current frame.
* Also updates the deltaTime to match the time that has elapsed since the last call of update().
*/
void Renderer::update()
{
/*
Note on below: Caching textures is making it run super smoothly, already,
it can be optimized more by caching sprites or making renderer.sprites a
vector of pointers to sprites contained by different entities. These sprites
would live across frames and be modified physicsSteps and similar functions.
*/
/*
Read note above ^
I think I'll be able to speed up rendering by keeping sprites alive
(in a map or array or something) and just updating the textures when needed
(see https://wiki.libsdl.org/SDL2/SDL_UpdateTexture). First try keeping
needed sprites alive in a map and instead of destroying the texture just
update it. I think the way it works is the texture is just width and height,
and position is determined at runtime (so faster).
Try this first for real vvv
Lowkey I might just be able to have the image cache contain textures
I think how it works is surfaces are the image files themselves,
then textures give them widths and heights, then rendercopy gives a position
and possibly a stretched width/height.
*/
SDL_SetRenderDrawColor(sdlRenderer, 191, 191, 255, 255);
SDL_RenderClear(sdlRenderer);
renderSprites();
flushSprites(); // After rendering, free up space for the next frame of sprites
SDL_RenderPresent(sdlRenderer);
prevFrameTime = currentFrameTime; // Save last frame time
currentFrameTime = clock(); // Get current frame time, difference is time the last frame took to process/render
// std::cout << 1.0f / deltaTime() << "fps" << std::endl;
}
void Renderer::adjustCamera(int rel_x, int rel_y, float rel_zoom)
{
camera.x += rel_x;
camera.y += rel_y;
camera.zoom *= rel_zoom;
if (camera.zoom < 0)
camera.zoom = 1;
}
// Maybe make this a camera function?
void Renderer::focusCameraLeft(std::pair<int, int> position)
{
camera.x = position.first + windowWidth / 6; // Object is 1/3 of the window width from the left
if (position.second < -350)
camera.y = position.second + 350;
else
camera.y = 0;
if (camera.transitionRemaining > 0) // If there is a transition active
{
camera.x += camera.transitionOffset.first * (camera.transitionRemaining / camera.transitionDuration);
camera.y += camera.transitionOffset.second * (camera.transitionRemaining / camera.transitionDuration);
camera.transitionRemaining -= deltaTime();
}
}
void Renderer::transitionCamera(std::pair<int, int> offset, float duration)
{
camera.transition(offset, duration);
}