From 09e319024dbabca19b2af40d92cde7cfa111b018 Mon Sep 17 00:00:00 2001 From: Zy Date: Thu, 10 Oct 2024 23:25:15 +0200 Subject: [PATCH] part: add mlx3ds_assets_get() and file embedding * TODO: docs also add tester for sdcard files --- Makefile | 16 +- assets/bonjour | 1 + assets/hello | 1 + assets/sous/fichier | 1 + assets/sous/sous/fichier | 1 + embedassets.sh | 30 +++ include/mlx3ds.h | 16 ++ include/mlx3ds_embeddedassets.h | 26 ++ include/utilsconsole.hpp | 5 + include/utilsconsole_extern_c.h | 22 ++ include/utilsconsole_pause.h | 17 -- source/main.cpp | 242 +++++++++++++++++- source/mlx_xpm.c | 2 +- source/utilsconsole.cpp | 17 +- ...le_pause.cpp => utilsconsole_extern_c.cpp} | 19 +- 15 files changed, 392 insertions(+), 24 deletions(-) create mode 100644 assets/bonjour create mode 100644 assets/hello create mode 100644 assets/sous/fichier create mode 100644 assets/sous/sous/fichier create mode 100755 embedassets.sh create mode 100644 include/mlx3ds.h create mode 100644 include/mlx3ds_embeddedassets.h create mode 100644 include/utilsconsole_extern_c.h delete mode 100644 include/utilsconsole_pause.h rename source/{utilsconsole_pause.cpp => utilsconsole_extern_c.cpp} (50%) diff --git a/Makefile b/Makefile index 266f7d4..60085a2 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ include $(DEVKITARM)/3ds_rules # GFXBUILD is the directory where converted graphics files will be placed # If set to $(BUILD), it will statically link in the converted # files as if they were data files. +# ASSETS is the directory of assets to embed in the rom (rule for __embeddedassets.o) # # NO_SMDH: if set to anything, no SMDH file is generated. # ROMFS is the directory which contains the RomFS, relative to the Makefile (Optional) @@ -43,6 +44,7 @@ OUTPUT := output RESOURCES := resources ROMFS := romfs GFXBUILD := $(ROMFS)/gfx +ASSETS := assets #--------------------------------------------------------------------------------- # Resource Setup #--------------------------------------------------------------------------------- @@ -112,7 +114,7 @@ export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) export OFILES_BIN := $(addsuffix .o,$(BINFILES)) \ $(PICAFILES:.v.pica=.shbin.o) $(SHLISTFILES:.shlist=.shbin.o) \ $(if $(filter $(BUILD),$(GFXBUILD)),$(addsuffix .o,$(T3XFILES))) -export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) +export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) __embeddedassets.o export HFILES := $(PICAFILES:.v.pica=_shbin.h) $(SHLISTFILES:.shlist=_shbin.h) \ $(addsuffix .h,$(subst .,_,$(BINFILES))) \ $(GFXFILES:.t3s=.h) @@ -345,7 +347,7 @@ endif #--------------------------------------------------------------------------------------- # Added by Zy -# To check if the headers are protected and if they include everything they need. +# To check if the headers are protected and if they include everything they need. check_headers : @ERROR=0; \ for HEADER in $(wildcard *.h) $(wildcard **/*.h) $(wildcard *.hpp) $(wildcard **/*.hpp); \ @@ -367,3 +369,13 @@ check_headers : if [ $$ERROR -eq 0 ]; then true; else false; fi; .PHONY : check_headers + + +# Added by Zy +# To embed assets in the rom. + +__embeddedassets.o : $(shell find $(TOPDIR)/$(ASSETS)/ -type f) + $(TOPDIR)/embedassets.sh $(TOPDIR)/$(ASSETS)/ $^ + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ __embeddedassets.c + +.PHONY : __embeddedassets.o diff --git a/assets/bonjour b/assets/bonjour new file mode 100644 index 0000000..092bfb9 --- /dev/null +++ b/assets/bonjour @@ -0,0 +1 @@ +yo diff --git a/assets/hello b/assets/hello new file mode 100644 index 0000000..3d9ff13 --- /dev/null +++ b/assets/hello @@ -0,0 +1 @@ +hiya diff --git a/assets/sous/fichier b/assets/sous/fichier new file mode 100644 index 0000000..efffb38 --- /dev/null +++ b/assets/sous/fichier @@ -0,0 +1 @@ +contenu diff --git a/assets/sous/sous/fichier b/assets/sous/sous/fichier new file mode 100644 index 0000000..38a2818 --- /dev/null +++ b/assets/sous/sous/fichier @@ -0,0 +1 @@ +woah diff --git a/embedassets.sh b/embedassets.sh new file mode 100755 index 0000000..16afa68 --- /dev/null +++ b/embedassets.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# USED BY MAKEFILE, DO NOT MOVE OR RENAME + +ASSETS_FULL_DIR=$1 +shift +ASSET_FILES=$@ + +> __embeddedassets.c echo '// File created by rule __embeddedassets.o' +>> __embeddedassets.c echo '#include ' +>> __embeddedassets.c echo '#include "mlx3ds.h"' +>> __embeddedassets.c echo 'const t_embeddedasset *mlx3ds_assets_get(const char *name)' +>> __embeddedassets.c echo '{' +>> __embeddedassets.c echo ' const static t_embeddedasset assets[] = {' +for file in $ASSET_FILES +do + if [ ! -f $file ] + then + continue + fi + >> __embeddedassets.c echo -n "{\"$(echo $file | sed -e "s/^${ASSETS_FULL_DIR//\//\\\/}//")\", " + >> __embeddedassets.c echo -n "$(stat --printf=%s $file)"', ' + >> __embeddedassets.c echo "\"$(< $file xxd -i | sed -e 's/[, ] 0x/\\x/g')\"}," +done +>> __embeddedassets.c echo ' {NULL, 0, NULL}};' +>> __embeddedassets.c echo ' for (int i = 0; assets[i].name; i++)' +>> __embeddedassets.c echo ' if (!strcmp(assets[i].name, name))' +>> __embeddedassets.c echo ' return &assets[i];' +>> __embeddedassets.c echo ' return NULL;' +>> __embeddedassets.c echo '}' diff --git a/include/mlx3ds.h b/include/mlx3ds.h new file mode 100644 index 0000000..e9d705f --- /dev/null +++ b/include/mlx3ds.h @@ -0,0 +1,16 @@ +/** + * mlx3ds.h + * for the project "MinilibX for 3DS" + * by Zy + * at https://github.com/frzysk/mlx3ds + */ + +// TODO docs + +#ifndef MLX3DS_H +# define MLX3DS_H + +# include "mlx3ds_typealiases.h" +# include "mlx3ds_embeddedassets.h" + +#endif diff --git a/include/mlx3ds_embeddedassets.h b/include/mlx3ds_embeddedassets.h new file mode 100644 index 0000000..3fd1c0b --- /dev/null +++ b/include/mlx3ds_embeddedassets.h @@ -0,0 +1,26 @@ +/** + * mlx3ds_embeddedassets.h + * for the project "MinilibX for 3DS" + * by Zy + * at https://github.com/frzysk/mlx3ds + */ + +// TODO docs + +#ifndef MLX3DS_ASSETS_H +# define MLX3DS_ASSETS_H + +# include + +// TODO docs +typedef struct s_embeddedasset +{ + const char *name; + size_t size; + const char *data; +} t_embeddedasset; + +// TODO docs +const t_embeddedasset *mlx3ds_assets_get(const char *name); + +#endif diff --git a/include/utilsconsole.hpp b/include/utilsconsole.hpp index 09e3ddd..52a8fd4 100644 --- a/include/utilsconsole.hpp +++ b/include/utilsconsole.hpp @@ -50,3 +50,8 @@ int uc_menu_quick(const char *str, ...); /// @brief Blocks the program until the user presses A. /// @details Will also return if aptMainLoop() becomes false. void uc_pause(void); + +/// @brief Show the 3DS keyboard to ask the user for a text. +/// @param def The default text to show the user. +/// @return A freeable address to the text prompted by the user. NULL if error. +char *uc_keyboard(const char *def); diff --git a/include/utilsconsole_extern_c.h b/include/utilsconsole_extern_c.h new file mode 100644 index 0000000..0e9965c --- /dev/null +++ b/include/utilsconsole_extern_c.h @@ -0,0 +1,22 @@ +/** + * utilsconsole_extern_c.h + * by Zy + */ + +/** + * utilsconsole_extern_c.h (uc_*() functions) + */ + +#ifndef UTILSCONSOLE_EXTERN_C_H +# define UTILSCONSOLE_EXTERN_C_H + +/// @brief Blocks the program until the user presses A. +/// @details Will also return if aptMainLoop() becomes false. +void uc_pause(void); + +/// @brief Show the 3DS keyboard to ask the user for a text. +/// @param def The default text to show the user. +/// @return A freeable address to the text prompted by the user. NULL if error. +char *uc_keyboard(const char *def); + +#endif diff --git a/include/utilsconsole_pause.h b/include/utilsconsole_pause.h deleted file mode 100644 index 396eadd..0000000 --- a/include/utilsconsole_pause.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * utilsconsole_pause.h - * by Zy - */ - -/** - * utilsconsole_pause.h (uc_pause() function) - */ - -#ifndef UTILSCONSOLE_PAUSE_H -# define UTILSCONSOLE_PAUSE_H - -/// @brief Blocks the program until the user presses A. -/// @details Will also return if aptMainLoop() becomes false. -void uc_pause(void); - -#endif diff --git a/source/main.cpp b/source/main.cpp index 8a8830a..ed72f6a 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,11 +1,15 @@ extern "C" { #include "mlx.h" #include "3ds.h" +#include "mlx3ds.h" } #include #include #include "utilsconsole.hpp" +#include "utilsconsole.hpp" #include "firsk.xpm" +#include +#include using namespace std; @@ -42,7 +46,8 @@ int main(void) { // MENU { - switch (uc_menu_quick("pixels", "images", "xpm", "quit", NULL)) + switch (uc_menu_quick("pixels", "images", "xpm", "files", + "navigate", "assets", "quit", NULL)) { case 0: goto pixels; @@ -53,6 +58,15 @@ int main(void) { case 2: goto xpm; break; + case 3: + goto files; + break; + case 4: + goto navigate; + break; + case 5: + goto assets; + break; } goto end; } @@ -262,6 +276,232 @@ xpm: goto end; +files: + cout << "Current working directory: " << getcwd(NULL, 0) << endl; + uc_pause(); + + cout << "List of files in /:" << endl; + { + DIR *dir = opendir("/"); + dirent *dir_in; + do + { + dir_in = readdir(dir); + if (!dir_in) + break; + cout << "> " << dir_in->d_name << endl; + } while (dir_in); + closedir(dir); + } + uc_pause(); + + cout << "Read file abc.txt..." << endl; + { + int f = 0; + f = open("/abc.txt", O_RDONLY); + if (!f) + { + cout << "open() error :(" << endl; + goto file_read_end; + } + char buf[1000]; + { + cout << "file content:" << endl << "\""; + int n = 1; + while (n) + { + n = read(f, buf, 1000); + if (n < 0) + { + cout << "read() error :(" << endl; + goto file_read_end; + } + if (n > 0) + write(1, buf, n); + } + cout << "\"" << endl; + } + + file_read_end: + if (f) + close(f); + } + uc_pause(); + + cout << "Write \"hello :3\" in abc.txt..." << endl; + { + int f = 0; + f = open("/abc.txt", O_WRONLY | O_CREAT); + if (!f) + { + cout << "open() error :(" << endl; + goto file_write_end; + } + if (write(f, "hello :3", 8) < 0) + cout << "write() error :(" << endl; + else + cout << "write() success :)" << endl; + + file_write_end: + if (f) + close(f); + } + uc_pause(); + + cout << "Read file abc.txt again..." << endl; + { + int f = 0; + f = open("/abc.txt", O_RDONLY); + if (!f) + { + cout << "open() error :(" << endl; + goto file_read2_end; + } + char buf[1000]; + { + cout << "file content:" << endl << "\""; + int n = 1; + while (n) + { + n = read(f, buf, 1000); + if (n < 0) + { + cout << "read() error :(" << endl; + goto file_read2_end; + } + if (n > 0) + write(1, buf, n); + } + cout << "\"" << endl; + } + + file_read2_end: + if (f) + close(f); + } + uc_pause(); + + goto end; + +navigate: + fsInit(); + { + char *last = new char[2]; + last[0] = '/'; + last[1] = '\n'; + while (true) + { + cout << "Write a file path or a directory path toshow it." << endl; + cout << "Put an empty text to return." << endl; + uc_pause(); + char *name = NULL; + if (aptMainLoop()) + name = uc_keyboard(last); + if (!name || !strcmp(name, "")) + break; + cout << "\e[7m" << name << "\e[0m" << endl; + cout << "Try to open as directory..." << endl; + DIR *dir = opendir(name); + if (dir) + { + cout << "Directory opened!" << endl; + uc_pause(); + cout << "Files:" << endl; + dirent *dir_in; + do { + dir_in = readdir(dir); + cout << "> " << dir_in->d_name << endl; + } while (dir_in); + closedir(dir); + } + else { + cout << "Can't open as directory." << endl; + cout << "Try to open as file..." << endl; + int file = open(name, O_RDONLY); + if (file) + { + cout << "File opened!" << endl; + uc_pause(); + cout << "Content:" << endl; + int n = 1; + char buf[1000]; + while (n) + { + n = read(file, buf, 1000); + if (n < 0) + { + cout << "read() error :(" << endl; + } + if (n > 0) + write(1, buf, n); + } + cout << "[EOF]" << endl; + close(file); + } + else + { + cout << "Can't either." << endl; + cout << "Seems nothing works." << endl; + cout << "That is sad." << endl; + cout << "Anyway," << endl; + } + } + delete [] last; + last = name; + uc_pause(); + } + delete [] last; + } + fsExit(); // TODO keep? + goto end; + +assets: + { + const t_embeddedasset *asset; + + cout << "Read asset \"hello\"..." << endl; + asset = mlx3ds_assets_get("hello"); + if (asset) + cout << "Content (" << asset->size << "): \"" + << asset->data << "\"" << endl; + else + cout << "No asset" << endl; + uc_pause(); + cout << "Read asset \"bonjour\"..." << endl; + asset = mlx3ds_assets_get("bonjour"); + if (asset) + cout << "Content (" << asset->size << "): \"" + << asset->data << "\"" << endl; + else + cout << "No asset" << endl; + uc_pause(); + cout << "Read asset \"hola\"..." << endl; + asset = mlx3ds_assets_get("hola"); + if (asset) + cout << "Content (" << asset->size << "): \"" + << asset->data << "\"" << endl; + else + cout << "No asset" << endl; + uc_pause(); + cout << "Read asset \"sous/fichier\"..." << endl; + asset = mlx3ds_assets_get("sous/fichier"); + if (asset) + cout << "Content (" << asset->size << "): \"" + << asset->data << "\"" << endl; + else + cout << "No asset" << endl; + uc_pause(); + cout << "Read asset \"sous/sous/fichier\"..." << endl; + asset = mlx3ds_assets_get("sous/sous/fichier"); + if (asset) + cout << "Content (" << asset->size << "): \"" + << asset->data << "\"" << endl; + else + cout << "No asset" << endl; + uc_pause(); + } + goto end; + end: cout << "Exit..." << endl; uc_pause(); diff --git a/source/mlx_xpm.c b/source/mlx_xpm.c index da0c217..b245843 100644 --- a/source/mlx_xpm.c +++ b/source/mlx_xpm.c @@ -8,7 +8,7 @@ ** Last update Sat Oct 1 14:56:13 2005 Olivier Crouzet */ -#include "utilsconsole_pause.h" +#include "utilsconsole_extern_c.h" // TODO remove #include "mlx_int.h" #include "mlx.h" diff --git a/source/utilsconsole.cpp b/source/utilsconsole.cpp index 5706453..a555921 100644 --- a/source/utilsconsole.cpp +++ b/source/utilsconsole.cpp @@ -8,6 +8,7 @@ #include <3ds.h> #include #include +#include int uc_menu(vector &elements) { static void *lastElements = NULL; @@ -90,4 +91,18 @@ void uc_pause(void) { break; gfxFlushBuffers(); } -} \ No newline at end of file +} + +char *uc_keyboard(const char *def) +{ + char *buf; + SwkbdState swkbd; + + buf = new char[1000]; + if (!buf) + return (NULL); + swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, 1, -1); + swkbdSetInitialText(&swkbd, def); + swkbdInputText(&swkbd, buf, 1000); + return (buf); +} diff --git a/source/utilsconsole_pause.cpp b/source/utilsconsole_extern_c.cpp similarity index 50% rename from source/utilsconsole_pause.cpp rename to source/utilsconsole_extern_c.cpp index 59e92fd..9a2442f 100644 --- a/source/utilsconsole_pause.cpp +++ b/source/utilsconsole_extern_c.cpp @@ -1,5 +1,5 @@ /** - * utilsconsole.cpp + * utilsconsole_extern_c.cpp * by Zy */ @@ -8,9 +8,10 @@ extern "C" { -#include "utilsconsole_pause.h" +#include "utilsconsole_extern_c.h" #include <3ds.h> #include +#include using namespace std; @@ -24,4 +25,18 @@ void uc_pause(void) { } } +char *uc_keyboard(const char *def) +{ + char *buf; + SwkbdState swkbd; + + buf = new char[1000]; + if (!buf) + return (NULL); + swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, 1, -1); + swkbdSetInitialText(&swkbd, def); + swkbdInputText(&swkbd, buf, 1000); + return (buf); +} + }