Skip to content

Commit

Permalink
Don't redefine standard complex macro; use a new cmplx typedef
Browse files Browse the repository at this point in the history
This also introduces `float32`, `float64`, and `real` typedefs to be
used in place of `float` and `double` later. `real` is for game code and
other places where we don't particularly care about the precision and
format of the underlying type, and is currently defined to `double`.
`float32` and `float64` should replace `float` and `double` respectively
  • Loading branch information
Akaricchi committed Nov 22, 2019
1 parent 78191b3 commit 80b1026
Show file tree
Hide file tree
Showing 68 changed files with 520 additions and 494 deletions.
12 changes: 6 additions & 6 deletions src/boss.c
Expand Up @@ -31,7 +31,7 @@ typedef struct SpellBonus {

static void calc_spell_bonus(Attack *a, SpellBonus *bonus);

Boss* create_boss(char *name, char *ani, complex pos) {
Boss* create_boss(char *name, char *ani, cmplx pos) {
Boss *boss = calloc(1, sizeof(Boss));

boss->name = strdup(name);
Expand Down Expand Up @@ -401,20 +401,20 @@ static void draw_spell_warning(Font *font, float y_pos, float f, float opacity)
float flash = 0.2 + 0.8 * pow(psin(M_PI + 5 * M_PI * f), 0.5);
f = 0.15 * f + 0.85 * (0.5 * pow(2 * f - 1, 3) + 0.5);
opacity *= 1 - 2 * fabs(f - 0.5);
complex pos = (VIEWPORT_W + msg_width) * f - msg_width * 0.5 + I * y_pos;
cmplx pos = (VIEWPORT_W + msg_width) * f - msg_width * 0.5 + I * y_pos;

draw_boss_text(ALIGN_CENTER, creal(pos), cimag(pos), msg, font, color_mul_scalar(RGBA(1, flash, flash, 1), opacity));
}

static void draw_spell_name(Boss *b, int time, bool healthbar_radial) {
Font *font = get_font("standard");

complex x0 = VIEWPORT_W/2+I*VIEWPORT_H/3.5;
cmplx x0 = VIEWPORT_W/2+I*VIEWPORT_H/3.5;
float f = clamp((time - 40.0) / 60.0, 0, 1);
float f2 = clamp(time / 80.0, 0, 1);
float y_offset = 26 + healthbar_radial * -15;
float y_text_offset = 5 - font_get_metrics(font)->descent;
complex x = x0 + ((VIEWPORT_W - 10) + I*(y_offset + y_text_offset) - x0) * f*(f+1)*0.5;
cmplx x = x0 + ((VIEWPORT_W - 10) + I*(y_offset + y_text_offset) - x0) * f*(f+1)*0.5;
int strw = text_width(font, b->current->name, 0);

float opacity_noplr = b->hud.spell_opacity * b->hud.global_opacity;
Expand Down Expand Up @@ -810,8 +810,8 @@ static void boss_rule_extra(Boss *boss, float alpha) {

for(int i = 0; i < cnt; ++i) {
float a = i*2*M_PI/cnt + global.frames / 100.0;
complex dir = cexp(I*(a+global.frames/50.0));
complex vel = dir * 3;
cmplx dir = cexp(I*(a+global.frames/50.0));
cmplx vel = dir * 3;
float v = max(0, alpha - 1);
float psina = psin(a);

Expand Down
6 changes: 3 additions & 3 deletions src/boss.h
Expand Up @@ -76,7 +76,7 @@ typedef struct AttackInfo {
BossRule rule;
BossRule draw_rule;

complex pos_dest;
cmplx pos_dest;
int bonus_rank;
} AttackInfo;

Expand Down Expand Up @@ -105,7 +105,7 @@ typedef struct Attack {

typedef struct Boss {
ENTITY_INTERFACE_NAMED(struct Boss, ent);
complex pos;
cmplx pos;

Attack *attacks;
Attack *current;
Expand Down Expand Up @@ -149,7 +149,7 @@ typedef struct Boss {
} hud;
} Boss;

Boss* create_boss(char *name, char *ani, complex pos) attr_nonnull(1, 2) attr_returns_nonnull;
Boss* create_boss(char *name, char *ani, cmplx pos) attr_nonnull(1, 2) attr_returns_nonnull;
void free_boss(Boss *boss) attr_nonnull(1);
void process_boss(Boss **boss) attr_nonnull(1);

Expand Down
1 change: 0 additions & 1 deletion src/color.c
Expand Up @@ -8,7 +8,6 @@

#include "taisei.h"

#include <stdio.h>
#include "color.h"

#define COLOR_OP(c1, op, c2) do { \
Expand Down
12 changes: 6 additions & 6 deletions src/enemy.c
Expand Up @@ -57,8 +57,8 @@ static void fix_pos0_visual(Enemy *e) {
e->pos0_visual = x + y * I;
}

Enemy *create_enemy_p(EnemyList *enemies, complex pos, float hp, EnemyVisualRule visual_rule, EnemyLogicRule logic_rule,
complex a1, complex a2, complex a3, complex a4) {
Enemy *create_enemy_p(EnemyList *enemies, cmplx pos, float hp, EnemyVisualRule visual_rule, EnemyLogicRule logic_rule,
cmplx a1, cmplx a2, cmplx a3, cmplx a4) {
if(IN_DRAW_CODE) {
log_fatal("Tried to spawn an enemy while in drawing code");
}
Expand Down Expand Up @@ -142,21 +142,21 @@ void delete_enemies(EnemyList *enemies) {
alist_foreach(enemies, _delete_enemy, NULL);
}

static complex enemy_visual_pos(Enemy *enemy) {
static cmplx enemy_visual_pos(Enemy *enemy) {
double t = (global.frames - enemy->birthtime) / 30.0;

if(t >= 1 || enemy->hp == ENEMY_IMMUNE) {
return enemy->pos;
}

complex p = enemy->pos - enemy->pos0;
cmplx p = enemy->pos - enemy->pos0;
p += t * enemy->pos0 + (1 - t) * enemy->pos0_visual;

return p;
}

static void call_visual_rule(Enemy *e, bool render) {
complex tmp = e->pos;
cmplx tmp = e->pos;
e->pos = enemy_visual_pos(e);
e->visual_rule(e, global.frames - e->birthtime, render);
e->pos = tmp;
Expand Down Expand Up @@ -216,7 +216,7 @@ int enemy_flare(Projectile *p, int t) { // a[0] velocity, a[1] ref to enemy
void BigFairy(Enemy *e, int t, bool render) {
if(!render) {
if(!(t % 5)) {
complex offset = (frand()-0.5)*30 + (frand()-0.5)*20.0*I;
cmplx offset = (frand()-0.5)*30 + (frand()-0.5)*20.0*I;

PARTICLE(
.sprite = "smoothdot",
Expand Down
12 changes: 6 additions & 6 deletions src/enemy.h
Expand Up @@ -36,9 +36,9 @@ enum {
struct Enemy {
ENTITY_INTERFACE_NAMED(Enemy, ent);

complex pos;
complex pos0;
complex pos0_visual;
cmplx pos;
cmplx pos0;
cmplx pos0_visual;

long birthtime;

Expand All @@ -51,7 +51,7 @@ struct Enemy {
float spawn_hp;
float hp;

complex args[RULE_ARGC];
cmplx args[RULE_ARGC];
float alpha;

#ifdef ENEMY_DEBUG
Expand All @@ -65,8 +65,8 @@ struct Enemy {
#define create_enemy1c(p,h,d,l,a1) create_enemy_p(&global.enemies,p,h,d,l,a1,0,0,0)

Enemy *create_enemy_p(
EnemyList *enemies, complex pos, float hp, EnemyVisualRule draw_rule, EnemyLogicRule logic_rule,
complex a1, complex a2, complex a3, complex a4
EnemyList *enemies, cmplx pos, float hp, EnemyVisualRule draw_rule, EnemyLogicRule logic_rule,
cmplx a1, cmplx a2, cmplx a3, cmplx a4
);

#ifdef ENEMY_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/entity.c
Expand Up @@ -185,7 +185,7 @@ DamageResult ent_damage(EntityInterface *ent, const DamageInfo *damage) {
return res;
}

void ent_area_damage(complex origin, float radius, const DamageInfo *damage, EntityAreaDamageCallback callback, void *callback_arg) {
void ent_area_damage(cmplx origin, float radius, const DamageInfo *damage, EntityAreaDamageCallback callback, void *callback_arg) {
for(Enemy *e = global.enemies.first; e; e = e->next) {
if(
cabs(origin - e->pos) < radius &&
Expand Down
4 changes: 2 additions & 2 deletions src/entity.h
Expand Up @@ -82,7 +82,7 @@ typedef void (*EntityDrawFunc)(EntityInterface *ent);
typedef bool (*EntityPredicate)(EntityInterface *ent);
typedef DamageResult (*EntityDamageFunc)(EntityInterface *target, const DamageInfo *damage);
typedef void (*EntityDrawHookCallback)(EntityInterface *ent, void *arg);
typedef void (*EntityAreaDamageCallback)(EntityInterface *ent, complex ent_origin, void *arg);
typedef void (*EntityAreaDamageCallback)(EntityInterface *ent, cmplx ent_origin, void *arg);

#define ENTITY_INTERFACE_BASE(typename) struct { \
LIST_INTERFACE(typename); \
Expand Down Expand Up @@ -144,7 +144,7 @@ void ent_register(EntityInterface *ent, EntityType type) attr_nonnull(1);
void ent_unregister(EntityInterface *ent) attr_nonnull(1);
void ent_draw(EntityPredicate predicate);
DamageResult ent_damage(EntityInterface *ent, const DamageInfo *damage) attr_nonnull(1, 2);
void ent_area_damage(complex origin, float radius, const DamageInfo *damage, EntityAreaDamageCallback callback, void *callback_arg) attr_nonnull(3);
void ent_area_damage(cmplx origin, float radius, const DamageInfo *damage, EntityAreaDamageCallback callback, void *callback_arg) attr_nonnull(3);
void ent_area_damage_ellipse(Ellipse ellipse, const DamageInfo *damage, EntityAreaDamageCallback callback, void *callback_arg) attr_nonnull(2);

void ent_hook_pre_draw(EntityDrawHookCallback callback, void *arg);
Expand Down
1 change: 1 addition & 0 deletions src/eventloop/executor_emscripten.c
Expand Up @@ -11,6 +11,7 @@
#include "eventloop_private.h"
#include "events.h"
#include "global.h"

#include <emscripten.h>

static FrameTimes frame_times;
Expand Down
4 changes: 2 additions & 2 deletions src/framerate.c
Expand Up @@ -20,7 +20,7 @@ void fpscounter_reset(FPSCounter *fps) {
fps->frametimes[i] = frametime;
}

fps->fps = HRTIME_RESOLUTION / (long double)frametime;
fps->fps = HRTIME_RESOLUTION / (float64x)frametime;
fps->frametime = frametime;
fps->last_update_time = time_get();
}
Expand All @@ -38,7 +38,7 @@ void fpscounter_update(FPSCounter *fps) {
avg += fps->frametimes[i];
}

fps->fps = HRTIME_RESOLUTION / (avg / (long double)log_size);
fps->fps = HRTIME_RESOLUTION / (avg / (float64x)log_size);
fps->frametime = avg / log_size;
fps->last_update_time = time_get();
}
Expand Down
1 change: 1 addition & 0 deletions src/gamepad.h
Expand Up @@ -12,6 +12,7 @@
#include "taisei.h"

#include <SDL.h>

#include "events.h"
#include "config.h"

Expand Down
36 changes: 18 additions & 18 deletions src/item.c
Expand Up @@ -46,7 +46,7 @@ static const char* item_indicator_sprite_name(ItemType type) {
[ITEM_SURGE - ITEM_FIRST] = NULL,
[ITEM_VOLTAGE - ITEM_FIRST] = "item/voltage_indicator",
};

uint index = type - 1;

assert(index < ARRAY_SIZE(map));
Expand All @@ -69,7 +69,7 @@ static void ent_draw_item(EntityInterface *ent) {
Item *i = ENT_CAST(ent, Item);

const int indicator_display_y = 6;

float y = cimag(i->pos);
if(y < 0) {
Sprite *s = item_indicator_sprite(i->type);
Expand All @@ -85,12 +85,12 @@ static void ent_draw_item(EntityInterface *ent) {
}
}


float alpha = 1;
if(i->type == ITEM_PIV && !i->auto_collect) {
alpha *= clamp(2.0 - (global.frames - i->birthtime) / 60.0, 0.1, 1.0);
}

Color *c = RGBA_MUL_ALPHA(1, 1, 1, alpha);

r_draw_sprite(&(SpriteParams) {
Expand All @@ -101,7 +101,7 @@ static void ent_draw_item(EntityInterface *ent) {

}

Item* create_item(complex pos, complex v, ItemType type) {
Item* create_item(cmplx pos, cmplx v, ItemType type) {
if((creal(pos) < 0 || creal(pos) > VIEWPORT_W)) {
// we need this because we clamp the item position to the viewport boundary during motion
// e.g. enemies that die offscreen shouldn't spawn any items inside the viewport
Expand Down Expand Up @@ -135,7 +135,7 @@ void delete_item(Item *item) {
objpool_release(stage_object_pools.items, alist_unlink(&global.items, item));
}

Item *create_clear_item(complex pos, uint clear_flags) {
Item *create_clear_item(cmplx pos, uint clear_flags) {
ItemType type = ITEM_PIV;

if(clear_flags & CLEAR_HAZARDS_SPAWN_VOLTAGE) {
Expand Down Expand Up @@ -166,23 +166,23 @@ void delete_items(void) {
}
}

static complex move_item(Item *i) {
static cmplx move_item(Item *i) {
int t = global.frames - i->birthtime;
complex lim = 0 + 2.0*I;
cmplx lim = 0 + 2.0*I;

complex oldpos = i->pos;
cmplx oldpos = i->pos;

if(i->auto_collect && i->collecttime <= global.frames && global.frames - i->birthtime > 20) {
i->pos -= (7+i->auto_collect)*cexp(I*carg(i->pos - global.plr.pos));
} else {
i->pos = i->pos0 + log(t/5.0 + 1)*5*(i->v + lim) + lim*t;

complex v = i->pos - oldpos;
cmplx v = i->pos - oldpos;
double half = item_sprite(i->type)->w/2.0;
bool over = false;

if((over = creal(i->pos) > VIEWPORT_W-half) || creal(i->pos) < half) {
complex normal = over ? -1 : 1;
cmplx normal = over ? -1 : 1;
v -= 2 * normal * (creal(normal)*creal(v));
v = 1.5*creal(v) - I*fabs(cimag(v));

Expand Down Expand Up @@ -280,7 +280,7 @@ void process_items(void) {
}
}

complex deltapos = move_item(item);
cmplx deltapos = move_item(item);
int v = may_collect ? collision_item(item) : 0;

if(v == 1) {
Expand Down Expand Up @@ -346,7 +346,7 @@ int collision_item(Item *i) {
return 0;
}

static void spawn_item_internal(complex pos, ItemType type, float collect_value) {
static void spawn_item_internal(cmplx pos, ItemType type, float collect_value) {
tsrand_fill(2);
Item *i = create_item(pos, (12 + 6 * afrand(0)) * (cexp(I*(3*M_PI/2 + anfrand(1)*M_PI/11))) - 3*I, type);

Expand All @@ -355,15 +355,15 @@ static void spawn_item_internal(complex pos, ItemType type, float collect_value)
}
}

void spawn_item(complex pos, ItemType type) {
void spawn_item(cmplx pos, ItemType type) {
spawn_item_internal(pos, type, -1);
}

void spawn_and_collect_item(complex pos, ItemType type, float collect_value) {
void spawn_and_collect_item(cmplx pos, ItemType type, float collect_value) {
spawn_item_internal(pos, type, collect_value);
}

static void spawn_items_internal(complex pos, float collect_value, SpawnItemsArgs groups[]) {
static void spawn_items_internal(cmplx pos, float collect_value, SpawnItemsArgs groups[]) {
for(SpawnItemsArgs *g = groups; g->type > 0; ++g) {
for(uint i = 0; i < g->count; ++i) {
spawn_item_internal(pos, g->type, collect_value);
Expand All @@ -372,12 +372,12 @@ static void spawn_items_internal(complex pos, float collect_value, SpawnItemsArg
}

#undef spawn_items
void spawn_items(complex pos, SpawnItemsArgs groups[]) {
void spawn_items(cmplx pos, SpawnItemsArgs groups[]) {
spawn_items_internal(pos, -1, groups);
}

#undef spawn_and_collect_items
void spawn_and_collect_items(complex pos, float collect_value, SpawnItemsArgs groups[]) {
void spawn_and_collect_items(cmplx pos, float collect_value, SpawnItemsArgs groups[]) {
spawn_items_internal(pos, collect_value, groups);
}

Expand Down
18 changes: 9 additions & 9 deletions src/item.h
Expand Up @@ -42,35 +42,35 @@ struct Item {

int birthtime;
int collecttime;
complex pos;
complex pos0;
cmplx pos;
cmplx pos0;

int auto_collect;
ItemType type;
float pickup_value;

complex v;
cmplx v;
};

Item *create_item(complex pos, complex v, ItemType type);
Item *create_item(cmplx pos, cmplx v, ItemType type);
void delete_item(Item *item);
void delete_items(void);

Item* create_clear_item(complex pos, uint clear_flags);
Item* create_clear_item(cmplx pos, uint clear_flags);

int collision_item(Item *p);
void process_items(void);

void spawn_item(complex pos, ItemType type);
void spawn_and_collect_item(complex pos, ItemType type, float collect_value);
void spawn_item(cmplx pos, ItemType type);
void spawn_and_collect_item(cmplx pos, ItemType type, float collect_value);

typedef struct SpawnItemsArgs {
ItemType type;
int count;
} SpawnItemsArgs;

void spawn_items(complex pos, SpawnItemsArgs groups[]);
void spawn_and_collect_items(complex pos, float collect_value, SpawnItemsArgs groups[]);
void spawn_items(cmplx pos, SpawnItemsArgs groups[]);
void spawn_and_collect_items(cmplx pos, float collect_value, SpawnItemsArgs groups[]);

#define spawn_items(pos, ...) \
spawn_items(pos, ((SpawnItemsArgs[]) { __VA_ARGS__, { 0 } }))
Expand Down

0 comments on commit 80b1026

Please sign in to comment.