1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-22 17:49:43 +01:00

Add POS_MOD macro and use for all positive modulos

This macro is to make it so it won't be error-prone to write the
semi-confusing `(a % b + b) % b` statement, and you can just use an easy
macro instead.

Currently, the only places a positive modulo is needed is when switching
tilesets, enemies, and warp directions in the editor, as well as when
getting a tile in the tower, since towers just repeat themselves
vertically. Towers used this weird while-loop to sort of emulate a
modulo, which isn't half-bad, but is unnecessary, and I don't think any
compiler would recognize it as a modulo. (And if it's not optimized to a
proper modulo... what happens if the number being moduloed is really,
really big?)
This commit is contained in:
Misa 2021-09-24 17:48:15 -07:00
parent 891ca527f9
commit cace685020
3 changed files with 12 additions and 9 deletions

View file

@ -4175,7 +4175,7 @@ void editorclass::switch_tileset(const bool reversed)
}
const int modulus = SDL_arraysize(tilesets);
tiles = (tiles % modulus + modulus) % modulus;
tiles = POS_MOD(tiles, modulus);
cl.setroomtileset(levx, levy, tiles);
clamp_tilecol(levx, levy, false);
@ -4270,7 +4270,7 @@ void editorclass::switch_enemy(const bool reversed)
}
const int modulus = 10;
enemy = (enemy % modulus + modulus) % modulus;
enemy = POS_MOD(enemy, modulus);
cl.setroomenemytype(levx, levy, enemy);
note = "Enemy Type Changed";
@ -4293,7 +4293,7 @@ void editorclass::switch_warpdir(const bool reversed)
++warpdir;
}
warpdir = (warpdir % modulus + modulus) % modulus;
warpdir = POS_MOD(warpdir, modulus);
cl.setroomwarpdir(levx, levy, warpdir);
switch (warpdir)

View file

@ -5,6 +5,7 @@
#include "Constants.h"
#include "MakeAndPlay.h"
#include "UtilityClass.h"
towerclass::towerclass(void)
{
@ -24,8 +25,7 @@ int towerclass::backat(int xp, int yp, int yoff)
if (xp >= 0 && xp < 40)
{
while (yp < 0) yp += 120;
while (yp >= 120) yp -= 120;
yp = POS_MOD(yp, 120);
return back[TILE_IDX(xp, yp)];
}
return 0;
@ -41,8 +41,7 @@ int towerclass::at(int xp, int yp, int yoff)
{
yp = (yp*8 + yoff) / 8;
while (yp < 0) yp += 700;
while (yp >= 700) yp -= 700;
yp = POS_MOD(yp, 700);
if (xp >= 0 && xp < 40)
{
return contents[TILE_IDX(xp, yp)];
@ -63,8 +62,7 @@ int towerclass::miniat(int xp, int yp, int yoff)
{
yp = (yp*8 + yoff) / 8;
while (yp < 0) yp += 100;
while (yp >= 100) yp -= 100;
yp = POS_MOD(yp, 100);
if (xp >= 0 && xp < 40)
{
return minitower[TILE_IDX(xp, yp)];

View file

@ -88,6 +88,11 @@ void _VVV_between(
} \
while (false)
/* Positive modulo, because C/C++'s modulo operator sucks and is negative given
* negative divisors.
* WARNING! This double- and triple- evaluates. */
#define POS_MOD(a, b) (((a) % (b) + (b)) % (b))
//helperClass
class UtilityClass
{