Remove SpecialRenderable and all associated code related to it. Performance gains.

sigonasr2 6 months ago
parent a0c78b9a85
commit fd6d186fb6
  1. BIN
      assets/hamster_jet.png
  2. 1
      src/AnimationState.h
  3. 10
      src/Hamster.cpp
  4. 2
      src/Hamster.h
  5. 66
      src/HamsterGame.cpp
  6. 3
      src/HamsterGame.h
  7. 10
      src/HamsterJet.cpp
  8. 3
      src/HamsterJet.h
  9. 97
      src/SpecialRenderable.cpp
  10. 57
      src/SpecialRenderable.h

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 21 KiB

@ -41,6 +41,7 @@ namespace AnimationState{
DEFAULT,
WHEEL_TOP,
WHEEL_BOTTOM,
JET,
JET_LIGHTS,
JET_FLAMES,
KNOCKOUT,

@ -148,8 +148,6 @@ void Hamster::UpdateHamsters(const float fElapsedTime){
h.TurnTowardsTargetDirection();
h.MoveHamster();
if(h.IsPlayerControlled){
h.hamsterJetDisplay.Update(fElapsedTime);
h.hamsterJetLightsDisplay.Update(fElapsedTime);
h.readyFlashTimer+=fElapsedTime;
h.jetFuelDisplayAmt+=(h.jetFuel-h.jetFuelDisplayAmt)*4.f*fElapsedTime;
}
@ -169,8 +167,6 @@ void Hamster::LoadHamsters(const vf2d startingLoc){
for(int i:std::ranges::iota_view(0U,NPC_HAMSTER_COUNT)){
HAMSTER_LIST.emplace_back(startingLoc,NPC_HAMSTER_IMAGES.at(util::random()%NPC_HAMSTER_IMAGES.size()),NPC);
}
playerHamster.value()->hamsterJetDisplay.Initialize("hamster_jet.png",{78,223,208},{79,81,128});
playerHamster.value()->hamsterJetLightsDisplay.Initialize("hamster_jet.png",{245,233,130},{245,233,130});
}
void Hamster::DrawHamsters(TransformedView&tv){
@ -215,8 +211,10 @@ void Hamster::DrawOverlay(){
if(GetPlayer().HasPowerup(Powerup::JET))jetDisplayCol=WHITE;
const Animate2D::FrameSequence&lightAnim{HamsterGame::Game().GetAnimation("hamster_jet.png",AnimationState::JET_LIGHTS)};
const Animate2D::Frame&lightFrame{lightAnim.GetFrame(HamsterGame::Game().GetRuntime())};
HamsterGame::Game().DrawPartialRotatedDecal(jetDisplayOffset+vf2d{48.f,80.f},GetPlayer().hamsterJetDisplay.Decal(),0.f,{24.f,24.f},{0.f,0.f},{48.f,48.f},{2.f,2.f},jetDisplayCol);
HamsterGame::Game().DrawPartialRotatedDecal(jetDisplayOffset+vf2d{48.f,80.f},GetPlayer().hamsterJetLightsDisplay.Decal(),0.f,{24.f,24.f},lightFrame.GetSourceRect().pos,lightFrame.GetSourceRect().size,{2.f,2.f},jetDisplayCol);
const Animate2D::FrameSequence&jetAnim{HamsterGame::Game().GetAnimation("hamster_jet.png",AnimationState::JET)};
const Animate2D::Frame&jetFrame{jetAnim.GetFrame(HamsterGame::Game().GetRuntime())};
HamsterGame::Game().DrawPartialRotatedDecal(jetDisplayOffset+vf2d{48.f,80.f},jetFrame.GetSourceImage()->Decal(),0.f,jetFrame.GetSourceRect().size/2,jetFrame.GetSourceRect().pos,jetFrame.GetSourceRect().size,{2.f,2.f},jetDisplayCol);
HamsterGame::Game().DrawPartialRotatedDecal(jetDisplayOffset+vf2d{48.f,80.f},lightFrame.GetSourceImage()->Decal(),0.f,lightFrame.GetSourceRect().size/2,lightFrame.GetSourceRect().pos,jetFrame.GetSourceRect().size,{2.f,2.f},jetDisplayCol);
}
if(GetPlayer().HasPowerup(Powerup::JET)&&!GetPlayer().hamsterJet.has_value()){

@ -107,8 +107,6 @@ class Hamster{
std::optional<HamsterJet>hamsterJet;
float lastTappedSpace{0.f};
float drawingOffsetY{0.f};
SpecialRenderable hamsterJetDisplay;
SpecialRenderable hamsterJetLightsDisplay;
float readyFlashTimer{};
float jetFuel{0.f};
float jetFuelDisplayAmt{0.f};

@ -53,7 +53,6 @@ void HamsterGame::LoadGraphics(){
_LoadImage("shadow.png");
_LoadImage("drownmeter.png");
_LoadImage("burnmeter.png");
_LoadImage("hamster_jet.png");
_LoadImage("dot.png");
_LoadImage("clouds.png");
_LoadImage("aimingTarget.png");
@ -67,7 +66,6 @@ void HamsterGame::LoadGraphics(){
_LoadImage("radar.png");
_LoadImage("checkpoint_arrow.png");
_LoadImage("radaricons.png");
UpdateMatrixTexture();
}
void HamsterGame::LoadAnimations(){
@ -101,6 +99,7 @@ void HamsterGame::LoadAnimations(){
lavaAnimFrames.AddFrame(Animate2D::Frame{&GetGFX("gametiles.png"),{sourcePos,{16,16}}});
}
LoadAnimation(AnimationState::JET_LIGHTS,"hamster_jet.png",{{0,48},{48,48}},0.3f,Animate2D::Style::Repeat,{48,48});
LoadAnimation(AnimationState::JET,"hamster_jet.png",{{},{0,96},{48,96},{96,96},{0,144},{48,144},{96,144},{0,192},{48,192},{96,192}},0.2f,Animate2D::Style::Repeat,{48,48});
LoadAnimation(AnimationState::JET_FLAMES,"hamster_jet.png",{{48,0},{96,0}},0.15f,Animate2D::Style::Repeat,{48,48});
LoadAnimation(AnimationState::DEFAULT,"checkpoint.png",{{}},0.f,Animate2D::Style::OneShot,{128,128});
LoadAnimation(AnimationState::CHECKPOINT_CYCLING,"checkpoint.png",{{},{128,0}},0.4f,Animate2D::Style::Repeat,{128,128});
@ -168,8 +167,6 @@ void HamsterGame::UpdateGame(const float fElapsedTime){
}else if(GetMouseWheel()<0){
radarScale=std::clamp(radarScale*2.f,6.f,96.f);
}
UpdateMatrixTexture();
UpdateWaterTexture();
cloudOffset+=cloudSpd*fElapsedTime;
camera.SetViewSize(tv.GetWorldVisibleArea());
@ -266,6 +263,7 @@ void HamsterGame::DrawGame(){
DrawStringDecal(SCREEN_FRAME.pos+SCREEN_FRAME.size-speedometerStrSize-vf2d{4.f,4.f},speedometerStr,speedometerCol);
DrawDecal({2.f,4.f},GetGFX("radar.png").Decal());
DrawRadar();
DrawStringDecal({0,8.f},std::to_string(GetFPS()));
}
const Terrain::TerrainType HamsterGame::GetTerrainTypeAtPos(const vf2d pos)const{
@ -344,66 +342,6 @@ const double HamsterGame::GetRuntime()const{
return runTime;
}
void HamsterGame::UpdateMatrixTexture(){
const auto result{GFX.insert({"MATRIX_TEXTURE",Renderable{}})};
Renderable&texture{(*result.first).second};
if(result.second){
texture.Create(64,64,false,false);
texture.Sprite()->SetSampleMode(Sprite::PERIODIC);
}
const std::array<char,10>matrixLetters{'0','1','2','3','4','5','6','7','8','9'};
if(matrixTimer==0){
activeLetters.emplace_back(vf2d{float(rand()%64),float(64)},util::random(-40)-20,matrixLetters[rand()%matrixLetters.size()]);
matrixTimer=util::random(0.125);
}
if(updatePixelsTimer==0){
SetDrawTarget(texture.Sprite());
Sprite*img=texture.Sprite();
for(int y=63;y>=0;y--){
for(int x=63;x>=0;x--){
Pixel col=img->GetPixel(x,y);
if(col.r>0){
if(x>0){
Pixel leftCol=img->GetPixel(x-1,y);
if(leftCol.r<col.r){
leftCol=PixelLerp(col,leftCol,0.125);
}
Draw(x-1,y,leftCol);
}
if(x<img->width-1){
Pixel rightCol=img->GetPixel(x+1,y);
if(rightCol.r<col.r){
rightCol=PixelLerp(col,rightCol,0.125);
}
Draw(x+1,y,rightCol);
}
col/=8;
Draw(x,y,col);
}
}
}
for(int y=0;y<64;y++){
Draw({0,y},img->GetPixel(1,y));
}
SetDrawTarget(nullptr);
updatePixelsTimer=0.1;
}
if(activeLetters.size()>0){
SetDrawTarget(texture.Sprite());
for(Letter&letter:activeLetters){
letter.pos.y+=letter.spd*GetElapsedTime();
DrawString(letter.pos,std::string(1,letter.c));
}
SetDrawTarget(nullptr);
texture.Decal()->Update();
}
matrixTimer=std::max(0.f,matrixTimer-GetElapsedTime());
updatePixelsTimer=std::max(0.f,updatePixelsTimer-GetElapsedTime());
std::erase_if(activeLetters,[](Letter&letter){return letter.pos.y<-32;});
}
void HamsterGame::UpdateWaterTexture(){
const Animate2D::FrameSequence&waterAnimSequence{ANIMATED_TILE_IDS[1384]};
const Animate2D::Frame&frame{waterAnimSequence.GetFrame(GetRuntime())};

@ -45,7 +45,6 @@ All rights reserved.
#include "TMXParser.h"
#include "TSXParser.h"
#include "Terrain.h"
#include "SpecialRenderable.h"
#include "olcPGEX_Graphics3D.h"
#include "AnimationState.h"
#include "olcPGEX_Viewport.h"
@ -98,8 +97,6 @@ private:
double runTime{};
Camera2D camera;
Renderable mapImage;
void UpdateMatrixTexture();
float matrixTimer;
std::vector<Letter>activeLetters;
float updatePixelsTimer;
Renderable animatedWaterTile;

@ -42,12 +42,8 @@ All rights reserved.
HamsterJet::HamsterJet(Hamster&hamster)
:hamster(hamster),hamsterOriginalPos(hamster.GetPos()),pos({hamster.GetPos().x-128.f,hamster.GetPos().y+32.f}),z(3.f),state(SWOOP_DOWN),timer(3.f){
jet.Initialize("hamster_jet.png",{78,223,208},{79,81,128});
lights.Initialize("hamster_jet.png",{245,233,130},{245,233,130});
}
void HamsterJet::Update(const float fElapsedTime){
jet.Update(fElapsedTime);
lights.Update(fElapsedTime);
timer=std::max(0.f,timer-fElapsedTime);
lastTappedSpace+=fElapsedTime;
switch(state){
@ -149,7 +145,9 @@ void HamsterJet::Draw(){
hamster.SetDrawingOffsetY(util::lerp(48.f,0.f,easeInTimer/0.6f));
}
HamsterGame::Game().SetZ(z);
HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},jet.Decal(),0.f,{24,24},{},{48,48});
const Animate2D::FrameSequence&jetAnim{HamsterGame::Game().GetAnimation("hamster_jet.png",AnimationState::JET)};
const Animate2D::Frame&jetFrame{jetAnim.GetFrame(HamsterGame::Game().GetRuntime())};
HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},jetFrame.GetSourceImage()->Decal(),0.f,jetFrame.GetSourceRect().size/2,jetFrame.GetSourceRect().pos,jetFrame.GetSourceRect().size);
const Animate2D::FrameSequence&flameAnim{HamsterGame::Game().GetAnimation("hamster_jet.png",AnimationState::JET_FLAMES)};
const Animate2D::Frame&flameFrame{flameAnim.GetFrame(HamsterGame::Game().GetRuntime())};
HamsterGame::Game().SetZ(z+0.01f);
@ -160,7 +158,7 @@ void HamsterJet::Draw(){
const Animate2D::FrameSequence&lightAnim{HamsterGame::Game().GetAnimation("hamster_jet.png",AnimationState::JET_LIGHTS)};
const Animate2D::Frame&lightFrame{lightAnim.GetFrame(HamsterGame::Game().GetRuntime())};
HamsterGame::Game().SetZ(z+0.02f);
HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},lights.Decal(),0.f,lightFrame.GetSourceRect().size/2.f,lightFrame.GetSourceRect().pos,lightFrame.GetSourceRect().size);
HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},lightFrame.GetSourceImage()->Decal(),0.f,lightFrame.GetSourceRect().size/2.f,lightFrame.GetSourceRect().pos,lightFrame.GetSourceRect().size);
HamsterGame::Game().SetZ(0.f);
}

@ -38,7 +38,6 @@ All rights reserved.
#pragma once
#include "olcUTIL_Geometry2D.h"
#include "SpecialRenderable.h"
#include "Terrain.h"
class Hamster;
@ -73,8 +72,6 @@ private:
float z;
float targetZ{};
State state;
SpecialRenderable jet;
SpecialRenderable lights;
float timer{};
std::array<JetState,4>jetState{};
float lastTappedSpace{};

@ -1,97 +0,0 @@
#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Portions of this software are copyright © 2024 The FreeType
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
All rights reserved.
*/
#pragma endregion
#include "SpecialRenderable.h"
#include "HamsterGame.h"
#include <ranges>
SpecialRenderable::SpecialRenderable(){};
void SpecialRenderable::Initialize(const std::string&imgName,const Pixel overrideCol,const Pixel matrixCol){
IsInitialized=true;
originalImgName=imgName;
this->overrideCol=overrideCol;
this->matrixCol=matrixCol;
const Renderable&refImg{HamsterGame::GetGFX(imgName)};
modifiedImg.Create(refImg.Sprite()->width,refImg.Sprite()->height);
HamsterGame::Game().SetPixelMode(Pixel::ALPHA);
for(int y:std::ranges::iota_view(0,refImg.Sprite()->height)){
for(int x:std::ranges::iota_view(0,refImg.Sprite()->width)){
modifiedImg.Sprite()->SetPixel(x,y,refImg.Sprite()->GetPixel(x,y));
}
}
modifiedImg.Decal()->Update();
Update(0.1f);
HamsterGame::Game().SetPixelMode(Pixel::MASK);
}
void SpecialRenderable::Update(const float fElapsedTime){
if(!IsInitialized)throw std::runtime_error{std::format("SpecialRenderable for {} is not properly initialized!",originalImgName)};
lastPixelsUpdateTimer-=fElapsedTime;
HamsterGame::Game().SetDrawTarget(modifiedImg.Sprite());
HamsterGame::Game().SetPixelMode(Pixel::ALPHA);
HamsterGame::Game().SetPixelBlend(0.5f);
if(lastPixelsUpdateTimer<=0.f){
lastPixelsUpdateTimer+=0.1f;
for(int y:std::ranges::iota_view(0,modifiedImg.Sprite()->height)){
for(int x:std::ranges::iota_view(0,modifiedImg.Sprite()->width)){
if(HamsterGame::GetGFX(originalImgName).Sprite()->GetPixel(x,y)==overrideCol){
modifiedImg.Sprite()->SetPixel(x,y,HamsterGame::GetGFX("MATRIX_TEXTURE").Sprite()->GetPixel(x,y));
HamsterGame::Game().Draw({x,y},matrixCol);
}
}
}
}
HamsterGame::Game().SetDrawTarget(nullptr);
HamsterGame::Game().SetPixelMode(Pixel::MASK);
HamsterGame::Game().SetPixelBlend(1.f);
modifiedImg.Decal()->Update();
}
const Renderable&SpecialRenderable::Get()const{
if(!IsInitialized)throw std::runtime_error{std::format("SpecialRenderable for {} is not properly initialized!",originalImgName)};
return modifiedImg;
}
void SpecialRenderable::ChangeMatrixColor(const Pixel newMatrixCol){
matrixCol=newMatrixCol;
}
Decal*SpecialRenderable::Decal()const{
return modifiedImg.Decal();
}
Sprite*SpecialRenderable::Sprite()const{
return modifiedImg.Sprite();
}

@ -1,57 +0,0 @@
#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Portions of this software are copyright © 2024 The FreeType
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
All rights reserved.
*/
#pragma endregion
#pragma once
#include "olcPixelGameEngine.h"
class SpecialRenderable{
std::string originalImgName;
Renderable modifiedImg;
Pixel overrideCol; //All pixels of this color will have the matrix texture applied to it.
Pixel matrixCol;
float lastPixelsUpdateTimer{};
bool IsInitialized{false};
public:
SpecialRenderable();
void Initialize(const std::string&imgName,const Pixel overrideCol,const Pixel matrixCol);
void Update(const float fElapsedTime);
const Renderable&Get()const;
::Decal*Decal()const;
::Sprite*Sprite()const;
void ChangeMatrixColor(const Pixel newMatrixCol);
};
Loading…
Cancel
Save