Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rendering: Adds SDL_gpu based rendering. #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
[submodule "3rdparty/NaturalSort"]
path = 3rdparty/NaturalSort
url = https://github.com/scopeInfinity/NaturalSort.git
[submodule "3rdparty/pbgl"]
path = 3rdparty/pbgl
url = https://github.com/abaire/pbgl.git
[submodule "3rdparty/sdl-gpu"]
path = 3rdparty/sdl-gpu
url = https://github.com/abaire/sdl-gpu.git
1 change: 1 addition & 0 deletions 3rdparty/pbgl
Submodule pbgl added at f47dd3
1 change: 1 addition & 0 deletions 3rdparty/sdl-gpu
Submodule sdl-gpu added at fceafc
8 changes: 7 additions & 1 deletion Includes/font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
#include <string>
#include <utility>
#include <vector>
#include "3rdparty/SDL_FontCache/SDL_FontCache.h"
#include "renderer.hpp"

// clang-format off
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
#include "3rdparty/SDL_FontCache/SDL_FontCache.h"
#pragma clang diagnostic pop
// clang-format on

class Font {
private:
FC_Font* fcFont;
Expand Down
43 changes: 35 additions & 8 deletions Includes/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@
#include <SDL.h>
#include <vector>

int min(int lhs, int rhs);
int max(int lhs, int rhs);
// clang-format off
#ifdef FC_USE_SDL_GPU
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
#include "SDL_gpu.h"
#pragma clang diagnostic pop
#endif
// clang-format on

#ifdef FC_USE_SDL_GPU
typedef GPU_Image NX_Texture;
typedef GPU_Rect NX_Rect;
typedef GPU_Target NX_Target;
#else
typedef SDL_Texture NX_Texture;
typedef SDL_Rect NX_Rect;
typedef SDL_Renderer NX_Target;
#endif

class Renderer {
public:
Expand All @@ -17,28 +33,35 @@ class Renderer {
int clear();
void flip();

#ifdef FC_USE_SDL_GPU
GPU_Target* getRenderer() { return renderer; }
#else
SDL_Renderer* getRenderer() { return renderer; }
#endif
int getWidth() const { return width; }
int getHeight() const { return height; }

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

void drawTexture(SDL_Texture* tex, SDL_Rect& src, SDL_Rect& dst);
void drawTexture(SDL_Texture* tex, SDL_Rect& dst);
void drawTexture(SDL_Texture* tex, int x, int y);
void drawTexture(NX_Texture* tex, NX_Rect& src, NX_Rect& dst);
void drawTexture(NX_Texture* tex, NX_Rect& dst);
void drawTexture(NX_Texture* tex, int x, int y);

void fillRectangle(const SDL_Rect& dst);
void fillRectangle(const NX_Rect& dst);
void fillRectangle(const SDL_FRect& dst);

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

void drawBackground();

private:
SDL_Renderer* renderer = nullptr;
NX_Target* renderer = nullptr;
NX_Texture* background = nullptr;

SDL_Window* window = nullptr;
SDL_Texture* background = nullptr;
#ifndef FC_USE_SDL_GPU
Uint32 renderFlags = 0;
#endif
Uint32 windowFlags = 0;

int height = 0;
Expand All @@ -48,6 +71,10 @@ class Renderer {
size_t menuItemCount = 0;
size_t lowerHalf = 0;
size_t upperHalf = 0;

#ifdef FC_USE_SDL_GPU
SDL_Color drawColor;
#endif
};

#endif
31 changes: 26 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SRCS += \
$(SRCDIR)/wipeCache.cpp \
$(SRCDIR)/xbeLauncher.cpp \
$(SRCDIR)/xbeScanner.cpp \
$(CURDIR)/nxdk-sdl-gpu/nxdkSDLGPU.cpp \
$(CURDIR)/3rdparty/SDL_FontCache/SDL_FontCache.c

NXDK_DIR ?= $(CURDIR)/../nxdk
Expand All @@ -38,21 +39,41 @@ NXDK_DISABLE_AUTOMOUNT_D = y

GEN_XISO = ${XBE_TITLE}.iso

CXXFLAGS += -I$(CURDIR) -I$(INCDIR) -Wall -Wextra -std=gnu++11
CFLAGS += -std=gnu11
CXXFLAGS += -I$(CURDIR) -I$(INCDIR) -I$(SDL_GPU_DIR)/include -I$(PBGL_DIR)/include -Wall -Wextra -std=gnu++11 -DFC_USE_SDL_GPU
CFLAGS += -I$(SDL_GPU_DIR)/include -std=gnu11 -DFC_USE_SDL_GPU

ifneq ($(DEBUG),y)
CFLAGS += -O2
CXXFLAGS += -O2
endif

new_all: copy_resources all
CLEANRULES = clean-resources clean-gl

include $(NXDK_DIR)/Makefile

copy_resources: $(OUTPUT_DIR)/config.json
@cp $(RESOURCEDIR)/480.png $(RESOURCEDIR)/720.png $(RESOURCEDIR)/vegur.ttf $(OUTPUT_DIR)
override PBGL_DIR := 3rdparty/pbgl
include 3rdparty/pbgl/Makefile

override SDL_GPU_DIR := 3rdparty/sdl-gpu
include nxdk-sdl-gpu/Makefile.inc

RESOURCES = \
$(OUTPUT_DIR)/config.json \
$(OUTPUT_DIR)/480.png \
$(OUTPUT_DIR)/720.png

TARGET += $(RESOURCES)
$(GEN_XISO): $(RESOURCES)

$(OUTPUT_DIR)/config.json: $(CURDIR)/sampleconfig.json
@mkdir -p $(OUTPUT_DIR)
cp $(CURDIR)/sampleconfig.json $(OUTPUT_DIR)/config.json
$(OUTPUT_DIR)/%: $(RESOURCEDIR)/%
$(VE)cp -r '$<' '$@'

.PHONY: clean-resources
clean-resources:
$(VE)rm -rf $(OUTPUT_DIR)/NeXThemes

.PHONY: clean-gl
clean-gl: clean-sdl-gpu clean-pbgl
8 changes: 6 additions & 2 deletions Sources/font.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include "font.hpp"
#include <cassert>
#include "3rdparty/SDL_FontCache/SDL_FontCache.h"
#include "infoLog.hpp"


Font::Font(Renderer& renderer, const char* path) : renderer(renderer) {
fcFont = FC_CreateFont();
assert(fcFont);
#ifdef FC_USE_SDL_GPU
bool load_success = FC_LoadFont(fcFont, path, 20, FC_MakeColor(250, 250, 250, 255),
TTF_STYLE_NORMAL);
#else
bool load_success = FC_LoadFont(fcFont, renderer.getRenderer(), path, 20,
FC_MakeColor(250, 250, 250, 255), TTF_STYLE_NORMAL);
#endif
assert(load_success);
}

Expand Down
17 changes: 17 additions & 0 deletions Sources/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
#include "settingsMenu.hpp"
#include "xbeLauncher.hpp"
#include "xbeScanner.hpp"
#ifdef NXDK
// clang-format off
#ifdef FC_USE_SDL_GPU
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
#include "SDL_gpu.h"
#pragma clang diagnostic pop
#endif
// clang-format on
#endif

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

if (i == this->currentMenu->getSelected()) {
#ifdef FC_USE_SDL_GPU
GPU_Rect rect = { std::get<0>(coordinates) - 10, std::get<1>(coordinates),
std::get<0>(dimensions) + 20, std::get<1>(dimensions) };
SDL_Color color = { 0xFF, 0xFF, 0xFF, 0xFF };
GPU_Rectangle2(renderer.getRenderer(), rect, color);
#else
SDL_Rect rect;
rect.w = std::get<0>(dimensions) + 20;
rect.h = std::get<1>(dimensions);
rect.x = std::get<0>(coordinates) - 10;
rect.y = std::get<1>(coordinates);
renderer.setDrawColor(0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawRect(renderer.getRenderer(), &rect);
#endif
}

coordinates = std::pair<float, float>(
Expand Down
Loading