Add spritesheet and change image format

This commit is contained in:
Pk11
2021-08-26 11:12:27 -05:00
parent ad6e5c4d2d
commit a1ac060ce3
8 changed files with 336 additions and 162 deletions
+2 -2
View File
@@ -60,8 +60,8 @@ private:
static u8 textBuf[2][256 * 192];
#endif
u8 tileWidth, tileHeight;
u16 tileSize;
u8 tileWidth = 0, tileHeight = 0;
u16 tileSize = 0;
u16 questionMark = 0;
std::vector<u8> fontTiles;
std::vector<u8> fontWidths;
+8 -8
View File
@@ -28,9 +28,9 @@
#define _UNIVERSAL_CORE_GUI_HPP
#include "font.hpp"
#include "image.hpp"
#include "screen.hpp"
#include "sprite.hpp"
#include "spritesheet.hpp"
#include <nds/ndstypes.h>
@@ -66,23 +66,23 @@ namespace Gui {
void clearScreen(bool top);
/*
Load an image (TODO: spritesheets?).
Load an a spritesheet
Path: Path to the image file. (GFX)
sheet: Reference to the Image declaration.
Path: Path to the spritesheet file. (TDX)
sheet: Reference to the Spritesheet declaration.
*/
bool loadSheet(const char *Path, Image &image);
bool loadSheet(const char *Path, Spritesheet &image);
/*
Draw an image.
image: The image which should be used.
imgIndex: For compatibility with 3DS, unused.
image: The spritesheet which should be used.
imgIndex: Index within the spritesheet.
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(Image image, size_t imgindex, int x, int y, float ScaleX = 1.0f, float ScaleY = 1.0f);
void DrawSprite(Spritesheet &sheet, size_t imgindex, int x, int y, float ScaleX = 1.0f, float ScaleY = 1.0f);
/*
Initialize the GUI.
+28 -36
View File
@@ -33,46 +33,52 @@
class Image {
private:
u16 _width;
u16 _height;
std::vector<u8> _bitmap;
std::vector<u16> _palette;
u16 _palOfs;
u32 _width = 0;
u32 _height = 0;
u8 _paletteStart = 0;
std::vector<u8> _bitmap = {};
std::vector<u16> _palette = {};
static void decompressGrf(void *dst, const void *src);
void load(const u8 *grf, u8 paletteStart);
public:
/**
* Image drawing class
* @brief Image drawing class
* @param paths The paths to try load the image from, from highest to lowest priority
* @param paletteStart Where to start the palette in VRAM
*/
Image(const std::vector<std::string> &paths);
Image(const std::vector<std::string> &paths, u8 paletteStart);
/**
* @brief Text printing class
* @brief Image drawing class
* @param path The path to load the image from
* @param paletteStart Where to start the palette in VRAM
*/
Image(const std::string &path) : Image(std::vector<std::string>({path})) {};
Image(const std::string &path, u8 paletteStart) : Image(std::vector<std::string>({path}), paletteStart) {};
/**
* @brief Image drawing class
* @param file The file to load the image from, seeked to the '.GFX' magic
* @param grf Pointer to a GRF in memory
* @param paletteStart Where to start the palette in VRAM
*/
Image(FILE *file);
Image(const u8 *grf, u8 paletteStart);
Image() {};
~Image(void) {};
u16 width(void) const { return _width; }
u16 height(void) const { return _height; }
u8 paletteStart(void) const { return _paletteStart; }
std::vector<u8> bitmap(void) const { return _bitmap; }
std::vector<u16> palette(void) const { return _palette; }
u16 palOfs(void) const { return _palOfs; }
void changePaletteStart(u8 paletteStart);
/**
* @brief Draws the image to a background layer, faster but without alpha, scaling, or palette offsetting
* @param x The X position to draw at
* @param y The Y position to draw at
* @param copyPal (Optional) Whether to copy the image's palette into palette VRAM
* @brief Copies the palette into VRAM
* @param paletteStart Where to start the palette in VRAM
*/
void draw(int x, int y, bool copyPal = true);
void copyPalette(int paletteStart);
/**
* @brief Draws the image to a background layer, slower but can skip alpha, scale, and offset the palette
@@ -80,22 +86,9 @@ public:
* @param y The Y position to draw at
* @param scaleX (Optional) The scale for the X axis
* @param scaleY (Optional) The scale for the Y axis
* @param paletteOffset (Optional) How much to offset the palette by
* @param copyPal (Optional) Whether to copy the image's palette into palette VRAM
* @param skipAlpha (Optional) Whether to skip transparent pixels, draws faster when disabled at 1.0 scale
*/
void drawSpecial(int x, int y, float scaleX = 1.0f, float scaleY = 1.0f, int paletteOffset = 0, bool copyPal = true);
/**
* @brief Draws a segment of an image to a background layer, faster but overwrites alpha and no scaling or palette offsetting
* @param x The X position to draw at
* @param y The Y position to draw at
* @param imageX The X position in the image to draw from
* @param imageY The Y position in the image to draw from
* @param w The width to draw
* @param h The height to draw
* @param copyPal (Optional) Whether to copy the image's palette into palette VRAM
*/
void drawSegment(int x, int y, int imageX, int imageY, int w, int h, bool copyPal = true);
void draw(int x, int y, float scaleX = 1.0f, float scaleY = 1.0f, bool skipAlpha = true);
/**
* @brief Draws a segment of an image to a background layer, slower but can skip alpha, scale, and offset the palette
@@ -107,10 +100,9 @@ public:
* @param h The height to draw
* @param scaleX (Optional) The scale for the X axis
* @param scaleY (Optional) The scale for the Y axis
* @param paletteOffset (Optional) How much to offset the palette by
* @param copyPal (Optional) Whether to copy the image's palette into palette VRAM
* @param skipAlpha (Optional) Whether to skip transparent pixels, draws faster when disabled at 1.0 scale
*/
void drawSegmentSpecial(int x, int y, int imageX, int imageY, int w, int h, float scaleX = 1.0f, float scaleY = 1.0f, int paletteOffset = 0, bool copyPal = true);
void drawSegment(int x, int y, int imageX, int imageY, int w, int h, float scaleX = 1.0f, float scaleY = 1.0f, bool skipAlpha = true);
};
+66
View File
@@ -0,0 +1,66 @@
/*
* This file is part of Universal-Core
* Copyright (C) 2021 Universal-Team
*
* 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/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#ifndef _UNIVERSAL_CORE_SPRITESHEET_HPP
#define _UNIVERSAL_CORE_SPRITESHEET_HPP
#include "image.hpp"
class Spritesheet {
private:
static Image _blank;
std::vector<Image *> _images;
public:
/**
* @brief Image group class
* @param paths The paths to try load the spritesheet from, from highest to lowest priority
* @param indexes The image indexes to load, if blank all images will be loaded
*/
Spritesheet(const std::vector<std::string> &paths, const std::vector<u32> indexes = {});
/**
* @brief Image group class
* @param path The path to load the spritesheet from
* @param indexes The image indexes to load, if blank all images will be loaded
*/
Spritesheet(const std::string &path, const std::vector<u32> indexes = {}) : Spritesheet(std::vector<std::string>({path}), indexes) {};
~Spritesheet(void);
/**
* @brief Returns the number of images in the spritesheet
*/
size_t size(void) { return _images.size(); }
/**
* @brief Returns the Image at index
* @param index The Image to get
*/
Image &operator[](size_t index);
};
#endif