crashing on wii u =(

This commit is contained in:
Maschell 2020-09-01 16:03:56 +02:00
parent 16862f2a88
commit 4fbb83e4ad
29 changed files with 3753 additions and 3597 deletions

View File

@ -11,8 +11,8 @@ add_executable(${PROJECT_NAME}
src/gui/GuiImage.cpp
src/gui/GuiImage.h
src/gui/sigslot.h
src/CVideo.cpp
src/CVideo.h
src/system/SDLSystem.cpp
src/system/SDLSystem.h
src/gui/GuiElement.cpp
src/gui/GuiText.cpp
src/gui/GuiText.h
@ -25,13 +25,14 @@ add_executable(${PROJECT_NAME}
src/gui/GuiButton.h
src/gui/SDLController.h src/MainWindow.cpp src/MainWindow.h src/gui/SDLControllerJoystick.h src/gui/SDLControllerMouse.h
src/gui/SDLControllerWiiUGamepad.h
src/gui/SDLControllerWiiUProContoller.h
src/input/SDLController.h src/menu/MainWindow.cpp src/menu/MainWindow.h src/input/SDLControllerJoystick.h src/input/SDLControllerMouse.h
src/input/SDLControllerWiiUGamepad.h
src/input/SDLControllerWiiUProContoller.h
src/gui/GuiTexture.cpp src/gui/GuiTexture.h
src/gui/SDL_FontCache.h src/gui/SDL_FontCache.c
src/system/video/SDL_FontCache.h
src/system/video/SDL_FontCache.cpp
)
src/system/video/Renderer.h)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)

View File

@ -28,7 +28,12 @@ endif
TARGET := SDL2_Playground
BUILD := build-pc-win
SOURCES := src \
src/gui
src/gui \
src/input \
src/menu \
src/system \
src/system/video \
src/utils
DATA := data
INCLUDES := source

View File

@ -21,7 +21,12 @@ WUMS_ROOT := $(DEVKITPRO)/wums
TARGET := SDL2_Playground
BUILD := build
SOURCES := src \
src/gui
src/gui \
src/input \
src/menu \
src/system \
src/system/video \
src/utils
DATA := data
INCLUDES := source

View File

@ -133,7 +133,7 @@ void GuiButton::setTrigger(GuiTrigger *t, int32_t idx) {
}
}
void GuiButton::resetState(void) {
void GuiButton::resetState() {
clickedTrigger = NULL;
heldTrigger = NULL;
GuiElement::resetState();
@ -142,7 +142,7 @@ void GuiButton::resetState(void) {
/**
* Draw the button on screen
*/
void GuiButton::draw(CVideo *v) {
void GuiButton::draw(Renderer *v) {
if (!this->isVisible()) {
return;
}

View File

@ -20,7 +20,7 @@
#include "GuiText.h"
#include "GuiSound.h"
#include "GuiTrigger.h"
#include "../CVideo.h"
#include "../system/SDLSystem.h"
//!Display, manage, and manipulate buttons in the GUI. Buttons can have images, icons, text, and sound set (all of which are optional)
class GuiButton : public GuiElement {
@ -94,7 +94,7 @@ public:
void resetState(void) override;
//!Constantly called to draw the GuiButton
void draw(CVideo *video) override;
void draw(Renderer *video) override;
//!Constantly called to allow the GuiButton to respond to updated input data
//!\param t Pointer to a GuiTrigger, containing the current input data from PAD/WPAD

View File

@ -20,14 +20,15 @@
#include <vector>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <wchar.h>
#include <math.h>
#include <cwchar>
#include <cmath>
#include <iostream>
#include "sigslot.h"
#include "../system/video/Renderer.h"
enum {
EFFECT_NONE = 0x00,
@ -59,7 +60,7 @@ enum {
//!Forward declaration
class GuiController;
class CVideo;
class SDLSystem;
//!Primary GUI class. Most other classes inherit from this class.
class GuiElement {
@ -524,7 +525,7 @@ public:
virtual void update(GuiController *t) {}
//!Called constantly to redraw the element
virtual void draw(CVideo *v) {}
virtual void draw(Renderer * v) {}
//!Called constantly to process stuff in the element
virtual void process() {}

View File

@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "GuiFrame.h"
#include "../system/video/Renderer.h"
GuiFrame::GuiFrame(GuiFrame *p) {
parent = p;
@ -156,12 +157,12 @@ int32_t GuiFrame::getSelected() {
return found;
}
void GuiFrame::draw(CVideo *v) {
void GuiFrame::draw(Renderer *v) {
if (!this->isVisible() && parentElement) {
return;
}
if (parentElement && dim == true) {
if (parentElement && dim) {
//GXColor dimColor = (GXColor){0, 0, 0, 0x70};
//Menu_DrawRectangle(0, 0, GetZPosition(), screenwidth,screenheight, &dimColor, false, true);
}

View File

@ -20,6 +20,7 @@
#include <mutex>
#include "GuiElement.h"
#include "sigslot.h"
#include "../system/video/Renderer.h"
//!Allows GuiElements to be grouped together into a "window"
class GuiFrame : public GuiElement {
@ -33,7 +34,7 @@ public:
GuiFrame(float w, float h, GuiFrame *parent = 0);
//!Destructor
virtual ~GuiFrame();
~GuiFrame() override;
//!Appends a GuiElement to the GuiFrame
//!\param e The GuiElement to append. If it is already in the GuiFrame, it is removed first
@ -84,7 +85,7 @@ public:
int32_t getSelected() override;
//!Draws all the elements in this GuiFrame
void draw(CVideo *v) override;
void draw(Renderer *v) override;
//!Updates the window and all elements contains within
//!Allows the GuiFrame and all elements to respond to the input data specified

View File

@ -17,7 +17,7 @@
#include <SDL2/SDL_image.h>
#include <iostream>
#include "GuiImage.h"
#include "../CVideo.h"
#include "../system/SDLSystem.h"
GuiImage::GuiImage(const std::string& path) : GuiTexture(path){
}

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "GuiSound.h"
#include "../logger.h"
#include "../utils/logger.h"
GuiSound::GuiSound(const char *filepath) {
Load(filepath);

View File

@ -17,23 +17,19 @@
#include <cstdarg>
#include <SDL2/SDL_surface.h>
#include "GuiText.h"
#include "../CVideo.h"
#include "../logger.h"
#include "SDL_FontCache.h"
/**
* Constructor for the GuiText class.
*/
GuiText::GuiText(const std::string& text, int32_t s, SDL_Color c, TTF_Font* gFont) {
GuiText::GuiText(const std::string& text, SDL_Color c, FC_Font* gFont) {
this->text = text;
this->size = s;
this->color = c;
this->ttf_font = gFont;
this->invalid = true;
this->updateText = true;
this->maxWidth = 200;
this->fc_font = gFont;
this->updateText = false;
updateSize();
updateTexture();
}
GuiText::~GuiText(){
@ -44,45 +40,13 @@ GuiText::~GuiText(){
delete texture;
}
void GuiText::draw(CVideo *pVideo) {
void GuiText::draw(Renderer *renderer) {
if (!this->isVisible()) {
return;
}
if(invalid){
if(fc_font){
FC_FreeFont(fc_font);
fc_font = nullptr;
}
fc_font = FC_CreateFont();
invalid = !FC_LoadFontFromTTF(fc_font, pVideo->getRenderer(), this->ttf_font, color);
DEBUG_FUNCTION_LINE("FC_CACHE init done");
}
if(updateText || !texture){
delete texture;
// Create the intermediate texture target
SDL_Texture* temp = SDL_CreateTexture(pVideo->getRenderer(), pVideo->getPixelFormat(), SDL_TEXTUREACCESS_TARGET, width, height);
if(temp){
texture = new GuiTexture(temp);
texture->setParent(this);
texture->setBlendMode(SDL_BLENDMODE_BLEND);
// Draw the text onto it
SDL_SetRenderTarget(pVideo->getRenderer(), temp);
// make sure the texture is clean.
SDL_RenderClear(pVideo->getRenderer());
FC_DrawColumn(fc_font, pVideo->getRenderer(), 0, 0, maxWidth, text.c_str());
SDL_SetRenderTarget(pVideo->getRenderer(), NULL);
updateText = false;
}else{
DEBUG_FUNCTION_LINE("Failed to create texture");
}
}
if(texture){
texture->draw(pVideo);
texture->draw(renderer);
}
}
@ -90,15 +54,37 @@ void GuiText::process() {
GuiElement::process();
if(updateText && fc_font){
auto height = FC_GetColumnHeight(fc_font, maxWidth, text.c_str());
auto width = FC_GetWidth(fc_font, text.c_str());
width = width > maxWidth ? maxWidth : width;
this->setSize(width, height);
updateTexture();
updateText = false;
}
}
void GuiText::setMaxWidth(float width) {
this->maxWidth = width;
// Rebuild the texture cache.
updateText = true;
}
void GuiText::updateSize() {
auto height = FC_GetColumnHeight(fc_font, maxWidth, text.c_str());
auto width = FC_GetWidth(fc_font, text.c_str());
width = width > maxWidth ? maxWidth : width;
this->setSize(width, height);
}
void GuiText::updateTexture() {
updateSize();
SDL_Texture* temp = FC_CreateTexture(fc_font, SDL_PIXELFORMAT_RGBA8888, width, height);
if(temp){
delete texture;
texture = new GuiTexture(temp);
texture->setParent(this);
texture->setBlendMode(SDL_BLENDMODE_BLEND);
FC_DrawColumnToTexture(fc_font, temp, 0,0, maxWidth, text.c_str());
}else{
DEBUG_FUNCTION_LINE("Failed to create texture");
}
}

View File

@ -17,12 +17,11 @@
#pragma once
#include "GuiElement.h"
#include "SDL_FontCache.h"
#include "GuiTexture.h"
#include "../system/video/SDL_FontCache.h"
#include <mutex>
#include <SDL2/SDL_ttf.h>
//!Display, manage, and manipulate text in the GUI
class GuiText : public GuiElement {
public:
@ -30,10 +29,10 @@ public:
//!\param t Text
//!\param s Font size
//!\param c Font color
GuiText(const std::string &t, int32_t s, SDL_Color c, TTF_Font *gFont);
GuiText(const std::string &t, SDL_Color c, FC_Font *font);
~GuiText() override;
void draw(CVideo *pVideo) override;
void draw(Renderer *pVideo) override;
void process() override;
@ -42,12 +41,13 @@ public:
protected:
GuiTexture* texture = nullptr;
std::string text;
int32_t size;
SDL_Color color;
TTF_Font *ttf_font = nullptr;
FC_Font *fc_font = nullptr;
bool invalid = true;
bool updateText = true;
uint16_t maxWidth = 0xFFFF;
void updateSize();
void updateTexture();
};

View File

@ -1,7 +1,7 @@
#include <SDL2/SDL_image.h>
#include "GuiTexture.h"
#include "../CVideo.h"
#include "../logger.h"
#include "../system/SDLSystem.h"
#include "../utils/logger.h"
GuiTexture::GuiTexture(const std::string& path) {
imgSurface = IMG_Load( path.c_str() );
@ -49,20 +49,20 @@ GuiTexture::~GuiTexture() {
}
}
void GuiTexture::draw(CVideo *pVideo) {
void GuiTexture::draw(Renderer *renderer) {
if (!this->isVisible()) {
DEBUG_FUNCTION_LINE("not visible!");
return;
}
if (texture == NULL && imgSurface) {
SDL_Surface *optimizedSurface = SDL_ConvertSurfaceFormat(imgSurface, pVideo->getPixelFormat(), 0);
SDL_Surface *optimizedSurface = SDL_ConvertSurfaceFormat(imgSurface, renderer->getPixelFormat(), 0);
if (optimizedSurface != NULL) {
SDL_FreeSurface(imgSurface);
imgSurface = optimizedSurface;
DEBUG_FUNCTION_LINE("Optimized surface");
}
texture = SDL_CreateTextureFromSurface(pVideo->getRenderer(), imgSurface);
texture = SDL_CreateTextureFromSurface(renderer->getRenderer(), imgSurface);
}
if (!texture) {
DEBUG_FUNCTION_LINE("no texture!");
@ -79,9 +79,9 @@ void GuiTexture::draw(CVideo *pVideo) {
rect.h = currScaleY * getHeight();
if (getAngle() == 0) {
SDL_RenderCopy(pVideo->getRenderer(), texture, nullptr, &rect);
SDL_RenderCopy(renderer->getRenderer(), texture, nullptr, &rect);
} else {
SDL_RenderCopyEx(pVideo->getRenderer(), texture, nullptr, &rect, getAngle(), nullptr, SDL_FLIP_NONE);
SDL_RenderCopyEx(renderer->getRenderer(), texture, nullptr, &rect, getAngle(), nullptr, SDL_FLIP_NONE);
}
}

View File

@ -12,7 +12,7 @@ public:
~GuiTexture() override;
//!Constantly called to draw the image
void draw(CVideo *pVideo) override;
void draw(Renderer *pVideo) override;
int setBlendMode(SDL_BlendMode blendMode);

View File

@ -1,60 +1,60 @@
#pragma once
#include <SDL2/SDL_mouse.h>
#include <iostream>
#include <SDL2/SDL_events.h>
#include "GuiController.h"
#include "../logger.h"
#define printButton(chan, x) if(data.buttons_d & x) DEBUG_FUNCTION_LINE("Controller #%d %s", chan, #x)
class SDLController : public GuiController {
public:
explicit SDLController(int32_t channel) : GuiController(channel) {
}
virtual bool update(SDL_Event *e, int32_t screenWidth, int32_t screenHeight) = 0;
virtual void before() {
lastData = data;
data.buttons_d = 0;
data.buttons_r = 0;
}
virtual void after() {
data.buttons_d |= (data.buttons_h & (~(lastData.buttons_h)));
data.buttons_r |= ((lastData.buttons_h) & (~data.buttons_h));
if (data.buttons_h != 0 || data.buttons_d != 0 || data.buttons_r != 0) {
// DEBUG_FUNCTION_LINE("Controller #%d: h %08X d %08X r %08X", chanIdx, data.buttons_h, data.buttons_d, data.buttons_r);
printButton(chanIdx, GuiTrigger::BUTTON_A);
printButton(chanIdx, GuiTrigger::BUTTON_B);
printButton(chanIdx, GuiTrigger::BUTTON_X);
printButton(chanIdx, GuiTrigger::BUTTON_Y);
printButton(chanIdx, GuiTrigger::BUTTON_STICK_L);
printButton(chanIdx, GuiTrigger::BUTTON_STICK_R);
printButton(chanIdx, GuiTrigger::BUTTON_L);
printButton(chanIdx, GuiTrigger::BUTTON_R);
printButton(chanIdx, GuiTrigger::BUTTON_ZL);
printButton(chanIdx, GuiTrigger::BUTTON_ZR);
printButton(chanIdx, GuiTrigger::BUTTON_PLUS);
printButton(chanIdx, GuiTrigger::BUTTON_MINUS);
printButton(chanIdx, GuiTrigger::BUTTON_LEFT);
printButton(chanIdx, GuiTrigger::BUTTON_UP);
printButton(chanIdx, GuiTrigger::BUTTON_RIGHT);
printButton(chanIdx, GuiTrigger::BUTTON_DOWN);
printButton(chanIdx, GuiTrigger::STICK_L_LEFT);
printButton(chanIdx, GuiTrigger::STICK_L_UP);
printButton(chanIdx, GuiTrigger::STICK_L_RIGHT);
printButton(chanIdx, GuiTrigger::STICK_L_DOWN);
printButton(chanIdx, GuiTrigger::STICK_R_LEFT);
printButton(chanIdx, GuiTrigger::STICK_R_UP);
printButton(chanIdx, GuiTrigger::STICK_R_RIGHT);
printButton(chanIdx, GuiTrigger::STICK_R_DOWN);
printButton(chanIdx, GuiTrigger::TOUCHED);
}
}
};
#pragma once
#include <SDL2/SDL_mouse.h>
#include <iostream>
#include <SDL2/SDL_events.h>
#include "../gui/GuiController.h"
#include "../utils/logger.h"
#define printButton(chan, x) if(data.buttons_d & x) DEBUG_FUNCTION_LINE("Controller #%d %s", chan, #x)
class SDLController : public GuiController {
public:
explicit SDLController(int32_t channel) : GuiController(channel) {
}
virtual bool update(SDL_Event *e, int32_t screenWidth, int32_t screenHeight) = 0;
virtual void before() {
lastData = data;
data.buttons_d = 0;
data.buttons_r = 0;
}
virtual void after() {
data.buttons_d |= (data.buttons_h & (~(lastData.buttons_h)));
data.buttons_r |= ((lastData.buttons_h) & (~data.buttons_h));
if (data.buttons_h != 0 || data.buttons_d != 0 || data.buttons_r != 0) {
// DEBUG_FUNCTION_LINE("Controller #%d: h %08X d %08X r %08X", chanIdx, data.buttons_h, data.buttons_d, data.buttons_r);
printButton(chanIdx, GuiTrigger::BUTTON_A);
printButton(chanIdx, GuiTrigger::BUTTON_B);
printButton(chanIdx, GuiTrigger::BUTTON_X);
printButton(chanIdx, GuiTrigger::BUTTON_Y);
printButton(chanIdx, GuiTrigger::BUTTON_STICK_L);
printButton(chanIdx, GuiTrigger::BUTTON_STICK_R);
printButton(chanIdx, GuiTrigger::BUTTON_L);
printButton(chanIdx, GuiTrigger::BUTTON_R);
printButton(chanIdx, GuiTrigger::BUTTON_ZL);
printButton(chanIdx, GuiTrigger::BUTTON_ZR);
printButton(chanIdx, GuiTrigger::BUTTON_PLUS);
printButton(chanIdx, GuiTrigger::BUTTON_MINUS);
printButton(chanIdx, GuiTrigger::BUTTON_LEFT);
printButton(chanIdx, GuiTrigger::BUTTON_UP);
printButton(chanIdx, GuiTrigger::BUTTON_RIGHT);
printButton(chanIdx, GuiTrigger::BUTTON_DOWN);
printButton(chanIdx, GuiTrigger::STICK_L_LEFT);
printButton(chanIdx, GuiTrigger::STICK_L_UP);
printButton(chanIdx, GuiTrigger::STICK_L_RIGHT);
printButton(chanIdx, GuiTrigger::STICK_L_DOWN);
printButton(chanIdx, GuiTrigger::STICK_R_LEFT);
printButton(chanIdx, GuiTrigger::STICK_R_UP);
printButton(chanIdx, GuiTrigger::STICK_R_RIGHT);
printButton(chanIdx, GuiTrigger::STICK_R_DOWN);
printButton(chanIdx, GuiTrigger::TOUCHED);
}
}
};

View File

@ -1,58 +1,58 @@
#pragma once
class SDLControllerJoystick : public SDLController {
public:
SDLControllerJoystick(int32_t channel, SDL_JoystickID joystickId) : SDLController(channel) {
}
bool update(SDL_Event *e, int32_t screenWidth, int32_t screenHeight) override {
if (e->type == SDL_JOYBUTTONDOWN) {
data.buttons_h |= (1 << e->jbutton.button);
} else if (e->type == SDL_JOYBUTTONUP) {
data.buttons_h &= ~(1 << e->jbutton.button);
} else if (e->type == SDL_JOYHATMOTION) {
auto val = e->jhat.value;
auto hatMask = (GuiTrigger::BUTTON_LEFT | GuiTrigger::BUTTON_UP | GuiTrigger::BUTTON_DOWN | GuiTrigger::BUTTON_RIGHT);
// Remove hat values so we can add the new value.
data.buttons_h &= ~hatMask;
switch (val) {
case SDL_HAT_LEFTUP:
data.buttons_h |= GuiTrigger::BUTTON_LEFT;
data.buttons_h |= GuiTrigger::BUTTON_UP;
break;
case SDL_HAT_LEFT:
data.buttons_h |= GuiTrigger::BUTTON_LEFT;
break;
case SDL_HAT_LEFTDOWN:
data.buttons_h |= GuiTrigger::BUTTON_LEFT;
data.buttons_h |= GuiTrigger::BUTTON_DOWN;
break;
case SDL_HAT_UP:
data.buttons_h |= GuiTrigger::BUTTON_UP;
break;
case SDL_HAT_DOWN:
data.buttons_h |= GuiTrigger::BUTTON_DOWN;
break;
case SDL_HAT_RIGHTUP:
data.buttons_h |= GuiTrigger::BUTTON_RIGHT;
data.buttons_h |= GuiTrigger::BUTTON_UP;
break;
case SDL_HAT_RIGHT:
data.buttons_h |= GuiTrigger::BUTTON_RIGHT;
break;
case SDL_HAT_RIGHTDOWN:
data.buttons_h |= GuiTrigger::BUTTON_RIGHT;
data.buttons_h |= GuiTrigger::BUTTON_DOWN;
break;
}
} else if (e->type == SDL_JOYAXISMOTION) {
//
}
return true;
}
};
#pragma once
class SDLControllerJoystick : public SDLController {
public:
SDLControllerJoystick(int32_t channel, SDL_JoystickID joystickId) : SDLController(channel) {
}
bool update(SDL_Event *e, int32_t screenWidth, int32_t screenHeight) override {
if (e->type == SDL_JOYBUTTONDOWN) {
data.buttons_h |= (1 << e->jbutton.button);
} else if (e->type == SDL_JOYBUTTONUP) {
data.buttons_h &= ~(1 << e->jbutton.button);
} else if (e->type == SDL_JOYHATMOTION) {
auto val = e->jhat.value;
auto hatMask = (GuiTrigger::BUTTON_LEFT | GuiTrigger::BUTTON_UP | GuiTrigger::BUTTON_DOWN | GuiTrigger::BUTTON_RIGHT);
// Remove hat values so we can add the new value.
data.buttons_h &= ~hatMask;
switch (val) {
case SDL_HAT_LEFTUP:
data.buttons_h |= GuiTrigger::BUTTON_LEFT;
data.buttons_h |= GuiTrigger::BUTTON_UP;
break;
case SDL_HAT_LEFT:
data.buttons_h |= GuiTrigger::BUTTON_LEFT;
break;
case SDL_HAT_LEFTDOWN:
data.buttons_h |= GuiTrigger::BUTTON_LEFT;
data.buttons_h |= GuiTrigger::BUTTON_DOWN;
break;
case SDL_HAT_UP:
data.buttons_h |= GuiTrigger::BUTTON_UP;
break;
case SDL_HAT_DOWN:
data.buttons_h |= GuiTrigger::BUTTON_DOWN;
break;
case SDL_HAT_RIGHTUP:
data.buttons_h |= GuiTrigger::BUTTON_RIGHT;
data.buttons_h |= GuiTrigger::BUTTON_UP;
break;
case SDL_HAT_RIGHT:
data.buttons_h |= GuiTrigger::BUTTON_RIGHT;
break;
case SDL_HAT_RIGHTDOWN:
data.buttons_h |= GuiTrigger::BUTTON_RIGHT;
data.buttons_h |= GuiTrigger::BUTTON_DOWN;
break;
}
} else if (e->type == SDL_JOYAXISMOTION) {
//
}
return true;
}
};

View File

@ -1,17 +1,17 @@
#include <SDL2/SDL.h>
#include "CVideo.h"
#include "system/SDLSystem.h"
#include "gui/GuiFrame.h"
#include "gui/GuiImage.h"
#include "gui/GuiButton.h"
#include "gui/GuiController.h"
#include "gui/SDLController.h"
#include "MainWindow.h"
#include "logger.h"
#include "gui/SDLControllerJoystick.h"
#include "gui/SDLControllerMouse.h"
#include "gui/SDLControllerWiiUGamepad.h"
#include "gui/SDLControllerXboxOne.h"
#include "gui/SDLControllerWiiUProContoller.h"
#include "menu/MainWindow.h"
#include "utils/logger.h"
#include "input/SDLController.h"
#include "input/SDLControllerMouse.h"
#include "input/SDLControllerWiiUGamepad.h"
#include "input/SDLControllerXboxOne.h"
#include "input/SDLControllerWiiUProContoller.h"
#include "input/SDLControllerJoystick.h"
#include <cstdio>
#include <fcntl.h>
@ -58,7 +58,7 @@ GuiTrigger::eChannels increaseChannel(GuiTrigger::eChannels channel);
void removeJoystick(int32_t instanceId, std::map<GuiTrigger::eChannels, SDLController *> &controllerList, std::map<int32_t, GuiTrigger::eChannels>& joystickToChannel);
int main(int argc, char *args[]) {
auto *video = new CVideo();
auto *video = new SDLSystem();
#if defined _WIN32
// Create the Console
@ -82,7 +82,7 @@ int main(int argc, char *args[]) {
WHBLogUdpInit();
#endif
GuiFrame *frame = new MainWindow(video->getWidth(), video->getHeight());
GuiFrame *frame = new MainWindow(video->getWidth(), video->getHeight(), video->getRenderer());
std::map<GuiTrigger::eChannels, SDLController*> controllerList;
std::map<int32_t , GuiTrigger::eChannels> joystickToChannel;
@ -99,7 +99,6 @@ int main(int argc, char *args[]) {
break;
}
#endif
//! Read out inputs
for( auto const& [channel, controller] : controllerList ){
controller->before();
@ -156,15 +155,25 @@ int main(int argc, char *args[]) {
frame->process();
// clear the screen
SDL_RenderClear(video->getRenderer());
SDL_RenderClear(video->getRenderer()->getRenderer());
frame->draw(video);
frame->draw(video->getRenderer());
frame->updateEffects();
// flip the backbuffer
// this means that everything that we prepared behind the screens is actually shown
SDL_RenderPresent(video->getRenderer());
DEBUG_FUNCTION_LINE("%08X", video);
if(video){
DEBUG_FUNCTION_LINE("%08X", video->getRenderer());
if(video->getRenderer()){
DEBUG_FUNCTION_LINE("%08X", video->getRenderer()->getRenderer());
if(video->getRenderer()->getRenderer()) {
SDL_RenderPresent(video->getRenderer()->getRenderer());
}
}
}
}

View File

@ -1,5 +1,4 @@
#include "MainWindow.h"
#include "gui/SDL_FontCache.h"
MainWindow::~MainWindow() {
delete label;;
@ -16,7 +15,7 @@ MainWindow::~MainWindow() {
delete bgMusic;;
}
MainWindow::MainWindow(int32_t w, int32_t h) : GuiFrame(w, h) {
MainWindow::MainWindow(int32_t w, int32_t h, Renderer* renderer) : GuiFrame(w, h) {
#if defined _WIN32
auto picture_path = "test.png";
auto font_path = "FreeSans.ttf";
@ -34,7 +33,14 @@ MainWindow::MainWindow(int32_t w, int32_t h) : GuiFrame(w, h) {
font = TTF_OpenFont(font_path, 28);
label = new GuiText("This is a test.This is a test. This is a test.This is a test.This is a test.This is a test.", 25, {255, 255, 0, 255}, font);
FC_Font* fc_font = FC_CreateFont();
if(!fc_font){
DEBUG_FUNCTION_LINE("Failed to create font");
}
FC_LoadFontFromTTF(fc_font, renderer->getRenderer(), font, {255, 255, 255, 255});
label = new GuiText("This is a test.This is a test. This is a test.This is a test.This is a test.This is a test.", {255, 255, 0, 255}, fc_font);
bgMusic = new GuiSound(bgMusic_path);
bgMusic->SetLoop(true);
@ -87,3 +93,7 @@ void MainWindow::process() {
}
button->setAngle(res);
}
void MainWindow::test(GuiButton *, const GuiController *, GuiTrigger *) {
DEBUG_FUNCTION_LINE("Hello, you have clicked the button");
}

View File

@ -1,19 +1,17 @@
#pragma once
#include <iostream>
#include "gui/GuiFrame.h"
#include "gui/GuiButton.h"
#include "logger.h"
#include "../gui/GuiFrame.h"
#include "../gui/GuiButton.h"
#include "../utils/logger.h"
class MainWindow : public GuiFrame, public sigslot::has_slots<> {
public:
void test(GuiButton *, const GuiController *, GuiTrigger *) {
DEBUG_FUNCTION_LINE("Hello, you have clicked the button");
}
void test(GuiButton *, const GuiController *, GuiTrigger *);
~MainWindow();
~MainWindow() override;
MainWindow(int32_t w, int32_t h);
MainWindow(int32_t w, int32_t h, Renderer* renderer);
void process() override;
private:
GuiText *label = nullptr;

View File

@ -1,74 +1,81 @@
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "CVideo.h"
#include "logger.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
CVideo::CVideo() {
SDL_Init(SDL_INIT_EVERYTHING);
auto SDLFlags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
//Setup window
window = SDL_CreateWindow(nullptr, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, 0);
if (!window) { return; }
renderer = SDL_CreateRenderer(window, -1, SDLFlags);
if (!renderer) { return; }
SDL_SetRenderTarget(renderer, NULL);
if (SDL_Init(SDL_INIT_AUDIO) != 0) {
DEBUG_FUNCTION_LINE("SDL init error: %s\n", SDL_GetError());
return;
}
int flags = 0;
int result = 0;
if (flags != (result = Mix_Init(flags))) {
DEBUG_FUNCTION_LINE("Could not initialize mixer (result: %d).\n", result);
DEBUG_FUNCTION_LINE("Mix_Init: %s\n", Mix_GetError());
}
auto dev = Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640);
SDL_PauseAudioDevice(dev, 0);
}
CVideo::~CVideo() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
SDL_Renderer *CVideo::getRenderer() {
return renderer;
}
float CVideo::getHeight() {
int h = 0;
SDL_GetWindowSize(window, NULL, &h);
return h;
}
float CVideo::getWidth() {
int w = 0;
SDL_GetWindowSize(window, &w, NULL);
return w;
}
unsigned int CVideo::getPixelFormat() {
return SDL_GetWindowPixelFormat(window);
}
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "SDLSystem.h"
#include "../utils/logger.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
SDLSystem::SDLSystem() {
SDL_Init(SDL_INIT_EVERYTHING);
auto SDLFlags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
//Setup window
window = SDL_CreateWindow(nullptr, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, 0);
if (!window) {
DEBUG_FUNCTION_LINE("Failed to create window");
return;
}
auto sdl_renderer = SDL_CreateRenderer(window, -1, SDLFlags);
if (!sdl_renderer) {
DEBUG_FUNCTION_LINE("Failed to init sdl renderer");
return;
}
SDL_SetRenderTarget(sdl_renderer, nullptr);
this->renderer = new Renderer(sdl_renderer, SDL_GetWindowPixelFormat(window));
if (!renderer) {
DEBUG_FUNCTION_LINE("Failed to init renderer");
return;
}
if (SDL_Init(SDL_INIT_AUDIO) != 0) {
DEBUG_FUNCTION_LINE("SDL init error: %s\n", SDL_GetError());
return;
}
int flags = 0;
int result = 0;
if (flags != (result = Mix_Init(flags))) {
DEBUG_FUNCTION_LINE("Could not initialize mixer (result: %d).\n", result);
DEBUG_FUNCTION_LINE("Mix_Init: %s\n", Mix_GetError());
}
auto dev = Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640);
SDL_PauseAudioDevice(dev, 0);
}
SDLSystem::~SDLSystem() {
SDL_DestroyWindow(window);
delete renderer;
SDL_Quit();
}
float SDLSystem::getHeight() {
int h = 0;
SDL_GetWindowSize(window, nullptr, &h);
return h;
}
float SDLSystem::getWidth() {
int w = 0;
SDL_GetWindowSize(window, &w, nullptr);
return w;
}
Renderer *SDLSystem::getRenderer() {
return renderer;
}

View File

@ -1,36 +1,35 @@
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#pragma once
#include <SDL2/SDL_render.h>
class CVideo {
public:
CVideo();
virtual ~CVideo();
SDL_Renderer *getRenderer();
float getHeight();
float getWidth();
unsigned int getPixelFormat();
private:
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#pragma once
#include <SDL2/SDL_render.h>
#include "video/Renderer.h"
class SDLSystem {
public:
SDLSystem();
virtual ~SDLSystem();
Renderer *getRenderer();
float getHeight();
float getWidth();
private:
SDL_Window *window = NULL;
Renderer *renderer = NULL;
};

View File

@ -0,0 +1,28 @@
#pragma once
#include <SDL2/SDL_render.h>
class Renderer {
public:
Renderer(SDL_Renderer *renderer, uint32_t pixelFormat) : sdl_renderer(renderer), pixelFormat(pixelFormat){
}
virtual ~Renderer() {
if(sdl_renderer){
SDL_DestroyRenderer(sdl_renderer);
}
}
SDL_Renderer *getRenderer(){
return sdl_renderer;
}
uint32_t getPixelFormat(){
return pixelFormat;
}
private:
SDL_Renderer *sdl_renderer = NULL;
uint32_t pixelFormat = SDL_PIXELFORMAT_RGBA8888;
};

File diff suppressed because it is too large Load Diff

View File

@ -1,328 +1,322 @@
/*
SDL_FontCache v0.10.0: A font cache for SDL and SDL_ttf
by Jonathan Dearborn
Dedicated to the memory of Florian Hufsky
License:
The short:
Use it however you'd like, but keep the copyright and license notice
whenever these files or parts of them are distributed in uncompiled form.
The long:
Copyright (c) 2019 Jonathan Dearborn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _SDL_FONTCACHE_H__
#define _SDL_FONTCACHE_H__
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "../logger.h"
#ifdef FC_USE_SDL_GPU
#include "SDL_gpu.h"
#endif
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
// Let's pretend this exists...
#define TTF_STYLE_OUTLINE 16
// Differences between SDL_Renderer and SDL_gpu
#ifdef FC_USE_SDL_GPU
#define FC_Rect GPU_Rect
#define FC_Target GPU_Target
#define FC_Image GPU_Image
#define FC_Log GPU_LogError
#else
#define FC_Rect SDL_Rect
#define FC_Target SDL_Renderer
#define FC_Image SDL_Texture
#define FC_Log DEBUG_FUNCTION_LINE
#endif
// SDL_FontCache types
typedef enum
{
FC_ALIGN_LEFT,
FC_ALIGN_CENTER,
FC_ALIGN_RIGHT
} FC_AlignEnum;
typedef enum
{
FC_FILTER_NEAREST,
FC_FILTER_LINEAR
} FC_FilterEnum;
typedef struct FC_Scale
{
float x;
float y;
} FC_Scale;
typedef struct FC_Effect
{
FC_AlignEnum alignment;
FC_Scale scale;
SDL_Color color;
} FC_Effect;
// Opaque type
typedef struct FC_Font FC_Font;
typedef struct FC_GlyphData
{
SDL_Rect rect;
int cache_level;
} FC_GlyphData;
// Object creation
FC_Rect FC_MakeRect(float x, float y, float w, float h);
FC_Scale FC_MakeScale(float x, float y);
SDL_Color FC_MakeColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
FC_Effect FC_MakeEffect(FC_AlignEnum alignment, FC_Scale scale, SDL_Color color);
FC_GlyphData FC_MakeGlyphData(int cache_level, Sint16 x, Sint16 y, Uint16 w, Uint16 h);
// Font object
FC_Font* FC_CreateFont(void);
#ifdef FC_USE_SDL_GPU
Uint8 FC_LoadFont(FC_Font* font, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
Uint8 FC_LoadFontFromTTF(FC_Font* font, TTF_Font* ttf, SDL_Color color);
Uint8 FC_LoadFont_RW(FC_Font* font, SDL_RWops* file_rwops_ttf, Uint8 own_rwops, Uint32 pointSize, SDL_Color color, int style);
#else
Uint8 FC_LoadFont(FC_Font* font, SDL_Renderer* renderer, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
Uint8 FC_LoadFontFromTTF(FC_Font* font, SDL_Renderer* renderer, TTF_Font* ttf, SDL_Color color);
Uint8 FC_LoadFont_RW(FC_Font* font, SDL_Renderer* renderer, SDL_RWops* file_rwops_ttf, Uint8 own_rwops, Uint32 pointSize, SDL_Color color, int style);
#endif
#ifndef FC_USE_SDL_GPU
// note: handle SDL event types SDL_RENDER_TARGETS_RESET(>= SDL 2.0.2) and SDL_RENDER_DEVICE_RESET(>= SDL 2.0.4)
void FC_ResetFontFromRendererReset(FC_Font* font, SDL_Renderer* renderer, Uint32 evType);
#endif
void FC_ClearFont(FC_Font* font);
void FC_FreeFont(FC_Font* font);
// Built-in loading strings
char* FC_GetStringASCII(void);
char* FC_GetStringLatin1(void);
char* FC_GetStringASCII_Latin1(void);
// UTF-8 to SDL_FontCache codepoint conversion
/*!
Returns the Uint32 codepoint (not UTF-32) parsed from the given UTF-8 string.
\param c A pointer to a string of proper UTF-8 character values.
\param advance_pointer If true, the source pointer will be incremented to skip the extra bytes from multibyte codepoints.
*/
Uint32 FC_GetCodepointFromUTF8(const char** c, Uint8 advance_pointer);
/*!
Parses the given codepoint and stores the UTF-8 bytes in 'result'. The result is NULL terminated.
\param result A memory buffer for the UTF-8 values. Must be at least 5 bytes long.
\param codepoint The Uint32 codepoint to parse (not UTF-32).
*/
void FC_GetUTF8FromCodepoint(char* result, Uint32 codepoint);
// UTF-8 string operations
/*! Allocates a new string of 'size' bytes that is already NULL-terminated. The NULL byte counts toward the size limit, as usual. Returns NULL if size is 0. */
char* U8_alloc(unsigned int size);
/*! Deallocates the given string. */
void U8_free(char* string);
/*! Allocates a copy of the given string. */
char* U8_strdup(const char* string);
/*! Returns the number of UTF-8 characters in the given string. */
int U8_strlen(const char* string);
/*! Returns the number of bytes in the UTF-8 multibyte character pointed at by 'character'. */
int U8_charsize(const char* character);
/*! Copies the source multibyte character into the given buffer without overrunning it. Returns 0 on failure. */
int U8_charcpy(char* buffer, const char* source, int buffer_size);
/*! Returns a pointer to the next UTF-8 character. */
const char* U8_next(const char* string);
/*! Inserts a UTF-8 string into 'string' at the given position. Use a position of -1 to append. Returns 0 when unable to insert the string. */
int U8_strinsert(char* string, int position, const char* source, int max_bytes);
/*! Erases the UTF-8 character at the given position, moving the subsequent characters down. */
void U8_strdel(char* string, int position);
// Internal settings
/*! Sets the string from which to load the initial glyphs. Use this if you need upfront loading for any reason (such as lack of render-target support). */
void FC_SetLoadingString(FC_Font* font, const char* string);
/*! Returns the size of the internal buffer which is used for unpacking variadic text data. This buffer is shared by all FC_Fonts. */
unsigned int FC_GetBufferSize(void);
/*! Changes the size of the internal buffer which is used for unpacking variadic text data. This buffer is shared by all FC_Fonts. */
void FC_SetBufferSize(unsigned int size);
/*! Returns the width of a single horizontal tab in multiples of the width of a space (default: 4) */
unsigned int FC_GetTabWidth(void);
/*! Changes the width of a horizontal tab in multiples of the width of a space (default: 4) */
void FC_SetTabWidth(unsigned int width_in_spaces);
void FC_SetRenderCallback(FC_Rect (*callback)(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale));
FC_Rect FC_DefaultRenderCallback(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale);
// Custom caching
/*! Returns the number of cache levels that are active. */
int FC_GetNumCacheLevels(FC_Font* font);
/*! Returns the cache source texture at the given cache level. */
FC_Image* FC_GetGlyphCacheLevel(FC_Font* font, int cache_level);
// TODO: Specify ownership of the texture (should be shareable)
/*! Sets a cache source texture for rendering. New cache levels must be sequential. */
Uint8 FC_SetGlyphCacheLevel(FC_Font* font, int cache_level, FC_Image* cache_texture);
/*! Copies the given surface to the given cache level as a texture. New cache levels must be sequential. */
Uint8 FC_UploadGlyphCache(FC_Font* font, int cache_level, SDL_Surface* data_surface);
/*! Returns the number of codepoints that are stored in the font's glyph data map. */
unsigned int FC_GetNumCodepoints(FC_Font* font);
/*! Copies the stored codepoints into the given array. */
void FC_GetCodepoints(FC_Font* font, Uint32* result);
/*! Stores the glyph data for the given codepoint in 'result'. Returns 0 if the codepoint was not found in the cache. */
Uint8 FC_GetGlyphData(FC_Font* font, FC_GlyphData* result, Uint32 codepoint);
/*! Sets the glyph data for the given codepoint. Duplicates are not checked. Returns a pointer to the stored data. */
FC_GlyphData* FC_SetGlyphData(FC_Font* font, Uint32 codepoint, FC_GlyphData glyph_data);
// Rendering
FC_Rect FC_Draw(FC_Font* font, FC_Target* dest, float x, float y, const char* formatted_text, ...);
FC_Rect FC_DrawAlign(FC_Font* font, FC_Target* dest, float x, float y, FC_AlignEnum align, const char* formatted_text, ...);
FC_Rect FC_DrawScale(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* formatted_text, ...);
FC_Rect FC_DrawColor(FC_Font* font, FC_Target* dest, float x, float y, SDL_Color color, const char* formatted_text, ...);
FC_Rect FC_DrawEffect(FC_Font* font, FC_Target* dest, float x, float y, FC_Effect effect, const char* formatted_text, ...);
FC_Rect FC_DrawBox(FC_Font* font, FC_Target* dest, FC_Rect box, const char* formatted_text, ...);
FC_Rect FC_DrawBoxAlign(FC_Font* font, FC_Target* dest, FC_Rect box, FC_AlignEnum align, const char* formatted_text, ...);
FC_Rect FC_DrawBoxScale(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Scale scale, const char* formatted_text, ...);
FC_Rect FC_DrawBoxColor(FC_Font* font, FC_Target* dest, FC_Rect box, SDL_Color color, const char* formatted_text, ...);
FC_Rect FC_DrawBoxEffect(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Effect effect, const char* formatted_text, ...);
FC_Rect FC_DrawColumn(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, const char* formatted_text, ...);
FC_Rect FC_DrawColumnAlign(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_AlignEnum align, const char* formatted_text, ...);
FC_Rect FC_DrawColumnScale(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Scale scale, const char* formatted_text, ...);
FC_Rect FC_DrawColumnColor(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, SDL_Color color, const char* formatted_text, ...);
FC_Rect FC_DrawColumnEffect(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Effect effect, const char* formatted_text, ...);
// Getters
FC_FilterEnum FC_GetFilterMode(FC_Font* font);
Uint16 FC_GetLineHeight(FC_Font* font);
Uint16 FC_GetHeight(FC_Font* font, const char* formatted_text, ...);
Uint16 FC_GetWidth(FC_Font* font, const char* formatted_text, ...);
// Returns a 1-pixel wide box in front of the character in the given position (index)
FC_Rect FC_GetCharacterOffset(FC_Font* font, Uint16 position_index, int column_width, const char* formatted_text, ...);
Uint16 FC_GetColumnHeight(FC_Font* font, Uint16 width, const char* formatted_text, ...);
int FC_GetAscent(FC_Font* font, const char* formatted_text, ...);
int FC_GetDescent(FC_Font* font, const char* formatted_text, ...);
int FC_GetBaseline(FC_Font* font);
int FC_GetSpacing(FC_Font* font);
int FC_GetLineSpacing(FC_Font* font);
Uint16 FC_GetMaxWidth(FC_Font* font);
SDL_Color FC_GetDefaultColor(FC_Font* font);
FC_Rect FC_GetBounds(FC_Font* font, float x, float y, FC_AlignEnum align, FC_Scale scale, const char* formatted_text, ...);
Uint8 FC_InRect(float x, float y, FC_Rect input_rect);
// Given an offset (x,y) from the text draw position (the upper-left corner), returns the character position (UTF-8 index)
Uint16 FC_GetPositionFromOffset(FC_Font* font, float x, float y, int column_width, FC_AlignEnum align, const char* formatted_text, ...);
// Returns the number of characters in the new wrapped text written into `result`.
int FC_GetWrappedText(FC_Font* font, char* result, int max_result_size, Uint16 width, const char* formatted_text, ...);
// Setters
void FC_SetFilterMode(FC_Font* font, FC_FilterEnum filter);
void FC_SetSpacing(FC_Font* font, int LetterSpacing);
void FC_SetLineSpacing(FC_Font* font, int LineSpacing);
void FC_SetDefaultColor(FC_Font* font, SDL_Color color);
#ifdef __cplusplus
}
#endif
#endif
/*
SDL_FontCache v0.10.0: A font cache for SDL and SDL_ttf
by Jonathan Dearborn
Dedicated to the memory of Florian Hufsky
License:
The short:
Use it however you'd like, but keep the copyright and license notice
whenever these files or parts of them are distributed in uncompiled form.
The long:
Copyright (c) 2019 Jonathan Dearborn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _SDL_FONTCACHE_H__
#define _SDL_FONTCACHE_H__
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_pixels.h>
#include "../../utils/logger.h"
#ifdef FC_USE_SDL_GPU
#include "SDL_gpu.h"
#endif
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
// Let's pretend this exists...
#define TTF_STYLE_OUTLINE 16
// Differences between SDL_Renderer and SDL_gpu
#ifdef FC_USE_SDL_GPU
#define FC_Rect GPU_Rect
#define FC_Target GPU_Target
#define FC_Image GPU_Image
#define FC_Log GPU_LogError
#else
#define FC_Rect SDL_Rect
#define FC_Target SDL_Renderer
#define FC_Image SDL_Texture
#define FC_Log DEBUG_FUNCTION_LINE
#endif
// SDL_FontCache types
typedef enum
{
FC_ALIGN_LEFT,
FC_ALIGN_CENTER,
FC_ALIGN_RIGHT
} FC_AlignEnum;
typedef enum
{
FC_FILTER_NEAREST,
FC_FILTER_LINEAR
} FC_FilterEnum;
typedef struct FC_Scale
{
float x;
float y;
} FC_Scale;
typedef struct FC_Effect
{
FC_AlignEnum alignment;
FC_Scale scale;
SDL_Color color;
} FC_Effect;
// Opaque type
typedef struct FC_Font FC_Font;
typedef struct FC_GlyphData
{
SDL_Rect rect;
int cache_level;
} FC_GlyphData;
// Object creation
FC_Rect FC_MakeRect(float x, float y, float w, float h);
FC_Scale FC_MakeScale(float x, float y);
SDL_Color FC_MakeColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
FC_Effect FC_MakeEffect(FC_AlignEnum alignment, FC_Scale scale, SDL_Color color);
FC_GlyphData FC_MakeGlyphData(int cache_level, Sint16 x, Sint16 y, Uint16 w, Uint16 h);
SDL_Texture* FC_CreateTexture(FC_Font *pFont, uint32_t pixelFormat, float width, float height);
// Font object
FC_Font* FC_CreateFont(void);
#ifdef FC_USE_SDL_GPU
Uint8 FC_LoadFont(FC_Font* font, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
Uint8 FC_LoadFontFromTTF(FC_Font* font, TTF_Font* ttf, SDL_Color color);
Uint8 FC_LoadFont_RW(FC_Font* font, SDL_RWops* file_rwops_ttf, Uint8 own_rwops, Uint32 pointSize, SDL_Color color, int style);
#else
Uint8 FC_LoadFont(FC_Font* font, SDL_Renderer* renderer, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
Uint8 FC_LoadFontFromTTF(FC_Font* font, SDL_Renderer* renderer, TTF_Font* ttf, SDL_Color color);
Uint8 FC_LoadFont_RW(FC_Font* font, SDL_Renderer* renderer, SDL_RWops* file_rwops_ttf, Uint8 own_rwops, Uint32 pointSize, SDL_Color color, int style);
#endif
#ifndef FC_USE_SDL_GPU
// note: handle SDL event types SDL_RENDER_TARGETS_RESET(>= SDL 2.0.2) and SDL_RENDER_DEVICE_RESET(>= SDL 2.0.4)
void FC_ResetFontFromRendererReset(FC_Font* font, SDL_Renderer* renderer, Uint32 evType);
#endif
void FC_ClearFont(FC_Font* font);
void FC_FreeFont(FC_Font* font);
// Built-in loading strings
char* FC_GetStringASCII(void);
char* FC_GetStringLatin1(void);
char* FC_GetStringASCII_Latin1(void);
// UTF-8 to SDL_FontCache codepoint conversion
/*!
Returns the Uint32 codepoint (not UTF-32) parsed from the given UTF-8 string.
\param c A pointer to a string of proper UTF-8 character values.
\param advance_pointer If true, the source pointer will be incremented to skip the extra bytes from multibyte codepoints.
*/
Uint32 FC_GetCodepointFromUTF8(const char** c, Uint8 advance_pointer);
/*!
Parses the given codepoint and stores the UTF-8 bytes in 'result'. The result is NULL terminated.
\param result A memory buffer for the UTF-8 values. Must be at least 5 bytes long.
\param codepoint The Uint32 codepoint to parse (not UTF-32).
*/
void FC_GetUTF8FromCodepoint(char* result, Uint32 codepoint);
// UTF-8 string operations
/*! Allocates a new string of 'size' bytes that is already NULL-terminated. The NULL byte counts toward the size limit, as usual. Returns NULL if size is 0. */
char* U8_alloc(unsigned int size);
/*! Deallocates the given string. */
void U8_free(char* string);
/*! Allocates a copy of the given string. */
char* U8_strdup(const char* string);
/*! Returns the number of UTF-8 characters in the given string. */
int U8_strlen(const char* string);
/*! Returns the number of bytes in the UTF-8 multibyte character pointed at by 'character'. */
int U8_charsize(const char* character);
/*! Copies the source multibyte character into the given buffer without overrunning it. Returns 0 on failure. */
int U8_charcpy(char* buffer, const char* source, int buffer_size);
/*! Returns a pointer to the next UTF-8 character. */
const char* U8_next(const char* string);
/*! Inserts a UTF-8 string into 'string' at the given position. Use a position of -1 to append. Returns 0 when unable to insert the string. */
int U8_strinsert(char* string, int position, const char* source, int max_bytes);
/*! Erases the UTF-8 character at the given position, moving the subsequent characters down. */
void U8_strdel(char* string, int position);
// Internal settings
/*! Sets the string from which to load the initial glyphs. Use this if you need upfront loading for any reason (such as lack of render-target support). */
void FC_SetLoadingString(FC_Font* font, const char* string);
/*! Returns the size of the internal buffer which is used for unpacking variadic text data. This buffer is shared by all FC_Fonts. */
unsigned int FC_GetBufferSize(void);
/*! Changes the size of the internal buffer which is used for unpacking variadic text data. This buffer is shared by all FC_Fonts. */
void FC_SetBufferSize(unsigned int size);
/*! Returns the width of a single horizontal tab in multiples of the width of a space (default: 4) */
unsigned int FC_GetTabWidth(void);
/*! Changes the width of a horizontal tab in multiples of the width of a space (default: 4) */
void FC_SetTabWidth(unsigned int width_in_spaces);
void FC_SetRenderCallback(FC_Rect (*callback)(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale));
FC_Rect FC_DefaultRenderCallback(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale);
// Custom caching
/*! Returns the number of cache levels that are active. */
int FC_GetNumCacheLevels(FC_Font* font);
/*! Returns the cache source texture at the given cache level. */
FC_Image* FC_GetGlyphCacheLevel(FC_Font* font, int cache_level);
// TODO: Specify ownership of the texture (should be shareable)
/*! Sets a cache source texture for rendering. New cache levels must be sequential. */
Uint8 FC_SetGlyphCacheLevel(FC_Font* font, int cache_level, FC_Image* cache_texture);
/*! Copies the given surface to the given cache level as a texture. New cache levels must be sequential. */
Uint8 FC_UploadGlyphCache(FC_Font* font, int cache_level, SDL_Surface* data_surface);
/*! Returns the number of codepoints that are stored in the font's glyph data map. */
unsigned int FC_GetNumCodepoints(FC_Font* font);
/*! Copies the stored codepoints into the given array. */
void FC_GetCodepoints(FC_Font* font, Uint32* result);
/*! Stores the glyph data for the given codepoint in 'result'. Returns 0 if the codepoint was not found in the cache. */
Uint8 FC_GetGlyphData(FC_Font* font, FC_GlyphData* result, Uint32 codepoint);
/*! Sets the glyph data for the given codepoint. Duplicates are not checked. Returns a pointer to the stored data. */
FC_GlyphData* FC_SetGlyphData(FC_Font* font, Uint32 codepoint, FC_GlyphData glyph_data);
// Rendering
FC_Rect FC_Draw(FC_Font* font, FC_Target* dest, float x, float y, const char* formatted_text, ...);
FC_Rect FC_DrawAlign(FC_Font* font, FC_Target* dest, float x, float y, FC_AlignEnum align, const char* formatted_text, ...);
FC_Rect FC_DrawScale(FC_Font* font, FC_Target* dest, float x, float y, FC_Scale scale, const char* formatted_text, ...);
FC_Rect FC_DrawColor(FC_Font* font, FC_Target* dest, float x, float y, SDL_Color color, const char* formatted_text, ...);
FC_Rect FC_DrawEffect(FC_Font* font, FC_Target* dest, float x, float y, FC_Effect effect, const char* formatted_text, ...);
FC_Rect FC_DrawBox(FC_Font* font, FC_Target* dest, FC_Rect box, const char* formatted_text, ...);
FC_Rect FC_DrawBoxAlign(FC_Font* font, FC_Target* dest, FC_Rect box, FC_AlignEnum align, const char* formatted_text, ...);
FC_Rect FC_DrawBoxScale(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Scale scale, const char* formatted_text, ...);
FC_Rect FC_DrawBoxColor(FC_Font* font, FC_Target* dest, FC_Rect box, SDL_Color color, const char* formatted_text, ...);
FC_Rect FC_DrawBoxEffect(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Effect effect, const char* formatted_text, ...);
FC_Rect FC_DrawColumn(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, const char* formatted_text, ...);
FC_Rect FC_DrawColumnToTexture(FC_Font* font, SDL_Texture* target, float x, float y, Uint16 width, const char* text);
FC_Rect FC_DrawColumnAlign(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_AlignEnum align, const char* formatted_text, ...);
FC_Rect FC_DrawColumnScale(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Scale scale, const char* formatted_text, ...);
FC_Rect FC_DrawColumnColor(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, SDL_Color color, const char* formatted_text, ...);
FC_Rect FC_DrawColumnEffect(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Effect effect, const char* formatted_text, ...);
// Getters
FC_FilterEnum FC_GetFilterMode(FC_Font* font);
Uint16 FC_GetLineHeight(FC_Font* font);
Uint16 FC_GetHeight(FC_Font* font, const char* formatted_text, ...);
Uint16 FC_GetWidth(FC_Font* font, const char* formatted_text, ...);
// Returns a 1-pixel wide box in front of the character in the given position (index)
FC_Rect FC_GetCharacterOffset(FC_Font* font, Uint16 position_index, int column_width, const char* formatted_text, ...);
Uint16 FC_GetColumnHeight(FC_Font* font, Uint16 width, const char* formatted_text, ...);
int FC_GetAscent(FC_Font* font, const char* formatted_text, ...);
int FC_GetDescent(FC_Font* font, const char* formatted_text, ...);
int FC_GetBaseline(FC_Font* font);
int FC_GetSpacing(FC_Font* font);
int FC_GetLineSpacing(FC_Font* font);
Uint16 FC_GetMaxWidth(FC_Font* font);
SDL_Color FC_GetDefaultColor(FC_Font* font);
FC_Rect FC_GetBounds(FC_Font* font, float x, float y, FC_AlignEnum align, FC_Scale scale, const char* formatted_text, ...);
Uint8 FC_InRect(float x, float y, FC_Rect input_rect);
// Given an offset (x,y) from the text draw position (the upper-left corner), returns the character position (UTF-8 index)
Uint16 FC_GetPositionFromOffset(FC_Font* font, float x, float y, int column_width, FC_AlignEnum align, const char* formatted_text, ...);
// Returns the number of characters in the new wrapped text written into `result`.
int FC_GetWrappedText(FC_Font* font, char* result, int max_result_size, Uint16 width, const char* formatted_text, ...);
// Setters
void FC_SetFilterMode(FC_Font* font, FC_FilterEnum filter);
void FC_SetSpacing(FC_Font* font, int LetterSpacing);
void FC_SetLineSpacing(FC_Font* font, int LineSpacing);
void FC_SetDefaultColor(FC_Font* font, SDL_Color color);
#ifdef __cplusplus
}
#endif
#endif