Skip to content

Commit

Permalink
Changed the convimg.yaml to create rlet and flipped sprites at compil…
Browse files Browse the repository at this point in the history
…e time instead of runtime which reduced malloc calls and memory usage.
  • Loading branch information
WolfVdS committed Sep 18, 2024
1 parent 8ac52d1 commit c9f15a3
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 39 deletions.
18 changes: 7 additions & 11 deletions src/bird.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ Bird::Bird(int x, int y)

hitPipe = false;
scored = false;

//Create and assign the RLE sprites.
bird_0_rle = gfx_ConvertMallocRLETSprite(bird_0);
bird_1_rle = gfx_ConvertMallocRLETSprite(bird_1);
bird_2_rle = gfx_ConvertMallocRLETSprite(bird_2);
currentSprite = bird_1_rle;

currentSprite = bird_1;

//Create the sprite that will hold the background.
behindBird = gfx_MallocSprite(bird_0_width, bird_0_height);
Expand Down Expand Up @@ -143,7 +139,7 @@ void Bird::Reset()
x = originalx;
y = originaly;
vy = 0;
currentSprite = bird_1_rle;
currentSprite = bird_1;
hitPipe = false;
scored = false;
Jump();
Expand All @@ -153,7 +149,7 @@ gfx_rletsprite_t* Bird::GetSpriteToDraw()
{
//If the bird is dead or it has hit a pipe, it won't animate anymore.
if (!IsAlive() || hitPipe)
return bird_1_rle;
return bird_1;

static int spriteIndex = 0;
//Increase the spriteIndex everytime this method is ran.
Expand All @@ -166,13 +162,13 @@ gfx_rletsprite_t* Bird::GetSpriteToDraw()

//If we divide spriteIndex by BIRD_ANIMATION_SPEED we get the number of times we have entered this if clause.
if (relativeSpriteIndex % 2 == 0)
return bird_1_rle;
return bird_1;

else if(relativeSpriteIndex % 3 == 0)
return bird_2_rle;
return bird_2;

else if(relativeSpriteIndex % 1 == 0)
return bird_0_rle;
return bird_0;

else
return currentSprite;
Expand Down
4 changes: 0 additions & 4 deletions src/bird.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ class Bird
void Reset();

private:
//The rle sprites that are used for animation.
gfx_rletsprite_t* bird_0_rle;
gfx_rletsprite_t* bird_1_rle;
gfx_rletsprite_t* bird_2_rle;
//The sprite that conatins the background behind the bird from the previous frame.
gfx_sprite_t* behindBird;
//The previous frame's position of the bird.
Expand Down
20 changes: 18 additions & 2 deletions src/gfx/convimg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,36 @@ palettes:
converts:
- name: sprites
palette: global_palette
transparent-color-index: 0
transparent-color-index: 1
images:
- pipe.png
- background_0.png
- background_1.png
- ground_0.png

- name: spritesRLET
palette: global_palette
transparent-color-index: 1
style: rlet
images:
- pipe.png
- bird_0.png
- bird_1.png
- bird_2.png

- name: spritesflippedRLET
palette: global_palette
transparent-color-index: 1
style: rlet
flip-x: true
images:
- pipe_flipped.png

outputs:
- type: c
include-file: gfx.h
palettes:
- global_palette
converts:
- sprites
- spritesRLET
- spritesflippedRLET
Binary file added src/gfx/pipe_flipped.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,6 @@ void Initialize()
//Initialize the Bird.
bird = new Bird((LCD_WIDTH / 2) - bird_0_width, (LCD_HEIGHT - bird_0_height) / 2);

//Initialize the pipes.
//Because the upper and lower sprites are shared beatween each PipePair they are static
//to save memory and stop the program from crashing.
//Because they are static they cant be assigned via the constructor.
//And because the transparent color must be set first they can also not be assigned in the pipepair.cpp file,
//and thus they need to be assigned here.
gfx_sprite_t* temp = gfx_MallocSprite(pipe_width, pipe_height);
gfx_RotateSpriteHalf(pipe, temp);
PipePair::upperSprite = gfx_ConvertMallocRLETSprite(pipe);
PipePair::lowerSprite = gfx_ConvertMallocRLETSprite(temp);
free(temp);

for (int i = 0; i < PIPE_AMOUNT; i++)
{
int x = ((i + 1) * (LCD_WIDTH / 2)) - ((PIPE_AMOUNT - i) * (pipe_width / 2)) + LCD_WIDTH;
Expand Down
8 changes: 2 additions & 6 deletions src/pipepair.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "pipepair.hpp"

//Initialize the static variables.
gfx_rletsprite_t* PipePair::upperSprite = nullptr;
gfx_rletsprite_t* PipePair::lowerSprite = nullptr;

PipePair::PipePair(int upperx, int uppery)
{
this->upperx = upperx;
Expand Down Expand Up @@ -35,8 +31,8 @@ void PipePair::PreDraw()
void PipePair::Draw()
{
//Draw the sprites.
gfx_RLETSprite(upperSprite, upperx, uppery);
gfx_RLETSprite(lowerSprite, lowerx, lowery);
gfx_RLETSprite(pipe, upperx, uppery);
gfx_RLETSprite(pipe_flipped, lowerx, lowery);
}

void PipePair::Cleanup()
Expand Down
4 changes: 0 additions & 4 deletions src/pipepair.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ class PipePair
int upperx, uppery;
//The position of the lowerPipe.
int lowerx, lowery;

//The sprites of the pipes.
static gfx_rletsprite_t* upperSprite;
static gfx_rletsprite_t* lowerSprite;

//Constructor.
PipePair(int upperx, int uppery);
Expand Down

0 comments on commit c9f15a3

Please sign in to comment.