~ndiddy/libremo

e27c322003a77f8cfa3ff52206d01ffdcd6fc3cf — Nathan Misner 2 years ago 2fb91b1
added alpha transparency for blocks
4 files changed, 37 insertions(+), 6 deletions(-)

M block.cpp
M block.h
M level.cpp
M level.h
M block.cpp => block.cpp +27 -3
@@ 28,14 28,27 @@ static std::vector<std::unique_ptr<PakFrame>> blockTextures;
static std::vector<std::unique_ptr<PakFrame>> 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;

M block.h => block.h +4 -3
@@ 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();
};


M level.cpp => level.cpp +3 -0
@@ 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();

M level.h => level.h +3 -0
@@ 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