Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Allegro, masked_blit

Ask ALL your questions here

Allegro, masked_blit

Postby vEjEsE on Sat Oct 10, 2009 11:50 am

For those there who know a little bit of allegro, I need some information.

I made a class 'Level', public of class 'Buffer', with a SetLevel function that mask blits the "walls" of the game to the buffer. Well, it actually doesn't, the sprite is not visible in the window. I tried then mask bliting it in the main function by making functions that return the buffer and the 'wall' variables ... and it worked.

Now I'm just confused. The masked_blit() can't blit from inside a class? The blit function can.

Here are the classes:

Code: Select all
// CreateBuffer.h

class CreateBuffer {

    protected:
        BITMAP* Buffer;

    public:
        CreateBuffer();
        ~CreateBuffer();

        BITMAP* GetBuffer() { return Buffer; }
        void BlitBuffer();
        void ClearBuffer();
};

CreateBuffer::CreateBuffer() {

    Buffer = create_bitmap( 800, 600 );
}

CreateBuffer::~CreateBuffer() {

    destroy_bitmap( Buffer );
}

void CreateBuffer::BlitBuffer() {

    blit( Buffer, screen, 0, 0, 0, 0, 800, 600 );
}

void CreateBuffer::ClearBuffer() {

    clear_bitmap( Buffer );
}



Code: Select all
// Level.h

class Level: public CreateBuffer {

  private:
        BITMAP* Wall;

  public:
        Level();
        void SetLevel();

        BITMAP* GetWall() { return Wall; }

        ~Level();

};


Level::Level() {

    Wall = load_bitmap( "data/castle.bmp", NULL );
}


void Level::SetLevel() {

    masked_blit ( Wall, Buffer, 0, 0, 0, 0, 96, 28 );
}


Level::~Level() {

    destroy_bitmap( Wall );
}




And here is the main function that works:
Code: Select all
#include "Library.h"


int main() {

    AllegroSetup cInitialized(1);

    CreateBuffer cBuffer;

    Level cLevel;

    masked_blit( cLevel.GetWall(), cBuffer.GetBuffer(), 0, 0, 0, 0, 800, 600 );

    cBuffer.BlitBuffer();

    readkey();

    return 0;
}
END_OF_MAIN();



But when I try to use the SetLevel function inside of the Level class I get no sprite on the buffer :(
Code: Select all
#include "Library.h"


int main() {

    AllegroSetup cInitialized(1);

    CreateBuffer cBuffer;

    Level cLevel;

    cLevel.SetLevel();

    cBuffer.BlitBuffer();

    readkey();

    return 0;
}
END_OF_MAIN();



All my header files are included in a 'library' header which I include in all .cpp files. The AllegroSetup class just initializes allegro.
I did, redid, rerererereredid it all, tried different ways to do it, and that's the single think that won't work, mask blitting from inside a class!

I read the docs' about masked_blit and there's nothing about 'not working in a class', it kind of sounds unreal, why wouldn't it work in a class ? :\

Just give me a straight answer, even if it's 'YOU ARE IDIOT !". Thanks. :-<


Side note: I used Moosader's sprites for this project, thank you.
Does a universe exist if there is no life to acknowledge it's existence?
User avatar
vEjEsE
 
Posts: 48
Joined: Mon Aug 03, 2009 12:48 am

Re: Allegro, masked_blit

Postby C++ on Sat Oct 10, 2009 3:31 pm

The code below should work. In the comments I explained why.

Code: Select all
    #include "Library.h"


    int main() {

        AllegroSetup cInitialized(1);

        //CreateBuffer cBuffer; Level inherits from CreateBuffer

        Level cLevel;

        cLevel.SetLevel(); // This mask_blit uses the buffer bitmap of the base class of Level, which is CreateBuffer

        cLevel.BlitBuffer(); // CreateBuffer class has no knowledge of classes that derive from it, so you need to use Level here

        readkey();

        return 0;
    }
    END_OF_MAIN();
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Allegro, masked_blit

Postby vEjEsE on Sun Oct 11, 2009 6:44 am

That way it works.

So there's nothing wrong with the masked_blit function, it was all about the inheritance.

I somehow thought about that too, but I came up with: the inherited classes configure the buffer and it doesn't matter if it knows of the other classes because he's modified before output.

Thanks for your answer, now it's all clearer.


Late edit:
Is it then good to make a class that inherits all the game-specific classes ( Level, Player, Objects ) so that I can blit them?
Does a universe exist if there is no life to acknowledge it's existence?
User avatar
vEjEsE
 
Posts: 48
Joined: Mon Aug 03, 2009 12:48 am


Return to QA

Who is online

Users browsing this forum: No registered users and 0 guests