summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Shaw <jshaw@netsurf-browser.org>2008-03-23 23:38:36 (GMT)
committer James Shaw <jshaw@netsurf-browser.org>2008-03-23 23:38:36 (GMT)
commit2b29c4dcde7e673d50cfbde6eb41810c3f9ec21b (patch)
tree1ccf3344bc721a0e1e40995c5af30470106678d5
parentee314c5193f4b9045b47a0028e43265c9e422581 (diff)
downloadlibrosprite-2b29c4dcde7e673d50cfbde6eb41810c3f9ec21b.tar.gz
librosprite-2b29c4dcde7e673d50cfbde6eb41810c3f9ec21b.tar.bz2
Change example to use broken memory loader
svn path=/import/jshaw/libsprite/; revision=10015
-rw-r--r--trunk/example.c39
1 files changed, 33 insertions, 6 deletions
diff --git a/trunk/example.c b/trunk/example.c
index 7325c4a..c42756d 100644
--- a/trunk/example.c
+++ b/trunk/example.c
@@ -32,6 +32,29 @@ void sdl_blank(SDL_Surface* surface)
}
}
+int load_file_to_memory(const char *filename, uint8_t **result)
+{
+ int size = 0;
+ FILE *f = fopen(filename, "rb");
+ if (f == NULL)
+ {
+ *result = NULL;
+ return -1; // -1 means file opening fail
+ }
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ *result = (uint8_t *)malloc(size+1);
+ if ((unsigned int) size != fread(*result, sizeof(char), size, f))
+ {
+ free(*result);
+ return -2; // -2 means file reading fail
+ }
+ fclose(f);
+ (*result)[size] = 0;
+ return size;
+}
+
int main(int argc, char *argv[])
{
if (argc < 2) {
@@ -46,18 +69,21 @@ int main(int argc, char *argv[])
atexit(SDL_Quit);
char* filename = argv[1];
+ uint8_t* content;
- FILE* f = fopen(filename, "rb");
- if (f == NULL) {
+ int size = load_file_to_memory(filename, &content);
+ if (size < 0) {
printf("Can't load spritefile %s\n", filename);
exit(EXIT_FAILURE);
+ } else {
+ printf("Loaded file of size %d into memory", size);
}
- struct rosprite_file_context* ctx = rosprite_create_file_context(f);
+ struct rosprite_mem_context* ctx = rosprite_create_mem_context(content, size);
printf("Loading %s\n", filename);
- struct rosprite_area* sprite_area = rosprite_load(rosprite_file_reader, ctx);
+ struct rosprite_area* sprite_area = rosprite_load(rosprite_mem_reader, ctx);
printf("sprite_count %u\n", sprite_area->sprite_count);
printf("extension_size %u\n", sprite_area->extension_size);
@@ -94,9 +120,10 @@ int main(int argc, char *argv[])
fgetc(stdin);
}
- fclose(f);
- rosprite_destroy_file_context(ctx);
+ rosprite_destroy_mem_context(ctx);
rosprite_destroy_sprite_area(sprite_area);
return EXIT_SUCCESS;
}
+
+