From 79d09d11849a044deff08778b6934864974a8413 Mon Sep 17 00:00:00 2001 From: Wei-ju Wu Date: Tue, 14 Jun 2016 20:53:18 -0700 Subject: [PATCH] moved the map reading function to common --- scrolling_tricks/common.c | 36 +++++++++++++++++++++++++ scrolling_tricks/common.h | 19 +++++++++++++ scrolling_tricks/map.h | 12 --------- scrolling_tricks/xlimited.c | 50 ++++++----------------------------- scrolling_tricks/xunlimited.c | 43 ++++++------------------------ 5 files changed, 71 insertions(+), 89 deletions(-) delete mode 100644 scrolling_tricks/map.h diff --git a/scrolling_tricks/common.c b/scrolling_tricks/common.c index ef8ced7..e27f24c 100644 --- a/scrolling_tricks/common.c +++ b/scrolling_tricks/common.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "global_defs.h" @@ -68,3 +69,38 @@ BOOL get_arguments(struct PrgOptions *options, char *s) } return TRUE; } + +BOOL read_level_map(struct LevelMap *level_map, char *s) +{ + LONG l; + BPTR fhandle; + struct RawMap *raw_map; + + if (!(fhandle = Open(MAPNAME, MODE_OLDFILE))) + { + Fault(IoErr(), 0, s, 255); + return FALSE; + } + + Seek(fhandle, 0, OFFSET_END); + l = Seek(fhandle, 0, OFFSET_BEGINNING); + + if (!(raw_map = AllocVec(l, MEMF_PUBLIC))) { + strcpy(s, "Out of memory!"); + if (fhandle) Close(fhandle); + return FALSE; + } + + if (Read(fhandle, raw_map, l) != l) + { + Fault(IoErr(), 0, s, 255); + if (fhandle) Close(fhandle); + return FALSE; + } + Close(fhandle); + + level_map->data = raw_map->data; + level_map->width = raw_map->mapwidth; + level_map->height = raw_map->mapheight; + return TRUE; +} diff --git a/scrolling_tricks/common.h b/scrolling_tricks/common.h index 33f0901..ddd43bb 100644 --- a/scrolling_tricks/common.h +++ b/scrolling_tricks/common.h @@ -18,7 +18,26 @@ struct FetchInfo WORD scrollpixels; }; +struct RawMap +{ + WORD mapwidth; + WORD mapheight; + WORD maplayers; + WORD blockwidth; + WORD blockheight; + BYTE bytesperblock; + BYTE transparentblock; + UBYTE data[1]; +}; + +struct LevelMap { + LONG width, height; + UBYTE *data; + struct RawMap *raw_map; +}; + extern ULONG get_mode_id(BOOL option_how, BOOL option_ntsc); extern BOOL get_arguments(struct PrgOptions *options, char *s); +extern BOOL read_level_map(struct LevelMap *level_map, char *s); #endif /* __COMMON_H__ */ diff --git a/scrolling_tricks/map.h b/scrolling_tricks/map.h deleted file mode 100644 index 993452a..0000000 --- a/scrolling_tricks/map.h +++ /dev/null @@ -1,12 +0,0 @@ -struct RawMap -{ - WORD mapwidth; - WORD mapheight; - WORD maplayers; - WORD blockwidth; - WORD blockheight; - BYTE bytesperblock; - BYTE transparentblock; - UBYTE data[1]; -}; - diff --git a/scrolling_tricks/xlimited.c b/scrolling_tricks/xlimited.c index 73f1b58..910d7c9 100644 --- a/scrolling_tricks/xlimited.c +++ b/scrolling_tricks/xlimited.c @@ -16,8 +16,6 @@ #include "hardware.h" #include "cop.h" -#include "map.h" - #include "global_defs.h" #include "common.h" @@ -39,7 +37,6 @@ struct Screen *scr; struct RastPort *ScreenRastPort; struct BitMap *BlocksBitmap,*ScreenBitmap; -struct RawMap *Map; UBYTE *frontbuffer,*blocksbuffer; WORD mapposx,videoposx; @@ -48,8 +45,7 @@ BYTE previous_direction; WORD *savewordpointer; WORD saveword; -LONG mapwidth,mapheight; -UBYTE *mapdata; +struct LevelMap level_map; UWORD colors[BLOCKSCOLORS]; @@ -109,43 +105,12 @@ static void Cleanup (char *msg) FreeBitMap(BlocksBitmap); } - if (Map) FreeVec(Map); + if (level_map.raw_map) FreeVec(level_map.raw_map); + if (MyHandle) Close(MyHandle); exit(rc); } -static void OpenMap(void) -{ - LONG l; - - if (!(MyHandle = Open(MAPNAME,MODE_OLDFILE))) - { - Fault(IoErr(),0,s,255); - Cleanup(s); - } - - Seek(MyHandle,0,OFFSET_END); - l = Seek(MyHandle,0,OFFSET_BEGINNING); - - if (!(Map = AllocVec(l,MEMF_PUBLIC))) - { - Cleanup("Out of memory!"); - } - - if (Read(MyHandle,Map,l) != l) - { - Fault(IoErr(),0,s,255); - Cleanup(s); - } - - Close(MyHandle); - MyHandle = 0; - - mapdata = Map->data; - mapwidth = Map->mapwidth; - mapheight = Map->mapheight; -} - static void OpenBlocks(void) { LONG l; @@ -184,7 +149,7 @@ static void OpenDisplay(void) ULONG modeid; bitmapheight = BITMAPHEIGHT + - (mapwidth / BITMAPBLOCKSPERROW / BLOCKSDEPTH) + 1 + + (level_map.width / BITMAPBLOCKSPERROW / BLOCKSDEPTH) + 1 + 3; if (!(ScreenBitmap = AllocBitMap(BITMAPWIDTH,bitmapheight,BLOCKSDEPTH,BMF_STANDARD | BMF_INTERLEAVED | BMF_CLEAR,0))) @@ -291,7 +256,7 @@ static void DrawBlock(LONG x, LONG y, LONG mapx, LONG mapy) x = (x / 8) & 0xFFFE; y = y * BITMAPBYTESPERROW; - block = mapdata[mapy * mapwidth + mapx]; + block = level_map.data[mapy * level_map.width + mapx]; mapx = (block % BLOCKSPERROW) * (BLOCKWIDTH / 8); mapy = (block / BLOCKSPERROW) * (BLOCKPLANELINES * BLOCKSBYTESPERROW); @@ -356,7 +321,7 @@ static void ScrollRight(void) { WORD mapx,mapy,x,y; - if (mapposx >= (mapwidth * BLOCKWIDTH - SCREENWIDTH - BLOCKWIDTH)) return; + if (mapposx >= (level_map.width * BLOCKWIDTH - SCREENWIDTH - BLOCKWIDTH)) return; mapx = mapposx / BLOCKWIDTH + BITMAPBLOCKSPERROW; mapy = mapposx & (NUMSTEPS - 1); @@ -480,8 +445,9 @@ int main(int argc, char **argv) { BOOL res = get_arguments(&options, s); if (!res) Cleanup(s); + res = read_level_map(&level_map, s); + if (!res) Cleanup(s); - OpenMap(); OpenBlocks(); OpenDisplay(); diff --git a/scrolling_tricks/xunlimited.c b/scrolling_tricks/xunlimited.c index 7b401ec..7b8b21c 100644 --- a/scrolling_tricks/xunlimited.c +++ b/scrolling_tricks/xunlimited.c @@ -16,7 +16,6 @@ #include "hardware.h" #include "cop.h" -#include "map.h" #include "global_defs.h" #include "common.h" @@ -37,13 +36,12 @@ struct Screen *scr; struct RastPort *ScreenRastPort; struct BitMap *BlocksBitmap, *ScreenBitmap; -struct RawMap *Map; + UBYTE *frontbuffer,*blocksbuffer; WORD mapposx,videoposx; -LONG mapwidth; -UBYTE *mapdata; +struct LevelMap level_map; UWORD colors[BLOCKSCOLORS]; @@ -82,38 +80,11 @@ static void Cleanup(char *msg) FreeBitMap(BlocksBitmap); } - if (Map) free(Map); + if (level_map.raw_map) free(level_map.raw_map); if (MyHandle) Close(MyHandle); exit(rc); } -static void OpenMap(void) -{ - LONG l; - - if (!(MyHandle = Open(MAPNAME,MODE_OLDFILE))) { - Fault(IoErr(),0,s,255); - Cleanup(s); - } - - Seek(MyHandle,0,OFFSET_END); - l = Seek(MyHandle,0,OFFSET_BEGINNING); - - if (!(Map = calloc(l, sizeof(UBYTE)))) { - Cleanup("Out of memory!"); - } - - if (Read(MyHandle,Map,l) != l) { - Fault(IoErr(),0,s,255); - Cleanup(s); - } - Close(MyHandle); - MyHandle = 0; - - mapdata = Map->data; - mapwidth = Map->mapwidth; -} - static void OpenBlocks(void) { LONG l; @@ -252,7 +223,7 @@ static void DrawBlock(LONG x, LONG y, LONG mapx, LONG mapy) x = x / 8; y = y * BITMAPBYTESPERROW; - block = mapdata[mapy * mapwidth + mapx]; + block = level_map.data[mapy * level_map.width + mapx]; mapx = (block % BLOCKSPERROW) * (BLOCKWIDTH / 8); mapy = (block / BLOCKSPERROW) * (BLOCKPLANELINES * BLOCKSBYTESPERROW); @@ -311,7 +282,7 @@ static void ScrollRight(void) { WORD mapx,mapy,x,y; - if (mapposx >= (mapwidth * BLOCKWIDTH - SCREENWIDTH - BLOCKWIDTH)) return; + if (mapposx >= (level_map.width * BLOCKWIDTH - SCREENWIDTH - BLOCKWIDTH)) return; mapx = mapposx / BLOCKWIDTH + HALFBITMAPBLOCKSPERROW; mapy = mapposx & (NUMSTEPS - 1); @@ -412,7 +383,9 @@ int main(int argc, char **argv) { BOOL res = get_arguments(&options, s); if (!res) Cleanup(s); - OpenMap(); + res = read_level_map(&level_map, s); + if (!res) Cleanup(s); + OpenBlocks(); OpenDisplay();