From 125695dc452524b4f3578fe86fabfed1b5c50676 Mon Sep 17 00:00:00 2001 From: kdx Date: Mon, 18 Mar 2024 16:42:54 +0100 Subject: fade in color --- inc/color.h | 2 ++ inc/entity.h | 12 +++++++++--- map/0.tmj | 2 +- map/1.tmj | 46 +++++++++++++++++++++++++++++++++++++++++++--- map/propertytypes.json | 5 +++++ src/_color.c | 19 ++++++++++++++++++- src/color.c | 37 +++++++++++++++++++++++++++++++------ src/entity.c | 14 ++++++++++++++ src/trigger.c | 9 ++++----- 9 files changed, 127 insertions(+), 19 deletions(-) diff --git a/inc/color.h b/inc/color.h index 1d6deb4..27ce108 100644 --- a/inc/color.h +++ b/inc/color.h @@ -1,5 +1,7 @@ #pragma once void setbgcolor(float r, float g, float b); void setfgcolor(float r, float g, float b); +void getbgcolor(float *r, float *g, float *b); +void getfgcolor(float *r, float *g, float *b); void bgcolor(); void fgcolor(); diff --git a/inc/entity.h b/inc/entity.h index 40c2bce..ee8b935 100644 --- a/inc/entity.h +++ b/inc/entity.h @@ -3,6 +3,10 @@ enum { MAX_GROUP_WORDS = 8 }; enum { EVENT_INIT, EVENT_JUMP, EVENT_DOUBLEJUMP, EVENT_DEATH, EVENT_LASTKEY }; +typedef struct EntityColor { + f32 r, g, b; +} EntityColor; + typedef struct Entity { ETable *table; u64 uuid; @@ -42,9 +46,9 @@ typedef struct Entity { bool do_x; bool do_y; u8 color_group; - struct { - u8 r, g, b, a; - } color; + u32 fade_in; + EntityColor initial_color; + EntityColor color; } Entity; vec_decl(EntityP, Entity *); @@ -65,6 +69,7 @@ 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); +f32 entity_propertyf(Entity *, const char *property, f32 def); bool entity_propertyb(Entity *, const char *property); const char *entity_propertyc(Entity *, const char *property); void entity_append_group(Entity *, u32 gid); @@ -86,6 +91,7 @@ bool entity_in_group(Entity *, u32 gid); #define propertyi_default(X, V) entity_propertyi_default(this, (X), (V)) #define propertyo(X) entity_propertyo(this, (X)) #define propertyo_default(X, V) entity_propertyo_default(this, (X), (V)) +#define propertyf(X, V) entity_propertyf(this, (X), (V)) #define propertyb(X) entity_propertyb(this, (X)) #define propertyc(X) entity_propertyc(this, (X)) #define draw_hitbox(...) \ diff --git a/map/0.tmj b/map/0.tmj index cc5ebde..dcffd42 100644 --- a/map/0.tmj +++ b/map/0.tmj @@ -789,7 +789,7 @@ { "name":"color", "type":"color", - "value":"#ffffffff" + "value":"#ffb8ffb8" }, { "name":"color group", diff --git a/map/1.tmj b/map/1.tmj index 649e09a..ada5812 100644 --- a/map/1.tmj +++ b/map/1.tmj @@ -124,7 +124,7 @@ "visible":true, "width":8, "x":0, - "y":-40 + "y":-56 }, { "height":8, @@ -141,7 +141,7 @@ "visible":true, "width":8, "x":0, - "y":-32 + "y":-48 }, { "gid":115, @@ -608,6 +608,46 @@ "width":8, "x":192, "y":176 + }, + { + "height":8, + "id":189, + "name":"99", + "properties":[ + { + "name":"color", + "type":"color", + "value":"#ff007e24" + }, + { + "name":"color group", + "propertytype":"ColorGroup", + "type":"int", + "value":1 + }], + "rotation":0, + "type":"color", + "visible":true, + "width":8, + "x":0, + "y":-40 + }, + { + "height":8, + "id":190, + "name":"99", + "properties":[ + { + "name":"color", + "type":"color", + "value":"#ff79fdff" + }], + "rotation":0, + "type":"color", + "visible":true, + "width":8, + "x":0, + "y":-32 }], "opacity":1, "type":"objectgroup", @@ -652,7 +692,7 @@ "y":0 }], "nextlayerid":12, - "nextobjectid":189, + "nextobjectid":191, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.10.2", diff --git a/map/propertytypes.json b/map/propertytypes.json index 40c53f2..fccc896 100644 --- a/map/propertytypes.json +++ b/map/propertytypes.json @@ -14,6 +14,11 @@ "propertyType": "ColorGroup", "type": "int", "value": 0 + }, + { + "name": "fade in", + "type": "int", + "value": 0 } ], "name": "color", diff --git a/src/_color.c b/src/_color.c index 2f3c7cf..7e9aa08 100644 --- a/src/_color.c +++ b/src/_color.c @@ -11,7 +11,24 @@ void setbgcolor(float r, float g, float b) { _br = r; _bg = g; _bb = b; - plog("%f %f %f", _br, _bg, _bb); +} + +void getfgcolor(float *r, float *g, float *b) { + if (r) + *r = _fr; + if (g) + *g = _fg; + if (b) + *b = _fb; +} + +void getbgcolor(float *r, float *g, float *b) { + if (r) + *r = _br; + if (g) + *g = _bg; + if (b) + *b = _bb; } void fgcolor() { diff --git a/src/color.c b/src/color.c index 67c3fb8..d1c918f 100644 --- a/src/color.c +++ b/src/color.c @@ -1,11 +1,36 @@ NAME(color); PARENT(trigger); +INIT { + super("init"); + this->tick = 0; +} + IMPL(enter) { - if (this->color_group) - setfgcolor(this->color.r / 255., this->color.g / 255., - this->color.b / 255.); - else - setbgcolor(this->color.r / 255., this->color.g / 255., - this->color.b / 255.); + if (this->fade_in == 0) { + const auto setfn = this->color_group ? setfgcolor : setbgcolor; + setfn(this->color.r, this->color.g, this->color.b); + } else { + const auto getfn = this->color_group ? getfgcolor : getbgcolor; + getfn(&this->initial_color.r, &this->initial_color.g, + &this->initial_color.b); + this->tick = 1; + } +} + +f32 mix(f32 a, f32 b, f32 x) { + return a * x + b * (1. - x); +} + +UPDATE_POST { + if (this->tick) { + const auto setfn = this->color_group ? setfgcolor : setbgcolor; + const auto x = this->tick / (f32)this->fade_in; + setfn(mix(this->color.r, this->initial_color.r, x), + mix(this->color.g, this->initial_color.g, x), + mix(this->color.b, this->initial_color.b, x)); + this->tick += 1; + if (this->tick > this->fade_in) + this->tick = 0; + } } diff --git a/src/entity.c b/src/entity.c index 8076a86..1180c1c 100644 --- a/src/entity.c +++ b/src/entity.c @@ -220,6 +220,20 @@ i32 entity_propertyo_default(Entity *this, const char *property, i32 def) { return _propertyi_default(this, property, def, "object"); } +f32 entity_propertyf(Entity *this, const char *property, f32 def) { + 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, "float") == 0); + return e->valuenumber; + } + } + return def; +} + bool entity_propertyb(Entity *this, const char *property) { assert(this != nullptr); assert(this->t2c != nullptr); diff --git a/src/trigger.c b/src/trigger.c index 7790d9d..86a39be 100644 --- a/src/trigger.c +++ b/src/trigger.c @@ -14,18 +14,17 @@ INIT { this->do_x = propertyb("flip x") || propertyb("x"); this->do_y = propertyb("flip y") || propertyb("y"); this->color_group = propertyi_default("color group", 0); + this->fade_in = propertyi_default("fade in", 0); const auto color = propertyc("color"); if (color != nullptr && strlen(color) > 8) { - this->color.r = strtobyte(color + 3); - this->color.g = strtobyte(color + 5); - this->color.b = strtobyte(color + 7); - this->color.a = strtobyte(color + 1); + this->color.r = strtobyte(color + 3) / 255.; + this->color.g = strtobyte(color + 5) / 255.; + this->color.b = strtobyte(color + 7) / 255.; } else { this->color.r = 0; this->color.g = 0; this->color.b = 0; - this->color.a = 255; } } -- cgit v1.2.3