moved the map reading function to common

This commit is contained in:
Wei-ju Wu 2016-06-14 20:53:18 -07:00
parent c18ccf30b7
commit 79d09d1184
5 changed files with 71 additions and 89 deletions

View File

@ -1,5 +1,6 @@
#include <clib/graphics_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <string.h>
#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;
}

View File

@ -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__ */

View File

@ -1,12 +0,0 @@
struct RawMap
{
WORD mapwidth;
WORD mapheight;
WORD maplayers;
WORD blockwidth;
WORD blockheight;
BYTE bytesperblock;
BYTE transparentblock;
UBYTE data[1];
};

View File

@ -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();

View File

@ -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();