part: add mlx3ds_assets_get() and file embedding *

TODO: docs
also add tester for sdcard files
This commit is contained in:
Zy 2024-10-10 23:25:15 +02:00
parent d57afcccc6
commit 09e319024d
15 changed files with 392 additions and 24 deletions

View file

@ -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

1
assets/bonjour Normal file
View file

@ -0,0 +1 @@
yo

1
assets/hello Normal file
View file

@ -0,0 +1 @@
hiya

1
assets/sous/fichier Normal file
View file

@ -0,0 +1 @@
contenu

1
assets/sous/sous/fichier Normal file
View file

@ -0,0 +1 @@
woah

30
embedassets.sh Executable file
View file

@ -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 <string.h>'
>> __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 '}'

16
include/mlx3ds.h Normal file
View file

@ -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

View file

@ -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 <string.h>
// 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

View file

@ -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);

View file

@ -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

View file

@ -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

View file

@ -1,11 +1,15 @@
extern "C" {
#include "mlx.h"
#include "3ds.h"
#include "mlx3ds.h"
}
#include <iostream>
#include <string.h>
#include "utilsconsole.hpp"
#include "utilsconsole.hpp"
#include "firsk.xpm"
#include <sys/dirent.h>
#include <fcntl.h>
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();

View file

@ -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"

View file

@ -8,6 +8,7 @@
#include <3ds.h>
#include <iostream>
#include <stdarg.h>
#include <cstring>
int uc_menu(vector<s_uc_menu_element> &elements) {
static void *lastElements = NULL;
@ -91,3 +92,17 @@ void uc_pause(void) {
gfxFlushBuffers();
}
}
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);
}

View file

@ -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 <stdarg.h>
#include <string.h>
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);
}
}