Jump to content
  • Advertisement
Sign in to follow this  

Problem using buffer in allegro

This topic is 4969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to blit everything to a buffer before I turn it over to the screen, but my program crashes on run every time I try to use the buffer at all. I included a header file that declares a BITMAP...well, here's the header...

#include <allegro.h>

#define WIDTH 640
#define HEIGHT 480
#define START_MONEY 100

#define RED makecol(255,0,0)
#define BLUE makecol(0,0,255)
#define YELLOW makecol(255,255,0)
#define WHITE makecol(255,255,255)
#define BLACK makecol(0,0,0)

int money = START_MONEY;
int bgcol = RED;
int txtcol = WHITE;
BITMAP *buffer = create_bitmap(SCREEN_W,SCREEN_H-10);

void setupscreen();


So that should declare the *buffer as global, so I can use it, right? Now here's my main file:
#include "blackjack.h"

void setupscreen()
    //set video mode    
    int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
    if (ret != 0) {
    textout_centre(buffer,font,"Funky BlackJack",WIDTH/2,20,txtcol);
    //print title
    textprintf(buffer, font, 1, 1, WHITE, "BlackJack Allegro");

    //draw screen border        
    rect(buffer, 0, 12, SCREEN_W-1, SCREEN_H-1, YELLOW);
    rect(buffer, 1, 13, SCREEN_W-2, SCREEN_H-2, YELLOW);

void main(void) 


What am I doing wrong?

Share this post

Link to post
Share on other sites
You want to make a video bitmap using the create_video_bitmap() function. Instead of calling the function to create the bitmap in the globals area, you might want to consider creating the video bitmap after setting up your graphics stuff, since calling this function before then will most likely crash it.

Share this post

Link to post
Share on other sites
You're attempting to create the bitmap before you initialized Allegro. Heck, you shouldn't even try to create a bitmap before you've set the video mode.

Share this post

Link to post
Share on other sites
Also, when you created your buffer variable, you are making it size "SCREEN_W, SCREEN_H-10" Why in the world would you make the Height the screen height minus 10? Then when you try to blit a full screen to the buffer you are going to overflow that BITMAP which will probably crash. I don't remember if allegro automatically clips in a situation like this or not, but I wouldn't try it.

You should never actually define variables in a header. You should only Declare them. If you want the BUFFER to be global, you should declare it like this:

In the Header

extern BITMAP* buffer;

In the Source File

//Inside your main() function, after allegro is initialized.
BITMAP* buffer = create_bitmap(SCREEN_W, SCREEN_H);

Share this post

Link to post
Share on other sites
Also, add error checking code to see if the buffer was actually created. Like this:

buffer = create_bitmap(WIDTH, HEIGHT);

if(buffer == NULL)
// error.

You should always do that error check when you load or create a bitmap in allegro.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!