Skip to content

Commit 55efa36

Browse files
committed
rendering: Adds SDL_gpu based rendering.
1 parent c936b6d commit 55efa36

File tree

14 files changed

+404
-27
lines changed

14 files changed

+404
-27
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@
44
[submodule "3rdparty/NaturalSort"]
55
path = 3rdparty/NaturalSort
66
url = https://github.com/scopeInfinity/NaturalSort.git
7+
[submodule "3rdparty/pbgl"]
8+
path = 3rdparty/pbgl
9+
url = https://github.com/abaire/pbgl.git
10+
[submodule "3rdparty/sdl-gpu"]
11+
path = 3rdparty/sdl-gpu
12+
url = https://github.com/abaire/sdl-gpu.git

3rdparty/pbgl

Submodule pbgl added at f47dd37

3rdparty/sdl-gpu

Submodule sdl-gpu added at c1c3334

Includes/font.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#include "font.h"
22
#include <cassert>
3-
#include "3rdparty/SDL_FontCache/SDL_FontCache.h"
4-
#include "infoLog.h"
3+
54

65
Font::Font(Renderer& renderer, const char* path) : renderer(renderer) {
76
fcFont = FC_CreateFont();
87
assert(fcFont);
8+
#ifdef FC_USE_SDL_GPU
9+
bool load_success = FC_LoadFont(fcFont, path, 20, FC_MakeColor(250, 250, 250, 255),
10+
TTF_STYLE_NORMAL);
11+
#else
912
bool load_success = FC_LoadFont(fcFont, renderer.getRenderer(), path, 20,
1013
FC_MakeColor(250, 250, 250, 255), TTF_STYLE_NORMAL);
14+
#endif
1115
assert(load_success);
1216
}
1317

Includes/font.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
#include <string>
55
#include <utility>
66
#include <vector>
7-
#include "3rdparty/SDL_FontCache/SDL_FontCache.h"
87
#include "renderer.h"
98

9+
// clang-format off
10+
#pragma clang diagnostic push
11+
#pragma clang diagnostic ignored "-Wunused-function"
12+
#include "3rdparty/SDL_FontCache/SDL_FontCache.h"
13+
#pragma clang diagnostic pop
14+
// clang-format on
15+
1016
class Font {
1117
private:
1218
FC_Font* fcFont;

Includes/menu.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
#include "settingsMenu.hpp"
66
#include "xbeLauncher.h"
77
#include "xbeScanner.h"
8+
#ifdef NXDK
9+
// clang-format off
10+
#ifdef FC_USE_SDL_GPU
11+
#pragma clang diagnostic push
12+
#pragma clang diagnostic ignored "-Wunused-function"
13+
#include "SDL_gpu.h"
14+
#pragma clang diagnostic pop
15+
#endif
16+
// clang-format on
17+
#endif
818

919
// Character used in the config.json to separate multiple path entries.
1020
#define PATH_DELIMITER ','
@@ -384,13 +394,20 @@ void Menu::render(Font& font) {
384394
dimensions = font.draw(menutext, coordinates);
385395

386396
if (i == this->currentMenu->getSelected()) {
397+
#ifdef FC_USE_SDL_GPU
398+
GPU_Rect rect = { std::get<0>(coordinates) - 10, std::get<1>(coordinates),
399+
std::get<0>(dimensions) + 20, std::get<1>(dimensions) };
400+
SDL_Color color = { 0xFF, 0xFF, 0xFF, 0xFF };
401+
GPU_Rectangle2(renderer.getRenderer(), rect, color);
402+
#else
387403
SDL_Rect rect;
388404
rect.w = std::get<0>(dimensions) + 20;
389405
rect.h = std::get<1>(dimensions);
390406
rect.x = std::get<0>(coordinates) - 10;
391407
rect.y = std::get<1>(coordinates);
392408
renderer.setDrawColor(0xFF, 0xFF, 0xFF, 0xFF);
393409
SDL_RenderDrawRect(renderer.getRenderer(), &rect);
410+
#endif
394411
}
395412

396413
coordinates = std::pair<float, float>(

Includes/renderer.cpp

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#ifdef NXDK
88
#include <hal/video.h>
9+
#include "nxdk-sdl-gpu/nxdkSDLGPU.h"
910
#endif
1011

1112
// One line of text with the default font is 31 pixels high.
@@ -18,6 +19,9 @@ Renderer::Renderer() {
1819
height = xmode.height;
1920
width = xmode.width;
2021
windowFlags = SDL_WINDOW_SHOWN;
22+
#ifdef FC_USE_SDL_GPU
23+
windowFlags |= SDL_WINDOW_OPENGL;
24+
#endif
2125
#else
2226
height = 480;
2327
width = 640;
@@ -33,14 +37,24 @@ Renderer::Renderer() {
3337

3438
Renderer::~Renderer() {
3539
if (background != nullptr) {
40+
#ifdef FC_USE_SDL_GPU
41+
GPU_FreeImage(background);
42+
#else
3643
SDL_DestroyTexture(background);
44+
#endif
3745
}
3846
if (renderer != nullptr) {
47+
#ifdef FC_USE_SDL_GPU
48+
GPU_FreeTarget(renderer);
49+
#else
3950
SDL_DestroyRenderer(renderer);
51+
#endif
4052
}
53+
#ifndef FC_USE_SDL_GPU
4154
if (window != nullptr) {
4255
SDL_DestroyWindow(window);
4356
}
57+
#endif
4458
}
4559

4660
int Renderer::init() {
@@ -49,11 +63,24 @@ int Renderer::init() {
4963
if (window == nullptr) {
5064
return 1;
5165
}
66+
67+
#ifdef FC_USE_SDL_GPU
68+
GPU_SetInitWindow(SDL_GetWindowID(window));
69+
renderer = GPU_Init(width, height, GPU_DEFAULT_INIT_FLAGS);
70+
if (!renderer) {
71+
return 1;
72+
}
73+
#else
5274
renderer = SDL_CreateRenderer(window, -1, renderFlags);
75+
#endif
5376
if (renderer == nullptr) {
5477
return 2;
5578
}
56-
SDL_SetRenderDrawBlendMode(getRenderer(), SDL_BLENDMODE_BLEND);
79+
#ifdef FC_USE_SDL_GPU
80+
GPU_SetShapeBlendMode(GPU_BLEND_NORMAL);
81+
#else
82+
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
83+
#endif
5784
setDrawColor();
5885
clear();
5986
return 0;
@@ -72,7 +99,26 @@ int Renderer::init(const char* bgpath) {
7299
InfoLog::outputLine(InfoLog::ERROR, "Creating background surface failed.\n");
73100
return 3;
74101
}
102+
#ifdef FC_USE_SDL_GPU
103+
// OpenGL wants the surface in BGR format, but pbgl does not currently implement a
104+
// conversion. Since IMG_Load is only used in this one place, it is converted here
105+
// manually.
106+
auto srcFormat = static_cast<SDL_PixelFormatEnum>(bgsurf->format->format);
107+
switch (srcFormat) {
108+
case SDL_PIXELFORMAT_RGB24:
109+
case SDL_PIXELFORMAT_RGB888:
110+
case SDL_PIXELFORMAT_RGBA32:
111+
case SDL_PIXELFORMAT_RGBA8888:
112+
bgsurf = convertRGBToBGR(bgsurf);
113+
break;
114+
default:
115+
// Ignore surfaces that may already be BGR.
116+
break;
117+
}
118+
background = GPU_CopyImageFromSurface(bgsurf);
119+
#else
75120
background = SDL_CreateTextureFromSurface(renderer, bgsurf);
121+
#endif
76122
SDL_FreeSurface(bgsurf);
77123
if (background == nullptr) {
78124
InfoLog::outputLine(InfoLog::ERROR, "Creating background texture failed.\n");
@@ -82,44 +128,80 @@ int Renderer::init(const char* bgpath) {
82128
}
83129

84130
int Renderer::clear() {
131+
#ifdef FC_USE_SDL_GPU
132+
GPU_ClearColor(renderer, drawColor);
133+
return 0;
134+
#else
85135
int ret = SDL_RenderClear(renderer);
86136
return ret;
137+
#endif
87138
}
88139

89140
void Renderer::flip() {
141+
#ifdef FC_USE_SDL_GPU
142+
GPU_Flip(renderer);
143+
#else
90144
setDrawColor(0, 0, 0, 0xFF);
91145
SDL_RenderDrawRect(renderer, nullptr);
92146
setDrawColor();
93147
SDL_RenderPresent(renderer);
94-
#ifdef NXDK
95-
XVideoWaitForVBlank();
96148
#endif
97149
}
98150

99151
int Renderer::setDrawColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
152+
#ifdef FC_USE_SDL_GPU
153+
drawColor.r = r;
154+
drawColor.g = g;
155+
drawColor.b = b;
156+
drawColor.a = a;
157+
return 0;
158+
#else
100159
return SDL_SetRenderDrawColor(renderer, r, g, b, a);
160+
#endif
101161
}
102162

103-
void Renderer::drawTexture(SDL_Texture* tex, SDL_Rect& src, SDL_Rect& dst) {
163+
void Renderer::drawTexture(NX_Texture* tex, NX_Rect& src, NX_Rect& dst) {
164+
#ifdef FC_USE_SDL_GPU
165+
GPU_BlitRect(tex, &src, renderer, &dst);
166+
#else
104167
SDL_RenderCopy(renderer, tex, &src, &dst);
168+
#endif
105169
}
106170

107-
void Renderer::drawTexture(SDL_Texture* tex, SDL_Rect& dst) {
171+
void Renderer::drawTexture(NX_Texture* tex, NX_Rect& dst) {
172+
#ifdef FC_USE_SDL_GPU
173+
GPU_BlitRect(tex, nullptr, renderer, &dst);
174+
#else
108175
SDL_RenderCopy(renderer, tex, nullptr, &dst);
176+
#endif
109177
}
110178

111-
void Renderer::drawTexture(SDL_Texture* tex, int x, int y) {
112-
SDL_Rect dst = { x, y, 0, 0 };
179+
void Renderer::drawTexture(NX_Texture* tex, int x, int y) {
180+
#ifdef FC_USE_SDL_GPU
181+
NX_Rect dst = { static_cast<float>(x), static_cast<float>(y), static_cast<float>(tex->w),
182+
static_cast<float>(tex->h) };
183+
#else
184+
NX_Rect dst = { x, y, 0, 0 };
113185
SDL_QueryTexture(tex, nullptr, nullptr, &dst.w, &dst.h);
186+
#endif
114187
drawTexture(tex, dst);
115188
}
116189

117-
void Renderer::fillRectangle(const SDL_Rect& dst) {
190+
void Renderer::fillRectangle(const NX_Rect& dst) {
191+
#ifdef FC_USE_SDL_GPU
192+
GPU_RectangleFilled2(renderer, dst, drawColor);
193+
#else
118194
SDL_RenderFillRect(renderer, &dst);
195+
#endif
119196
}
120197

121198
void Renderer::fillRectangle(const SDL_FRect& dst) {
199+
#ifdef FC_USE_SDL_GPU
200+
GPU_Rect rect = { dst.x, dst.y, dst.w, dst.h };
201+
GPU_RectangleFilled2(renderer, rect, drawColor);
202+
#else
122203
SDL_RenderFillRectF(renderer, &dst);
204+
#endif
123205
}
124206

125207
void Renderer::blitSurface(SDL_Surface* bg, SDL_Surface* fg, int offset) {

Includes/renderer.h

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,24 @@
44
#include <SDL.h>
55
#include <vector>
66

7-
int min(int lhs, int rhs);
8-
int max(int lhs, int rhs);
7+
// clang-format off
8+
#ifdef FC_USE_SDL_GPU
9+
#pragma clang diagnostic push
10+
#pragma clang diagnostic ignored "-Wunused-function"
11+
#include "SDL_gpu.h"
12+
#pragma clang diagnostic pop
13+
#endif
14+
// clang-format on
15+
16+
#ifdef FC_USE_SDL_GPU
17+
typedef GPU_Image NX_Texture;
18+
typedef GPU_Rect NX_Rect;
19+
typedef GPU_Target NX_Target;
20+
#else
21+
typedef SDL_Texture NX_Texture;
22+
typedef SDL_Rect NX_Rect;
23+
typedef SDL_Renderer NX_Target;
24+
#endif
925

1026
class Renderer {
1127
public:
@@ -17,28 +33,35 @@ class Renderer {
1733
int clear();
1834
void flip();
1935

36+
#ifdef FC_USE_SDL_GPU
37+
GPU_Target* getRenderer() { return renderer; }
38+
#else
2039
SDL_Renderer* getRenderer() { return renderer; }
40+
#endif
2141
int getWidth() const { return width; }
2242
int getHeight() const { return height; }
2343

2444
int setDrawColor(uint8_t r = 0x40, uint8_t g = 0x40, uint8_t b = 0xE0, uint8_t a = 0x00);
2545

26-
void drawTexture(SDL_Texture* tex, SDL_Rect& src, SDL_Rect& dst);
27-
void drawTexture(SDL_Texture* tex, SDL_Rect& dst);
28-
void drawTexture(SDL_Texture* tex, int x, int y);
46+
void drawTexture(NX_Texture* tex, NX_Rect& src, NX_Rect& dst);
47+
void drawTexture(NX_Texture* tex, NX_Rect& dst);
48+
void drawTexture(NX_Texture* tex, int x, int y);
2949

30-
void fillRectangle(const SDL_Rect& dst);
50+
void fillRectangle(const NX_Rect& dst);
3151
void fillRectangle(const SDL_FRect& dst);
3252

3353
void blitSurface(SDL_Surface* bg, SDL_Surface* fg, int offset);
3454

3555
void drawBackground();
3656

3757
private:
38-
SDL_Renderer* renderer = nullptr;
58+
NX_Target* renderer = nullptr;
59+
NX_Texture* background = nullptr;
60+
3961
SDL_Window* window = nullptr;
40-
SDL_Texture* background = nullptr;
62+
#ifndef FC_USE_SDL_GPU
4163
Uint32 renderFlags = 0;
64+
#endif
4265
Uint32 windowFlags = 0;
4366

4467
int height = 0;
@@ -48,6 +71,10 @@ class Renderer {
4871
size_t menuItemCount = 0;
4972
size_t lowerHalf = 0;
5073
size_t upperHalf = 0;
74+
75+
#ifdef FC_USE_SDL_GPU
76+
SDL_Color drawColor;
77+
#endif
5178
};
5279

5380
#endif

Includes/sntpClient.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ void sntpClient::updateTime() const {
7474
message.originTimestamp.fractionalSeconds =
7575
htonl(message.originTimestamp.fractionalSeconds);
7676
if (message.originTimestamp.seconds || message.originTimestamp.fractionalSeconds) {
77-
InfoLog::outputLine(InfoLog::INFO, "SNTP: Origin epoch is not 0: %lld\n", message.originTimestamp);
77+
InfoLog::outputLine(InfoLog::INFO, "SNTP: Origin epoch is not 0: %lld\n",
78+
message.originTimestamp);
7879
}
7980

8081
message.transmitTimestamp.seconds = htonl(message.transmitTimestamp.seconds);
@@ -95,7 +96,8 @@ void sntpClient::updateTime() const {
9596
}
9697

9798
if (delta > allowedDriftSeconds) {
98-
InfoLog::outputLine(InfoLog::DEBUG, "SNTP: Updating system clock (%llu seconds of drift)\n", delta);
99+
InfoLog::outputLine(InfoLog::DEBUG,
100+
"SNTP: Updating system clock (%llu seconds of drift)\n", delta);
99101
NTSTATUS status = NtSetSystemTime(&serverTime, nullptr);
100102
if (!NT_SUCCESS(status)) {
101103
InfoLog::outputLine(InfoLog::INFO, "SNTP: NtSetSystemTime failed: %X\n", status);

0 commit comments

Comments
 (0)