From e27c322003a77f8cfa3ff52206d01ffdcd6fc3cf Mon Sep 17 00:00:00 2001 From: Nathan Misner Date: Sun, 5 Jun 2022 15:55:33 -0400 Subject: [PATCH] added alpha transparency for blocks --- block.cpp | 30 +++++++++++++++++++++++++++--- block.h | 7 ++++--- level.cpp | 3 +++ level.h | 3 +++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/block.cpp b/block.cpp index 7920cba..51fc75e 100644 --- a/block.cpp +++ b/block.cpp @@ -28,14 +28,27 @@ static std::vector> blockTextures; static std::vector> tutorialBlockTextures; static SDL_Rect blockRect; -Block::Block(float xPos, float yPos, int type, int mode, Level *parent) { +Block::Block(float xPos, float yPos, int type, int mode, Level *parent) : + SpriteInfo(NULL, NULL, &blockRect) +{ x = xPos; y = yPos; + alpha = 255; this->type = type; this->mode = mode; this->parent = parent; crack = BlockCrack(); shine = BlockShine(); + + // initialize textures + if (type != INV) { + if (mode == BLOCK_MODE_NORMAL) { + texture = blockTextures[type]->texture; + } + else if (mode == BLOCK_MODE_TUTORIAL) { + texture = tutorialBlockTextures[type]->texture; + } + } } int Block::shouldBounce(int power) { @@ -147,7 +160,19 @@ int Block::collide(int power) { } } +void Block::setAlpha(int alpha) { + // small optimization so i can call this function every frame in level.cpp + if (alpha != this->alpha) { + SpriteInfo::setAlpha(alpha); + } +} + void Block::draw() { + if (type != INV) { + if (mode == BLOCK_MODE_NORMAL) { + texture = blockTextures[type]->texture; + } + } if (type == INV) { // don't draw invisible blocks return; @@ -162,12 +187,11 @@ void Block::draw() { } spr.x = x; spr.y = y; + spr.setAlpha(alpha); spr.draw(); } void Block_Init() { - printf("---block_init---\n"); - blockTextures = Sprite_LoadPak("block.pak"); tutorialBlockTextures = Sprite_LoadPak("tutorialblock.pak"); blockRect.x = 0; diff --git a/block.h b/block.h index e488943..7a2b8d7 100644 --- a/block.h +++ b/block.h @@ -64,10 +64,9 @@ class Level; #define BLOCK_WIDTH (16) #define BLOCK_HEIGHT (8) -class Block { +class Block : public SpriteInfo{ public: - float x; - float y; + int alpha; int type; int mode; Level *parent; @@ -80,6 +79,8 @@ public: Block(float xPos, float yPos, int type, int mode, Level *parent); int shouldBounce(int power); int collide(int power); + // makes the block partially transparent. alpha should be between 0 and 255 + void setAlpha(int alpha); void draw(); }; diff --git a/level.cpp b/level.cpp index 16e13bc..3347179 100644 --- a/level.cpp +++ b/level.cpp @@ -98,6 +98,7 @@ Level::Level(int xPos, int yPos, int num, int mode) { Block_Stretch_RemoveAll(); levelNum = num; + alpha = 255; // level should start opaque this->mode = mode; if (mode == BLOCK_MODE_TUTORIAL) { @@ -195,6 +196,7 @@ void Level::animate() { else { for (Block &block : animRow) { block.y = rowY; + block.setAlpha(alpha); block.draw(); } } @@ -247,6 +249,7 @@ void Level::disp() { //draw all the blocks in the block arr for (Block &block : blocks) { + block.setAlpha(alpha); block.draw(); block.shine.move(block.x, block.y); block.shine.draw(); diff --git a/level.h b/level.h index 03d4832..673303d 100644 --- a/level.h +++ b/level.h @@ -49,6 +49,9 @@ private: // where the level is on the screen int xPos; int yPos; + // transparency on the level, should be between 0 and 255. Used for fade-outs + // at the end of a stage + int alpha; // how fast the blocks move during animations float blockSpeed; // mover blocks -- 2.45.2