Clean-up Map.cpp

This commit is contained in:
Clownacy 2019-11-15 14:24:21 +00:00
parent 0c245b995f
commit 9513a04f45
2 changed files with 61 additions and 60 deletions

View file

@ -18,7 +18,7 @@ MAP_DATA gMap;
static const char *code_pxma = "PXM"; static const char *code_pxma = "PXM";
BOOL InitMapData2() BOOL InitMapData2(void)
{ {
gMap.data = (unsigned char*)malloc(PXM_BUFFER_SIZE); gMap.data = (unsigned char*)malloc(PXM_BUFFER_SIZE);
return TRUE; return TRUE;
@ -41,34 +41,28 @@ BOOL LoadMapData2(const char *path_map)
char check[3]; char check[3];
fread(check, 1, 3, fp); fread(check, 1, 3, fp);
if (memcmp(check, code_pxma, 3) != 0) if (memcmp(check, code_pxma, 3))
{ {
fclose(fp); fclose(fp);
return FALSE; return FALSE;
} }
else
{
fread(&dum, 1, 1, fp);
// Get width and height
// This fails on big-endian hardware, and platforms where short is not two bytes long.
fread(&gMap.width, 2, 1, fp);
fread(&gMap.length, 2, 1, fp);
if (gMap.data == NULL) fread(&dum, 1, 1, fp);
{ // Get width and height
fclose(fp); // This fails on big-endian hardware, and platforms where short is not two bytes long.
return FALSE; fread(&gMap.width, 2, 1, fp);
} fread(&gMap.length, 2, 1, fp);
else
{ if (gMap.data == NULL)
// Read tile data {
fread(gMap.data, 1, gMap.length * gMap.width, fp); fclose(fp);
fclose(fp); return FALSE;
return TRUE;
}
} }
return FALSE; // Read tile data
fread(gMap.data, 1, gMap.width * gMap.length, fp);
fclose(fp);
return TRUE;
} }
BOOL LoadAttributeData(const char *path_atrb) BOOL LoadAttributeData(const char *path_atrb)
@ -82,28 +76,30 @@ BOOL LoadAttributeData(const char *path_atrb)
return FALSE; return FALSE;
// Read data // Read data
fread(gMap.atrb, 1, 0x100, fp); fread(gMap.atrb, 1, sizeof(gMap.atrb), fp);
fclose(fp); fclose(fp);
return TRUE; return TRUE;
} }
void EndMapData() void EndMapData(void)
{ {
free(gMap.data); free(gMap.data);
} }
void ReleasePartsImage() void ReleasePartsImage(void)
{ {
ReleaseSurface(SURFACE_ID_LEVEL_TILESET); ReleaseSurface(SURFACE_ID_LEVEL_TILESET);
} }
void GetMapData(unsigned char **data, short *mw, short *ml) void GetMapData(unsigned char **data, short *mw, short *ml)
{ {
if (data) if (data != NULL)
*data = gMap.data; *data = gMap.data;
if (mw)
if (mw != NULL)
*mw = gMap.width; *mw = gMap.width;
if (ml)
if (ml != NULL)
*ml = gMap.length; *ml = gMap.length;
} }
@ -112,7 +108,7 @@ unsigned char GetAttribute(int x, int y)
if (x < 0 || y < 0 || x >= gMap.width || y >= gMap.length) if (x < 0 || y < 0 || x >= gMap.width || y >= gMap.length)
return 0; return 0;
const size_t a = *(gMap.data + x + (y * gMap.width)); // Yes, the original code really does do this const size_t a = *(gMap.data + x + (y * gMap.width)); // Yes, the original code really does do this instead of a regular array access
return gMap.atrb[a]; return gMap.atrb[a];
} }
@ -128,13 +124,15 @@ void ShiftMapParts(int x, int y)
BOOL ChangeMapParts(int x, int y, unsigned char no) BOOL ChangeMapParts(int x, int y, unsigned char no)
{ {
int i;
if (*(gMap.data + x + (y * gMap.width)) == no) if (*(gMap.data + x + (y * gMap.width)) == no)
return FALSE; return FALSE;
*(gMap.data + x + (y * gMap.width)) = no; *(gMap.data + x + (y * gMap.width)) = no;
for (int i = 0; i < 3; i++) for (i = 0; i < 3; ++i)
SetNpChar(4, x * 0x200 * 0x10, y * 0x200 * 0x10, 0, 0, 0, 0, 0); SetNpChar(4, x * 0x200 * 0x10, y * 0x200 * 0x10, 0, 0, 0, NULL, 0);
return TRUE; return TRUE;
} }
@ -154,27 +152,27 @@ void PutStage_Back(int fx, int fy)
// Get range to draw // Get range to draw
num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1; num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1;
num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1; num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1;
put_x = (fx / 0x200 + 8) / 16; put_x = ((fx / 0x200) + 8) / 16;
put_y = (fy / 0x200 + 8) / 16; put_y = ((fy / 0x200) + 8) / 16;
for (j = put_y; j < put_y + num_y; j++) for (j = put_y; j < put_y + num_y; ++j)
{ {
for (i = put_x; i < put_x + num_x; i++) for (i = put_x; i < put_x + num_x; ++i)
{ {
// Get attribute // Get attribute
offset = i + j * gMap.width; offset = (j * gMap.width) + i;
atrb = GetAttribute(i, j); atrb = GetAttribute(i, j);
if (atrb >= 0x20) if (atrb >= 0x20)
continue; continue;
// Draw tile // Draw tile
rect.left = 16 * (gMap.data[offset] % 16); rect.left = (gMap.data[offset] % 16) * 16;
rect.top = 16 * (gMap.data[offset] / 16); rect.top = (gMap.data[offset] / 16) * 16;
rect.right = rect.left + 16; rect.right = rect.left + 16;
rect.bottom = rect.top + 16; rect.bottom = rect.top + 16;
PutBitmap3(&grcGame, (i * 16 - 8) - (fx / 0x200), (j * 16 - 8) - (fy / 0x200), &rect, SURFACE_ID_LEVEL_TILESET); PutBitmap3(&grcGame, ((i * 16) - 8) - (fx / 0x200), ((j * 16) - 8) - (fy / 0x200), &rect, SURFACE_ID_LEVEL_TILESET);
} }
} }
} }
@ -195,30 +193,30 @@ void PutStage_Front(int fx, int fy)
// Get range to draw // Get range to draw
num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1; num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1;
num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1; num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1;
put_x = (fx / 0x200 + 8) / 16; put_x = ((fx / 0x200) + 8) / 16;
put_y = (fy / 0x200 + 8) / 16; put_y = ((fy / 0x200) + 8) / 16;
for (j = put_y; j < put_y + num_y; j++) for (j = put_y; j < put_y + num_y; ++j)
{ {
for (i = put_x; i < put_x + num_x; i++) for (i = put_x; i < put_x + num_x; ++i)
{ {
// Get attribute // Get attribute
offset = i + j * gMap.width; offset = (j * gMap.width) + i;
atrb = GetAttribute(i, j); atrb = GetAttribute(i, j);
if (atrb < 0x40 || atrb >= 0x80) if (atrb < 0x40 || atrb >= 0x80)
continue; continue;
// Draw tile // Draw tile
rect.left = 16 * (gMap.data[offset] % 16); rect.left = (gMap.data[offset] % 16) * 16;
rect.top = 16 * (gMap.data[offset] / 16); rect.top = (gMap.data[offset] / 16) * 16;
rect.right = rect.left + 16; rect.right = rect.left + 16;
rect.bottom = rect.top + 16; rect.bottom = rect.top + 16;
PutBitmap3(&grcGame, (i * 16 - 8) - (fx / 0x200), (j * 16 - 8) - (fy / 0x200), &rect, SURFACE_ID_LEVEL_TILESET); PutBitmap3(&grcGame, ((i * 16) - 8) - (fx / 0x200), ((j * 16) - 8) - (fy / 0x200), &rect, SURFACE_ID_LEVEL_TILESET);
if (atrb == 0x43) if (atrb == 0x43)
PutBitmap3(&grcGame, (i * 16 - 8) - (fx / 0x200), (j * 16 - 8) - (fy / 0x200), &rcSnack, SURFACE_ID_NPC_SYM); PutBitmap3(&grcGame, ((i * 16) - 8) - (fx / 0x200), ((j * 16) - 8) - (fy / 0x200), &rcSnack, SURFACE_ID_NPC_SYM);
} }
} }
} }
@ -242,15 +240,15 @@ void PutMapDataVector(int fx, int fy)
// Get range to draw // Get range to draw
num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1; num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1;
num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1; num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1;
put_x = (fx / 0x200 + 8) / 16; put_x = ((fx / 0x200) + 8) / 16;
put_y = (fy / 0x200 + 8) / 16; put_y = ((fy / 0x200) + 8) / 16;
for (j = put_y; j < put_y + num_y; j++) for (j = put_y; j < put_y + num_y; ++j)
{ {
for (i = put_x; i < put_x + num_x; i++) for (i = put_x; i < put_x + num_x; ++i)
{ {
// Get attribute // Get attribute
offset = i + j * gMap.width; offset = (j * gMap.width) + i;
atrb = GetAttribute(i, j); atrb = GetAttribute(i, j);
if (atrb != 0x80 if (atrb != 0x80
@ -272,6 +270,7 @@ void PutMapDataVector(int fx, int fy)
rect.top = 48; rect.top = 48;
rect.bottom = rect.top + 16; rect.bottom = rect.top + 16;
break; break;
case 129: case 129:
case 161: case 161:
rect.left = 224; rect.left = 224;
@ -279,6 +278,7 @@ void PutMapDataVector(int fx, int fy)
rect.top = 48 + (count % 16); rect.top = 48 + (count % 16);
rect.bottom = rect.top + 16; rect.bottom = rect.top + 16;
break; break;
case 130: case 130:
case 162: case 162:
rect.left = 240 - (count % 16); rect.left = 240 - (count % 16);
@ -286,6 +286,7 @@ void PutMapDataVector(int fx, int fy)
rect.top = 48; rect.top = 48;
rect.bottom = rect.top + 16; rect.bottom = rect.top + 16;
break; break;
case 131: case 131:
case 163: case 163:
rect.left = 224; rect.left = 224;
@ -295,7 +296,7 @@ void PutMapDataVector(int fx, int fy)
break; break;
} }
PutBitmap3(&grcGame, (i * 16 - 8) - (fx / 0x200), (j * 16 - 8) - (fy / 0x200), &rect, SURFACE_ID_CARET); PutBitmap3(&grcGame, ((i * 16) - 8) - (fx / 0x200), ((j * 16) - 8) - (fy / 0x200), &rect, SURFACE_ID_CARET);
} }
} }
} }

View file

@ -2,21 +2,21 @@
#include "WindowsWrapper.h" #include "WindowsWrapper.h"
struct MAP_DATA typedef struct MAP_DATA
{ {
unsigned char *data; unsigned char *data;
unsigned char atrb[0x101]; //Why is this 257 bytes? unsigned char atrb[0x100];
short width; short width;
short length; short length;
}; } MAP_DATA;
extern MAP_DATA gMap; extern MAP_DATA gMap;
BOOL InitMapData2(); BOOL InitMapData2(void);
BOOL LoadMapData2(const char *path_map); BOOL LoadMapData2(const char *path_map);
BOOL LoadAttributeData(const char *path_atrb); BOOL LoadAttributeData(const char *path_atrb);
void EndMapData(); void EndMapData(void);
void ReleasePartsImage(); void ReleasePartsImage(void);
void GetMapData(unsigned char **data, short *mw, short *ml); void GetMapData(unsigned char **data, short *mw, short *ml);
unsigned char GetAttribute(int x, int y); unsigned char GetAttribute(int x, int y);
void DeleteMapParts(int x, int y); void DeleteMapParts(int x, int y);