Remove `vmult` lookup tables

There's really no need to put the y-multiplication in a lookup table.
The compiler will optimize the multiplication better than putting it in
a lookup table will.

To improve readability and to hardcode things less, the new
SCREEN_WIDTH_TILES and SCREEN_HEIGHT_TILES constant names are used, as
well as adding a new TILE_IDX macro to calculate the index of a tile in
a concatenated-rows (row-major in formal parlance) array. Also, tile
numbers are stored in a temporary variable to improve readability as
well (no more copy-pasting `contents[i + vmult[j]]` over and over
again).
This commit is contained in:
Misa 2021-09-24 16:37:27 -07:00
parent 491f3732aa
commit 6192269128
8 changed files with 64 additions and 64 deletions

View File

@ -12,6 +12,8 @@
#define SCREEN_WIDTH_PIXELS (SCREEN_WIDTH_TILES * 8)
#define SCREEN_HEIGHT_PIXELS (SCREEN_WIDTH_TILES * 8)
#define TILE_IDX(x, y) (x + y * SCREEN_WIDTH_TILES)
/* 4 bytes per char, for UTF-8 encoding. */
#define SCREEN_WIDTH_CHARS (SCREEN_WIDTH_TILES * 4)

View File

@ -9,6 +9,7 @@
#include <tinyxml2.h>
#include <utf8/unchecked.h>
#include "Constants.h"
#include "Editor.h"
#include "Enums.h"
#include "FileSystemUtils.h"
@ -35,6 +36,9 @@
#include <inttypes.h>
#endif
#define VMULT(y) (y * SCREEN_WIDTH_TILES * maxwidth)
#define Y_INBOUNDS(y) (y >= 0 && y < SCREEN_HEIGHT_TILES * maxheight)
RoomProperty::RoomProperty(void)
{
tileset=0;
@ -55,11 +59,6 @@ RoomProperty::RoomProperty(void)
customlevelclass::customlevelclass(void)
{
for (size_t i = 0; i < SDL_arraysize(vmult); i++)
{
vmult[i] = i * 40 * maxwidth;
}
reset();
}
@ -709,9 +708,9 @@ int customlevelclass::gettileidx(
int mult;
int idx;
if (INBOUNDS_ARR(yoff, vmult))
if (Y_INBOUNDS(yoff))
{
mult = vmult[yoff];
mult = VMULT(yoff);
}
else
{
@ -761,9 +760,9 @@ int customlevelclass::getabstile(const int x, const int y)
int idx;
int yoff;
if (INBOUNDS_ARR(y, vmult))
if (Y_INBOUNDS(y))
{
yoff = vmult[y];
yoff = VMULT(y);
}
else
{

View File

@ -134,7 +134,6 @@ public:
static const int maxwidth = 20, maxheight = 20; //Special; the physical max the engine allows
static const int numrooms = maxwidth * maxheight;
int contents[40 * 30 * numrooms];
int vmult[30 * maxheight];
int numtrinkets(void);
int numcrewmates(void);
RoomProperty roomproperties[numrooms]; //Maxwidth*maxheight

View File

@ -3,6 +3,7 @@
#include <utf8/unchecked.h>
#include "Constants.h"
#include "CustomLevels.h"
#include "Entity.h"
#include "Exit.h"
@ -2641,7 +2642,8 @@ void Graphics::drawmap(void)
{
for (int i = 0; i < 40; i++)
{
if(map.contents[i + map.vmult[j]]>0) drawforetile(i * 8, j * 8, map.contents[i + map.vmult[j]]);
const int tile = map.contents[TILE_IDX(i, j)];
if(tile>0) drawforetile(i * 8, j * 8, tile);
}
}
}
@ -2651,7 +2653,8 @@ void Graphics::drawmap(void)
{
for (int it = 0; it < 40; it++)
{
if(map.contents[it + map.vmult[jt]]>0) drawforetile2(it * 8, jt * 8, map.contents[it + map.vmult[jt]]);
const int tile = map.contents[TILE_IDX(it, jt)];
if(tile>0) drawforetile2(it * 8, jt * 8, tile);
}
}
}
@ -2661,7 +2664,8 @@ void Graphics::drawmap(void)
{
for (int i = 0; i < 40; i++)
{
if(map.contents[i + map.vmult[j]]>0) drawforetile3(i * 8, j * 8, map.contents[i + map.vmult[j]],map.rcol);
const int tile = map.contents[TILE_IDX(i, j)];
if(tile>0) drawforetile3(i * 8, j * 8, tile,map.rcol);
}
}
}
@ -2678,14 +2682,14 @@ void Graphics::drawfinalmap(void)
if(map.tileset==0){
for (int j = 0; j < 30; j++) {
for (int i = 0; i < 40; i++) {
if((map.contents[i + map.vmult[j]])>0)
if((map.contents[TILE_IDX(i, j)])>0)
drawforetile(i * 8, j * 8, map.finalat(i,j));
}
}
}else if (map.tileset == 1) {
for (int j = 0; j < 30; j++) {
for (int i = 0; i < 40; i++) {
if((map.contents[i + map.vmult[j]])>0)
if((map.contents[TILE_IDX(i, j)])>0)
drawforetile2(i * 8, j * 8, map.finalat(i,j));
}
}

View File

@ -1,6 +1,7 @@
#define MAP_DEFINITION
#include "Map.h"
#include "Constants.h"
#include "CustomLevels.h"
#include "Entity.h"
#include "Game.h"
@ -57,11 +58,6 @@ mapclass::mapclass(void)
//This needs to be in map instead!
invincibility = false;
//We init the lookup table:
for (size_t i = 0; i < SDL_arraysize(vmult); i++)
{
vmult[i] = i * 40;
}
//We create a blank map
SDL_memset(contents, 0, sizeof(contents));
@ -473,7 +469,8 @@ void mapclass::initcustommapdata(void)
int mapclass::finalat(int x, int y)
{
//return the tile index of the final stretch tiles offset by the colour difference
if (contents[x + vmult[y]] == 740)
const int tile = contents[TILE_IDX(x, y)];
if (tile == 740)
{
//Special case: animated tiles
if (final_mapcol == 1)
@ -500,16 +497,16 @@ int mapclass::finalat(int x, int y)
{
offset = final_aniframe * 40;
}
return contents[x + vmult[y]] - (final_mapcol * 3) + offset;
return tile - (final_mapcol * 3) + offset;
}
}
else if (contents[x + vmult[y]] >= 80)
else if (tile >= 80)
{
return contents[x + vmult[y]] - (final_mapcol * 3);
return tile - (final_mapcol * 3);
}
else
{
return contents[x + vmult[y]];
return tile;
}
}
@ -694,35 +691,39 @@ bool mapclass::collide(int x, int y)
}
else if (tileset == 2)
{
int tile;
if (y == -1) return collide(x, y + 1);
if (y == 29+extrarow) return collide(x, y - 1);
if (x == -1) return collide(x + 1, y);
if (x == 40) return collide(x - 1, y);
if (x < 0 || y < 0 || x >= 40 || y >= 29 + extrarow) return false;
if (contents[x + vmult[y]] >= 12 && contents[x + vmult[y]] <= 27) return true;
tile = contents[TILE_IDX(x, y)];
if (tile >= 12 && tile <= 27) return true;
if (invincibility)
{
if (contents[x + vmult[y]] >= 6 && contents[x + vmult[y]] <= 11) return true;
if (tile >= 6 && tile <= 11) return true;
}
}
else
{
int tile;
if (y == -1) return collide(x, y + 1);
if (y == 29+extrarow) return collide(x, y - 1);
if (x == -1) return collide(x + 1, y);
if (x == 40) return collide(x - 1, y);
if (x < 0 || y < 0 || x >= 40 || y >= 29+extrarow) return false;
if (contents[x + vmult[y]] == 1) return true;
if (tileset==0 && contents[x + vmult[y]] == 59) return true;
if (contents[x + vmult[y]]>= 80 && contents[x + vmult[y]] < 680) return true;
if (contents[x + vmult[y]] == 740 && tileset==1) return true;
tile = contents[TILE_IDX(x, y)];
if (tile == 1) return true;
if (tileset==0 && tile == 59) return true;
if (tile>= 80 && tile < 680) return true;
if (tile == 740 && tileset==1) return true;
if (invincibility)
{
if (contents[x + vmult[y]]>= 6 && contents[x + vmult[y]] <= 9) return true;
if (contents[x + vmult[y]]>= 49 && contents[x + vmult[y]] <= 50) return true;
if (tile>= 6 && tile <= 9) return true;
if (tile>= 49 && tile <= 50) return true;
if (tileset == 1)
{
if (contents[x + vmult[y]]>= 49 && contents[x + vmult[y]] < 80) return true;
if (tile>= 49 && tile < 80) return true;
}
}
}
@ -733,7 +734,7 @@ void mapclass::settile(int xp, int yp, int t)
{
if (xp >= 0 && xp < 40 && yp >= 0 && yp < 29+extrarow)
{
contents[xp + vmult[yp]] = t;
contents[TILE_IDX(xp, yp)] = t;
}
}
@ -1995,20 +1996,21 @@ void mapclass::loadlevel(int rx, int ry)
{
for (int i = 0; i < 40; i++)
{
int tile = contents[TILE_IDX(i, j)];
//Damage blocks
if(tileset==0)
{
if (contents[i + vmult[j]] == 6 || contents[i + vmult[j]] == 8)
if (tile == 6 || tile == 8)
{
//sticking up
obj.createblock(2, (i * 8), (j * 8)+4, 8, 4);
}
if (contents[i + vmult[j]] == 7 || contents[i + vmult[j]] == 9)
if (tile == 7 || tile == 9)
{
//Sticking down
obj.createblock(2, (i * 8), (j * 8), 8, 4);
}
if (contents[i + vmult[j]] == 49 || contents[i + vmult[j]] == 50)
if (tile == 49 || tile == 50)
{
//left or right
obj.createblock(2, (i * 8), (j * 8)+3, 8, 2);
@ -2016,13 +2018,13 @@ void mapclass::loadlevel(int rx, int ry)
}
else if(tileset==1)
{
if ((contents[i + vmult[j]] >= 63 && contents[i + vmult[j]] <= 74) ||
(contents[i + vmult[j]] >= 6 && contents[i + vmult[j]] <= 9))
if ((tile >= 63 && tile <= 74) ||
(tile >= 6 && tile <= 9))
{
//sticking up) {
if (contents[i + vmult[j]] < 10) contents[i + vmult[j]]++;
if (tile < 10) tile++;
//sticking up
if(contents[i + vmult[j]]%2==0)
if(tile%2==0)
{
obj.createblock(2, (i * 8), (j * 8), 8, 4);
}
@ -2031,9 +2033,9 @@ void mapclass::loadlevel(int rx, int ry)
//Sticking down
obj.createblock(2, (i * 8), (j * 8) + 4, 8, 4);
}
if (contents[i + vmult[j]] < 11) contents[i + vmult[j]]--;
if (tile < 11) tile--;
}
if (contents[i + vmult[j]] >= 49 && contents[i + vmult[j]] <= 62)
if (tile >= 49 && tile <= 62)
{
//left or right
obj.createblock(2, (i * 8), (j * 8)+3, 8, 2);
@ -2041,27 +2043,27 @@ void mapclass::loadlevel(int rx, int ry)
}
else if(tileset==2)
{
if (contents[i + vmult[j]] == 6 || contents[i + vmult[j]] == 8)
if (tile == 6 || tile == 8)
{
//sticking up
obj.createblock(2, (i * 8), (j * 8)+4, 8, 4);
}
if (contents[i + vmult[j]] == 7 || contents[i + vmult[j]] == 9)
if (tile == 7 || tile == 9)
{
//Sticking down
obj.createblock(2, (i * 8), (j * 8), 8, 4);
}
}
//Breakable blocks
if (contents[i + vmult[j]] == 10)
if (tile == 10)
{
contents[i + vmult[j]] = 0;
tile = 0;
obj.createentity(i * 8, j * 8, 4);
}
//Directional blocks
if (contents[i + vmult[j]] >= 14 && contents[i + vmult[j]] <= 17)
if (tile >= 14 && tile <= 17)
{
obj.createblock(3, i * 8, j * 8, 8, 8, contents[i + vmult[j]]-14);
obj.createblock(3, i * 8, j * 8, 8, 8, tile-14);
}
}
}

View File

@ -95,7 +95,6 @@ public:
static const int areamap[20 * 20];
int contents[40 * 30];
bool explored[20 * 20];
int vmult[30];
bool isexplored(const int rx, const int ry);
void setexplored(const int rx, const int ry, const bool status);

View File

@ -3,16 +3,12 @@
#include <SDL_stdinc.h>
#include <stddef.h>
#include "Constants.h"
#include "MakeAndPlay.h"
towerclass::towerclass(void)
{
minitowermode = false;
//We init the lookup table:
for (size_t i = 0; i < SDL_arraysize(vmult); i++)
{
vmult[i] = i * 40;
}
//We create a blank map
SDL_memset(contents, 0, sizeof(contents));
SDL_memset(back, 0, sizeof(back));
@ -33,7 +29,7 @@ int towerclass::backat(int xp, int yp, int yoff)
{
while (yp < 0) yp += 120;
while (yp >= 120) yp -= 120;
return back[xp + vmult[yp]];
return back[TILE_IDX(xp, yp)];
}
return 0;
}
@ -55,15 +51,15 @@ int towerclass::at(int xp, int yp, int yoff)
while (yp >= 700) yp -= 700;
if (xp >= 0 && xp < 40)
{
return contents[xp + vmult[yp]];
return contents[TILE_IDX(xp, yp)];
}
else if (xp == -1)
{
return contents[vmult[yp]];
return contents[TILE_IDX(0, yp)];
}
else if (xp == 40)
{
return contents[39 + vmult[yp]];
return contents[TILE_IDX(39, yp)];
}
return 0;
}
@ -80,15 +76,15 @@ int towerclass::miniat(int xp, int yp, int yoff)
while (yp >= 100) yp -= 100;
if (xp >= 0 && xp < 40)
{
return minitower[xp + vmult[yp]];
return minitower[TILE_IDX(xp, yp)];
}
else if (xp == -1)
{
return minitower[vmult[yp]];
return minitower[TILE_IDX(0, yp)];
}
else if (xp == 40)
{
return minitower[39 + vmult[yp]];
return minitower[TILE_IDX(39, yp)];
}
return 0;
}

View File

@ -23,7 +23,6 @@ public:
short back[40 * 120];
short contents[40 * 700];
short minitower[40 * 100];
int vmult[40 * 700];
bool minitowermode;
};