From 3699adec82786a58fed21e8381b0682af4f7b2c7 Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 30 Apr 2020 17:34:37 -0700 Subject: [PATCH] Fix, for in-GAMEMODE sprites, their colors updating too fast Okay, so the problem here is that Graphics::setcol() is called right before a sprite is drawn in a render function, but render functions are done in deltatime, meaning that the color of a sprite keeps being recalculated every time. This only affects sprites that use fRandom() (the other thing that can dynamically determine a color is help.glow, but that's only updated in the fixed-timestep loop), but is especially noticeable for sprites that flash wildly, like the teleporter, trinket, and elephant. To fix this, we need to make the color be recalculated only in the fixed-timestep loop. However, this means that we MUST store the color of the sprite SOMEWHERE for the delta-timesteps to render it, otherwise the color calculation will just be lost or something. So each entity now has a new attribute, `realcol`, which is the actual raw color used to render the sprite in render functions. This is not to be confused with their `colour` attribute, which is more akin to a color "ID" of sorts, but which isn't an actual color. At the end of gamelogic(), as well as when an entity is first created, the `colour` is given to Graphics::setcol() and then `realcol` gets set to the actual color. Then when it comes time to render the entity, `realcol` gets used instead. Gravitron squares are a somewhat tricky case where there's technically TWO colors for it - one is the actual sprite itself and the other is the indicator. However, usually the indicator and the square aren't both onscreen at the same time, so we can simply switch the realcol between the two as needed. However, we can't use this system for the sprite colors used on the title and map screen, so we'll have to do something else for those. --- desktop_version/src/Ent.cpp | 49 ++++++++++++++++++++++++++++++++ desktop_version/src/Ent.h | 6 ++++ desktop_version/src/Entity.cpp | 4 +++ desktop_version/src/Graphics.cpp | 33 +++++++-------------- desktop_version/src/Graphics.h | 2 +- desktop_version/src/Logic.cpp | 11 +++++++ 6 files changed, 82 insertions(+), 23 deletions(-) diff --git a/desktop_version/src/Ent.cpp b/desktop_version/src/Ent.cpp index 14c02369..50360b71 100644 --- a/desktop_version/src/Ent.cpp +++ b/desktop_version/src/Ent.cpp @@ -53,6 +53,8 @@ entclass::entclass() walkingframe = 0; dir = 0; actionframe = 0; + + realcol = 0; } bool entclass::outside() @@ -586,3 +588,50 @@ void entclass::settreadmillcolour( int rx, int ry ) break; } } + +void entclass::updatecolour() +{ + switch (size) + { + case 0: // Sprites + case 7: // Teleporter + case 9: // Really Big Sprite! (2x2) + case 10: // 2x1 Sprite + case 13: // Special for epilogue: huge hero! + graphics.setcol(colour); + realcol = graphics.ct.colour; + break; + case 3: // Big chunky pixels! + realcol = graphics.bigchunkygetcol(colour); + break; + case 4: // Small pickups + graphics.huetilesetcol(colour); + realcol = graphics.ct.colour; + break; + case 11: // The fucking elephant + if (game.noflashingmode) + { + graphics.setcol(22); + } + else + { + graphics.setcol(colour); + } + realcol = graphics.ct.colour; + break; + case 12: // Regular sprites that don't wrap + // if we're outside the screen, we need to draw indicators + if ((xp < -20 && vx > 0) || (xp > 340 && vx < 0)) + { + graphics.setcol(23); + } + else + { + graphics.setcol(colour); + } + realcol = graphics.ct.colour; + break; + default: + break; + } +} diff --git a/desktop_version/src/Ent.h b/desktop_version/src/Ent.h index 508269ae..3007ccf2 100644 --- a/desktop_version/src/Ent.h +++ b/desktop_version/src/Ent.h @@ -1,6 +1,8 @@ #ifndef ENT_H #define ENT_H +#include "Graphics.h" + #define rn( rx, ry) ((rx) + ((ry) * 100)) class entclass @@ -16,6 +18,8 @@ public: void settreadmillcolour(int rx, int ry); + void updatecolour(); + public: //Fundamentals bool invis; @@ -45,6 +49,8 @@ public: //Animation int framedelay, drawframe, walkingframe, dir, actionframe; int yp;int xp; + + Uint32 realcol; }; #endif /* ENT_H */ diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index dee34ca9..5da74fb8 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -2008,6 +2008,10 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo } entity.drawframe = entity.tile; + if (!entity.invis) + { + entity.updatecolour(); + } entities.push_back(entity); } diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index d980bea8..29011461 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -1488,8 +1488,6 @@ void Graphics::drawentities() yoff = 0; } - trinketcolset = false; - for (int i = obj.entities.size() - 1; i >= 0; i--) { if (obj.entities[i].invis) @@ -1510,7 +1508,7 @@ void Graphics::drawentities() } tpoint.x = xp; tpoint.y = yp - yoff; - setcol(obj.entities[i].colour); + setcolreal(obj.entities[i].realcol); drawRect = sprites_rect; drawRect.x += tpoint.x; @@ -1599,10 +1597,10 @@ void Graphics::drawentities() case 3: // Big chunky pixels! prect.x = xp; prect.y = yp - yoff; - FillRect(backBuffer, prect, bigchunkygetcol(obj.entities[i].colour)); + FillRect(backBuffer, prect, obj.entities[i].realcol); break; case 4: // Small pickups - huetilesetcol(obj.entities[i].colour); + setcol(obj.entities[i].realcol); drawhuetile(xp, yp - yoff, obj.entities[i].tile); break; case 5: //Horizontal Line @@ -1620,7 +1618,7 @@ void Graphics::drawentities() drawgravityline(i); break; case 7: //Teleporter - drawtele(xp, yp - yoff, obj.entities[i].drawframe, obj.entities[i].colour); + drawtele(xp, yp - yoff, obj.entities[i].drawframe, obj.entities[i].realcol); break; //case 8: // Special: Moving platform, 8 tiles // Note: This code is in the 4-tile code @@ -1630,7 +1628,7 @@ void Graphics::drawentities() { continue; } - setcol(obj.entities[i].colour); + setcolreal(obj.entities[i].realcol); tpoint.x = xp; tpoint.y = yp - yoff; @@ -1669,7 +1667,7 @@ void Graphics::drawentities() { continue; } - setcol(obj.entities[i].colour); + setcolreal(obj.entities[i].realcol); tpoint.x = xp; tpoint.y = yp - yoff; @@ -1688,14 +1686,7 @@ void Graphics::drawentities() BlitSurfaceColoured((*spritesvec)[obj.entities[i].drawframe+1],NULL, backBuffer, &drawRect, ct); break; case 11: //The fucking elephant - if (game.noflashingmode) - { - setcol(22); - } - else - { - setcol(obj.entities[i].colour); - } + setcolreal(obj.entities[i].realcol); drawimagecol(3, xp, yp - yoff); break; case 12: // Regular sprites that don't wrap @@ -1705,7 +1696,7 @@ void Graphics::drawentities() } tpoint.x = xp; tpoint.y = yp - yoff; - setcol(obj.entities[i].colour); + setcolreal(obj.entities[i].realcol); // drawRect = sprites_rect; drawRect.x += tpoint.x; @@ -1727,7 +1718,6 @@ void Graphics::drawentities() } tpoint.y = tpoint.y+4; - setcol(23); drawRect = tiles_rect; @@ -1748,7 +1738,6 @@ void Graphics::drawentities() } tpoint.y = tpoint.y+4; - setcol(23); // drawRect = tiles_rect; @@ -1766,7 +1755,7 @@ void Graphics::drawentities() } tpoint.x = xp; tpoint.y = yp - yoff; - setcol(obj.entities[i].colour); + setcolreal(obj.entities[i].realcol); SDL_Rect drawRect = {Sint16(obj.entities[i].xp ), Sint16(obj.entities[i].yp - yoff), Sint16(sprites_rect.x * 6), Sint16(sprites_rect.y * 6 ) }; SDL_Surface* TempSurface = ScaleSurface( (*spritesvec)[obj.entities[i].drawframe], 6 * sprites_rect.w,6* sprites_rect.h ); BlitSurfaceColoured(TempSurface, NULL , backBuffer, &drawRect, ct ); @@ -2867,7 +2856,7 @@ void Graphics::bigrprint(int x, int y, std::string& t, int r, int g, int b, bool } } -void Graphics::drawtele(int x, int y, int t, int c) +void Graphics::drawtele(int x, int y, int t, Uint32 c) { setcolreal(getRGB(16,16,16)); @@ -2878,7 +2867,7 @@ void Graphics::drawtele(int x, int y, int t, int c) BlitSurfaceColoured(tele[0], NULL, backBuffer, &telerect, ct); } - setcol(c); + setcolreal(c); if (t > 9) t = 8; if (t < 0) t = 0; diff --git a/desktop_version/src/Graphics.h b/desktop_version/src/Graphics.h index 2b0105b8..a59ee4e4 100644 --- a/desktop_version/src/Graphics.h +++ b/desktop_version/src/Graphics.h @@ -148,7 +148,7 @@ public: void bigrprint(int x, int y, std::string& t, int r, int g, int b, bool cen = false, float sc = 2); - void drawtele(int x, int y, int t, int c); + void drawtele(int x, int y, int t, Uint32 c); Uint32 getRGBA(Uint8 r, Uint8 g, Uint8 b, Uint8 a); diff --git a/desktop_version/src/Logic.cpp b/desktop_version/src/Logic.cpp index 2bcf1da0..8e762d5f 100644 --- a/desktop_version/src/Logic.cpp +++ b/desktop_version/src/Logic.cpp @@ -1608,4 +1608,15 @@ void gamelogic() graphics.linedelay--; } } + + graphics.trinketcolset = false; + for (int i = obj.entities.size() - 1; i >= 0; i--) + { + if (obj.entities[i].invis) + { + continue; + } + + obj.entities[i].updatecolour(); + } }