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
3438Renderer::~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
4660int 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
84130int 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
89140void 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
99151int 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
121198void 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
125207void Renderer::blitSurface (SDL_Surface* bg, SDL_Surface* fg, int offset) {
0 commit comments