2020-08-03 22:10:06 +00:00
|
|
|
#include <cstring>
|
2021-09-12 21:08:02 +00:00
|
|
|
#include <string>
|
2022-08-01 18:48:39 +00:00
|
|
|
#include <memory>
|
2022-08-06 15:33:30 +00:00
|
|
|
#include <sys/stat.h>
|
2020-08-03 22:10:06 +00:00
|
|
|
|
2020-08-30 03:35:57 +00:00
|
|
|
// BMP
|
2022-08-01 18:48:39 +00:00
|
|
|
#include "libnsbmp.h"
|
2020-08-30 03:35:57 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
// GIF
|
|
|
|
#include <gif_lib.h>
|
2020-10-22 16:17:10 +00:00
|
|
|
|
2020-08-21 20:28:15 +00:00
|
|
|
// JPEG
|
|
|
|
#include <turbojpeg.h>
|
|
|
|
|
2020-08-03 22:10:06 +00:00
|
|
|
// STB
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
2020-08-30 03:35:57 +00:00
|
|
|
#define STBI_NO_BMP
|
2020-08-03 22:10:06 +00:00
|
|
|
#define STBI_NO_HDR
|
2020-08-21 20:28:15 +00:00
|
|
|
#define STBI_NO_JPEG
|
2020-08-03 22:10:06 +00:00
|
|
|
#define STBI_NO_PIC
|
2020-08-21 20:28:15 +00:00
|
|
|
#define STBI_NO_PNG
|
2020-08-03 22:10:06 +00:00
|
|
|
#define STBI_ONLY_GIF
|
|
|
|
#define STBI_ONLY_PNM
|
|
|
|
#define STBI_ONLY_PSD
|
|
|
|
#define STBI_ONLY_TGA
|
2022-08-01 18:48:39 +00:00
|
|
|
#include "stb_image.h"
|
2020-08-03 22:10:06 +00:00
|
|
|
|
2020-08-21 20:28:15 +00:00
|
|
|
// PNG
|
|
|
|
#include <png.h>
|
|
|
|
|
|
|
|
// WEBP
|
|
|
|
#include <webp/decode.h>
|
|
|
|
|
2020-08-03 22:10:06 +00:00
|
|
|
#include <switch.h>
|
|
|
|
|
2022-08-01 16:01:10 +00:00
|
|
|
#include "fs.hpp"
|
|
|
|
#include "gui.hpp"
|
|
|
|
#include "imgui_impl_switch.hpp"
|
|
|
|
#include "log.hpp"
|
|
|
|
#include "textures.hpp"
|
2022-08-02 03:37:47 +00:00
|
|
|
#include "windows.hpp"
|
2020-08-03 22:10:06 +00:00
|
|
|
|
2020-08-30 03:35:57 +00:00
|
|
|
#define BYTES_PER_PIXEL 4
|
|
|
|
#define MAX_IMAGE_BYTES (48 * 1024 * 1024)
|
|
|
|
|
2022-08-02 03:24:49 +00:00
|
|
|
std::vector<Tex> file_icons;
|
|
|
|
Tex folder_icon, check_icon, uncheck_icon;
|
2020-08-03 22:10:06 +00:00
|
|
|
|
2020-08-30 03:35:57 +00:00
|
|
|
namespace BMP {
|
2021-09-12 23:54:27 +00:00
|
|
|
static void *bitmap_create(int width, int height, [[maybe_unused]] unsigned int state) {
|
|
|
|
/* ensure a stupidly large (>50Megs or so) bitmap is not created */
|
|
|
|
if ((static_cast<long long>(width) * static_cast<long long>(height)) > (MAX_IMAGE_BYTES/BYTES_PER_PIXEL))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return std::calloc(width * height, BYTES_PER_PIXEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char *bitmap_get_buffer(void *bitmap) {
|
|
|
|
assert(bitmap);
|
|
|
|
return static_cast<unsigned char *>(bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t bitmap_get_bpp([[maybe_unused]] void *bitmap) {
|
|
|
|
return BYTES_PER_PIXEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bitmap_destroy(void *bitmap) {
|
|
|
|
assert(bitmap);
|
|
|
|
std::free(bitmap);
|
|
|
|
}
|
2020-08-30 03:35:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 22:10:06 +00:00
|
|
|
namespace Textures {
|
2021-09-12 23:54:27 +00:00
|
|
|
typedef enum ImageType {
|
|
|
|
ImageTypeBMP,
|
|
|
|
ImageTypeGIF,
|
|
|
|
ImageTypeJPEG,
|
|
|
|
ImageTypePNG,
|
|
|
|
ImageTypeWEBP,
|
|
|
|
ImageTypeOther
|
|
|
|
} ImageType;
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
static bool ReadFile(const std::string &path, unsigned char **buffer, std::size_t &size) {
|
|
|
|
FILE *file = fopen(path.c_str(), "rb");
|
|
|
|
if (!file) {
|
|
|
|
Log::Error("Textures::ReadFile (%s) failed to open file.\n", path.c_str());
|
|
|
|
return false;
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
2022-08-06 15:33:30 +00:00
|
|
|
|
|
|
|
struct stat file_stat = { 0 };
|
|
|
|
if (stat(path.c_str(), std::addressof(file_stat)) != 0) {
|
|
|
|
Log::Error("Textures::ReadFile (%s) failed to get file size.\n", path.c_str());
|
|
|
|
return false;
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
size = file_stat.st_size;
|
|
|
|
*buffer = new unsigned char[size + 1];
|
|
|
|
std::size_t bytes_read = fread(*buffer, sizeof(unsigned char), size, file);
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
if (bytes_read != size) {
|
|
|
|
Log::Error("Textures::ReadFile (%s) failed to read file.\n", path.c_str());
|
|
|
|
fclose(file);
|
|
|
|
return false;
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
fclose(file);
|
|
|
|
return true;
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
static bool Create(unsigned char *data, GLint format, Tex &texture) {
|
2022-08-02 19:20:26 +00:00
|
|
|
glGenTextures(1, std::addressof(texture.id));
|
2022-07-30 20:38:14 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture.id);
|
2021-09-12 21:08:02 +00:00
|
|
|
|
2022-07-30 20:38:14 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // This is required on WebGL for non power-of-two textures
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Same
|
2021-09-12 21:08:02 +00:00
|
|
|
|
2022-07-30 20:38:14 +00:00
|
|
|
// Upload pixels into texture
|
|
|
|
#if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__)
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
#endif
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, texture.width, texture.height, 0, format, GL_UNSIGNED_BYTE, data);
|
2021-09-12 23:54:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
static bool LoadImagePNG(const std::string &path, Tex &texture) {
|
2021-09-12 23:54:27 +00:00
|
|
|
bool ret = false;
|
|
|
|
png_image image;
|
2022-08-02 19:20:26 +00:00
|
|
|
std::memset(std::addressof(image), 0, (sizeof image));
|
2021-09-12 23:54:27 +00:00
|
|
|
image.version = PNG_IMAGE_VERSION;
|
|
|
|
|
2022-08-02 19:20:26 +00:00
|
|
|
if (png_image_begin_read_from_file(std::addressof(image), path.c_str()) != 0) {
|
2021-09-12 23:54:27 +00:00
|
|
|
png_bytep buffer;
|
|
|
|
image.format = PNG_FORMAT_RGBA;
|
|
|
|
buffer = new png_byte[PNG_IMAGE_SIZE(image)];
|
|
|
|
|
2022-08-02 19:20:26 +00:00
|
|
|
if (buffer != nullptr && png_image_finish_read(std::addressof(image), nullptr, buffer, 0, nullptr) != 0) {
|
2021-09-12 23:54:27 +00:00
|
|
|
texture.width = image.width;
|
|
|
|
texture.height = image.height;
|
2022-08-01 18:48:39 +00:00
|
|
|
ret = Textures::Create(buffer, GL_RGBA, texture);
|
2021-09-12 23:54:27 +00:00
|
|
|
delete[] buffer;
|
2022-08-02 19:20:26 +00:00
|
|
|
png_image_free(std::addressof(image));
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (buffer == nullptr)
|
2022-08-02 19:20:26 +00:00
|
|
|
png_image_free(std::addressof(image));
|
2021-09-12 23:54:27 +00:00
|
|
|
else
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
static bool LoadImageBMP(unsigned char **data, std::size_t &size, Tex &texture) {
|
2021-09-12 23:54:27 +00:00
|
|
|
bmp_bitmap_callback_vt bitmap_callbacks = {
|
|
|
|
BMP::bitmap_create,
|
|
|
|
BMP::bitmap_destroy,
|
|
|
|
BMP::bitmap_get_buffer,
|
|
|
|
BMP::bitmap_get_bpp
|
|
|
|
};
|
|
|
|
|
|
|
|
bmp_result code = BMP_OK;
|
|
|
|
bmp_image bmp;
|
2022-08-02 19:20:26 +00:00
|
|
|
bmp_create(std::addressof(bmp), std::addressof(bitmap_callbacks));
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-02 19:20:26 +00:00
|
|
|
code = bmp_analyse(std::addressof(bmp), size, *data);
|
2021-09-12 23:54:27 +00:00
|
|
|
if (code != BMP_OK) {
|
2022-08-02 19:20:26 +00:00
|
|
|
bmp_finalise(std::addressof(bmp));
|
2021-09-12 23:54:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-02 19:20:26 +00:00
|
|
|
code = bmp_decode(std::addressof(bmp));
|
2021-09-12 23:54:27 +00:00
|
|
|
if (code != BMP_OK) {
|
|
|
|
if ((code != BMP_INSUFFICIENT_DATA) && (code != BMP_DATA_ERROR)) {
|
2022-08-02 19:20:26 +00:00
|
|
|
bmp_finalise(std::addressof(bmp));
|
2021-09-12 23:54:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip if the decoded image would be ridiculously large */
|
|
|
|
if ((bmp.width * bmp.height) > 200000) {
|
2022-08-02 19:20:26 +00:00
|
|
|
bmp_finalise(std::addressof(bmp));
|
2021-09-12 23:54:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
texture.width = bmp.width;
|
|
|
|
texture.height = bmp.height;
|
2022-08-01 18:50:57 +00:00
|
|
|
bool ret = Textures::Create(static_cast<unsigned char *>(bmp.bitmap), GL_RGBA, texture);
|
2022-08-02 19:20:26 +00:00
|
|
|
bmp_finalise(std::addressof(bmp));
|
2021-09-12 23:54:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
static bool LoadImageGIF(const std::string &path, std::vector<Tex> &textures) {
|
2021-09-12 23:54:27 +00:00
|
|
|
bool ret = false;
|
2022-08-01 18:48:39 +00:00
|
|
|
int error = 0;
|
2022-08-02 19:20:26 +00:00
|
|
|
GifFileType *gif = DGifOpenFileName(path.c_str(), std::addressof(error));
|
2022-08-01 18:48:39 +00:00
|
|
|
|
|
|
|
if (!gif) {
|
|
|
|
Log::Error("DGifOpenFileName failed: %d\n", error);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DGifSlurp(gif) != GIF_OK) {
|
|
|
|
Log::Error("DGifSlurp failed: %d\n", gif->Error);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gif->ImageCount <= 0) {
|
|
|
|
Log::Error("Gif does not contain any images.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-12 21:08:02 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
textures.resize(gif->ImageCount);
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
// seiken's example code from:
|
|
|
|
// https://forums.somethingawful.com/showthread.php?threadid=2773485&userid=0&perpage=40&pagenumber=487#post465199820
|
|
|
|
textures[0].width = gif->SWidth;
|
|
|
|
textures[0].height = gif->SHeight;
|
|
|
|
std::unique_ptr<u32[]> pixels(new u32[textures[0].width * textures[0].height]);
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
for (int i = 0; i < textures[0].width * textures[0].height; ++i)
|
|
|
|
pixels[i] = gif->SBackGroundColor;
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
for (int i = 0; i < gif->ImageCount; ++i) {
|
|
|
|
const SavedImage &frame = gif->SavedImages[i];
|
|
|
|
bool transparency = false;
|
|
|
|
unsigned char transparency_byte = 0;
|
|
|
|
|
|
|
|
// Delay time in hundredths of a second.
|
|
|
|
int delay_time = 1;
|
|
|
|
for (int j = 0; j < frame.ExtensionBlockCount; ++j) {
|
|
|
|
const ExtensionBlock &block = frame.ExtensionBlocks[j];
|
|
|
|
|
|
|
|
if (block.Function != GRAPHICS_EXT_FUNC_CODE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Here's the metadata for this frame.
|
|
|
|
char dispose = (block.Bytes[0] >> 2) & 7;
|
|
|
|
transparency = block.Bytes[0] & 1;
|
|
|
|
delay_time = block.Bytes[1] + (block.Bytes[2] << 8);
|
|
|
|
transparency_byte = block.Bytes[3];
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
if (dispose == 2) {
|
|
|
|
// Clear the canvas.
|
|
|
|
for (int k = 0; k < textures[0].width * textures[0].height; ++k)
|
|
|
|
pixels[k] = gif->SBackGroundColor;
|
|
|
|
}
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
2022-08-01 18:48:39 +00:00
|
|
|
|
|
|
|
// Colour map for this frame.
|
|
|
|
ColorMapObject *map = frame.ImageDesc.ColorMap ? frame.ImageDesc.ColorMap : gif->SColorMap;
|
|
|
|
|
|
|
|
// Region this frame draws to.
|
|
|
|
int fw = frame.ImageDesc.Width;
|
|
|
|
int fh = frame.ImageDesc.Height;
|
|
|
|
int fl = frame.ImageDesc.Left;
|
|
|
|
int ft = frame.ImageDesc.Top;
|
|
|
|
|
|
|
|
for (int y = 0; y < std::min(textures[0].height, fh); ++y) {
|
|
|
|
for (int x = 0; x < std::min(textures[0].width, fw); ++x) {
|
|
|
|
unsigned char byte = frame.RasterBits[x + y * fw];
|
|
|
|
|
|
|
|
// Transparent pixel.
|
|
|
|
if (transparency && byte == transparency_byte)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Draw to canvas.
|
|
|
|
const GifColorType &c = map->Colors[byte];
|
|
|
|
pixels[fl + x + (ft + y) * textures[0].width] = c.Red | (c.Green << 8) | (c.Blue << 16) | (0xff << 24);
|
|
|
|
}
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
2022-08-01 18:48:39 +00:00
|
|
|
|
|
|
|
textures[i].delay = delay_time * 10000000;
|
|
|
|
textures[i].width = textures[0].width;
|
|
|
|
textures[i].height = textures[0].height;
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
// Here's the actual frame, pixels.get() is now a pointer to the 32-bit RGBA
|
|
|
|
// data for this frame you might expect.
|
|
|
|
ret = Textures::Create(reinterpret_cast<unsigned char*>(pixels.get()), GL_RGBA, textures[i]);
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:20:26 +00:00
|
|
|
if (DGifCloseFile(gif, std::addressof(error)) != GIF_OK) {
|
2022-08-01 18:48:39 +00:00
|
|
|
Log::Error("DGifCloseFile failed: %d\n", error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
static bool LoadImageJPEG(unsigned char **data, std::size_t &size, Tex &texture) {
|
2021-09-12 23:54:27 +00:00
|
|
|
tjhandle jpeg = tjInitDecompress();
|
|
|
|
int jpegsubsamp = 0;
|
2022-08-02 19:20:26 +00:00
|
|
|
tjDecompressHeader2(jpeg, *data, size, std::addressof(texture.width), std::addressof(texture.height), std::addressof(jpegsubsamp));
|
2022-08-06 15:33:30 +00:00
|
|
|
unsigned char *buffer = new unsigned char[texture.width * texture.height * 4];
|
|
|
|
tjDecompress2(jpeg, *data, size, buffer, texture.width, 0, texture.height, TJPF_RGBA, TJFLAG_FASTDCT);
|
|
|
|
bool ret = Textures::Create(buffer, GL_RGBA, texture);
|
2021-09-12 23:54:27 +00:00
|
|
|
tjDestroy(jpeg);
|
|
|
|
delete[] buffer;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
static bool LoadImageOther(const std::string &path, Tex &texture) {
|
|
|
|
unsigned char *image = stbi_load(path.c_str(), std::addressof(texture.width), std::addressof(texture.height), nullptr, STBI_rgb_alpha);
|
2022-08-01 18:48:39 +00:00
|
|
|
bool ret = Textures::Create(image, GL_RGBA, texture);
|
2021-09-12 23:54:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
static bool LoadImageWEBP(unsigned char **data, std::size_t &size, Tex &texture) {
|
2022-08-02 19:20:26 +00:00
|
|
|
*data = WebPDecodeRGBA(*data, size, std::addressof(texture.width), std::addressof(texture.height));
|
2022-08-01 18:48:39 +00:00
|
|
|
bool ret = Textures::Create(*data, GL_RGBA, texture);
|
2021-09-12 23:54:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageType GetImageType(const std::string &filename) {
|
|
|
|
std::string ext = FS::GetFileExt(filename);
|
|
|
|
|
|
|
|
if (!ext.compare(".BMP"))
|
|
|
|
return ImageTypeBMP;
|
|
|
|
else if (!ext.compare(".GIF"))
|
|
|
|
return ImageTypeGIF;
|
|
|
|
else if ((!ext.compare(".JPG")) || (!ext.compare(".JPEG")))
|
|
|
|
return ImageTypeJPEG;
|
|
|
|
else if (!ext.compare(".PNG"))
|
|
|
|
return ImageTypePNG;
|
|
|
|
else if (!ext.compare(".WEBP"))
|
|
|
|
return ImageTypeWEBP;
|
|
|
|
|
|
|
|
return ImageTypeOther;
|
|
|
|
}
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
bool LoadImageFile(const std::string &path, std::vector<Tex> &textures) {
|
2021-09-12 23:54:27 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
// Resize to 1 initially. If the file is a GIF it will be resized accordingly.
|
|
|
|
textures.resize(1);
|
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
ImageType type = Textures::GetImageType(path);
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
if (type == ImageTypeGIF)
|
|
|
|
ret = Textures::LoadImageGIF(path, textures);
|
2022-08-06 15:33:30 +00:00
|
|
|
else if (type == ImageTypePNG)
|
|
|
|
ret = Textures::LoadImagePNG(path, textures[0]);
|
|
|
|
else if (type == ImageTypeOther)
|
|
|
|
ret = Textures::LoadImageOther(path, textures[0]);
|
2022-08-01 18:48:39 +00:00
|
|
|
else {
|
|
|
|
unsigned char *data = nullptr;
|
2022-08-06 15:33:30 +00:00
|
|
|
std::size_t size = 0;
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
if (!Textures::ReadFile(path, std::addressof(data), size)) {
|
2022-08-01 18:48:39 +00:00
|
|
|
delete[] data;
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
switch(type) {
|
|
|
|
case ImageTypeBMP:
|
2022-08-02 19:20:26 +00:00
|
|
|
ret = Textures::LoadImageBMP(std::addressof(data), size, textures[0]);
|
2022-08-01 18:48:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ImageTypeJPEG:
|
2022-08-02 19:20:26 +00:00
|
|
|
ret = Textures::LoadImageJPEG(std::addressof(data), size, textures[0]);
|
2022-08-01 18:48:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ImageTypeWEBP:
|
2022-08-02 19:20:26 +00:00
|
|
|
ret = Textures::LoadImageWEBP(std::addressof(data), size, textures[0]);
|
2022-08-01 18:48:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-09-12 23:54:27 +00:00
|
|
|
|
2022-08-01 18:48:39 +00:00
|
|
|
delete[] data;
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init(void) {
|
2022-08-02 03:24:49 +00:00
|
|
|
const int num_icons = 4;
|
|
|
|
|
|
|
|
const std::string paths[num_icons] {
|
2021-09-12 21:08:02 +00:00
|
|
|
"romfs:/file.png",
|
|
|
|
"romfs:/archive.png",
|
|
|
|
"romfs:/image.png",
|
|
|
|
"romfs:/text.png"
|
|
|
|
};
|
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
bool image_ret = Textures::LoadImagePNG("romfs:/folder.png", folder_icon);
|
2021-09-12 23:54:27 +00:00
|
|
|
IM_ASSERT(image_ret);
|
2020-08-12 01:48:10 +00:00
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
image_ret = Textures::LoadImagePNG("romfs:/check.png", check_icon);
|
2021-09-12 23:54:27 +00:00
|
|
|
IM_ASSERT(image_ret);
|
2020-08-05 00:39:03 +00:00
|
|
|
|
2022-08-06 15:33:30 +00:00
|
|
|
image_ret = Textures::LoadImagePNG("romfs:/uncheck.png", uncheck_icon);
|
2021-09-12 23:54:27 +00:00
|
|
|
IM_ASSERT(image_ret);
|
2021-09-12 21:08:02 +00:00
|
|
|
|
2022-08-02 03:24:49 +00:00
|
|
|
file_icons.resize(num_icons);
|
|
|
|
|
|
|
|
for (int i = 0; i < num_icons; i++) {
|
2022-08-06 15:33:30 +00:00
|
|
|
bool ret = Textures::LoadImagePNG(paths[i], file_icons[i]);
|
2021-09-12 21:08:02 +00:00
|
|
|
IM_ASSERT(ret);
|
|
|
|
}
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 03:24:49 +00:00
|
|
|
void Free(Tex &texture) {
|
2022-08-02 19:20:26 +00:00
|
|
|
glDeleteTextures(1, std::addressof(texture.id));
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Exit(void) {
|
2022-08-02 03:24:49 +00:00
|
|
|
for (unsigned int i = 0; i < file_icons.size(); i++)
|
|
|
|
Textures::Free(file_icons[i]);
|
|
|
|
|
|
|
|
Textures::Free(uncheck_icon);
|
|
|
|
Textures::Free(check_icon);
|
|
|
|
Textures::Free(folder_icon);
|
2022-08-02 03:37:47 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < data.textures.size(); i++)
|
|
|
|
Textures::Free(data.textures[i]);
|
2021-09-12 23:54:27 +00:00
|
|
|
}
|
2020-08-03 22:10:06 +00:00
|
|
|
}
|