-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.cpp
More file actions
65 lines (52 loc) · 1.99 KB
/
Copy pathgraphics.cpp
File metadata and controls
65 lines (52 loc) · 1.99 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
#include "graphics.h"
#include <iostream>
#include "constants.h"
#include "location.h"
#include "perf.h"
// Graphics-related constants.
constexpr char kGameTitle[] = "DBZ";
constexpr float kMsPerSecond = 1000.0;
Graphics::Graphics() {}
Graphics::~Graphics() {
// Destroy the window.
SDL_DestroyWindow(window_);
// Quit SDL gracefully.
SDL_Quit();
}
void Graphics::CreateWindow() {
// SDL method to create our window.
SDL_CreateWindowAndRenderer(constants::kWindowWidth, constants::kWindowHeight,
0, &window_, &renderer_);
SDL_SetWindowTitle(window_, kGameTitle);
// Clear the screen, putting our background color in every pixel.
SDL_RenderClear(renderer_);
// Displays our screen in the window.
SDL_RenderPresent(renderer_);
}
void Graphics::DrawNextFrame() {
// Draw the next frame to the screen.
SDL_RenderPresent(renderer_);
// Reset the renderer to prepare for the following frame.
SDL_RenderClear(renderer_);
}
void Graphics::AddSprite(const Sprite& sprite, const SDL_Rect& destination) {
// Draw this sprite on the screen.
AnimationFrame frame = sprite.GetSourceAnimationFrame();
Perf::GetPerf()->StartTimer("render_copy");
SDL_RenderCopy(renderer_, sprite.GetSpriteTexture(), &frame.source,
&destination);
Perf::GetPerf()->StopTimer("render_copy");
}
void Graphics::AddSprite(const Sprite& sprite, Location location) {
AnimationFrame frame = sprite.GetSourceAnimationFrame();
SDL_Rect destination = {
.x = static_cast<int>(location.x + (frame.offset.x * sprite.GetScale())),
.y = static_cast<int>(location.y + (frame.offset.y * sprite.GetScale())),
.w = static_cast<int>(frame.source.w * sprite.GetScale()),
.h = static_cast<int>(frame.source.h * sprite.GetScale())};
// Draw this sprite on the screen.
Perf::GetPerf()->StartTimer("render_copy");
SDL_RenderCopy(renderer_, sprite.GetSpriteTexture(), &frame.source,
&destination);
Perf::GetPerf()->StopTimer("render_copy");
}