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.cpp
src/gui/GuiImage.h src/gui/GuiImage.h
src/gui/sigslot.h src/gui/sigslot.h
src/CVideo.cpp src/system/SDLSystem.cpp
src/CVideo.h src/system/SDLSystem.h
src/gui/GuiElement.cpp src/gui/GuiElement.cpp
src/gui/GuiText.cpp src/gui/GuiText.cpp
src/gui/GuiText.h src/gui/GuiText.h
@ -25,13 +25,14 @@ add_executable(${PROJECT_NAME}
src/gui/GuiButton.h src/gui/GuiButton.h
src/gui/SDLController.h src/MainWindow.cpp src/MainWindow.h src/gui/SDLControllerJoystick.h src/gui/SDLControllerMouse.h src/input/SDLController.h src/menu/MainWindow.cpp src/menu/MainWindow.h src/input/SDLControllerJoystick.h src/input/SDLControllerMouse.h
src/gui/SDLControllerWiiUGamepad.h src/input/SDLControllerWiiUGamepad.h
src/gui/SDLControllerWiiUProContoller.h src/input/SDLControllerWiiUProContoller.h
src/gui/GuiTexture.cpp src/gui/GuiTexture.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) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)

View File

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

View File

@ -21,7 +21,12 @@ WUMS_ROOT := $(DEVKITPRO)/wums
TARGET := SDL2_Playground TARGET := SDL2_Playground
BUILD := build BUILD := build
SOURCES := src \ SOURCES := src \
src/gui src/gui \
src/input \
src/menu \
src/system \
src/system/video \
src/utils
DATA := data DATA := data
INCLUDES := source 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; clickedTrigger = NULL;
heldTrigger = NULL; heldTrigger = NULL;
GuiElement::resetState(); GuiElement::resetState();
@ -142,7 +142,7 @@ void GuiButton::resetState(void) {
/** /**
* Draw the button on screen * Draw the button on screen
*/ */
void GuiButton::draw(CVideo *v) { void GuiButton::draw(Renderer *v) {
if (!this->isVisible()) { if (!this->isVisible()) {
return; return;
} }

View File

@ -20,7 +20,7 @@
#include "GuiText.h" #include "GuiText.h"
#include "GuiSound.h" #include "GuiSound.h"
#include "GuiTrigger.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) //!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 { class GuiButton : public GuiElement {
@ -94,7 +94,7 @@ public:
void resetState(void) override; void resetState(void) override;
//!Constantly called to draw the GuiButton //!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 //!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 //!\param t Pointer to a GuiTrigger, containing the current input data from PAD/WPAD

View File

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

View File

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

View File

@ -20,6 +20,7 @@
#include <mutex> #include <mutex>
#include "GuiElement.h" #include "GuiElement.h"
#include "sigslot.h" #include "sigslot.h"
#include "../system/video/Renderer.h"
//!Allows GuiElements to be grouped together into a "window" //!Allows GuiElements to be grouped together into a "window"
class GuiFrame : public GuiElement { class GuiFrame : public GuiElement {
@ -33,7 +34,7 @@ public:
GuiFrame(float w, float h, GuiFrame *parent = 0); GuiFrame(float w, float h, GuiFrame *parent = 0);
//!Destructor //!Destructor
virtual ~GuiFrame(); ~GuiFrame() override;
//!Appends a GuiElement to the GuiFrame //!Appends a GuiElement to the GuiFrame
//!\param e The GuiElement to append. If it is already in the GuiFrame, it is removed first //!\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; int32_t getSelected() override;
//!Draws all the elements in this GuiFrame //!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 //!Updates the window and all elements contains within
//!Allows the GuiFrame and all elements to respond to the input data specified //!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 <SDL2/SDL_image.h>
#include <iostream> #include <iostream>
#include "GuiImage.h" #include "GuiImage.h"
#include "../CVideo.h" #include "../system/SDLSystem.h"
GuiImage::GuiImage(const std::string& path) : GuiTexture(path){ 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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/ ****************************************************************************/
#include "GuiSound.h" #include "GuiSound.h"
#include "../logger.h" #include "../utils/logger.h"
GuiSound::GuiSound(const char *filepath) { GuiSound::GuiSound(const char *filepath) {
Load(filepath); Load(filepath);

View File

@ -17,23 +17,19 @@
#include <cstdarg> #include <cstdarg>
#include <SDL2/SDL_surface.h> #include <SDL2/SDL_surface.h>
#include "GuiText.h" #include "GuiText.h"
#include "../CVideo.h"
#include "../logger.h"
#include "SDL_FontCache.h"
/** /**
* Constructor for the GuiText class. * 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->text = text;
this->size = s;
this->color = c; this->color = c;
this->ttf_font = gFont; this->fc_font = gFont;
this->invalid = true; this->updateText = false;
this->updateText = true;
this->maxWidth = 200; updateSize();
updateTexture();
} }
GuiText::~GuiText(){ GuiText::~GuiText(){
@ -44,45 +40,13 @@ GuiText::~GuiText(){
delete texture; delete texture;
} }
void GuiText::draw(CVideo *pVideo) { void GuiText::draw(Renderer *renderer) {
if (!this->isVisible()) { if (!this->isVisible()) {
return; 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){ if(texture){
texture->draw(pVideo); texture->draw(renderer);
} }
} }
@ -90,15 +54,37 @@ void GuiText::process() {
GuiElement::process(); GuiElement::process();
if(updateText && fc_font){ if(updateText && fc_font){
auto height = FC_GetColumnHeight(fc_font, maxWidth, text.c_str()); updateTexture();
auto width = FC_GetWidth(fc_font, text.c_str()); updateText = false;
width = width > maxWidth ? maxWidth : width;
this->setSize(width, height);
} }
} }
void GuiText::setMaxWidth(float width) { void GuiText::setMaxWidth(float width) {
this->maxWidth = width; this->maxWidth = width;
// Rebuild the texture cache. // Rebuild the texture cache.
updateText = true; 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 #pragma once
#include "GuiElement.h" #include "GuiElement.h"
#include "SDL_FontCache.h"
#include "GuiTexture.h" #include "GuiTexture.h"
#include "../system/video/SDL_FontCache.h"
#include <mutex> #include <mutex>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
//!Display, manage, and manipulate text in the GUI //!Display, manage, and manipulate text in the GUI
class GuiText : public GuiElement { class GuiText : public GuiElement {
public: public:
@ -30,10 +29,10 @@ public:
//!\param t Text //!\param t Text
//!\param s Font size //!\param s Font size
//!\param c Font color //!\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; ~GuiText() override;
void draw(CVideo *pVideo) override; void draw(Renderer *pVideo) override;
void process() override; void process() override;
@ -42,12 +41,13 @@ public:
protected: protected:
GuiTexture* texture = nullptr; GuiTexture* texture = nullptr;
std::string text; std::string text;
int32_t size;
SDL_Color color; SDL_Color color;
TTF_Font *ttf_font = nullptr;
FC_Font *fc_font = nullptr; FC_Font *fc_font = nullptr;
bool invalid = true;
bool updateText = true; bool updateText = true;
uint16_t maxWidth = 0xFFFF; uint16_t maxWidth = 0xFFFF;
void updateSize();
void updateTexture();
}; };

View File

@ -1,7 +1,7 @@
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include "GuiTexture.h" #include "GuiTexture.h"
#include "../CVideo.h" #include "../system/SDLSystem.h"
#include "../logger.h" #include "../utils/logger.h"
GuiTexture::GuiTexture(const std::string& path) { GuiTexture::GuiTexture(const std::string& path) {
imgSurface = IMG_Load( path.c_str() ); 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()) { if (!this->isVisible()) {
DEBUG_FUNCTION_LINE("not visible!"); DEBUG_FUNCTION_LINE("not visible!");
return; return;
} }
if (texture == NULL && imgSurface) { 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) { if (optimizedSurface != NULL) {
SDL_FreeSurface(imgSurface); SDL_FreeSurface(imgSurface);
imgSurface = optimizedSurface; imgSurface = optimizedSurface;
DEBUG_FUNCTION_LINE("Optimized surface"); DEBUG_FUNCTION_LINE("Optimized surface");
} }
texture = SDL_CreateTextureFromSurface(pVideo->getRenderer(), imgSurface); texture = SDL_CreateTextureFromSurface(renderer->getRenderer(), imgSurface);
} }
if (!texture) { if (!texture) {
DEBUG_FUNCTION_LINE("no texture!"); DEBUG_FUNCTION_LINE("no texture!");
@ -79,9 +79,9 @@ void GuiTexture::draw(CVideo *pVideo) {
rect.h = currScaleY * getHeight(); rect.h = currScaleY * getHeight();
if (getAngle() == 0) { if (getAngle() == 0) {
SDL_RenderCopy(pVideo->getRenderer(), texture, nullptr, &rect); SDL_RenderCopy(renderer->getRenderer(), texture, nullptr, &rect);
} else { } 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; ~GuiTexture() override;
//!Constantly called to draw the image //!Constantly called to draw the image
void draw(CVideo *pVideo) override; void draw(Renderer *pVideo) override;
int setBlendMode(SDL_BlendMode blendMode); int setBlendMode(SDL_BlendMode blendMode);

View File

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

View File

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

View File

@ -1,17 +1,17 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "CVideo.h" #include "system/SDLSystem.h"
#include "gui/GuiFrame.h" #include "gui/GuiFrame.h"
#include "gui/GuiImage.h" #include "gui/GuiImage.h"
#include "gui/GuiButton.h" #include "gui/GuiButton.h"
#include "gui/GuiController.h" #include "gui/GuiController.h"
#include "gui/SDLController.h" #include "menu/MainWindow.h"
#include "MainWindow.h" #include "utils/logger.h"
#include "logger.h" #include "input/SDLController.h"
#include "gui/SDLControllerJoystick.h" #include "input/SDLControllerMouse.h"
#include "gui/SDLControllerMouse.h" #include "input/SDLControllerWiiUGamepad.h"
#include "gui/SDLControllerWiiUGamepad.h" #include "input/SDLControllerXboxOne.h"
#include "gui/SDLControllerXboxOne.h" #include "input/SDLControllerWiiUProContoller.h"
#include "gui/SDLControllerWiiUProContoller.h" #include "input/SDLControllerJoystick.h"
#include <cstdio> #include <cstdio>
#include <fcntl.h> #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); void removeJoystick(int32_t instanceId, std::map<GuiTrigger::eChannels, SDLController *> &controllerList, std::map<int32_t, GuiTrigger::eChannels>& joystickToChannel);
int main(int argc, char *args[]) { int main(int argc, char *args[]) {
auto *video = new CVideo(); auto *video = new SDLSystem();
#if defined _WIN32 #if defined _WIN32
// Create the Console // Create the Console
@ -82,7 +82,7 @@ int main(int argc, char *args[]) {
WHBLogUdpInit(); WHBLogUdpInit();
#endif #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<GuiTrigger::eChannels, SDLController*> controllerList;
std::map<int32_t , GuiTrigger::eChannels> joystickToChannel; std::map<int32_t , GuiTrigger::eChannels> joystickToChannel;
@ -99,7 +99,6 @@ int main(int argc, char *args[]) {
break; break;
} }
#endif #endif
//! Read out inputs //! Read out inputs
for( auto const& [channel, controller] : controllerList ){ for( auto const& [channel, controller] : controllerList ){
controller->before(); controller->before();
@ -156,15 +155,25 @@ int main(int argc, char *args[]) {
frame->process(); frame->process();
// clear the screen // clear the screen
SDL_RenderClear(video->getRenderer()); SDL_RenderClear(video->getRenderer()->getRenderer());
frame->draw(video); frame->draw(video->getRenderer());
frame->updateEffects(); frame->updateEffects();
// flip the backbuffer // flip the backbuffer
// this means that everything that we prepared behind the screens is actually shown // 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 "MainWindow.h"
#include "gui/SDL_FontCache.h"
MainWindow::~MainWindow() { MainWindow::~MainWindow() {
delete label;; delete label;;
@ -16,7 +15,7 @@ MainWindow::~MainWindow() {
delete bgMusic;; 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 #if defined _WIN32
auto picture_path = "test.png"; auto picture_path = "test.png";
auto font_path = "FreeSans.ttf"; 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); 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 = new GuiSound(bgMusic_path);
bgMusic->SetLoop(true); bgMusic->SetLoop(true);
@ -87,3 +93,7 @@ void MainWindow::process() {
} }
button->setAngle(res); 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 #pragma once
#include <iostream> #include <iostream>
#include "gui/GuiFrame.h" #include "../gui/GuiFrame.h"
#include "gui/GuiButton.h" #include "../gui/GuiButton.h"
#include "logger.h" #include "../utils/logger.h"
class MainWindow : public GuiFrame, public sigslot::has_slots<> { class MainWindow : public GuiFrame, public sigslot::has_slots<> {
public: public:
void test(GuiButton *, const GuiController *, GuiTrigger *) { void test(GuiButton *, const GuiController *, GuiTrigger *);
DEBUG_FUNCTION_LINE("Hello, you have clicked the button");
}
~MainWindow(); ~MainWindow() override;
MainWindow(int32_t w, int32_t h); MainWindow(int32_t w, int32_t h, Renderer* renderer);
void process() override; void process() override;
private: private:
GuiText *label = nullptr; GuiText *label = nullptr;

View File

@ -1,74 +1,81 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2015 Dimok * Copyright (C) 2015 Dimok
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/ ****************************************************************************/
#include "CVideo.h" #include "SDLSystem.h"
#include "logger.h" #include "../utils/logger.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h> #include <SDL2/SDL_mixer.h>
CVideo::CVideo() { SDLSystem::SDLSystem() {
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
auto SDLFlags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; auto SDLFlags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
//Setup window //Setup window
window = SDL_CreateWindow(nullptr, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, 0); window = SDL_CreateWindow(nullptr, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, 0);
if (!window) { return; } if (!window) {
renderer = SDL_CreateRenderer(window, -1, SDLFlags); DEBUG_FUNCTION_LINE("Failed to create window");
if (!renderer) { return; } return;
SDL_SetRenderTarget(renderer, NULL); }
auto sdl_renderer = SDL_CreateRenderer(window, -1, SDLFlags);
if (SDL_Init(SDL_INIT_AUDIO) != 0) { if (!sdl_renderer) {
DEBUG_FUNCTION_LINE("SDL init error: %s\n", SDL_GetError()); DEBUG_FUNCTION_LINE("Failed to init sdl renderer");
return; return;
} }
SDL_SetRenderTarget(sdl_renderer, nullptr);
int flags = 0; this->renderer = new Renderer(sdl_renderer, SDL_GetWindowPixelFormat(window));
int result = 0; if (!renderer) {
if (flags != (result = Mix_Init(flags))) { DEBUG_FUNCTION_LINE("Failed to init renderer");
DEBUG_FUNCTION_LINE("Could not initialize mixer (result: %d).\n", result); return;
DEBUG_FUNCTION_LINE("Mix_Init: %s\n", Mix_GetError()); }
}
if (SDL_Init(SDL_INIT_AUDIO) != 0) {
auto dev = Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640); DEBUG_FUNCTION_LINE("SDL init error: %s\n", SDL_GetError());
SDL_PauseAudioDevice(dev, 0); return;
} }
CVideo::~CVideo() { int flags = 0;
SDL_DestroyRenderer(renderer); int result = 0;
SDL_DestroyWindow(window); if (flags != (result = Mix_Init(flags))) {
SDL_Quit(); DEBUG_FUNCTION_LINE("Could not initialize mixer (result: %d).\n", result);
} DEBUG_FUNCTION_LINE("Mix_Init: %s\n", Mix_GetError());
}
SDL_Renderer *CVideo::getRenderer() {
return renderer; auto dev = Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640);
} SDL_PauseAudioDevice(dev, 0);
}
float CVideo::getHeight() {
int h = 0; SDLSystem::~SDLSystem() {
SDL_GetWindowSize(window, NULL, &h); SDL_DestroyWindow(window);
return h; delete renderer;
} SDL_Quit();
}
float CVideo::getWidth() {
int w = 0; float SDLSystem::getHeight() {
SDL_GetWindowSize(window, &w, NULL); int h = 0;
return w; SDL_GetWindowSize(window, nullptr, &h);
} return h;
}
unsigned int CVideo::getPixelFormat() {
return SDL_GetWindowPixelFormat(window); 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 * Copyright (C) 2015 Dimok
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/ ****************************************************************************/
#pragma once #pragma once
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include "video/Renderer.h"
class CVideo {
public: class SDLSystem {
CVideo(); public:
SDLSystem();
virtual ~CVideo();
virtual ~SDLSystem();
SDL_Renderer *getRenderer();
Renderer *getRenderer();
float getHeight();
float getWidth(); float getHeight();
float getWidth();
unsigned int getPixelFormat();
private:
private: SDL_Window *window = NULL;
SDL_Window *window = NULL; Renderer *renderer = NULL;
SDL_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 SDL_FontCache v0.10.0: A font cache for SDL and SDL_ttf
by Jonathan Dearborn by Jonathan Dearborn
Dedicated to the memory of Florian Hufsky Dedicated to the memory of Florian Hufsky
License: License:
The short: The short:
Use it however you'd like, but keep the copyright and license notice 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. whenever these files or parts of them are distributed in uncompiled form.
The long: The long:
Copyright (c) 2019 Jonathan Dearborn Copyright (c) 2019 Jonathan Dearborn
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
#ifndef _SDL_FONTCACHE_H__ #ifndef _SDL_FONTCACHE_H__
#define _SDL_FONTCACHE_H__ #define _SDL_FONTCACHE_H__
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include "../logger.h" #include <SDL2/SDL_pixels.h>
#include "../../utils/logger.h"
#ifdef FC_USE_SDL_GPU
#include "SDL_gpu.h" #ifdef FC_USE_SDL_GPU
#endif #include "SDL_gpu.h"
#endif
#include <stdarg.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" { #ifdef __cplusplus
#endif extern "C" {
#endif
// Let's pretend this exists...
#define TTF_STYLE_OUTLINE 16 // Let's pretend this exists...
#define TTF_STYLE_OUTLINE 16
// Differences between SDL_Renderer and SDL_gpu
#ifdef FC_USE_SDL_GPU // Differences between SDL_Renderer and SDL_gpu
#define FC_Rect GPU_Rect #ifdef FC_USE_SDL_GPU
#define FC_Target GPU_Target #define FC_Rect GPU_Rect
#define FC_Image GPU_Image #define FC_Target GPU_Target
#define FC_Log GPU_LogError #define FC_Image GPU_Image
#else #define FC_Log GPU_LogError
#define FC_Rect SDL_Rect #else
#define FC_Target SDL_Renderer #define FC_Rect SDL_Rect
#define FC_Image SDL_Texture #define FC_Target SDL_Renderer
#define FC_Log DEBUG_FUNCTION_LINE #define FC_Image SDL_Texture
#endif #define FC_Log DEBUG_FUNCTION_LINE
#endif
// SDL_FontCache types
// SDL_FontCache types
typedef enum
{ typedef enum
FC_ALIGN_LEFT, {
FC_ALIGN_CENTER, FC_ALIGN_LEFT,
FC_ALIGN_RIGHT FC_ALIGN_CENTER,
} FC_AlignEnum; FC_ALIGN_RIGHT
} FC_AlignEnum;
typedef enum
{ typedef enum
FC_FILTER_NEAREST, {
FC_FILTER_LINEAR FC_FILTER_NEAREST,
} FC_FilterEnum; FC_FILTER_LINEAR
} FC_FilterEnum;
typedef struct FC_Scale
{ typedef struct FC_Scale
float x; {
float y; float x;
float y;
} FC_Scale;
} FC_Scale;
typedef struct FC_Effect
{ typedef struct FC_Effect
FC_AlignEnum alignment; {
FC_Scale scale; FC_AlignEnum alignment;
SDL_Color color; FC_Scale scale;
SDL_Color color;
} FC_Effect;
} FC_Effect;
// Opaque type
typedef struct FC_Font FC_Font; // Opaque type
typedef struct FC_Font FC_Font;
typedef struct FC_GlyphData
{ typedef struct FC_GlyphData
SDL_Rect rect; {
int cache_level; SDL_Rect rect;
int cache_level;
} FC_GlyphData;
} FC_GlyphData;
// Object creation
FC_Rect FC_MakeRect(float x, float y, float w, float h);
// Object creation
FC_Scale FC_MakeScale(float x, float y);
FC_Rect FC_MakeRect(float x, float y, float w, float h);
SDL_Color FC_MakeColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
FC_Scale FC_MakeScale(float x, float y);
FC_Effect FC_MakeEffect(FC_AlignEnum alignment, FC_Scale scale, SDL_Color color);
SDL_Color FC_MakeColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
FC_GlyphData FC_MakeGlyphData(int cache_level, Sint16 x, Sint16 y, Uint16 w, Uint16 h);
FC_Effect FC_MakeEffect(FC_AlignEnum alignment, FC_Scale scale, SDL_Color color);
SDL_Texture* FC_CreateTexture(FC_Font *pFont, uint32_t pixelFormat, float width, float height);
FC_GlyphData FC_MakeGlyphData(int cache_level, Sint16 x, Sint16 y, Uint16 w, Uint16 h);
// Font object
FC_Font* FC_CreateFont(void);
// Font object
#ifdef FC_USE_SDL_GPU
FC_Font* FC_CreateFont(void); Uint8 FC_LoadFont(FC_Font* font, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
#ifdef FC_USE_SDL_GPU Uint8 FC_LoadFontFromTTF(FC_Font* font, TTF_Font* ttf, SDL_Color color);
Uint8 FC_LoadFont(FC_Font* font, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
Uint8 FC_LoadFont_RW(FC_Font* font, SDL_RWops* file_rwops_ttf, Uint8 own_rwops, Uint32 pointSize, SDL_Color color, int style);
Uint8 FC_LoadFontFromTTF(FC_Font* font, TTF_Font* ttf, SDL_Color color); #else
Uint8 FC_LoadFont(FC_Font* font, SDL_Renderer* renderer, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
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_LoadFontFromTTF(FC_Font* font, SDL_Renderer* renderer, TTF_Font* ttf, SDL_Color color);
Uint8 FC_LoadFont(FC_Font* font, SDL_Renderer* renderer, const char* filename_ttf, Uint32 pointSize, SDL_Color color, int style);
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);
Uint8 FC_LoadFontFromTTF(FC_Font* font, SDL_Renderer* renderer, TTF_Font* ttf, SDL_Color color); #endif
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); #ifndef FC_USE_SDL_GPU
#endif // 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);
#ifndef FC_USE_SDL_GPU #endif
// 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); void FC_ClearFont(FC_Font* font);
#endif
void FC_FreeFont(FC_Font* font);
void FC_ClearFont(FC_Font* font);
void FC_FreeFont(FC_Font* font);
// Built-in loading strings
char* FC_GetStringASCII(void);
// Built-in loading strings
char* FC_GetStringLatin1(void);
char* FC_GetStringASCII(void);
char* FC_GetStringASCII_Latin1(void);
char* FC_GetStringLatin1(void);
char* FC_GetStringASCII_Latin1(void); // UTF-8 to SDL_FontCache codepoint conversion
/*!
// 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.
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. Uint32 FC_GetCodepointFromUTF8(const char** c, Uint8 advance_pointer);
\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).
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. void FC_GetUTF8FromCodepoint(char* result, Uint32 codepoint);
\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. */
// UTF-8 string operations char* U8_alloc(unsigned int size);
/*! 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. */ /*! Deallocates the given string. */
char* U8_alloc(unsigned int size); void U8_free(char* string);
/*! Deallocates the given string. */ /*! Allocates a copy of the given string. */
void U8_free(char* string); char* U8_strdup(const char* string);
/*! Allocates a copy of the given string. */ /*! Returns the number of UTF-8 characters in the given string. */
char* U8_strdup(const char* string); int U8_strlen(const char* string);
/*! Returns the number of UTF-8 characters in the given string. */ /*! Returns the number of bytes in the UTF-8 multibyte character pointed at by 'character'. */
int U8_strlen(const char* string); int U8_charsize(const char* character);
/*! Returns the number of bytes in the UTF-8 multibyte character pointed at by 'character'. */ /*! Copies the source multibyte character into the given buffer without overrunning it. Returns 0 on failure. */
int U8_charsize(const char* character); int U8_charcpy(char* buffer, const char* source, int buffer_size);
/*! Copies the source multibyte character into the given buffer without overrunning it. Returns 0 on failure. */ /*! Returns a pointer to the next UTF-8 character. */
int U8_charcpy(char* buffer, const char* source, int buffer_size); const char* U8_next(const char* string);
/*! Returns a pointer to the next UTF-8 character. */ /*! 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. */
const char* U8_next(const char* string); int U8_strinsert(char* string, int position, const char* source, int max_bytes);
/*! 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. */ /*! Erases the UTF-8 character at the given position, moving the subsequent characters down. */
int U8_strinsert(char* string, int position, const char* source, int max_bytes); void U8_strdel(char* string, int position);
/*! 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);
// Internal settings
/*! Returns the size of the internal buffer which is used for unpacking variadic text data. This buffer is shared by all FC_Fonts. */
/*! 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). */ unsigned int FC_GetBufferSize(void);
void FC_SetLoadingString(FC_Font* font, const char* string);
/*! Changes the size of the internal buffer which is used for unpacking variadic text data. This buffer is shared by all FC_Fonts. */
/*! Returns 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);
unsigned int FC_GetBufferSize(void);
/*! Returns the width of a single horizontal tab in multiples of the width of a space (default: 4) */
/*! Changes 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_GetTabWidth(void);
void FC_SetBufferSize(unsigned int size);
/*! Changes the width of a horizontal tab in multiples of the width of a space (default: 4) */
/*! Returns the width of a single horizontal tab in multiples of the width of a space (default: 4) */ void FC_SetTabWidth(unsigned int width_in_spaces);
unsigned int FC_GetTabWidth(void);
void FC_SetRenderCallback(FC_Rect (*callback)(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale));
/*! 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); FC_Rect FC_DefaultRenderCallback(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale);
void FC_SetRenderCallback(FC_Rect (*callback)(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale));
// Custom caching
FC_Rect FC_DefaultRenderCallback(FC_Image* src, FC_Rect* srcrect, FC_Target* dest, float x, float y, float xscale, float yscale);
/*! Returns the number of cache levels that are active. */
int FC_GetNumCacheLevels(FC_Font* font);
// Custom caching
/*! Returns the cache source texture at the given cache level. */
/*! Returns the number of cache levels that are active. */ FC_Image* FC_GetGlyphCacheLevel(FC_Font* font, int cache_level);
int FC_GetNumCacheLevels(FC_Font* font);
// TODO: Specify ownership of the texture (should be shareable)
/*! Returns the cache source texture at the given cache level. */ /*! Sets a cache source texture for rendering. New cache levels must be sequential. */
FC_Image* FC_GetGlyphCacheLevel(FC_Font* font, int cache_level); Uint8 FC_SetGlyphCacheLevel(FC_Font* font, int cache_level, FC_Image* cache_texture);
// TODO: Specify ownership of the texture (should be shareable) /*! Copies the given surface to the given cache level as a texture. New cache levels must be sequential. */
/*! Sets a cache source texture for rendering. New cache levels must be sequential. */ Uint8 FC_UploadGlyphCache(FC_Font* font, int cache_level, SDL_Surface* data_surface);
Uint8 FC_SetGlyphCacheLevel(FC_Font* font, int cache_level, FC_Image* cache_texture);
/*! Returns the number of codepoints that are stored in the font's glyph data map. */
/*! Copies the given surface to the given cache level as a texture. New cache levels must be sequential. */ unsigned int FC_GetNumCodepoints(FC_Font* font);
Uint8 FC_UploadGlyphCache(FC_Font* font, int cache_level, SDL_Surface* data_surface);
/*! Copies the stored codepoints into the given array. */
void FC_GetCodepoints(FC_Font* font, Uint32* result);
/*! Returns the number of codepoints that are stored in the font's glyph data map. */
unsigned int FC_GetNumCodepoints(FC_Font* font); /*! 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);
/*! Copies the stored codepoints into the given array. */
void FC_GetCodepoints(FC_Font* font, Uint32* result); /*! 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);
/*! 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); // Rendering
FC_Rect FC_Draw(FC_Font* font, FC_Target* dest, float x, float y, const char* formatted_text, ...);
/*! Sets the glyph data for the given codepoint. Duplicates are not checked. Returns a pointer to the stored data. */ FC_Rect FC_DrawAlign(FC_Font* font, FC_Target* dest, float x, float y, FC_AlignEnum align, const char* formatted_text, ...);
FC_GlyphData* FC_SetGlyphData(FC_Font* font, Uint32 codepoint, FC_GlyphData glyph_data); 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, ...);
// Rendering
FC_Rect FC_DrawBox(FC_Font* font, FC_Target* dest, FC_Rect box, const char* formatted_text, ...);
FC_Rect FC_Draw(FC_Font* font, FC_Target* dest, float x, float y, 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_DrawAlign(FC_Font* font, FC_Target* dest, float x, float y, 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_DrawScale(FC_Font* font, FC_Target* dest, float x, float y, 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_DrawColor(FC_Font* font, FC_Target* dest, float x, float y, 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_DrawEffect(FC_Font* font, FC_Target* dest, float x, float y, 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_DrawBox(FC_Font* font, FC_Target* dest, FC_Rect box, 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_DrawBoxAlign(FC_Font* font, FC_Target* dest, FC_Rect box, FC_AlignEnum align, 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_DrawBoxScale(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Scale scale, 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_DrawBoxColor(FC_Font* font, FC_Target* dest, FC_Rect box, SDL_Color color, 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_DrawBoxEffect(FC_Font* font, FC_Target* dest, FC_Rect box, FC_Effect effect, 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, ...);
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, ...); // Getters
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_FilterEnum FC_GetFilterMode(FC_Font* font);
FC_Rect FC_DrawColumnEffect(FC_Font* font, FC_Target* dest, float x, float y, Uint16 width, FC_Effect effect, const char* formatted_text, ...); 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, ...);
// Getters
// Returns a 1-pixel wide box in front of the character in the given position (index)
FC_FilterEnum FC_GetFilterMode(FC_Font* font); FC_Rect FC_GetCharacterOffset(FC_Font* font, Uint16 position_index, int column_width, const char* formatted_text, ...);
Uint16 FC_GetLineHeight(FC_Font* font); Uint16 FC_GetColumnHeight(FC_Font* font, Uint16 width, const char* formatted_text, ...);
Uint16 FC_GetHeight(FC_Font* font, const char* formatted_text, ...);
Uint16 FC_GetWidth(FC_Font* font, const char* formatted_text, ...); int FC_GetAscent(FC_Font* font, const char* formatted_text, ...);
int FC_GetDescent(FC_Font* font, const char* formatted_text, ...);
// Returns a 1-pixel wide box in front of the character in the given position (index) int FC_GetBaseline(FC_Font* font);
FC_Rect FC_GetCharacterOffset(FC_Font* font, Uint16 position_index, int column_width, const char* formatted_text, ...); int FC_GetSpacing(FC_Font* font);
Uint16 FC_GetColumnHeight(FC_Font* font, Uint16 width, const char* formatted_text, ...); int FC_GetLineSpacing(FC_Font* font);
Uint16 FC_GetMaxWidth(FC_Font* font);
int FC_GetAscent(FC_Font* font, const char* formatted_text, ...); SDL_Color FC_GetDefaultColor(FC_Font* font);
int FC_GetDescent(FC_Font* font, const char* formatted_text, ...);
int FC_GetBaseline(FC_Font* font); FC_Rect FC_GetBounds(FC_Font* font, float x, float y, FC_AlignEnum align, FC_Scale scale, const char* formatted_text, ...);
int FC_GetSpacing(FC_Font* font);
int FC_GetLineSpacing(FC_Font* font); Uint8 FC_InRect(float x, float y, FC_Rect input_rect);
Uint16 FC_GetMaxWidth(FC_Font* font); // Given an offset (x,y) from the text draw position (the upper-left corner), returns the character position (UTF-8 index)
SDL_Color FC_GetDefaultColor(FC_Font* font); Uint16 FC_GetPositionFromOffset(FC_Font* font, float x, float y, int column_width, FC_AlignEnum align, const char* formatted_text, ...);
FC_Rect FC_GetBounds(FC_Font* font, float x, float y, FC_AlignEnum align, FC_Scale scale, 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, ...);
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) // Setters
Uint16 FC_GetPositionFromOffset(FC_Font* font, float x, float y, int column_width, FC_AlignEnum align, const char* formatted_text, ...);
void FC_SetFilterMode(FC_Font* font, FC_FilterEnum filter);
// Returns the number of characters in the new wrapped text written into `result`. void FC_SetSpacing(FC_Font* font, int LetterSpacing);
int FC_GetWrappedText(FC_Font* font, char* result, int max_result_size, Uint16 width, const char* formatted_text, ...); void FC_SetLineSpacing(FC_Font* font, int LineSpacing);
void FC_SetDefaultColor(FC_Font* font, SDL_Color color);
// Setters
void FC_SetFilterMode(FC_Font* font, FC_FilterEnum filter); #ifdef __cplusplus
void FC_SetSpacing(FC_Font* font, int LetterSpacing); }
void FC_SetLineSpacing(FC_Font* font, int LineSpacing); #endif
void FC_SetDefaultColor(FC_Font* font, SDL_Color color);
#ifdef __cplusplus #endif
}
#endif
#endif