part: implement mlx3ds_assets_open/read/close()
This commit is contained in:
		
							parent
							
								
									3f5e13b2f2
								
							
						
					
					
						commit
						8649e86eaa
					
				
					 4 changed files with 133 additions and 7 deletions
				
			
		|  | @ -15,6 +15,9 @@ | |||
| 
 | ||||
| # include <string.h> | ||||
| 
 | ||||
| /// @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 | ||||
|  |  | |||
|  | @ -5,10 +5,11 @@ extern "C" { | |||
| #include "utilsconsole.h" | ||||
| } | ||||
| #include <iostream> | ||||
| #include <string.h> | ||||
| #include "firsk.xpm" | ||||
| #include <string.h> | ||||
| #include <sys/dirent.h> | ||||
| #include <fcntl.h> | ||||
| #include <unistd.h> | ||||
| 
 | ||||
| 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; | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										52
									
								
								source/mlx3ds_embeddedassets.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								source/mlx3ds_embeddedassets.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -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 <unistd.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| /// @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); | ||||
| } | ||||
|  | @ -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 <stdio.h> | ||||
| 
 | ||||
| void	mlx3ds_internal_fatalerror(const char *msg) | ||||
| { | ||||
|  | @ -34,4 +33,3 @@ void	mlx3ds_internal_drawend( | |||
| 	gfxFlushBuffers(); | ||||
| 	gfxSwapBuffers(); | ||||
| } | ||||
| } | ||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Zy
						Zy