From 4f63c6b7ff2de61fd08fbc7c6d73ceda11dfeb2f Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 15 Mar 2024 17:19:02 +0100 Subject: color trigger --- .clang-format | 8 +++ inc/entity.h | 52 ++++++++++------ map/0.tmj | 19 +++++- map/propertytypes.json | 11 ++++ run.sh | 2 +- src/_color.c | 22 +++++++ src/alarm.c | 3 +- src/color.c | 31 +++------- src/debug.c | 8 ++- src/delay.c | 5 +- src/entity.c | 161 +++++++++++++++++++++---------------------------- src/etable.c | 4 +- src/event.c | 5 +- src/font.c | 26 ++++---- src/game.c | 63 ++++++++++--------- src/gravity.c | 2 +- src/main.c | 58 ++++-------------- src/map.c | 36 +++++------ src/player.c | 23 ++++--- src/teleport.c | 5 +- src/tile.c | 3 +- src/tileset.c | 25 ++++---- src/touch.c | 6 +- src/trigger.c | 19 ++++++ 24 files changed, 305 insertions(+), 292 deletions(-) create mode 100644 .clang-format create mode 100644 src/_color.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..4253a15 --- /dev/null +++ b/.clang-format @@ -0,0 +1,8 @@ +BasedOnStyle: LLVM +IndentWidth: 8 +UseTab: AlignWithSpaces +AllowShortIfStatementsOnASingleLine: Never +AllowShortFunctionsOnASingleLine: Empty +IndentCaseLabels: false +ColumnLimit: 80 +AlignConsecutiveMacros: true diff --git a/inc/entity.h b/inc/entity.h index 6614fef..40c2bce 100644 --- a/inc/entity.h +++ b/inc/entity.h @@ -41,45 +41,57 @@ typedef struct Entity { bool leave; bool do_x; bool do_y; + u8 color_group; + struct { + u8 r, g, b, a; + } color; } Entity; -vec_decl(EntityP, Entity*); +vec_decl(EntityP, Entity *); void entity_method(Entity *, const char *method); void entity_super(Entity *, const char *method); -[[deprecated("replaced by entities")]] bool entity_collide(Entity *, vec2 off, u32 type); +[[deprecated("replaced by entities")]] bool entity_collide(Entity *, vec2 off, + u32 type); bool entity_collide_solid(Entity *, vec2 off); bool entity_collide_spike(Entity *, vec2 off); bool entity_intersect(Entity *, vec2 off, Entity *); vec2 entity_move(Entity *, vec2 force); -Entity *entity_place_meeting(Entity *, vec2 off, const char *type, bool visible); -VecEntityP *entity_place_meeting_list(Entity *, vec2 off, const char *type, bool visible); +Entity *entity_place_meeting(Entity *, vec2 off, const char *type, + bool visible); +VecEntityP *entity_place_meeting_list(Entity *, vec2 off, const char *type, + bool visible); i32 entity_propertyi(Entity *, const char *property); i32 entity_propertyi_default(Entity *, const char *property, i32 def); i32 entity_propertyo(Entity *, const char *property); i32 entity_propertyo_default(Entity *, const char *property, i32 def); bool entity_propertyb(Entity *, const char *property); +const char *entity_propertyc(Entity *, const char *property); void entity_append_group(Entity *, u32 gid); void entity_append_groups(Entity *, const char *); void entity_copy_groups(Entity *, Entity *); bool entity_in_group(Entity *, u32 gid); -#define method(X) entity_method(this, (X)) -#define super(X) entity_super(this, (X)) -#define collide(O, T) entity_collide(this, (O), (T)) -#define collide_solid(O) entity_collide_solid(this, (O)) -#define collide_spike(O) entity_collide_spike(this, (O)) -#define move(X) entity_move(this, (X)); -#define intersect(O, E) entity_intersect(this, (O), (E)) +#define method(X) entity_method(this, (X)) +#define super(X) entity_super(this, (X)) +#define collide(O, T) entity_collide(this, (O), (T)) +#define collide_solid(O) entity_collide_solid(this, (O)) +#define collide_spike(O) entity_collide_spike(this, (O)) +#define move(X) entity_move(this, (X)); +#define intersect(O, E) entity_intersect(this, (O), (E)) #define place_meeting(O, T, V) entity_place_meeting(this, (O), (T), (V)) -#define place_meeting_list(O, T, V) entity_place_meeting_list(this, (O), (T), (V)) -#define propertyi(X) entity_propertyi(this, (X)) +#define place_meeting_list(O, T, V) \ + entity_place_meeting_list(this, (O), (T), (V)) +#define propertyi(X) entity_propertyi(this, (X)) #define propertyi_default(X, V) entity_propertyi_default(this, (X), (V)) -#define propertyo(X) entity_propertyo(this, (X)) +#define propertyo(X) entity_propertyo(this, (X)) #define propertyo_default(X, V) entity_propertyo_default(this, (X), (V)) -#define propertyb(X) entity_propertyb(this, (X)) -#define draw_hitbox(...) \ - TZR_DrawSetColor(__VA_ARGS__, .a=0.5), \ - TZR_DrawRectangle((i32)POS.x, (i32)POS.y, DIM.x, DIM.y, true, true),\ - TZR_DrawRectangle((i32)POS.x, (i32)POS.y, DIM.x, DIM.y, false, true),\ - TZR_DrawSetColor(.a=1) +#define propertyb(X) entity_propertyb(this, (X)) +#define propertyc(X) entity_propertyc(this, (X)) +#define draw_hitbox(...) \ + TZR_DrawSetColor(__VA_ARGS__, .a = 0.5), \ + TZR_DrawRectangle((i32)POS.x, (i32)POS.y, DIM.x, DIM.y, true, \ + true), \ + TZR_DrawRectangle((i32)POS.x, (i32)POS.y, DIM.x, DIM.y, false, \ + true), \ + TZR_DrawSetColor(.a = 1) diff --git a/map/0.tmj b/map/0.tmj index 95fcc60..6cfb8a8 100644 --- a/map/0.tmj +++ b/map/0.tmj @@ -780,6 +780,23 @@ "width":8, "x":24, "y":80 + }, + { + "height":8, + "id":225, + "name":"", + "properties":[ + { + "name":"color", + "type":"color", + "value":"#ff00eaff" + }], + "rotation":0, + "type":"color", + "visible":true, + "width":8, + "x":184, + "y":96 }], "opacity":1, "type":"objectgroup", @@ -1015,7 +1032,7 @@ "y":0 }], "nextlayerid":12, - "nextobjectid":224, + "nextobjectid":226, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.10.2", diff --git a/map/propertytypes.json b/map/propertytypes.json index b4e40ad..9b5b498 100644 --- a/map/propertytypes.json +++ b/map/propertytypes.json @@ -1,4 +1,15 @@ [ + { + "id": 39, + "name": "ColorGroup", + "storageType": "int", + "type": "enum", + "values": [ + "Background", + "Foreground" + ], + "valuesAsFlags": false + }, { "color": "#fff9a6ff", "drawFill": true, diff --git a/run.sh b/run.sh index 7d7e4c5..ef1d4c0 100755 --- a/run.sh +++ b/run.sh @@ -1,2 +1,2 @@ #!/bin/sh -tup build-default && ./build-default/build/nulltopos . +tup build-default && gamescope -Fpixel -w480 -h360 -- ./build-default/build/nulltopos . diff --git a/src/_color.c b/src/_color.c new file mode 100644 index 0000000..a7573fe --- /dev/null +++ b/src/_color.c @@ -0,0 +1,22 @@ +static float _fr, _fg, _fb; +static float _br, _bg, _bb; + +void setfgcolor(float r, float g, float b) { + _fr = r; + _fg = g; + _fb = b; +} + +void setbgcolor(float r, float g, float b) { + _br = r; + _bg = g; + _bb = b; +} + +void fgcolor() { + TZR_DrawSetColor(_fr, _fg, _fb); +} + +void bgcolor() { + TZR_DrawSetColor(_br, _bg, _bb); +} diff --git a/src/alarm.c b/src/alarm.c index 81eb212..c0cf827 100644 --- a/src/alarm.c +++ b/src/alarm.c @@ -3,8 +3,7 @@ NAME(alarm); UPDATE { if (--this->tick == 0) { auto v = g_group(this->target); - repeat (i, v->size) - entity_method(VecEntityP_get(v, i), "enter"); + repeat(i, v->size) entity_method(VecEntityP_get(v, i), "enter"); VecEntityP_free(v); method("deinit"); } diff --git a/src/color.c b/src/color.c index a7573fe..8f5b71b 100644 --- a/src/color.c +++ b/src/color.c @@ -1,22 +1,11 @@ -static float _fr, _fg, _fb; -static float _br, _bg, _bb; - -void setfgcolor(float r, float g, float b) { - _fr = r; - _fg = g; - _fb = b; -} - -void setbgcolor(float r, float g, float b) { - _br = r; - _bg = g; - _bb = b; -} - -void fgcolor() { - TZR_DrawSetColor(_fr, _fg, _fb); -} - -void bgcolor() { - TZR_DrawSetColor(_br, _bg, _bb); +NAME(color); +PARENT(trigger); + +IMPL(enter) { + if (this->color_group) + setfgcolor(1. / 255. * this->color.r, 1. / 255. * this->color.g, + 1. / 255. * this->color.b); + else + setbgcolor(1. / 255. * this->color.r, 1. / 255. * this->color.g, + 1. / 255. * this->color.b); } diff --git a/src/debug.c b/src/debug.c index 1f9c5e2..eb2c587 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,5 +1,9 @@ NAME(debug); -INIT { g->debug = true; } +INIT { + g->debug = true; +} -DEINIT { g->debug = false; } +DEINIT { + g->debug = false; +} diff --git a/src/delay.c b/src/delay.c index 7d5e926..d245b0c 100644 --- a/src/delay.c +++ b/src/delay.c @@ -10,11 +10,10 @@ INIT { ENTER { if (this->tick == 0) { auto v = g_group(this->target); - repeat (i, v->size) - entity_method(v->p[i], "enter"); + repeat(i, v->size) entity_method(v->p[i], "enter"); VecEntityP_free(v); } else { - with (e, g_new_entity("alarm", true)) { + with(e, g_new_entity("alarm", true)) { e->tick = this->tick; e->target = this->target; } diff --git a/src/entity.c b/src/entity.c index 54e6137..5f81cda 100644 --- a/src/entity.c +++ b/src/entity.c @@ -1,11 +1,9 @@ -vec_impl(EntityP, Entity*); +vec_impl(EntityP, Entity *); -void -entity_method(Entity *this, const char *method) -{ +void entity_method(Entity *this, const char *method) { if (this == nullptr) { pwrn("this is null"); - return; \ + return; } if (this->table == nullptr) { pwrn("this is inactive"); @@ -25,18 +23,15 @@ entity_method(Entity *this, const char *method) table = etable_get(table->parent); else table = nullptr; - } while(table != nullptr); + } while (table != nullptr); _fn_found: if (strcasecmp(method, "deinit") == 0) this->table = nullptr; } -void -entity_super(Entity *this, const char *method) -{ +void entity_super(Entity *this, const char *method) { ETable *table = etable_get(this->table->parent); - repeat (_, this->super_depth) - table = etable_get(table->parent); + repeat(_, this->super_depth) table = etable_get(table->parent); while (table != nullptr) { foreach (e, table->methods) { if (strcasecmp(e->name, method) == 0) { @@ -53,21 +48,20 @@ entity_super(Entity *this, const char *method) } } -typedef struct { vec2 tl, br; } _Points; -static _Points -_points(Entity *this, vec2 off) -{ - auto p0 = v2_add(v2_sub(this->pos, v2_trunc(v2_div(this->dim, 2))), off); +typedef struct { + vec2 tl, br; +} _Points; +static _Points _points(Entity *this, vec2 off) { + auto p0 = + v2_add(v2_sub(this->pos, v2_trunc(v2_div(this->dim, 2))), off); auto p1 = v2_add(p0, this->dim); - return (_Points){ p0, p1 }; + return (_Points){p0, p1}; } -bool -entity_collide(Entity *this, vec2 off, u32 type) -{ +bool entity_collide(Entity *this, vec2 off, u32 type) { const auto p = _points(this, off); - rfor (y, p.tl.y, p.br.y) { - rfor (x, p.tl.x, p.br.x) { + rfor(y, p.tl.y, p.br.y) { + rfor(x, p.tl.x, p.br.x) { if (map_get_px(g_map(), x, y, type)) return true; } @@ -75,21 +69,15 @@ entity_collide(Entity *this, vec2 off, u32 type) return false; } -bool -entity_collide_solid(Entity *this, vec2 off) -{ +bool entity_collide_solid(Entity *this, vec2 off) { return place_meeting(off, "solid", true); } -bool -entity_collide_spike(Entity *this, vec2 off) -{ +bool entity_collide_spike(Entity *this, vec2 off) { return place_meeting(off, "spike", true); } -bool -entity_intersect(Entity *this, vec2 off, Entity *other) -{ +bool entity_intersect(Entity *this, vec2 off, Entity *other) { const i32 x1 = this->pos.x + off.x; const i32 y1 = this->pos.y + off.y; const i32 w1 = this->dim.x; @@ -101,56 +89,49 @@ entity_intersect(Entity *this, vec2 off, Entity *other) return abs(x1 - x2) * 2 < (w1 + w2) && abs(y1 - y2) * 2 < (h1 + h2); } -Entity * -entity_place_meeting(Entity *this, vec2 off, const char *type, bool visible) -{ - repeat (i, MAX_ENTITIES) { +Entity *entity_place_meeting(Entity *this, vec2 off, const char *type, + bool visible) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; - if (e->table && - strcasecmp(e->table->name, type) == 0 && - (e->visible || !visible) && - intersect(off, e)) + if (e->table && strcasecmp(e->table->name, type) == 0 && + (e->visible || !visible) && intersect(off, e)) return e; } return nullptr; } -VecEntityP * -entity_place_meeting_list(Entity *this, vec2 off, const char *type, bool visible) -{ +VecEntityP *entity_place_meeting_list(Entity *this, vec2 off, const char *type, + bool visible) { auto v = VecEntityP_new(0); - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; - if (e->table && - strcasecmp(e->table->name, type) == 0 && - (e->visible || !visible) && - intersect(off, e)) + if (e->table && strcasecmp(e->table->name, type) == 0 && + (e->visible || !visible) && intersect(off, e)) VecEntityP_push(v, e); } return v; } -vec2 -entity_move(Entity *this, vec2 force) -{ +vec2 entity_move(Entity *this, vec2 force) { if (collide_solid(v2_zero())) { REM = v2_zero(); return v2_zero(); } vec2 vel = VEL; - repeat (a, 2) { + repeat(a, 2) { const f32 sum = force.a[a] + REM.a[a]; int spd = sum; REM.a[a] = sum - spd; const int sign = (spd > 0) - (spd < 0); - if (sign == 0) continue; + if (sign == 0) + continue; while (spd != 0) { POS.a[a] += sign; if (collide_solid(v2_zero())) { POS.a[a] -= sign; REM.a[a] = 0; - //vel.a[a] *= 7/8.f; + // vel.a[a] *= 7/8.f; vel.a[a] = 0; break; } @@ -160,19 +141,16 @@ entity_move(Entity *this, vec2 force) return vel; } -void -entity_append_group(Entity *this, u32 gid) -{ +void entity_append_group(Entity *this, u32 gid) { assert(this != nullptr); assert(gid < MAX_GROUP_WORDS * 32); this->group[gid / 32] |= 1 << (gid % 32); } -void -entity_append_groups(Entity *this, const char *s) -{ +void entity_append_groups(Entity *this, const char *s) { assert(this != nullptr); - if (s == nullptr) return; + if (s == nullptr) + return; size_t i = 0; while (isdigit(s[i])) { @@ -184,46 +162,39 @@ entity_append_groups(Entity *this, const char *s) } } -void -entity_copy_groups(Entity *this, Entity *other) -{ +void entity_copy_groups(Entity *this, Entity *other) { assert(this != nullptr); assert(other != nullptr); - repeat (i, MAX_GROUP_WORDS) - this->group[i] = other->group[i]; + repeat(i, MAX_GROUP_WORDS) this->group[i] = other->group[i]; } -bool -entity_in_group(Entity *this, u32 gid) -{ +bool entity_in_group(Entity *this, u32 gid) { assert(this != nullptr); assert(gid < MAX_GROUP_WORDS * 32); return this->group[gid / 32] & (1 << (gid % 32)); } -static i32 -_propertyi(Entity *this, const char *property, const char *type) -{ +static i32 _propertyi(Entity *this, const char *property, const char *type) { assert(this != nullptr); assert(this->t2c != nullptr); assert(property != nullptr); - rfor (i, 0u, this->t2c->numproperties) { + rfor(i, 0u, this->t2c->numproperties) { const auto e = &this->t2c->properties[i]; if (strcasecmp(e->name, property) == 0) { assert(strcmp(e->type, type) == 0); return e->valuenumber; } } - panic("property '%s' not found in entity %u", property, (u32)this->uuid); + panic("property '%s' not found in entity %u", property, + (u32)this->uuid); } -static i32 -_propertyi_default(Entity *this, const char *property, i32 def, const char *type) -{ +static i32 _propertyi_default(Entity *this, const char *property, i32 def, + const char *type) { assert(this != nullptr); assert(this->t2c != nullptr); assert(property != nullptr); - rfor (i, 0u, this->t2c->numproperties) { + rfor(i, 0u, this->t2c->numproperties) { const auto e = &this->t2c->properties[i]; if (strcasecmp(e->name, property) == 0) { assert(strcmp(e->type, type) == 0); @@ -233,37 +204,27 @@ _propertyi_default(Entity *this, const char *property, i32 def, const char *type return def; } -i32 -entity_propertyi(Entity *this, const char *property) -{ +i32 entity_propertyi(Entity *this, const char *property) { return _propertyi(this, property, "int"); } -i32 -entity_propertyi_default(Entity *this, const char *property, i32 def) -{ +i32 entity_propertyi_default(Entity *this, const char *property, i32 def) { return _propertyi_default(this, property, def, "int"); } -i32 -entity_propertyo(Entity *this, const char *property) -{ +i32 entity_propertyo(Entity *this, const char *property) { return _propertyi(this, property, "object"); } -i32 -entity_propertyo_default(Entity *this, const char *property, i32 def) -{ +i32 entity_propertyo_default(Entity *this, const char *property, i32 def) { return _propertyi_default(this, property, def, "object"); } -bool -entity_propertyb(Entity *this, const char *property) -{ +bool entity_propertyb(Entity *this, const char *property) { assert(this != nullptr); assert(this->t2c != nullptr); assert(property != nullptr); - rfor (i, 0u, this->t2c->numproperties) { + rfor(i, 0u, this->t2c->numproperties) { const auto e = &this->t2c->properties[i]; if (strcasecmp(e->name, property) == 0) { assert(strcmp(e->type, "bool") == 0); @@ -272,3 +233,17 @@ entity_propertyb(Entity *this, const char *property) } return false; } + +const char *entity_propertyc(Entity *this, const char *property) { + assert(this != nullptr); + assert(this->t2c != nullptr); + assert(property != nullptr); + rfor(i, 0u, this->t2c->numproperties) { + const auto e = &this->t2c->properties[i]; + if (strcasecmp(e->name, property) == 0) { + assert(strcmp(e->type, "color") == 0); + return e->valuestring; + } + } + return "#000000ff"; +} diff --git a/src/etable.c b/src/etable.c index 341e0d6..2797063 100644 --- a/src/etable.c +++ b/src/etable.c @@ -1,9 +1,7 @@ ETable *etables = nullptr; u64 etable_i = 0; -ETable * -etable_get(const char *name) -{ +ETable *etable_get(const char *name) { if (name == nullptr) { pwrn("name is null"); return nullptr; diff --git a/src/event.c b/src/event.c index cdd9cf5..a09c525 100644 --- a/src/event.c +++ b/src/event.c @@ -10,7 +10,7 @@ INIT { IMPL(init_post) { if (this->type == EVENT_INIT) { method("broadcast"); - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { auto e = &g->entities[i]; e->visible = e->visible_next; } @@ -20,8 +20,7 @@ IMPL(init_post) { IMPL(broadcast) { auto v = g_group(this->target); - repeat (i, v->size) - entity_method(VecEntityP_get(v, i), "enter"); + repeat(i, v->size) entity_method(VecEntityP_get(v, i), "enter"); VecEntityP_free(v); } diff --git a/src/font.c b/src/font.c index a7a4af2..4035f16 100644 --- a/src/font.c +++ b/src/font.c @@ -1,8 +1,7 @@ -void -font_draw(const char *s, i32 x, i32 y) -{ +void font_draw(const char *s, i32 x, i32 y) { const TZR_Uint res = TZR_RES("res/tset.bmp"); - if (res == 0) return; + if (res == 0) + return; x -= cfg.tile_width; for (i32 i = 0; s[i] != '\0'; i++) { x += cfg.tile_width; @@ -11,13 +10,12 @@ font_draw(const char *s, i32 x, i32 y) const i32 v = s[i] + 800; const i32 ix = v % tileset_width() * cfg.tile_width; const i32 iy = v / tileset_width() * cfg.tile_height; - TZR_DrawImage(res, x, y, ix, iy, cfg.tile_width, cfg.tile_height); + TZR_DrawImage(res, x, y, ix, iy, cfg.tile_width, + cfg.tile_height); } } -void -font_print(i32 x, i32 y, const char *fmt, ...) -{ +void font_print(i32 x, i32 y, const char *fmt, ...) { char buf[256] = {}; va_list va; va_start(va, fmt); @@ -26,18 +24,16 @@ font_print(i32 x, i32 y, const char *fmt, ...) font_draw(buf, x, y); } -void -font_center(i32 x, i32 y, const char *fmt, ...) -{ +void font_center(i32 x, i32 y, const char *fmt, ...) { char buf[256] = {}; va_list va; va_start(va, fmt); vsnprintf(buf, sizeof(buf) - 1, fmt, va); va_end(va); bgcolor(); - TZR_DrawRectangle(x, y, - strlen(buf) * cfg.tile_width, cfg.tile_height, - .fill=true, .center=true); + TZR_DrawRectangle(x, y, strlen(buf) * cfg.tile_width, cfg.tile_height, + .fill = true, .center = true); fgcolor(); - font_draw(buf, x - strlen(buf)*cfg.tile_width/2,y - cfg.tile_height/2); + font_draw(buf, x - strlen(buf) * cfg.tile_width / 2, + y - cfg.tile_height / 2); } diff --git a/src/game.c b/src/game.c index 5f18fe9..9663a1d 100644 --- a/src/game.c +++ b/src/game.c @@ -15,6 +15,8 @@ void g_map_next() { } void g_map_restart() { + setbgcolor(1, 1, 1); + setfgcolor(0, 0, 0); g_spawn_entities(); } @@ -22,11 +24,11 @@ Map *g_map() { return VecMap_get(g->maps, g->map_id); } -#define PROCESS(X) \ - repeat (i, MAX_ENTITIES) { \ - const auto e = &g->entities[i]; \ - if (e->table != nullptr) \ - entity_method(e, #X); \ +#define PROCESS(X) \ + repeat(i, MAX_ENTITIES) { \ + const auto e = &g->entities[i]; \ + if (e->table != nullptr) \ + entity_method(e, #X); \ } void g_update() { @@ -35,7 +37,7 @@ void g_update() { PROCESS(update); PROCESS(update_post); memset(&g->ev, 0, sizeof(g->ev)); - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { auto e = &g->entities[i]; e->visible = e->visible_next; } @@ -54,8 +56,10 @@ void g_update() { } void g_draw(int x, int y) { - int cx = g_map()->t2c->width * cfg.tile_width/2 - cfg.display_width/2; - int cy = g_map()->t2c->height * cfg.tile_height/2 - cfg.display_height/2; + int cx = + g_map()->t2c->width * cfg.tile_width / 2 - cfg.display_width / 2; + int cy = + g_map()->t2c->height * cfg.tile_height / 2 - cfg.display_height / 2; if (g->shake) { cx += g->shake - rand() % (g->shake * 2); cy += g->shake - rand() % (g->shake * 2); @@ -71,7 +75,7 @@ void g_draw(int x, int y) { Entity *g_new_entity(const char *type, bool init) { assert(type != nullptr); Entity *e = nullptr; - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { if (g->entities[i].table == nullptr) { e = &g->entities[i]; break; @@ -125,8 +129,9 @@ Entity *g_new_entity_t2c(const Tiled2cObject *t2c) { type = "tile"; if (e_t2c->numobjects) { Entity *e = nullptr; - rfor (i, 0u, e_t2c->numobjects) { - e = g_new_entity_t2c(&e_t2c->objects[i]); + rfor(i, 0u, e_t2c->numobjects) { + e = g_new_entity_t2c( + &e_t2c->objects[i]); if (e == nullptr) break; e->pos.x += x - t2c->width / 2; @@ -144,8 +149,7 @@ Entity *g_new_entity_t2c(const Tiled2cObject *t2c) { e->t2c = t2c; e->tile = t2c->tile; - repeat (i, MAX_GROUP_WORDS) - e->group[i] = groups.group[i]; + repeat(i, MAX_GROUP_WORDS) e->group[i] = groups.group[i]; e->pos.x = x; e->pos.y = y; e->dim.x = (i32)t2c->width; @@ -157,7 +161,7 @@ Entity *g_new_entity_t2c(const Tiled2cObject *t2c) { } Entity *g_get_entity(const char *type) { - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table != nullptr && !strcasecmp(e->table->name, type)) return e; @@ -166,7 +170,7 @@ Entity *g_get_entity(const char *type) { } Entity *g_get_entity_t2c(u32 id) { - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table != nullptr && e->t2c && e->t2c->id == id) return e; @@ -175,7 +179,7 @@ Entity *g_get_entity_t2c(u32 id) { } void g_clear_entities() { - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table != nullptr) entity_method(e, "deinit"); @@ -185,24 +189,25 @@ void g_clear_entities() { void g_spawn_entities() { g_clear_entities(); const auto map = g_map(); - rfor (i, 0u, map->t2c->numobjects) { + rfor(i, 0u, map->t2c->numobjects) { const auto obj = &map->t2c->objects[i]; g_new_entity_t2c(obj); } - rfor (i, 0u, map->t2c->numlayers) { + rfor(i, 0u, map->t2c->numlayers) { const auto lay = &map->t2c->layers[i]; if (!map_object_layer(lay)) continue; Entity groups = {}; entity_append_groups(&groups, lay->class); const auto visible = lay->visible && lay->opacity > .8; - rfor (y, 0u, map->t2c->height) { - rfor (x, 0u, map->t2c->width) { + rfor(y, 0u, map->t2c->height) { + rfor(x, 0u, map->t2c->width) { const auto tile = - lay->tilelayer.data[x + y*map->t2c->width]; + lay->tilelayer + .data[x + y * map->t2c->width]; if (!tile) continue; - with (e, g_new_entity(lay->class, false)) { + with(e, g_new_entity(lay->class, false)) { e->tile = visible ? tile : 0; e->pos.x = x * map->t2c->tilewidth + map->t2c->tilewidth / 2.; @@ -218,13 +223,13 @@ void g_spawn_entities() { } } } - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table != nullptr) entity_method(e, "init_post"); } plog("free groups"); - rfor (i, 1, MAX_GROUP_WORDS * 32 - 1) { + rfor(i, 1, MAX_GROUP_WORDS * 32 - 1) { if (!g_group_count(i)) { u32 r = 0; while (i + r < MAX_GROUP_WORDS * 32 - 1 && @@ -241,7 +246,7 @@ void g_spawn_entities() { u32 g_count(const char *type) { u32 c = 0; - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table != nullptr && strcmp(e->table->name, type) == 0) c += 1; @@ -255,7 +260,7 @@ void g_shake(i32 duration) { u32 g_group_count(u32 gid) { u32 c = 0; - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table && entity_in_group(e, gid)) c += 1; @@ -264,7 +269,7 @@ u32 g_group_count(u32 gid) { } void g_group_set_visibility(u32 gid, bool visible) { - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table && entity_in_group(e, gid)) e->visible_next = visible; @@ -272,7 +277,7 @@ void g_group_set_visibility(u32 gid, bool visible) { } void g_group_set_flip(u32 gid, bool flip_x, bool flip_y) { - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table && entity_in_group(e, gid)) { e->flip_x ^= flip_x; @@ -283,7 +288,7 @@ void g_group_set_flip(u32 gid, bool flip_x, bool flip_y) { VecEntityP *g_group(u32 gid) { VecEntityP *v = VecEntityP_new(0); - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { const auto e = &g->entities[i]; if (e->table && entity_in_group(e, gid)) VecEntityP_push(v, e); diff --git a/src/gravity.c b/src/gravity.c index 37f5bc5..ce00898 100644 --- a/src/gravity.c +++ b/src/gravity.c @@ -7,7 +7,7 @@ INIT { } ENTER { - with (e, g_get_entity("player")) { + with(e, g_get_entity("player")) { switch (this->reverseGravity) { default: case 0: diff --git a/src/main.c b/src/main.c index 006057e..4c20966 100644 --- a/src/main.c +++ b/src/main.c @@ -1,17 +1,17 @@ static void _setup_input(); static int _main_loop(void *udata); -#define map(X) extern const Tiled2cMap m##X##_tmj; g_push_map(&m##X##_tmj); +#define map(X) \ + extern const Tiled2cMap m##X##_tmj; \ + g_push_map(&m##X##_tmj); int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) { defer(wdeinit); defer(input_deinit); _setup_input(); - assert(TZR_Init(.width=cfg.display_width, - .height=cfg.display_height, - .scale=3, - .target_fps=cfg.fps, - .basepath=argv[1]) == 0); + assert(TZR_Init(.width = cfg.display_width, + .height = cfg.display_height, .scale = 3, + .target_fps = cfg.fps, .basepath = argv[1]) == 0); defer(TZR_Quit); TZR_ToggleFullscreen(); g_init(); @@ -21,10 +21,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) { map(3); map(upndown); map(worldmap); - //map(beat); - //map(game); - //map(somewhere); - g_spawn_entities(); + // map(beat); + // map(game); + // map(somewhere); + g_map_restart(); TZR_MainLoop(_main_loop, nullptr); return 0; } @@ -71,7 +71,7 @@ static void _setup_input() { } static int _main_loop([[maybe_unused]] void *udata) { - //if (TZR_GetTick() == 0) + // if (TZR_GetTick() == 0) // TZR_PlayMusic("res/bgm.wav", -1); g_update(); @@ -80,42 +80,10 @@ static int _main_loop([[maybe_unused]] void *udata) { return 1; assert(TZR_DrawBegin() == 0); - TZR_DrawSetColor(1, 1, 1); + bgcolor(); TZR_DrawClear(); - vec2 v = v2(cfg.tile_width, 0); - static f32 or = 0.01; - if ((TZR_GetTick() + 15) / 30 % 2 == 0) - or += 0.01; - else - or -= 0.01; - const auto r = sinf(or * 14) / 12; - - TZR_BlendMode(SDL_BLENDMODE_MUL); - { - setbgcolor(1, 1, 1); - setfgcolor(0, 0, 0); - g_draw(0, 0); - } - { - vec2 c = v2_round(v2_transform(v, m3_rotating(r / 0.1592))); - setbgcolor(1, 1, 1); - setfgcolor(0, 1, 1); - g_draw(c.x, c.y); - } - { - vec2 c = v2_round(v2_transform(v, m3_rotating((r + 0.333) / 0.1592))); - setbgcolor(1, 1, 1); - setfgcolor(1, 0, 1); - g_draw(c.x, c.y); - } - { - vec2 c = v2_round(v2_transform(v, m3_rotating((r + 0.666) / 0.1592))); - setbgcolor(1, 1, 1); - setfgcolor(1, 1, 0); - g_draw(c.x, c.y); - } - TZR_BlendMode(SDL_BLENDMODE_BLEND); + g_draw(0, 0); assert(TZR_DrawEnd() == 0); return 0; diff --git a/src/map.c b/src/map.c index a2227d0..f98cfe6 100644 --- a/src/map.c +++ b/src/map.c @@ -1,6 +1,4 @@ -Map * -map_new(const Tiled2cMap *t2c) -{ +Map *map_new(const Tiled2cMap *t2c) { assert(t2c != nullptr); Map *this = alloc(sizeof(Map)); @@ -10,9 +8,7 @@ map_new(const Tiled2cMap *t2c) return this; } -bool -map_get_px(Map *this, int x, int y, u32 type) -{ +bool map_get_px(Map *this, int x, int y, u32 type) { assert(this != nullptr); if (x < 0 || y < 0) return type == TILE_NONE; @@ -20,7 +16,7 @@ map_get_px(Map *this, int x, int y, u32 type) y /= cfg.tile_height; if ((u32)x >= this->t2c->width || (u32)y >= this->t2c->height) return type == TILE_NONE; - rfor (i, 0u, this->t2c->numlayers) { + rfor(i, 0u, this->t2c->numlayers) { assert(this->t2c->layers->type == 't'); const auto layer = &this->t2c->layers->tilelayer; const u32 tile = layer->data[x + y * this->t2c->width]; @@ -30,27 +26,25 @@ map_get_px(Map *this, int x, int y, u32 type) return false; } -static void -_draw_layer(const Tiled2cMap *map, const Tiled2cLayer *this, vec2 off) -{ +static void _draw_layer(const Tiled2cMap *map, const Tiled2cLayer *this, + vec2 off) { assert(this != nullptr); - TZR_DrawSetColor(.a=this->opacity); - rfor (y, 0u, map->height) { + TZR_DrawSetColor(.a = this->opacity); + rfor(y, 0u, map->height) { const int dy = y * cfg.tile_height + off.y; - rfor (x, 0u, map->width) { + rfor(x, 0u, map->width) { const int dx = x * cfg.tile_width + off.x; - const auto tile = this->tilelayer.data[x + y * map->width]; + const auto tile = + this->tilelayer.data[x + y * map->width]; tile_draw(tile, dx, dy); } } - TZR_DrawSetColor(.a=1); + TZR_DrawSetColor(.a = 1); } -void -map_draw(Map *this, vec2 off) -{ +void map_draw(Map *this, vec2 off) { assert(this != nullptr); - rfor (i, 0u, this->t2c->numlayers) { + rfor(i, 0u, this->t2c->numlayers) { const auto layer = &this->t2c->layers[i]; assert(layer->type == 't'); if (layer->visible && !map_object_layer(layer)) @@ -58,9 +52,7 @@ map_draw(Map *this, vec2 off) } } -bool -map_object_layer(const Tiled2cLayer *layer) -{ +bool map_object_layer(const Tiled2cLayer *layer) { assert(layer != nullptr); return (layer->type == 't' && layer->class && layer->class[0]); } diff --git a/src/player.c b/src/player.c index 72ea1a9..ddc41c0 100644 --- a/src/player.c +++ b/src/player.c @@ -11,11 +11,7 @@ static const f32 airResistance = .01f; static const f32 jumpSpeed = -2.f; static const f32 gravity = .05f; -enum AirState { - AirState_NEUTRAL, - AirState_RISING, - AirState_BREAKING -}; +enum AirState { AirState_NEUTRAL, AirState_RISING, AirState_BREAKING }; INIT { DIM.x = DIM.y = 6; @@ -41,7 +37,8 @@ UPDATE { } const bool reverse_gravity = this->reverseGravity; - if (reverse_gravity) VEL.y *= -1; + if (reverse_gravity) + VEL.y *= -1; const bool on_ground = collide_solid(v2(0, reverse_gravity ? -1 : 1)); if (input_axis("move x")) @@ -90,7 +87,8 @@ UPDATE { VEL.y += gravity; } - if (reverse_gravity) VEL.y *= -1; + if (reverse_gravity) + VEL.y *= -1; vec2 vel = move(VEL); if (collide_spike(v2_zero())) { g->ev.death = true; @@ -103,14 +101,15 @@ DRAW { fgcolor(); if (this->dead && this->tick / 4 % 2 == 0) return; - TZR_DrawImage(TZR_RES("res/player.bmp"), v2_unpack(POS), .center=true); + TZR_DrawImage(TZR_RES("res/player.bmp"), v2_unpack(POS), + .center = true); } DRAW_END { if (!this->dead || this->tick / 32 % 2 == 1) return; - font_center(cfg.display_width/2, cfg.display_height/2 - cfg.tile_height/2, - "game over"); - font_center(cfg.display_width/2, cfg.display_height/2 + cfg.tile_height/2, - "press r"); + font_center(cfg.display_width / 2, + cfg.display_height / 2 - cfg.tile_height / 2, "game over"); + font_center(cfg.display_width / 2, + cfg.display_height / 2 + cfg.tile_height / 2, "press r"); } diff --git a/src/teleport.c b/src/teleport.c index 0e741e5..77bb699 100644 --- a/src/teleport.c +++ b/src/teleport.c @@ -11,9 +11,10 @@ ENTER { assert(player != nullptr); Entity *target = nullptr; - repeat (i, MAX_ENTITIES) { + repeat(i, MAX_ENTITIES) { auto e = &g->entities[i]; - if (e->table && e->visible && entity_in_group(e, this->target)) { + if (e->table && e->visible && + entity_in_group(e, this->target)) { target = e; break; } diff --git a/src/tile.c b/src/tile.c index bbcddfb..45d7eff 100644 --- a/src/tile.c +++ b/src/tile.c @@ -4,7 +4,8 @@ DRAW { if (!this->tile) return; fgcolor(); - const auto pos = v2_sub(POS, v2(cfg.tile_width/2., cfg.tile_height/2.)); + const auto pos = + v2_sub(POS, v2(cfg.tile_width / 2., cfg.tile_height / 2.)); auto tile = this->tile; if (this->flip_x) tile ^= tiled_flip_x; diff --git a/src/tileset.c b/src/tileset.c index 7f5634d..7ccdf8b 100644 --- a/src/tileset.c +++ b/src/tileset.c @@ -14,7 +14,7 @@ extern const Tiled2cSet tset_tsj; __attribute__((constructor)) static void match_tile_types() { const auto tset = &tset_tsj; assert(MAX_TILE_ID > tset->tilecount); - rfor (i, 0u, tset->numtiles) { + rfor(i, 0u, tset->numtiles) { const auto tile = &tset->tiles[i]; tile_t2cs[tile->id] = tile; @@ -23,12 +23,12 @@ __attribute__((constructor)) static void match_tile_types() { tile_frames[tile->id] = tile->frames; } } - rfor (i, 0, MAX_TILE_ID) { + rfor(i, 0, MAX_TILE_ID) { tile_frame_id[i] = 0; if (tile_numframes[i]) { tile_visuals[i] = tile_frames[i][0].tileid; - tile_next_frame[i] = tile_frames[i][0].duration * - cfg.fps / 1000; + tile_next_frame[i] = + tile_frames[i][0].duration * cfg.fps / 1000; } else tile_visuals[i] = i; } @@ -39,16 +39,17 @@ void tileset_update() { const unsigned long elapsed = tick - last_tick; last_tick = tick; - rfor (i, 0, MAX_TILE_ID) { - if (!tile_numframes[i]) continue; + rfor(i, 0, MAX_TILE_ID) { + if (!tile_numframes[i]) + continue; tile_next_frame[i] -= elapsed; while (tile_next_frame[i] <= 0) { tile_frame_id[i] += 1; tile_frame_id[i] %= tile_numframes[i]; const auto frame_id = tile_frame_id[i]; tile_visuals[i] = tile_frames[i][frame_id].tileid; - tile_next_frame[i] = tile_frames[i][frame_id] - .duration * cfg.fps / 1000; + tile_next_frame[i] = + tile_frames[i][frame_id].duration * cfg.fps / 1000; } } } @@ -61,12 +62,12 @@ void tile_draw(u32 id, int x, int y) { const bool flip_x = (id & tiled_flip_x) != 0; const bool flip_y = (id & tiled_flip_y) != 0; const auto v = tile_visual(id); - if (v < 0) return; + if (v < 0) + return; const int ix = v % tset_tsj.columns * cfg.tile_width; const int iy = v / tset_tsj.columns * cfg.tile_height; - TZR_DrawImage(TZR_RES(tset_tsj.path + 3), - x, y, ix, iy, cfg.tile_width, cfg.tile_height, - .flip_x=flip_x, .flip_y=flip_y); + TZR_DrawImage(TZR_RES(tset_tsj.path + 3), x, y, ix, iy, cfg.tile_width, + cfg.tile_height, .flip_x = flip_x, .flip_y = flip_y); } u32 tile_type(u32 id) { diff --git a/src/touch.c b/src/touch.c index 2ee9f31..a8f1a61 100644 --- a/src/touch.c +++ b/src/touch.c @@ -9,8 +9,7 @@ INIT { IMPL(enter) { if (!this->leave) { auto v = g_group(this->target); - repeat (i, v->size) - entity_method(VecEntityP_get(v, i), "enter"); + repeat(i, v->size) entity_method(VecEntityP_get(v, i), "enter"); VecEntityP_free(v); } if (!this->air) @@ -20,8 +19,7 @@ IMPL(enter) { IMPL(leave) { if (this->leave) { auto v = g_group(this->target); - repeat (i, v->size) - entity_method(VecEntityP_get(v, i), "enter"); + repeat(i, v->size) entity_method(VecEntityP_get(v, i), "enter"); VecEntityP_free(v); } this->animation = 0; diff --git a/src/trigger.c b/src/trigger.c index 41dc4a0..987b7f9 100644 --- a/src/trigger.c +++ b/src/trigger.c @@ -1,6 +1,11 @@ NAME(trigger); PARENT(tile); +static u8 strtobyte(const char *s) { + const char buf[3] = {s[0], s[1], '\0'}; + return strtol(buf, nullptr, 16); +} + INIT { super("init"); this->on = propertyb("on"); @@ -8,6 +13,20 @@ INIT { this->leave = propertyb("leave"); this->do_x = propertyb("flip x") || propertyb("x"); this->do_y = propertyb("flip y") || propertyb("y"); + this->color_group = propertyi_default("color group", 0); + + const auto color = propertyc("color"); + if (color != nullptr && strlen(color) > 8) { + this->color.r = strtobyte(color + 1); + this->color.g = strtobyte(color + 3); + this->color.b = strtobyte(color + 5); + this->color.a = strtobyte(color + 7); + } else { + this->color.r = 0; + this->color.g = 0; + this->color.b = 0; + this->color.a = 255; + } } IMPL(target) { -- cgit v1.2.3