diff --git a/include/mlx3ds_embeddedassets.h b/include/mlx3ds_embeddedassets.h index e1bfc93..b83dcdb 100644 --- a/include/mlx3ds_embeddedassets.h +++ b/include/mlx3ds_embeddedassets.h @@ -15,6 +15,9 @@ # include +/// @brief Socket to an asset. Equivalent to a file descriptor. +typedef void *t_assetsocket; + /// @brief Represents a file embedded with the program. typedef struct s_embeddedasset { @@ -32,6 +35,26 @@ typedef struct s_embeddedasset /// @return A reference to the asset. NULL if the asset doesn't exist. const t_embeddedasset *mlx3ds_assets_get(const char *name); -// TODO add mlx3ds_assets_open() +/// @brief Create a t_assetsocket from an asset. +/// +/// @param name Name of the asset to open. +/// @return A socket to the asset. NULL if error. +t_assetsocket mlx3ds_assets_open(const char *name); + +/// @brief Reads up to 'count' bytes of the asset content and write it in 'buf'. +/// +/// @param asset The asset to read. +/// @param buf The buffer to write the content to. +/// Its size must be at least 'count' bytes. +/// @param count The number of bytes to read. +/// @return The number of bytes actually read. Can be lower than 'count'. +/// 0 if EOF reached. +size_t mlx3ds_assets_read( + t_assetsocket asset, void *buf, size_t count); + +/// @brief Close the asset socket to avoid leaks. +/// +/// @param asset Asset to close. +void mlx3ds_assets_close(t_assetsocket asset); #endif diff --git a/source/main.cpp b/source/main.cpp index 3963f33..deb1b93 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -5,10 +5,11 @@ extern "C" { #include "utilsconsole.h" } #include -#include #include "firsk.xpm" +#include #include #include +#include using namespace std; @@ -495,6 +496,58 @@ assets: else cout << "No asset" << endl; uc_pause(); + { + char buf[10]; + cout + << "Read asset \"sous/sous/fichier\"" << endl + << "from mlx3ds_assets_open()..." << endl + << "(buf[10])" << endl; + t_assetsocket asset = mlx3ds_assets_open("sous/sous/fichier"); + if (!asset) + cout << "Error" << endl; + else { + cout << "Content: \""; + int n; + do { + n = mlx3ds_assets_read(asset, buf, 10); + if (n < 0) { + cout << "read error :/" << endl; + break; + } + if (n > 0) + write(1, buf, n); + } while (n); + cout << "\"" << endl; + mlx3ds_assets_close(asset); + } + } + uc_pause(); + { + char buf[100]; + cout + << "Read asset \"sous/sous/fichier\"" << endl + << "from mlx3ds_assets_open()..." << endl + << "(buf[100])" << endl; + t_assetsocket asset = mlx3ds_assets_open("sous/sous/fichier"); + if (!asset) + cout << "Error" << endl; + else { + cout << "Content: \""; + int n; + do { + n = mlx3ds_assets_read(asset, buf, 100); + if (n < 0) { + cout << "read error :/" << endl; + break; + } + if (n > 0) + write(1, buf, n); + } while (n); + cout << "\"" << endl; + mlx3ds_assets_close(asset); + } + } + uc_pause(); } goto end; diff --git a/source/mlx3ds_embeddedassets.c b/source/mlx3ds_embeddedassets.c new file mode 100644 index 0000000..5720007 --- /dev/null +++ b/source/mlx3ds_embeddedassets.c @@ -0,0 +1,52 @@ +/** + * mlx3ds_embeddedassets.c + * for the project "MinilibX for 3DS" + * by Zy + * at https://github.com/frzysk/mlx3ds + */ + +#include "mlx3ds_embeddedassets.h" +#include +#include + +/// @brief Internal of a t_assetsocket. +typedef struct s_in_assetsocket +{ + /// @brief Asset used. + const t_embeddedasset *asset; + /// @brief Index of the cursor. + size_t i; +} t_in_assetsocket; + +// This function is implemented in embedassets.sh +const t_embeddedasset *mlx3ds_assets_get(const char *name); + +t_assetsocket mlx3ds_assets_open(const char *name) { + const t_embeddedasset *asset; + t_in_assetsocket *r; + + asset = mlx3ds_assets_get(name); + if (!asset) + return NULL; + r = malloc(sizeof(t_in_assetsocket)); + if (!r) + return NULL; + r->asset = asset; + r->i = 0; + return r; +} + +size_t mlx3ds_assets_read(t_assetsocket asset, void *buf, size_t count) { + t_in_assetsocket *asset_ = asset; + + if (count > asset_->asset->size - asset_->i) + count = asset_->asset->size - asset_->i; + + memcpy(buf, asset_->asset->data + asset_->i, count); + asset_->i += count; + return count; +} + +void mlx3ds_assets_close(t_assetsocket asset) { + free(asset); +} diff --git a/source/mlx_internal.cpp b/source/mlx_internal.c similarity index 87% rename from source/mlx_internal.cpp rename to source/mlx_internal.c index 4a5fa64..885b2ae 100644 --- a/source/mlx_internal.cpp +++ b/source/mlx_internal.c @@ -1,17 +1,16 @@ /** - * mlx_internal.cpp + * mlx_internal.c * for the project "MinilibX for 3DS" * by Zy * at https://github.com/frzysk/mlx3ds */ -// TODO mlx_internal.cpp: embed uc_pause() and change to .c file +// TODO mlx_internal.c: embed uc_pause() - -extern "C" { #include "utilsconsole.h" #include "mlx_internal.h" #include "3ds.h" +#include void mlx3ds_internal_fatalerror(const char *msg) { @@ -34,4 +33,3 @@ void mlx3ds_internal_drawend( gfxFlushBuffers(); gfxSwapBuffers(); } -}