ref: 9513a04f45fb0c8ed89b2a5efc8082853a341177
parent: 0c245b995f3702ace7d7c3f3be56097176d89278
author: Clownacy <Clownacy@users.noreply.github.com>
date: Fri Nov 15 09:24:21 EST 2019
Clean-up Map.cpp
--- a/src/Map.cpp
+++ b/src/Map.cpp
@@ -18,7 +18,7 @@
static const char *code_pxma = "PXM";
-BOOL InitMapData2()
+BOOL InitMapData2(void)
{
gMap.data = (unsigned char*)malloc(PXM_BUFFER_SIZE);
return TRUE;
@@ -41,34 +41,28 @@
char check[3];
fread(check, 1, 3, fp);
- if (memcmp(check, code_pxma, 3) != 0)
+ if (memcmp(check, code_pxma, 3))
{
fclose(fp);
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)
- {
- fclose(fp);
- return FALSE;
- }
- else
- {
- // Read tile data
- fread(gMap.data, 1, gMap.length * gMap.width, fp);
- fclose(fp);
- return TRUE;
- }
+ 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)
+ {
+ fclose(fp);
+ return FALSE;
}
- return FALSE;
+ // Read tile data
+ fread(gMap.data, 1, gMap.width * gMap.length, fp);
+ fclose(fp);
+ return TRUE;
}
BOOL LoadAttributeData(const char *path_atrb)
@@ -82,17 +76,17 @@
return FALSE;
// Read data
- fread(gMap.atrb, 1, 0x100, fp);
+ fread(gMap.atrb, 1, sizeof(gMap.atrb), fp);
fclose(fp);
return TRUE;
}
-void EndMapData()
+void EndMapData(void)
{
free(gMap.data);
}
-void ReleasePartsImage()
+void ReleasePartsImage(void)
{
ReleaseSurface(SURFACE_ID_LEVEL_TILESET);
}
@@ -99,11 +93,13 @@
void GetMapData(unsigned char **data, short *mw, short *ml)
{
- if (data)
+ if (data != NULL)
*data = gMap.data;
- if (mw)
+
+ if (mw != NULL)
*mw = gMap.width;
- if (ml)
+
+ if (ml != NULL)
*ml = gMap.length;
}
@@ -112,7 +108,7 @@
if (x < 0 || y < 0 || x >= gMap.width || y >= gMap.length)
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];
}
@@ -128,13 +124,15 @@
BOOL ChangeMapParts(int x, int y, unsigned char no)
{
+ int i;
+
if (*(gMap.data + x + (y * gMap.width)) == no)
return FALSE;
*(gMap.data + x + (y * gMap.width)) = no;
- for (int i = 0; i < 3; i++)
- SetNpChar(4, x * 0x200 * 0x10, y * 0x200 * 0x10, 0, 0, 0, 0, 0);
+ for (i = 0; i < 3; ++i)
+ SetNpChar(4, x * 0x200 * 0x10, y * 0x200 * 0x10, 0, 0, 0, NULL, 0);
return TRUE;
}
@@ -154,15 +152,15 @@
// Get range to draw
num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1;
num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1;
- put_x = (fx / 0x200 + 8) / 16;
- put_y = (fy / 0x200 + 8) / 16;
+ put_x = ((fx / 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
- offset = i + j * gMap.width;
+ offset = (j * gMap.width) + i;
atrb = GetAttribute(i, j);
if (atrb >= 0x20)
@@ -169,12 +167,12 @@
continue;
// Draw tile
- rect.left = 16 * (gMap.data[offset] % 16);
- rect.top = 16 * (gMap.data[offset] / 16);
+ rect.left = (gMap.data[offset] % 16) * 16;
+ rect.top = (gMap.data[offset] / 16) * 16;
rect.right = rect.left + 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,15 +193,15 @@
// Get range to draw
num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1;
num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1;
- put_x = (fx / 0x200 + 8) / 16;
- put_y = (fy / 0x200 + 8) / 16;
+ put_x = ((fx / 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
- offset = i + j * gMap.width;
+ offset = (j * gMap.width) + i;
atrb = GetAttribute(i, j);
if (atrb < 0x40 || atrb >= 0x80)
@@ -210,15 +208,15 @@
continue;
// Draw tile
- rect.left = 16 * (gMap.data[offset] % 16);
- rect.top = 16 * (gMap.data[offset] / 16);
+ rect.left = (gMap.data[offset] % 16) * 16;
+ rect.top = (gMap.data[offset] / 16) * 16;
rect.right = rect.left + 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)
- 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 @@
// Get range to draw
num_x = ((WINDOW_WIDTH + (16 - 1)) / 16) + 1;
num_y = ((WINDOW_HEIGHT + (16 - 1)) / 16) + 1;
- put_x = (fx / 0x200 + 8) / 16;
- put_y = (fy / 0x200 + 8) / 16;
+ put_x = ((fx / 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
- offset = i + j * gMap.width;
+ offset = (j * gMap.width) + i;
atrb = GetAttribute(i, j);
if (atrb != 0x80
@@ -272,6 +270,7 @@
rect.top = 48;
rect.bottom = rect.top + 16;
break;
+
case 129:
case 161:
rect.left = 224;
@@ -279,6 +278,7 @@
rect.top = 48 + (count % 16);
rect.bottom = rect.top + 16;
break;
+
case 130:
case 162:
rect.left = 240 - (count % 16);
@@ -286,6 +286,7 @@
rect.top = 48;
rect.bottom = rect.top + 16;
break;
+
case 131:
case 163:
rect.left = 224;
@@ -295,7 +296,7 @@
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);
}
}
}
--- a/src/Map.h
+++ b/src/Map.h
@@ -2,21 +2,21 @@
#include "WindowsWrapper.h"
-struct MAP_DATA
+typedef struct MAP_DATA
{
unsigned char *data;
- unsigned char atrb[0x101]; //Why is this 257 bytes?
+ unsigned char atrb[0x100];
short width;
short length;
-};
+} MAP_DATA;
extern MAP_DATA gMap;
-BOOL InitMapData2();
+BOOL InitMapData2(void);
BOOL LoadMapData2(const char *path_map);
BOOL LoadAttributeData(const char *path_atrb);
-void EndMapData();
-void ReleasePartsImage();
+void EndMapData(void);
+void ReleasePartsImage(void);
void GetMapData(unsigned char **data, short *mw, short *ml);
unsigned char GetAttribute(int x, int y);
void DeleteMapParts(int x, int y);