From 9a7efed8fa94fed8f3f9f3cb2ec7a0657950904b Mon Sep 17 00:00:00 2001 From: EvanR Date: Sat, 15 May 2010 12:46:51 -0500 Subject: [PATCH] Added loading from not zip files. Changed path spec. --- edit.c | 11 ++++++--- graphics.c | 9 ++----- inner.c | 2 +- loader.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++------------- loader.h | 3 ++- midi.c | 4 +-- splash.c | 2 +- stage.c | 46 ++++++++++++++++++++++------------ text.c | 8 +++--- video.c | 4 +-- 10 files changed, 117 insertions(+), 55 deletions(-) diff --git a/edit.c b/edit.c index 623688b..fc2463e 100644 --- a/edit.c +++ b/edit.c @@ -583,9 +583,14 @@ int main(int argc, char* argv[]){ graphics_init(); raw_tiles = initialize_raw(raw_w, raw_h); - bgimage = load_bitmap("bgimages/background.tga"); - fgtiles = load_bitmap("tilesets/test.tga"); - bgtiles = load_bitmap("tilesets/test.tga"); + + loader_data_mode(0); + bgimage = load_bitmap("azone/gfx/background.tga"); +// loader_data_mode(0); +// fgtiles = load_bitmap("barf.tga"); +// loader_data_mode(1); + fgtiles = load_bitmap("azone/gfx/test.tga"); + bgtiles = load_bitmap("azone/gfx/test.tga"); raw_write(2, 2, 1, 3); diff --git a/graphics.c b/graphics.c index 5d62742..36eba02 100644 --- a/graphics.c +++ b/graphics.c @@ -57,7 +57,7 @@ int stage_enabled = 0; void graphics_init(){ int i; - minifont_gfx = load_gfx("smallfont.tga"); + minifont_gfx = load_gfx("gfx/smallfont.tga"); for(i=0; i struct reader { - zip_file* f; + zip_file* zf; + FILE* ff; int next_c; }; zip_archive* arc; +int data_mode = 1; +void loader_data_mode(int flag){ + if(flag == 0) data_mode = 0; + if(flag == 1) data_mode = 1; +} + void loader_init(){ char* filename = "data.zip"; arc = zip_aropenf(filename); @@ -52,22 +59,16 @@ void loader_quit(){ zip_arclose(arc); } -reader* data_open(char* dir, char* filename){ - char buf[1024]; - strcpy(buf, dir); - strcat(buf, filename); - return loader_open(buf); -} - -reader* loader_open(char* filename){ +reader* loader_opend(char* filename){ char buf[1024] = "data/"; int L = strlen(buf); strncpy(buf+L,filename,1024-L); buf[1023] = 0; reader* rd = xmalloc(sizeof(reader)); - rd->f = zip_fopen(arc, filename); - if(!rd->f){ + rd->ff = NULL; + rd->zf = zip_fopen(arc, filename); + if(!rd->zf){ error_msg("loader_open: can't open %s (%s)\n", filename, zip_geterror()); free(rd); return NULL; @@ -75,20 +76,59 @@ reader* loader_open(char* filename){ return rd; } +reader* loader_openf(char* path){ + reader* rd = xmalloc(sizeof(reader)); + rd->zf = NULL; + rd->ff = fopen(path, "r"); + if(rd->ff == NULL){ + error_msg("loader_fopen: can't open %s\n", path); + free(rd); + return NULL; + } + return rd; +} + +reader* loader_open(char* filename){ + if(data_mode == 0){ + return loader_openf(filename); + } + else if(data_mode == 1){ + return loader_opend(filename); + } + else{ + error_msg("loader_open: inconsistent data_mode\n"); + return NULL; + } +} void loader_close(reader* rd){ - zip_fclose(rd->f); + if(rd->ff) fclose(rd->ff); + if(rd->zf) zip_fclose(rd->zf); free(rd); } int loader_read(reader* rd, void* buf, int count){ - int n = zip_fread(rd->f, buf, count); - if(n < 0){ - error_msg("loader_read: %s\n", zip_geterror()); + if(rd->zf){ + int n = zip_fread(rd->zf, buf, count); + if(n < 0){ + error_msg("loader_read: %s\n", zip_geterror()); + return -1; + } + return n; + } + else if(rd->ff){ + int n = fread(buf, 1, count, rd->ff); + if(n < 0){ + error_msg("loader_read: read error\n"); + return -1; + } + return n; + } + else{ + error_msg("loader_read: inconsistent reader\n"); return -1; } - return n; } unsigned char* loader_readall(char* filename, int* size){ @@ -170,7 +210,16 @@ int loader_scanline(reader* rd, char* format, ...){ } int loader_feof(reader* rd){ - return zip_feof(rd->f); + if(rd->zf){ + return zip_feof(rd->zf); + } + else if(rd->ff){ + return feof(rd->ff); + } + else{ + error_msg("loader_feof: inconsistent reader\n"); + return 1; + } } diff --git a/loader.h b/loader.h index 131399f..dbce6a7 100644 --- a/loader.h +++ b/loader.h @@ -22,9 +22,10 @@ typedef struct reader reader; -reader* data_open(char* dir, char* filename); reader* loader_open(char* filename); +void loader_data_mode(int flag); + int loader_read(reader* rd, void* buf, int count); int loader_readline(reader* rd, char* buf, int size); int loader_scanline(reader* rd, char* format, ...); diff --git a/midi.c b/midi.c index 3741f1e..dc68c46 100644 --- a/midi.c +++ b/midi.c @@ -313,7 +313,7 @@ int read_header(reader* r, int* format, int* track_count, int* divraw){ return 0; } -list* midi_load(char* filename){ +list* midi_load(char* path){ reader* r; list* events = empty(); @@ -322,7 +322,7 @@ list* midi_load(char* filename){ int divraw; int i; - r = data_open("music/", filename); + r = loader_open(path); if(r == NULL){ return NULL; } diff --git a/splash.c b/splash.c index 51a154d..9f3cb00 100644 --- a/splash.c +++ b/splash.c @@ -67,5 +67,5 @@ void setup_splash(){ my.t1 = 100; my.t2 = 500; my.t3 = 600; - my.gfx = load_bitmap("splash.tga"); + my.gfx = load_bitmap("gfx/splash.tga"); } diff --git a/stage.c b/stage.c index fed80e8..68bade8 100644 --- a/stage.c +++ b/stage.c @@ -114,7 +114,7 @@ static void initialize_tiles(int n, tile* tiles){ } -static int load_stage(char* path){ +static int load_stage(char* gfxpath, char* stagepath){ /* [stage file] width height @@ -127,9 +127,10 @@ x y fg bg shape ... */ - reader* f = loader_open(path); - char* filename = path_ending(path); + reader* f = loader_open(stagepath); + char* filename = path_ending(stagepath); char buf[256]; + char gfx[256]; tile* tptr; stage* s = malloc(sizeof(stage)); int w = 20; @@ -137,12 +138,21 @@ x y fg bg shape int x, y, ox, oy, fg, bg, shape; loader_scanline(f, "%d %d %d %d", &w, &h, &ox, &oy); + loader_scanline(f, "%s", buf); - s->bgimage = load_bitmap(buf); + strcpy(gfx, gfxpath); + strcat(gfx, buf); + s->bgimage = load_bitmap(gfx); + loader_scanline(f, "%s", buf); - s->bgtiles = load_bitmap(buf); + strcpy(gfx, gfxpath); + strcat(gfx, buf); + s->bgtiles = load_bitmap(gfx); + loader_scanline(f, "%s", buf); - s->fgtiles = load_bitmap(buf); + strcpy(gfx, gfxpath); + strcat(gfx, buf); + s->fgtiles = load_bitmap(gfx); s->tiles = malloc(w*h*sizeof(tile)); initialize_tiles(w*h, s->tiles); @@ -183,16 +193,22 @@ the name of the file will be used as the id list* dirs; list* ptr; - char path[256] = ""; - char* filename; + char stages[256] = ""; + char gfxpath[256] = ""; + char* stagepath; + + strcat(stages, "zones/"); + strcat(stages, name); + strcat(stages, "/stages/"); + + strcat(gfxpath, "zones/"); + strcat(gfxpath, name); + strcat(gfxpath, "/gfx/"); - /* "zones/" ++ filename */ - strncat(path, "zones/", 256); /* check for correctness */ - strncat(path, name, 256 - strlen("zones/")); - strncpy(zone_name, name, 32); + strcpy(zone_name, name); zone_name[31] = '\0'; - dirs = loader_readdir(path); + dirs = loader_readdir(stages); if(dirs == NULL){ printf("ERROR cant read dirs\n"); return -1; @@ -200,8 +216,8 @@ the name of the file will be used as the id ptr = dirs->next; while(ptr){ - filename = ptr->item; - if(load_stage(filename) < 0){ + stagepath = ptr->item; + if(load_stage(gfxpath, stagepath) < 0){ printf("ERROR cant load stage\n"); loader_freedirlist(dirs); return -1; diff --git a/text.c b/text.c index 5daf8f0..c25a864 100644 --- a/text.c +++ b/text.c @@ -156,13 +156,11 @@ void randomly_insert(vwchar* C[], int count){ } -int load_font(char* filename){ +int load_font(char* path){ //printf("load_font: loading %s\n",filename); - char buf[256] = "fonts/"; - strmcat(buf, filename, 256); - reader* rd = loader_open(buf); + reader* rd = loader_open(path); if(!rd){ - fatal_error("load_font: cannot open %s\n",filename); + fatal_error("load_font: cannot open %s\n", path); } char str[256]; diff --git a/video.c b/video.c index cb7755a..f84868b 100644 --- a/video.c +++ b/video.c @@ -202,10 +202,8 @@ void debug_surf(char* msg, SDL_Surface* surf){ -SDL_Surface* load_tga(char* filename){ - char path[256] = "gfx/"; +SDL_Surface* load_tga(char* path){ unsigned char header[18]; - strmcat(path, filename, 256); reader* rd = loader_open(path); int w, h, bpp; int mask; -- 2.11.4.GIT