Skip to content

Commit

Permalink
Getting close to final
Browse files Browse the repository at this point in the history
  • Loading branch information
J-D-K committed Jan 16, 2024
1 parent 3fbd67f commit 8726e6f
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 162 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
TARGET := Avatool
BUILD := build
SOURCES := src src/graphics src/states src/filesystem src/ui
SOURCES := src src/graphics src/states src/filesystem src/ui src/io
DATA := data
INCLUDES := inc inc/graphics
INCLUDES := inc
APP_VERSION := 2.1.0
APP_AUTHOR := JK
ROMFS := romfs
Expand Down
3 changes: 1 addition & 2 deletions inc/avatool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include <memory>
#include <stack>
#include "avatoolState.hpp"
#include "graphics.hpp"
#include "type.hpp"
#include "graphics/graphics.hpp"

class avatool
{
Expand Down
3 changes: 1 addition & 2 deletions inc/avatoolState.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "graphics.hpp"
#include "graphics/graphics.hpp"

class avatoolState
{
Expand Down
5 changes: 5 additions & 0 deletions inc/filesystem/io.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include <string>

void copyFile(const std::string &sourcePath, const std::string &destinationPath);
void copyDirectory(const std::string &sourcePath, const std::string &destinationPath);
26 changes: 2 additions & 24 deletions inc/fs.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
#pragma once

#include <string>
#include <vector>
#include <dirent.h>

class dirList
{
public:
void assign(const std::string& _path);
void rescan();

std::string getItem(int index);
bool isDir(int index);
unsigned getCount();

private:
DIR *d;
struct dirent *ent;
std::string path;
std::vector<std::string> item;
};

void copyFile(const std::string& from, const std::string& to);
void copyDirToDir(const std::string& from, const std::string &to);
void delDir(const std::string& path);
#include "filesystem/directoryListing.hpp"
#include "filesystem/io.hpp"
9 changes: 5 additions & 4 deletions inc/graphics.hpp → inc/graphics/graphics.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <memory>
#include <SDL2/SDL.h>
#include "graphics/colors.hpp"
#include "graphics/font.hpp"
Expand All @@ -16,13 +17,13 @@ class graphics
// Font is private
void renderTextf(SDL_Texture *target, int fontSize, uint32_t color, int x, int y, const char *format, ...);
void renderTextfWrap(SDL_Texture *target, int fontSize, uint32_t color, int x, int y, int maxWidth, const char *format, ...);
int getTextWidth(std::string text, int fontSize) { return m_SharedFont->getTextWidth(m_Renderer, text, fontSize); }
int getTextWidth(const std::string &text, int fontSize) { return m_SystemFont->getTextWidth(m_Renderer, text, fontSize); }

// I dont' want to make the manager public
SDL_Texture *textureCreate(std::string textureName, int width, int height, uint32_t sdlTextureFlags) { return m_TextureManager->textureCreate(textureName, m_Renderer, width, height, sdlTextureFlags); }
SDL_Texture *textureCreateFromSurface(std::string textureName, SDL_Surface *surface) { return m_TextureManager->textureCreateFromSurface(textureName, m_Renderer, surface); }
SDL_Texture *textureLoadFromFile(std::string textureName, const char *path) { return m_TextureManager->textureLoadFromFile(textureName, m_Renderer, path); }
SDL_Texture *textureLoadFromMem(std::string textureName, imageTypes imgType, const byte *data, size_t dataSize) { return m_TextureManager->textureLoadFromMem(textureName, m_Renderer, imgType, data, dataSize); }
SDL_Texture *textureLoadFromMem(std::string textureName, imageTypes imgType, const std::byte *data, size_t dataSize) { return m_TextureManager->textureLoadFromMem(textureName, m_Renderer, imgType, data, dataSize); }

// Texture drawing/rendering functions
void clearTexture(SDL_Texture *texture, uint32_t color);
Expand All @@ -37,6 +38,6 @@ class graphics
private:
SDL_Renderer *m_Renderer;
SDL_Window *m_Window;
systemFont *m_SharedFont;
textureManager *m_TextureManager = NULL;
std::unique_ptr<systemFont> m_SystemFont;
std::unique_ptr<textureManager> m_TextureManager;
};
5 changes: 2 additions & 3 deletions inc/graphics/textureManager.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once
#include <SDL2/SDL.h>
#include <memory>
#include <cstddef>
#include <string>
#include <map>
#include "type.hpp"

typedef enum
{
Expand All @@ -25,7 +24,7 @@ class textureManager
// Loads path
SDL_Texture *textureLoadFromFile(std::string textureName, SDL_Renderer *renderer, const char *path);
// Loads image from data. Only really used for icons
SDL_Texture *textureLoadFromMem(std::string textureName, SDL_Renderer *renderer, imageTypes imgType, const byte *data, size_t dataSize);
SDL_Texture *textureLoadFromMem(std::string textureName, SDL_Renderer *renderer, imageTypes imgType, const std::byte *data, size_t dataSize);

private:
bool textureIsLoaded(std::string textureName);
Expand Down
3 changes: 0 additions & 3 deletions inc/type.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion inc/ui/menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <SDL2/SDL.h>
#include <string>
#include <vector>
#include "graphics.hpp"
#include "graphics/graphics.hpp"

class menu
{
Expand Down
46 changes: 46 additions & 0 deletions src/filesystem/io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <fstream>
#include <vector>
#include <sys/stat.h>
#include "filesystem/directoryListing.hpp"
#include "filesystem/io.hpp"

void copyFile(const std::string &sourcePath, const std::string &destinationPath)
{
std::ifstream source(sourcePath, std::ios::binary);
std::ofstream destination(destinationPath, std::ios::binary);

// Since we're just dealing with jpegs, read whole thing to RAM then out
std::vector<char> fileBuffer((std::istreambuf_iterator<char>(source)), std::istreambuf_iterator<char>());
destination.write(fileBuffer.data(), fileBuffer.size());
}

void copyDirectory(const std::string &sourcePath, const std::string &destinationPath)
{
directoryListing sourceList(sourcePath);

int sourceCount = sourceList.getDirectoryListingCount();
for(int i = 0; i < sourceCount; i++)
{
if(sourceList.itemAtIsDirectory(i))
{
//New paths
std::string newSource = sourcePath + sourceList.getDirectoryItemAt(i) + "/";
std::string newDestination = destinationPath + sourceList.getDirectoryItemAt(i) + "/";

//Use substring to create sub directory
mkdir(newDestination.substr(0, newDestination.npos - 1).c_str(), 0777);

//Recursion
copyDirectory(newSource, newDestination);
}
else
{
//Full paths
std::string fullSource = sourcePath + sourceList.getDirectoryItemAt(i);
std::string fullDestination = destinationPath + sourceList.getDirectoryItemAt(i);

//Copy it over
copyFile(fullSource, fullDestination);
}
}
}
109 changes: 0 additions & 109 deletions src/fs.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/graphics/font.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <switch.h>
#include <cstring>
#include "graphics/colors.hpp"
#include "font.hpp"
#include "graphics/font.hpp"

static const uint32_t redMask = 0xFF000000;
static const uint32_t greenMask = 0x00FF0000;
Expand Down Expand Up @@ -257,7 +257,7 @@ glyph *systemFont::getGlyph(SDL_Renderer *renderer, uint32_t c, int size)

//Prepare buffer to convert to surface
size_t bitmapSize = bitmap.rows * bitmap.width;
byte *bitmapPointer = bitmap.buffer;
uint8_t *bitmapPointer = reinterpret_cast<uint8_t *>(bitmap.buffer);
uint32_t *glyphBuffer = new uint32_t[bitmapSize];
uint32_t basePixel = 0xFFFFFF00;
//Use bitmap values to fill out alpha of texture
Expand Down
10 changes: 5 additions & 5 deletions src/graphics.cpp → src/graphics/graphics.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <SDL2/SDL_image.h>
#include <cstdio>
#include "graphics.hpp"
#include "graphics/graphics.hpp"

graphics::graphics()
{
Expand All @@ -12,8 +12,8 @@ graphics::graphics()

IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);

m_SharedFont = new systemFont();
m_TextureManager = new textureManager();
m_SystemFont = std::make_unique<systemFont>();
m_TextureManager = std::make_unique<textureManager>();
}

graphics::~graphics()
Expand All @@ -39,7 +39,7 @@ void graphics::renderTextf(SDL_Texture *target, int fontSize, uint32_t color, in
va_start(args, format);
vsprintf(str, format, args);
va_end(args);
m_SharedFont->renderText(m_Renderer, target, fontSize, color, x, y, str);
m_SystemFont->renderText(m_Renderer, target, fontSize, color, x, y, str);
}

void graphics::renderTextfWrap(SDL_Texture *target, int fontSize, uint32_t color, int x, int y, int maxWidth, const char *format, ...)
Expand All @@ -49,7 +49,7 @@ void graphics::renderTextfWrap(SDL_Texture *target, int fontSize, uint32_t color
va_start(args, format);
vsprintf(str, format, args);
va_end(args);
m_SharedFont->renderTextWrap(m_Renderer, target, fontSize, x, y, maxWidth, color, str);
m_SystemFont->renderTextWrap(m_Renderer, target, fontSize, x, y, maxWidth, color, str);
}

void graphics::clearTexture(SDL_Texture *texture, uint32_t color)
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/textureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <SDL2/SDL_image.h>
#include "graphics/textureManager.hpp"

static SDL_Texture *loadJPEGMem(SDL_Renderer *renderer, const byte *data, size_t dataSize)
static SDL_Texture *loadJPEGMem(SDL_Renderer *renderer, const std::byte *data, size_t dataSize)
{
SDL_Texture *ret = NULL;
SDL_RWops *jpg = SDL_RWFromConstMem(data, dataSize);
Expand All @@ -16,7 +16,7 @@ static SDL_Texture *loadJPEGMem(SDL_Renderer *renderer, const byte *data, size_t
return ret;
}

static SDL_Texture *loadPNGMem(SDL_Renderer *renderer, const byte *data, size_t dataSize)
static SDL_Texture *loadPNGMem(SDL_Renderer *renderer, const std::byte *data, size_t dataSize)
{
SDL_Texture *ret = NULL;
SDL_RWops *png = SDL_RWFromConstMem(data, dataSize);
Expand Down Expand Up @@ -86,7 +86,7 @@ SDL_Texture *textureManager::textureLoadFromFile(std::string textureName, SDL_Re
return ret;
}

SDL_Texture *textureManager::textureLoadFromMem(std::string textureName, SDL_Renderer *renderer, imageTypes imgType, const byte *data, size_t dataSize)
SDL_Texture *textureManager::textureLoadFromMem(std::string textureName, SDL_Renderer *renderer, imageTypes imgType, const std::byte *data, size_t dataSize)
{
if(textureIsLoaded(textureName))
{
Expand Down
4 changes: 2 additions & 2 deletions src/states/editState.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <switch.h>
#include <string>
#include "graphics.hpp"
#include "graphics/graphics.hpp"
#include "states/editState.hpp"

// Names of textures
Expand Down Expand Up @@ -88,7 +88,7 @@ void editState::update()
else if (padDown & HidNpadButton_Y)
{
// Copy all to SD card
copyDirToDir(s_TargetDirPath, s_SourceDirPath);
copyDirectory(s_TargetDirPath, s_SourceDirPath);

// Reset menu, source directory
m_SourceMenu->reset();
Expand Down

0 comments on commit 8726e6f

Please sign in to comment.