-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathrenderer.cpp
More file actions
132 lines (116 loc) · 3.32 KB
/
Copy pathrenderer.cpp
File metadata and controls
132 lines (116 loc) · 3.32 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
#include "renderer.hpp"
#include <SDL_image.h>
#include <algorithm>
#include <cmath>
#include "infoLog.hpp"
#ifdef NXDK
#include <hal/video.h>
#endif
// One line of text with the default font is 31 pixels high.
// FIXME: Should probably be dynamic and dependent of font settings.
const unsigned int FONT_TEX_SIZE = 31;
Renderer::Renderer() {
#ifdef NXDK
VIDEO_MODE xmode = XVideoGetMode();
height = xmode.height;
width = xmode.width;
windowFlags = SDL_WINDOW_SHOWN;
#else
height = 480;
width = 640;
windowFlags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
// renderFlags = SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_SOFTWARE;
#endif
overscanCompX = width * 0.075;
overscanCompY = height * 0.075;
menuItemCount = (height - (overscanCompY * 2)) / FONT_TEX_SIZE;
lowerHalf = menuItemCount / 2;
upperHalf = ceil(menuItemCount / 2.0);
}
Renderer::~Renderer() {
if (background != nullptr) {
SDL_DestroyTexture(background);
}
if (renderer != nullptr) {
SDL_DestroyRenderer(renderer);
}
if (window != nullptr) {
SDL_DestroyWindow(window);
}
}
int Renderer::init() {
window = SDL_CreateWindow("NevolutionX", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height, windowFlags);
if (window == nullptr) {
return 1;
}
renderer = SDL_CreateRenderer(window, -1, renderFlags);
if (renderer == nullptr) {
return 2;
}
SDL_SetRenderDrawBlendMode(getRenderer(), SDL_BLENDMODE_BLEND);
setDrawColor();
clear();
return 0;
}
int Renderer::init(std::string const& backgroundImagePath) {
int ret = init();
if (ret != 0) {
return ret;
}
SDL_Surface* bgsurf = IMG_Load(backgroundImagePath.c_str());
if (bgsurf == nullptr) {
InfoLog::outputLine(InfoLog::ERROR, "Creating background surface failed.\n");
return 3;
}
background = SDL_CreateTextureFromSurface(renderer, bgsurf);
SDL_FreeSurface(bgsurf);
if (background == nullptr) {
InfoLog::outputLine(InfoLog::ERROR, "Creating background texture failed.\n");
return 4;
}
return ret;
}
int Renderer::clear() {
int ret = SDL_RenderClear(renderer);
return ret;
}
void Renderer::flip() {
setDrawColor(0, 0, 0, 0xFF);
SDL_RenderDrawRect(renderer, nullptr);
setDrawColor();
SDL_RenderPresent(renderer);
#ifdef NXDK
XVideoWaitForVBlank();
#endif
}
int Renderer::setDrawColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
return SDL_SetRenderDrawColor(renderer, r, g, b, a);
}
void Renderer::drawTexture(SDL_Texture* tex, SDL_Rect& src, SDL_Rect& dst) {
SDL_RenderCopy(renderer, tex, &src, &dst);
}
void Renderer::drawTexture(SDL_Texture* tex, SDL_Rect& dst) {
SDL_RenderCopy(renderer, tex, nullptr, &dst);
}
void Renderer::drawTexture(SDL_Texture* tex, int x, int y) {
SDL_Rect dst = { x, y, 0, 0 };
SDL_QueryTexture(tex, nullptr, nullptr, &dst.w, &dst.h);
drawTexture(tex, dst);
}
void Renderer::fillRectangle(const SDL_Rect& dst) {
SDL_RenderFillRect(renderer, &dst);
}
void Renderer::fillRectangle(const SDL_FRect& dst) {
SDL_RenderFillRectF(renderer, &dst);
}
void Renderer::blitSurface(SDL_Surface* bg, SDL_Surface* fg, int offset) {
SDL_Rect dst = { offset, offset, fg->w, fg->h };
SDL_SetSurfaceBlendMode(fg, SDL_BLENDMODE_BLEND);
SDL_BlitSurface(fg, NULL, bg, &dst);
}
void Renderer::drawBackground() {
if (background != nullptr) {
drawTexture(background, 0, 0);
}
}