Some more checks to avoid possible crashes.

This commit is contained in:
StackZ
2020-05-21 05:49:21 +02:00
parent a2c87824ad
commit 4dc2426fa5
7 changed files with 186 additions and 113 deletions
+45 -48
View File
@@ -28,6 +28,7 @@
#include "screenCommon.hpp" #include "screenCommon.hpp"
#include <3ds.h> #include <3ds.h>
#include <unistd.h>
C3D_RenderTarget* Top; C3D_RenderTarget* Top;
C3D_RenderTarget* TopRight; C3D_RenderTarget* TopRight;
@@ -39,20 +40,19 @@ std::stack<std::unique_ptr<Screen>> screens;
bool currentScreen = false; bool currentScreen = false;
// Clear Text. // Clear Text.
void Gui::clearTextBufs(void) void Gui::clearTextBufs(void) { C2D_TextBufClear(TextBuf); }
{
C2D_TextBufClear(TextBuf);
}
// Draw a sprite from the sheet. // Draw a sprite from the sheet.
void Gui::DrawSprite(C2D_SpriteSheet sheet, size_t imgindex, int x, int y, float ScaleX, float ScaleY) void Gui::DrawSprite(C2D_SpriteSheet sheet, size_t imgindex, int x, int y, float ScaleX, float ScaleY) {
{ if (sheet != nullptr) {
C2D_DrawImageAt(C2D_SpriteSheetGetImage(sheet, imgindex), x, y, 0.5f, nullptr, ScaleX, ScaleY); if (C2D_SpriteSheetCount(sheet) >= imgindex) {
C2D_DrawImageAt(C2D_SpriteSheetGetImage(sheet, imgindex), x, y, 0.5f, nullptr, ScaleX, ScaleY);
}
}
} }
// Initialize GUI. // Initialize GUI.
Result Gui::init(void) Result Gui::init(void) {
{
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE); C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
C2D_Init(C2D_DEFAULT_MAX_OBJECTS); C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
C2D_Prepare(); C2D_Prepare();
@@ -67,43 +67,48 @@ Result Gui::init(void)
// Load a Font. // Load a Font.
Result Gui::loadFont(bool sysFont, const char* Path) { Result Gui::loadFont(bool sysFont, const char* Path) {
if (sysFont) Font = C2D_FontLoadSystem(CFG_REGION_USA); if (sysFont) {
else Font = C2D_FontLoad(Path); Font = C2D_FontLoadSystem(CFG_REGION_USA);
} else {
if (access(Path, F_OK) == 0) Font = C2D_FontLoad(Path); // Only load if found.
}
return 0; return 0;
} }
// Unload a Font. // Unload a Font.
Result Gui::unloadFont() { Result Gui::unloadFont() {
C2D_FontFree(Font); if (Font != nullptr) {
C2D_FontFree(Font); // Make sure to only unload if not nullptr.
}
return 0; return 0;
} }
// Load a Sheet. // Load a Sheet.
Result Gui::loadSheet(const char* Path, C2D_SpriteSheet &sheet) { Result Gui::loadSheet(const char* Path, C2D_SpriteSheet &sheet) {
sheet = C2D_SpriteSheetLoad(Path); if (access(Path, F_OK) == 0) sheet = C2D_SpriteSheetLoad(Path); // Only load if found.
return 0; return 0;
} }
// Unload a Sheet. // Unload a Sheet.
Result Gui::unloadSheet(C2D_SpriteSheet &sheet) { Result Gui::unloadSheet(C2D_SpriteSheet &sheet) {
C2D_SpriteSheetFree(sheet); if (sheet != nullptr) C2D_SpriteSheetFree(sheet); // Make sure to only unload if not nullptr.
return 0; return 0;
} }
// Exit the GUI. // Exit the GUI.
void Gui::exit(void) void Gui::exit(void) {
{
C2D_TextBufDelete(TextBuf); C2D_TextBufDelete(TextBuf);
C2D_Fini(); C2D_Fini();
C3D_Fini(); C3D_Fini();
} }
// Draw a Centered String.
void Gui::DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth, int maxHeight) { void Gui::DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth, int maxHeight) {
float lineHeight, widthScale; float lineHeight, widthScale;
lineHeight = Gui::GetStringHeight(size, " "); lineHeight = Gui::GetStringHeight(size, " ");
int line = 0; int line = 0;
while(Text.find('\n') != Text.npos) { while(Text.find('\n') != Text.npos) {
if(maxWidth == 0) { if (maxWidth == 0) {
widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))); widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')));
} else { } else {
widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')))); widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))));
@@ -112,7 +117,7 @@ void Gui::DrawStringCentered(float x, float y, float size, u32 color, std::strin
Text = Text.substr(Text.find('\n')+1); Text = Text.substr(Text.find('\n')+1);
line++; line++;
} }
if(maxWidth == 0) { if (maxWidth == 0) {
widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))); widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')));
} else { } else {
widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')))); widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))));
@@ -127,20 +132,19 @@ void Gui::DrawString(float x, float y, float size, u32 color, std::string Text,
C2D_TextOptimize(&c2d_text); C2D_TextOptimize(&c2d_text);
float heightScale; float heightScale;
if(maxHeight == 0) { if (maxHeight == 0) {
heightScale = size; heightScale = size;
} else { } else {
heightScale = std::min(size, size*(maxHeight/Gui::GetStringHeight(size, Text))); heightScale = std::min(size, size*(maxHeight/Gui::GetStringHeight(size, Text)));
} }
if(maxWidth == 0) { if (maxWidth == 0) {
C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, size, heightScale, color); C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, size, heightScale, color);
} else { } else {
C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, std::min(size, size*(maxWidth/Gui::GetStringWidth(size, Text))), heightScale, color); C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, std::min(size, size*(maxWidth/Gui::GetStringWidth(size, Text))), heightScale, color);
} }
} }
// Get String or Text Width. // Get String or Text Width.
float Gui::GetStringWidth(float size, std::string Text) { float Gui::GetStringWidth(float size, std::string Text) {
float width = 0; float width = 0;
@@ -170,31 +174,25 @@ bool Gui::Draw_Rect(float x, float y, float w, float h, u32 color) {
// Mainloop the GUI. // Mainloop the GUI.
void Gui::mainLoop(u32 hDown, u32 hHeld, touchPosition touch) { void Gui::mainLoop(u32 hDown, u32 hHeld, touchPosition touch) {
screens.top()->Draw(); if (!screens.empty()) {
screens.top()->Logic(hDown, hHeld, touch); screens.top()->Draw();
screens.top()->Logic(hDown, hHeld, touch);
}
} }
// Set the current Screen. // Set the current Screen.
void Gui::setScreen(std::unique_ptr<Screen> screen) void Gui::setScreen(std::unique_ptr<Screen> screen) { screens.push(std::move(screen)); }
{
screens.push(std::move(screen));
}
// Go a Screen back. // Go a Screen back.
void Gui::screenBack() void Gui::screenBack() { if (screens.size() > 0) screens.pop(); }
{
screens.pop();
}
// Select, on which Screen should be drawn. // Select, on which Screen should be drawn.
void Gui::ScreenDraw(C3D_RenderTarget * screen) void Gui::ScreenDraw(C3D_RenderTarget * screen) {
{
C2D_SceneBegin(screen); C2D_SceneBegin(screen);
currentScreen = (screen==Top || screen==TopRight) ? 1 : 0; currentScreen = (screen==Top || screen==TopRight) ? 1 : 0;
} }
void Gui::drawGrid(float xPos, float yPos, float Width, float Height, u32 color) void Gui::drawGrid(float xPos, float yPos, float Width, float Height, u32 color) {
{
static constexpr int w = 1; static constexpr int w = 1;
// BG Color for the Grid. (Transparent.) // BG Color for the Grid. (Transparent.)
C2D_DrawRectSolid(xPos, yPos, 0.5, Width, Height, C2D_Color32(0, 0, 0, 0)); C2D_DrawRectSolid(xPos, yPos, 0.5, Width, Height, C2D_Color32(0, 0, 0, 0));
@@ -206,24 +204,23 @@ void Gui::drawGrid(float xPos, float yPos, float Width, float Height, u32 color)
C2D_DrawRectSolid(xPos, yPos + Height - w, 0.5, Width, w, color); // bottom C2D_DrawRectSolid(xPos, yPos + Height - w, 0.5, Width, w, color); // bottom
} }
void Gui::drawAnimatedSelector(float xPos, float yPos, float Width, float Height, float speed, u32 SelectorColor, u32 colour) void Gui::drawAnimatedSelector(float xPos, float yPos, float Width, float Height, float speed, u32 SelectorColor, u32 bgColor) {
{ static constexpr int w = 2;
static constexpr int w = 2; static float timer = 0.0f;
static float timer = 0.0f; float highlight_multiplier = fmax(0.0, fabs(fmod(timer, 1.0) - 0.5) / 0.5);
float highlight_multiplier = fmax(0.0, fabs(fmod(timer, 1.0) - 0.5) / 0.5); u8 r = SelectorColor & 0xFF;
u8 r = SelectorColor & 0xFF; u8 g = (SelectorColor >> 8) & 0xFF;
u8 g = (SelectorColor >> 8) & 0xFF; u8 b = (SelectorColor >> 16) & 0xFF;
u8 b = (SelectorColor >> 16) & 0xFF; u32 color = C2D_Color32(r + (255 - r) * highlight_multiplier, g + (255 - g) * highlight_multiplier, b + (255 - b) * highlight_multiplier, 255);
u32 color = C2D_Color32(r + (255 - r) * highlight_multiplier, g + (255 - g) * highlight_multiplier, b + (255 - b) * highlight_multiplier, 255);
// BG Color for the Selector. // BG Color for the Selector.
C2D_DrawRectSolid(xPos, yPos, 0.5, Width, Height, colour); C2D_DrawRectSolid(xPos, yPos, 0.5, Width, Height, bgColor);
// Animated Selector part. // Animated Selector part.
C2D_DrawRectSolid(xPos, yPos, 0.5, Width, w, color); // top C2D_DrawRectSolid(xPos, yPos, 0.5, Width, w, color); // top
C2D_DrawRectSolid(xPos, yPos + w, 0.5, w, Height - 2 * w, color); // left C2D_DrawRectSolid(xPos, yPos + w, 0.5, w, Height - 2 * w, color); // left
C2D_DrawRectSolid(xPos + Width - w, yPos + w, 0.5, w, Height - 2 * w, color); // right C2D_DrawRectSolid(xPos + Width - w, yPos + w, 0.5, w, Height - 2 * w, color); // right
C2D_DrawRectSolid(xPos, yPos + Height - w, 0.5, Width, w, color); // bottom C2D_DrawRectSolid(xPos, yPos + Height - w, 0.5, Width, w, color); // bottom
timer += speed; // Speed of the animation. Example : .030f / .030 timer += speed; // Speed of the animation. Example : .030f / .030
} }
+96 -23
View File
@@ -24,8 +24,8 @@
* reasonable ways as different from the original version. * reasonable ways as different from the original version.
*/ */
#ifndef GUI_HPP #ifndef _UNIVERSAL_CORE_GUI_HPP
#define GUI_HPP #define _UNIVERSAL_CORE_GUI_HPP
#include "screen.hpp" #include "screen.hpp"
@@ -36,65 +36,138 @@
namespace Gui namespace Gui
{ {
// Clear Text Buffer. // Clear the Text Buffer.
void clearTextBufs(void); void clearTextBufs(void);
// Draw a sprite from a sheet. /* Draw a sprite from a SpriteSheet.
* sheet: The SpriteSheet which should be used.
* imgIndex: The index of the sprite from the sheet which should be drawn.
* x: The X Position where the sprite should be drawn.
* y: The Y Position where the sprite should be drawn.
* ScaleX: The X-Scale for the sprite. (Optional!)
* ScaleY: The Y-Scale for the sprite. (Optional!)
*/
void DrawSprite(C2D_SpriteSheet sheet, size_t imgindex, int x, int y, float ScaleX = 1, float ScaleY = 1); void DrawSprite(C2D_SpriteSheet sheet, size_t imgindex, int x, int y, float ScaleX = 1, float ScaleY = 1);
// Initialize the GUI with Citro2D & Citro3D and initialize the Textbuffer. // Initialize the GUI with Citro2D & Citro3D and initialize the Textbuffer. (call this when initializing.)
Result init(void); Result init(void);
// Load a Font. (bcfnt) /* Load a Font. (BCFNT)
* sysFont: Whether the system font should be loaded instead of custom Font.
* Path: Path to the BCFNT file.
* if you're unsure, just call 'Gui::loadFont();' and it will load the system font.
*/
Result loadFont(bool sysFont = true, const char * Path = ""); Result loadFont(bool sysFont = true, const char * Path = "");
// Unload a Font. (bcfnt) /* Unload a Font. (BCFNT)
* Only use this if a custom Font has been loaded.
*/
Result unloadFont(); Result unloadFont();
// Load a spritesheet. /* Load a spritesheet.
* Path: Path to the SpriteSheet file. (T3X)
* sheet: Reference to the C2D_SpriteSheet declaration.
*/
Result loadSheet(const char* Path, C2D_SpriteSheet &sheet); Result loadSheet(const char* Path, C2D_SpriteSheet &sheet);
// Unload a spritesheet. /* Unload a spritesheet.
* sheet: Reference to the C2D_SpriteSheet which should be free'd.
*/
Result unloadSheet(C2D_SpriteSheet &sheet); Result unloadSheet(C2D_SpriteSheet &sheet);
// Exit the GUI. // Exit the GUI. (Call this at exit.)
void exit(void); void exit(void);
// Draw a centered String. /* Draws a centered String.
* x: The X Offset from center. (Center: 200 px on top, 160 px on Bottom.)
* y: The Y Position of the Text.
* size: The size of the Text.
* color: The Color of the Text.
* Text: The Text which should be displayed.
* maxWidth: The maxWidth for the Text. (Optional!)
* maxHeight: The maxHeight of the Text. (Optional!)
*/
void DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0); void DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0);
// Draw a String. /* Draws a String.
* x: The X Position where the Text should be drawn.
* y: The Y Position where the Text should be drawn.
* size: The size of the Text.
* color: The Color of the Text.
* Text: The Text which should be displayed.
* maxWidth: The maxWidth for the Text. (Optional!)
* maxHeight: The maxHeight of the Text. (Optional!)
*/
void DrawString(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0); void DrawString(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0);
// Get the width of a String. /* Get the width of a String.
* size: The size of the Text.
* Text: The Text where the width should be getted from.
*/
float GetStringWidth(float size, std::string Text); float GetStringWidth(float size, std::string Text);
// Get the size of a String. /* Get the size of a String.
* size: The size of the Text.
* width: The width of the Text.
* height: The height of the Text.
* Text: The Text where the size should be getted from.
*/
void GetStringSize(float size, float *width, float *height, std::string Text); void GetStringSize(float size, float *width, float *height, std::string Text);
// Get the height of a String. /* Get the height of a String.
* size: The size of the Text.
* Text: The Text where the height should be getted from.
*/
float GetStringHeight(float size, std::string Text); float GetStringHeight(float size, std::string Text);
// Draw a Rectangle. /* Draw a Rectangle.
* x: X Position of the Rectangle.
* y: Y Position of the Rectangle.
* w: The width of the rectangle.
* h: The height of the rectangle.
* color: The color of the rectangle.
*/
bool Draw_Rect(float x, float y, float w, float h, u32 color); bool Draw_Rect(float x, float y, float w, float h, u32 color);
// Mainloop the GUI / Screen part. /* Used for the mainLoop to display the screens. (Optional!)
* hDown: the hidKeysDown() variable.
* hHeld: the HidKeysHeld() variable.
* touch: The TouchPosition variable.
*/
void mainLoop(u32 hDown, u32 hHeld, touchPosition touch); void mainLoop(u32 hDown, u32 hHeld, touchPosition touch);
// Set a specific Screen. /* Set a specific Screen.
* screen: unique_ptr of the screen. (Optional by using the screen class.)
*/
void setScreen(std::unique_ptr<Screen> screen); void setScreen(std::unique_ptr<Screen> screen);
// Go a Screen back. // Go a Screen back. (Optional by using the screen class.)
void screenBack(); void screenBack();
// Set on which screen to draw. /* Set on which screen to draw.
* screen: The render target. (Targets are inside the screenCommon.hpp file.)
*/
void ScreenDraw(C3D_RenderTarget * screen); void ScreenDraw(C3D_RenderTarget * screen);
// Draw a grid. /* Draws a grid.
* xPos: X Position of the grid.
* yPos: Y Position of the grid.
* Width: Width of the grid.
* Height: Height of the grid.
* color: Color of the grid.
*/
void drawGrid(float xPos, float yPos, float Width, float Height, u32 color); void drawGrid(float xPos, float yPos, float Width, float Height, u32 color);
// Draw an animated selector. /* Draws an animated selector.
void drawAnimatedSelector(float xPos, float yPos, float Width, float Height, float speed, u32 SelectorColor, u32 colour); * xPos: X Position of the selector.
* yPos: Y Position of the Selector.
* Width: Width of the Selector.
* Height: Height of the Selector.
* speed: The speed of the animation. (Use .030 or something by default.)
* SelectorColor: The Color of the Selector.
* bgColor: The Color from the middle of the Selector. (Optional! It's transparent by default.)
*/
void drawAnimatedSelector(float xPos, float yPos, float Width, float Height, float speed, u32 SelectorColor, u32 bgColor = C2D_Color32(0, 0, 0, 0));
} }
#endif #endif
+16 -30
View File
@@ -27,49 +27,35 @@
#include "hid.hpp" #include "hid.hpp"
HID::HID(size_t EntryPerPage, size_t EntryAmount) { HID::HID(size_t EntryPerPage, size_t EntryAmount) {
maxEntries = EntryAmount; this->maxEntries = EntryAmount;
pageEntry = EntryPerPage; this->pageEntry = EntryPerPage;
} }
size_t HID::getIndex() { size_t HID::getIndex() { return this->currentEntry; }
return currentEntry;
}
size_t HID::getMaxEntries() { size_t HID::getMaxEntries() { return this->maxEntries; }
return maxEntries;
}
void HID::nextEntry() { void HID::nextEntry() { if (this->currentEntry < this->maxEntries-1) this->currentEntry++; }
if (currentEntry < maxEntries-1) {
currentEntry++;
}
}
void HID::lastEntry() { void HID::lastEntry() { if (this->currentEntry > 0) this->currentEntry--; }
if (currentEntry > 0) {
currentEntry--;
}
}
void HID::nextPage() { void HID::nextPage() {
// Only go to the next page, if the next Page doesn't reach the maxEntries. // Only go to the next page, if the next Page doesn't reach the maxEntries.
if (currentEntry + pageEntry < maxEntries - 1) { if (this->currentEntry + this->pageEntry < this->maxEntries - 1) {
currentPage++; this->currentPage++;
currentEntry += pageEntry; this->currentEntry += this->pageEntry;
// If the first index of the page is smaller than maxEntries -> Go to the next page. // If the first index of the page is smaller than maxEntries -> Go to the next page.
} else if (currentPage * pageEntry < maxEntries - 1) { } else if (this->currentPage * this->pageEntry < this->maxEntries - 1) {
currentPage++; this->currentPage++;
currentEntry = currentPage * pageEntry; this->currentEntry = this->currentPage * this->pageEntry;
} }
} }
size_t HID::getPage() { size_t HID::getPage() { return this->currentPage; }
return currentPage;
}
void HID::prevPage() { void HID::prevPage() {
if (currentPage > 0) { if (this->currentPage > 0) {
currentPage--; this->currentPage--;
currentEntry -= pageEntry; this->currentEntry -= this->pageEntry;
} }
} }
+23 -6
View File
@@ -24,29 +24,46 @@
* reasonable ways as different from the original version. * reasonable ways as different from the original version.
*/ */
#ifndef HID_HPP #ifndef _UNIVERSAL_CORE_HID_HPP
#define HID_HPP #define _UNIVERSAL_CORE_HID_HPP
#include <cstddef> #include <cstddef>
class HID class HID
{ {
public: public:
// The parameters to initialize this class.
/* The parameters to initialize this class.
* EntryPage: The entries per page.
* EntryAmount: The amount of entries.
*/
HID(size_t EntryPerPage, size_t EntryAmount); HID(size_t EntryPerPage, size_t EntryAmount);
// get the current index.
/* Get the current index.
* returns the current index as a size_t.
*/
size_t getIndex(); size_t getIndex();
// get the max amount of the index.
/* Get the max amount of the entries per page.
* returns the max amount of entries per page as a size_t.
*/
size_t getMaxEntries(); size_t getMaxEntries();
// Go to the next Entry, if maxEntries is not reached. // Go to the next Entry, if maxEntries is not reached.
void nextEntry(); void nextEntry();
// Go to the last Entry, if 0 is not reached. // Go to the last Entry, if 0 is not reached.
void lastEntry(); void lastEntry();
// Go to the next Page. // Go to the next Page.
void nextPage(); void nextPage();
// Go to the previous Page. // Go to the previous Page.
void prevPage(); void prevPage();
// Get the current page.
/* Get the current page.
* returns the current page as a size_t.
*/
size_t getPage(); size_t getPage();
private: private:
+2 -2
View File
@@ -24,8 +24,8 @@
* reasonable ways as different from the original version. * reasonable ways as different from the original version.
*/ */
#ifndef SCREEN_HPP #ifndef _UNIVERSAL_CORE_SCREEN_HPP
#define SCREEN_HPP #define _UNIVERSAL_CORE_SCREEN_HPP
#include <3ds.h> #include <3ds.h>
#include <memory> #include <memory>
+2 -2
View File
@@ -24,8 +24,8 @@
* reasonable ways as different from the original version. * reasonable ways as different from the original version.
*/ */
#ifndef SCREENCOMMON_HPP #ifndef _UNIVERSAL_CORE_SCREENCOMMON_HPP
#define SCREENCOMMON_HPP #define _UNIVERSAL_CORE_SCREENCOMMON_HPP
#include "gui.hpp" #include "gui.hpp"
#include "structs.hpp" #include "structs.hpp"
+2 -2
View File
@@ -24,8 +24,8 @@
* reasonable ways as different from the original version. * reasonable ways as different from the original version.
*/ */
#ifndef STRUCTS_HPP #ifndef _UNIVERSAL_CORE_STRUCTS_HPP
#define STRUCTS_HPP #define _UNIVERSAL_CORE_STRUCTS_HPP
#include <string> #include <string>