Sign in to follow this  

Problem using buffer in allegro

This topic is 4664 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...
#ifndef BLACKJACK_H
#define BLACKJACK_H

#include <allegro.h>

#define MODE GFX_AUTODETECT_WINDOWED
#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();

#endif 

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) {
        allegro_message(allegro_error);
        return;
    }
    text_mode(-1);
    
    rectfill(buffer,0,0,SCREEN_W,SCREEN_H,bgcol);
    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);
    
    blit(buffer,screen,0,0,0,10,SCREEN_W,SCREEN_H);
   
}

void main(void) 
{ 
    allegro_init(); 
    install_keyboard(); 
    srand(time(NULL));
    setupscreen();
 
    while(!key[KEY_ESC]);
 
    allegro_exit();    
}     

END_OF_MAIN();

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
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

This topic is 4664 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this