Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Remove most instances of float usage #341

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Evade2/Asteroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void Asteroid::loop(Process *me, Object *o) {
respawn(me, o);
return;
}
o->theta += 16;
o->theta += FXP_RADIANS(16);
o->vy = Camera::vy / 2;
me->sleep(1);
}
Expand Down
2 changes: 1 addition & 1 deletion Evade2/Attract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void Attract::typewriter(Process *me, Object *o) {
Graphics::drawVectorGraphic(Enemy::enemy_graphic(ad->enemy), 64.0, 24.0, 0.0, 2.0);
}
if (game_mode == MODE_CREDITS) {
Font::scale = .9 * 256;
Font::scale = 0xE6; /* 0.9 */
}
PGM_P p = ad->text;
BYTE x = ad->x, y = ad->y;
Expand Down
17 changes: 8 additions & 9 deletions Evade2/Boss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#include "img/boss_2_img.h"
#include "img/boss_3_img.h"

static const FLOAT z_dist = 256;
static const FLOAT frames = 32;
static const FXP_WORLD_COORD z_dist = 256;
uint16_t Boss::hit_points = 0;
UBYTE Boss::boss_type;

Expand Down Expand Up @@ -70,10 +69,10 @@ static void engage_player_random_xy(Object *o) {
// Font::printf(5, 15, "%f", o->y - Camera::y);

if (o->state == 1) {
o->theta += 5 + Game::difficulty;
o->theta += FXP_RADIANS(5 + Game::difficulty);
}
else {
o->theta -= 5 + Game::difficulty;
o->theta -= FXP_RADIANS(5 + Game::difficulty);
}
// Debug
// o->x = Camera::x;
Expand Down Expand Up @@ -114,7 +113,7 @@ static void randomize_flee(Object *o) {
o->vx = random(-7, 7);
o->z = Camera::z - 50;
o->vz = Camera::vz + (random(1, 7) * Game::difficulty);
o->theta = random(-180, 180);
o->theta = FXP_RADIANS(180) - FXP_RADIANS(random(0, 360));
}

static void engage_player_flee(Object *o) {
Expand Down Expand Up @@ -151,7 +150,7 @@ static void engage_player_flee(Object *o) {

// Copy of init_assault
static void init_orbit(Object *o, BOOL left) {
FLOAT angle = left ? 0 : (2 * PI);
float angle = left ? 0 : (2 * PI);
o->x = cos(angle) * 256;
o->z = Camera::z + sin(angle) * 256;
o->y = Camera::y + random(30, 90);
Expand All @@ -173,7 +172,7 @@ static void engage_player_orbit(Object *o) {
o->flags &= ~ORBIT_LEFT;
}
else {
o->theta -= 12;
o->theta -= FXP_RADIANS(12);
}
}
else {
Expand All @@ -184,11 +183,11 @@ static void engage_player_orbit(Object *o) {
o->flags |= ORBIT_LEFT;
}
else {
o->theta += 12;
o->theta += FXP_RADIANS(12);
}
}

FLOAT rad = RADIANS(o->state);
float rad = RADIANS(o->state);
o->x = cos(rad) * 512;
o->z = Camera::z + sin(rad) * 512;

Expand Down
2 changes: 1 addition & 1 deletion Evade2/Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void Bullet::run() {
ObjectManager::free(o);
}
else {
o->theta += o->state;
o->theta += FXP_RADIANS(o->state);
}
}
o = next;
Expand Down
12 changes: 6 additions & 6 deletions Evade2/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "Evade2.h"

FLOAT Camera::x = 0;
FLOAT Camera::y = 0;
FLOAT Camera::z = 0;
FXP_WORLD_COORD Camera::x = 0;
FXP_WORLD_COORD Camera::y = 0;
FXP_WORLD_COORD Camera::z = 0;

FLOAT Camera::vx = 0;
FLOAT Camera::vy = 0;
FLOAT Camera::vz = 0;
FXP_WORLD_COORD Camera::vx = 0;
FXP_WORLD_COORD Camera::vy = 0;
FXP_WORLD_COORD Camera::vz = 0;

void Camera::move() {
Camera::x += Camera::vx;
Expand Down
6 changes: 2 additions & 4 deletions Evade2/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ class Object;

class Camera {
public:
static FLOAT x, y;
static FLOAT z;
static FLOAT vx, vy;
static FLOAT vz;
static FXP_WORLD_COORD x, y, z;
static FXP_WORLD_COORD vx, vy, vz;

public:
static void move();
Expand Down
4 changes: 2 additions & 2 deletions Evade2/EBullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ void EBullet::run() {
}
else {
// Put a wild spin on the missile
o->theta += (o->lines == ebomb_img) ? o->x : 40;
o->theta += (o->lines == ebomb_img) ? FXP_RADIANS(o->x) : FXP_RADIANS(40);
}
}
o = next;
}
}

BOOL EBullet::fire(Object *oo, BYTE type) {
const FLOAT frames = 90 / Game::difficulty; // time to hit player (how many ticks)
const int16_t frames = 90 / Game::difficulty; // time to hit player (how many ticks)

if (game_mode != MODE_GAME) {
return FALSE;
Expand Down
26 changes: 13 additions & 13 deletions Evade2/Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ void fire(Object *o) {
}
}

#define DELTA_THETA 8
static void bank(Object *o, WORD delta = 45) {
#define DELTA_THETA FXP_RADIANS(8)
static void bank(Object *o, FXP_ANGLE delta = FXP_RADIANS(45)) {
if (o->flags & BANK_LEFT) {
o->theta -= DELTA_THETA;
if (o->theta < -delta) {
if (o->theta < delta) {
o->flags &= ~BANK_LEFT;
}
}
Expand All @@ -87,7 +87,7 @@ static void bank(Object *o, WORD delta = 45) {
* Initialize Object for assault enemy
*/
static void init_assault(Object *o, BOOL left) {
FLOAT angle = left ? 0 : (2 * PI);
float angle = left ? 0 : (2 * PI);
o->x = cos(angle) * 256;
o->z = Camera::z + sin(angle) * 256;
o->y = Camera::y; // + 64 - random(0, 128);
Expand All @@ -104,7 +104,7 @@ static void init_scout(Object *o) {
o->z = Camera::z + 1024;
o->vz = CAMERA_VZ - 12;
o->vx = o->vy = 0;
o->theta = random(-50, 50);
o->theta = FXP_RADIANS(90+50)-FXP_RADIANS(random(0, 100));
}

/**
Expand All @@ -130,7 +130,7 @@ void Enemy::init(Process *me, Object *o) {
o->flags &= ~OFLAG_COLLISION;
o->set_type(OTYPE_ENEMY);
o->timer = FIRE_TIME;
o->theta = 0;
o->theta = FXP_RADIANS(180);


// One enemy type enters per wave
Expand Down Expand Up @@ -209,8 +209,8 @@ void Enemy::run_away(Process *me, Object *o) {
else {
o->vz += o->state;
}
o->vx += o->vx > 0 ? .1 : -.1;
o->vy += o->vy > 0 ? .1 : -.1;
o->vx += o->vx > 0 ? (256*.001) : -(256*.001);
o->vy += o->vy > 0 ? (256*.001) : -(256*.001);
if (behind_camera(o) || (o->z - Camera::z) > 1024) {
respawn(me, o);
return;
Expand All @@ -234,7 +234,7 @@ void Enemy::evade(Process *me, Object *o) {
me->sleep(1, explode);
return;
}
bank(o, 15);
bank(o, FXP_RADIANS(15));
fire(o);
me->sleep(1);
}
Expand All @@ -250,7 +250,7 @@ void Enemy::seek(Process *me, Object *o) {
}
// bank(o);
fire(o);
o->theta += 8;
o->theta += FXP_RADIANS(8);
if (o->z - Camera::z < random(256, 512)) {
o->state = -1;
me->sleep(1, run_away);
Expand All @@ -274,7 +274,7 @@ void Enemy::orbit(Process *me, Object *o) {
o->flags &= ~ORBIT_LEFT;
}
else {
o->theta -= 12;
o->theta -= FXP_RADIANS(12);
}
}
else {
Expand All @@ -284,11 +284,11 @@ void Enemy::orbit(Process *me, Object *o) {
o->flags |= ORBIT_LEFT;
}
else {
o->theta += 12;
o->theta += FXP_RADIANS(12);
}
}

FLOAT rad = RADIANS(o->state);
float rad = RADIANS(o->state);
o->vy = (Camera::y > o->y) ? -2 : 2;
o->y = Camera::y;
o->x = cos(rad) * 256;
Expand Down
8 changes: 4 additions & 4 deletions Evade2/Evade2.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

// Modus Create LOGO splash screen first time
#define ENABLE_MODUS_LOGO
// #undef ENABLE_MODUS_LOGO
#undef ENABLE_MODUS_LOGO

// If FAST_LINE_ENABLE is set, the fast line drawing algorithm is used
// While it is a bit faster, it seems to have some rounding errors that
Expand Down Expand Up @@ -64,11 +64,11 @@ const int NUM_PROCESSES = 7;
const int NUM_STARS = 5;

// joystick up/down/left/right changes camera by DELTACONTROL
const float DELTACONTROL = 11;
const FXP_WORLD_COORD DELTACONTROL = 11;
// speed of camera flying through stars
const float CAMERA_VZ = 6;
const FXP_WORLD_COORD CAMERA_VZ = 6;
// speed of player bullets
const float BULLET_VZ = 15;
const FXP_WORLD_COORD BULLET_VZ = 15;

const BYTE MAX_BULLETS = 6;

Expand Down
41 changes: 21 additions & 20 deletions Evade2/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,18 @@ static const PROGMEM BYTE *const charset[] = {
NULL, // ``
};

WORD Font::scale = 0x100;
FXP_SSCALE Font::scale = 0x100;

#define SCALE(val) (((val)*scale)>>8)

#ifdef ENABLE_ROTATING_TEXT
BYTE Font::print_string_rotatedx(BYTE x, BYTE y, FLOAT theta, const __FlashStringHelper *ifsh) {
theta = float(theta) * 3.1415926 / 180;
FLOAT cost = cos(theta),
sint = sin(theta);
BYTE Font::print_string_rotatedx(BYTE x, BYTE y, FXP_ANGLE theta, const __FlashStringHelper *ifsh) {
//theta = float(theta) * 3.1415926 / 180;
float f_theta = RADIANS(theta / (UINT16_MAX/360));
float cost = cos(f_theta),
sint = sin(f_theta);
PGM_P p = reinterpret_cast<PGM_P>(ifsh);

FLOAT fscale = FLOAT(scale >> 8) + FLOAT(scale & 0xff) / 256.0;

const BYTE size = 9;

BYTE xo = x;
Expand All @@ -188,21 +189,21 @@ BYTE Font::print_string_rotatedx(BYTE x, BYTE y, FLOAT theta, const __FlashStrin
BYTE lines = pgm_read_byte(glyph++);

for (BYTE i = 0; i < lines; i++) {
FLOAT x0 = (BYTE)pgm_read_byte(glyph++) * fscale + x,
y0 = (BYTE)pgm_read_byte(glyph++) * fscale + y,
x1 = (BYTE)pgm_read_byte(glyph++) * fscale + x,
y1 = (BYTE)pgm_read_byte(glyph++) * fscale + y;
BYTE x0 = SCALE((BYTE)pgm_read_byte(glyph++)) + x,
y0 = SCALE((BYTE)pgm_read_byte(glyph++)) + y,
x1 = SCALE((BYTE)pgm_read_byte(glyph++)) + x,
y1 = SCALE((BYTE)pgm_read_byte(glyph++)) + y;

Graphics::drawLine(
x0,
((y0 - y) * sint + cost + y),
x1,
((y1 - y) * sint + cost + y));
}
x += size * fscale;
x += SCALE(size);
}
else {
x += 6 * fscale;
x += SCALE(6);
}
}
return x - xo;
Expand All @@ -213,21 +214,21 @@ BYTE Font::write(BYTE x, BYTE y, char c) {
PGM_P glyph;
const BYTE width = 9;

FLOAT fscale = FLOAT(scale >> 8) + FLOAT(scale & 0xff) / 256.0;
float fscale = float(scale >> 8) + float(scale & 0xff) / 256.0;
glyph = (PGM_P)pgm_read_word(&charset[toupper(c) - 32]);
if (glyph) {
BYTE lines = pgm_read_byte(glyph++);

for (BYTE i = 0; i < lines; i++) {
BYTE x0 = pgm_read_byte(glyph++),
y0 = pgm_read_byte(glyph++),
x1 = pgm_read_byte(glyph++),
y1 = pgm_read_byte(glyph++);
BYTE x0 = SCALE((BYTE)pgm_read_byte(glyph++)),
y0 = SCALE((BYTE)pgm_read_byte(glyph++)),
x1 = SCALE((BYTE)pgm_read_byte(glyph++)),
y1 = SCALE((BYTE)pgm_read_byte(glyph++));

Graphics::drawLine(x + x0 * fscale, y + y0 * fscale, x + x1 * fscale, y + y1 * fscale);
Graphics::drawLine(x + x0, y + y0, x + x1, y + y1);
}
}
return width * fscale;
return SCALE(width);
}

BYTE Font::print_string(BYTE x, BYTE y, char *s) {
Expand Down
2 changes: 1 addition & 1 deletion Evade2/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Font {
static BYTE write(BYTE x, BYTE y, char c);
static BYTE _printf(BYTE x, BYTE y, const __FlashStringHelper *ifsh, ...);
#ifdef ENABLE_ROTATING_TEXT
static BYTE print_string_rotatedx(BYTE x, BYTE y, FLOAT angle, const __FlashStringHelper *ifsh);
static BYTE print_string_rotatedx(BYTE x, BYTE y, FXP_ANGLE angle, const __FlashStringHelper *ifsh);
#endif
static BYTE print_string(BYTE x, BYTE y, char *s);
static BYTE print_long(BYTE x, BYTE y, LONG n, BYTE base = 10);
Expand Down
6 changes: 3 additions & 3 deletions Evade2/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void Game::run() {
}

struct game_data {
FLOAT theta;
FXP_ANGLE theta;
WORD timer;
};

Expand Down Expand Up @@ -138,7 +138,7 @@ void Game::get_ready(Process *me, Object *o) {

#ifdef ENABLE_ROTATING_TEXT
Font::print_string_rotatedx(30, 35, d->theta, F("GET READY!"));
d->theta += 12;
d->theta += FXP_RADIANS(12);
#else
Font::printf(30, 35, "GET READY!");
#endif
Expand All @@ -155,7 +155,7 @@ void Game::get_ready(Process *me, Object *o) {
void Game::entry(Process *me, Object *o) {
game_data *d = (game_data *)&o->x;
d->timer = 65;
d->theta = 90;
d->theta = FXP_RADIANS(90);
Sound::play_score(NEXT_WAVE_SONG);
me->sleep(1, get_ready);
}
1 change: 0 additions & 1 deletion Evade2/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Game {
// game difficulty
static UBYTE difficulty;
static UBYTE kills;
static FLOAT z_end;

public:
// run each game loop
Expand Down
2 changes: 1 addition & 1 deletion Evade2/GameOver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void GameOver::loop(Process *me, Object *o) {
me->suicide();
}
#ifdef ENABLE_ROTATING_TEXT
o->theta += 12;
o->theta += FXP_RADIANS(12);
Font::print_string_rotatedx(30, 20, o->theta, F("GAME OVER"));
Font::scale = .75 * 256;
Font::printf(Game::wave < 9 ? 18 : 13, 45, "WAVES SURVIVED: %d", Game::wave - 1);
Expand Down
Loading