-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
69 lines (60 loc) · 1.79 KB
/
Renderer.cpp
File metadata and controls
69 lines (60 loc) · 1.79 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
#include <stdexcept>
#include "Renderer.h"
#include "SceneManager.h"
#include "Texture2D.h"
int GetOpenGLDriverIndex()
{
auto openglIndex = -1;
const auto driverCount = SDL_GetNumRenderDrivers();
for (auto i = 0; i < driverCount; i++)
{
SDL_RendererInfo info;
if (!SDL_GetRenderDriverInfo(i, &info))
if (!strcmp(info.name, "opengl"))
openglIndex = i;
}
return openglIndex;
}
void dae::Renderer::Init(SDL_Window* window)
{
m_window = window;
m_renderer = SDL_CreateRenderer(window, GetOpenGLDriverIndex(), SDL_RENDERER_ACCELERATED);
if (m_renderer == nullptr)
{
throw std::runtime_error(std::string("SDL_CreateRenderer Error: ") + SDL_GetError());
}
}
void dae::Renderer::Render() const
{
const auto& color = GetBackgroundColor();
SDL_SetRenderDrawColor(m_renderer, color.r, color.g, color.b, color.a);
SDL_RenderClear(m_renderer);
SceneManager::GetInstance().Render();
SDL_RenderPresent(m_renderer);
}
void dae::Renderer::Destroy()
{
if (m_renderer != nullptr)
{
SDL_DestroyRenderer(m_renderer);
m_renderer = nullptr;
}
}
void dae::Renderer::RenderTexture(const Texture2D& texture, const float x, const float y) const
{
SDL_Rect dst{};
dst.x = static_cast<int>(x);
dst.y = static_cast<int>(y);
SDL_QueryTexture(texture.GetSDLTexture(), nullptr, nullptr, &dst.w, &dst.h);
SDL_RenderCopy(GetSDLRenderer(), texture.GetSDLTexture(), nullptr, &dst);
}
void dae::Renderer::RenderTexture(const Texture2D& texture, const float x, const float y, const float width, const float height) const
{
SDL_Rect dst{};
dst.x = static_cast<int>(x);
dst.y = static_cast<int>(y);
dst.w = static_cast<int>(width);
dst.h = static_cast<int>(height);
SDL_RenderCopy(GetSDLRenderer(), texture.GetSDLTexture(), nullptr, &dst);
}
SDL_Renderer* dae::Renderer::GetSDLRenderer() const { return m_renderer; }