While loops instead of for where possible & get rid of goto

This commit is contained in:
KikooDX 2021-04-08 00:21:26 +02:00
parent de1e819c83
commit 85dc08d013
4 changed files with 18 additions and 9 deletions

View File

@ -13,8 +13,11 @@ void level_draw(void)
int x;
int y;
const int tileset_width = img_tileset.width / TILE_WIDTH;
for (y = 0; y < level.height; y += 1)
for (x = 0; x < level.width; x += 1) {
y = level.height;
while (y --> 0) {
x = level.width;
while (x --> 0) {
const int draw_x = x * TILE_WIDTH;
const int draw_y = y * TILE_HEIGHT;
const Tile tile =
@ -24,4 +27,5 @@ void level_draw(void)
(int)(tile / tileset_width) * TILE_HEIGHT,
TILE_WIDTH, TILE_HEIGHT), draw_x, draw_y);
}
}
}

View File

@ -52,7 +52,8 @@ int main(void)
/* main game loop */
while (!keydown(KEY_EXIT)) {
/* do x updates per frame, x being ups/fps */
for (i = 0; i < TARGET_UPS / TARGET_FPS; i += 1) {
i = TARGET_UPS / TARGET_FPS;
while (i-- > 0) {
/* speed limiter */
while (!has_ticked)
sleep();

View File

@ -27,10 +27,11 @@ void player_collide(Tile collisions[COLLIDE_POINTS], int x, int y,
int player_collide_tile(Tile collisions[COLLIDE_POINTS], int x, int y,
Tile tile, int margin, int update)
{
int i;
if (update)
player_collide(collisions, x, y, margin);
for (i = 0; i < COLLIDE_POINTS; i += 1)
int i = COLLIDE_POINTS;
while (i --> 0)
if (collisions[i] == tile)
return 1;
return 0;

View File

@ -25,8 +25,10 @@ struct Player player_init(void)
};
/* find spawn position in level */
for (x = 0; x < level.width; x += 1)
for (y = 0; y < level.height; y += 1)
x = level.width;
while (x --> 0) {
y = level.height;
while (y --> 0) {
if (level.data[x + y * level.width] ==
TILE_START) {
player.x =
@ -34,8 +36,9 @@ struct Player player_init(void)
(TILE_WIDTH - PLAYER_WIDTH) / 2;
player.y = y * TILE_HEIGHT +
TILE_HEIGHT - PLAYER_HEIGHT;
goto found;
return player;
}
found:
}
}
return player;
}