graphics hate me [still NOT fixed!]

Started by
7 comments, last by TraderJack 18 years, 11 months ago
Why is it that graphics hate me? I have this sidescroller testing device. It tests an object-based level design by creating rectangular objects for terrain. My program, however, compiles perfectly, and instead of being happy, flashes across the screen before instantly disappearing. I think it's an issue in the graphics, cause the game loop looks fine to me... I've included main.cpp. If you need anything else, ask away!

#include "allegro.h"
#include "player.h"
#include "object.h"

BITMAP *image;
BITMAP *buffer;
BITMAP *longbar;
player player1;
object sample[5];

/***********8*******Timer*************8*******/

volatile long speed_counter = 0;
void increment_speed_counter()
{
     speed_counter ++;
}
END_OF_FUNCTION(increment_speed_counter);

/******************LoadGFX********************/

void loadgfx()
{
     buffer = create_bitmap(800, 600);
     image = load_bitmap("C:/Program Code/Bitmaps/Bucky/Bucky1.bmp", NULL);
     longbar = load_bitmap("C:/Program Code/Bitmaps/MyBitmaps/Longbar.bmp", NULL);
}

/***********Draw**************/

void draw()
{
     player1.draw();
     sample[0].draw();
}

/**********Main****************/

int main (int argc, char *argv[])
{
    
/**********Initialization*********/
    
    allegro_init();
    install_keyboard();
    install_timer();
    
    LOCK_VARIABLE(speed_counter);
    LOCK_FUNCTION(increment_speed_counter); 
    install_int_ex(increment_speed_counter, BPS_TO_TIMER(30));
    
    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT, 800, 600, 0, 0);
    
/*********Load graphics**********/
    
    loadgfx();

/*********Game Loop***********/
    
    player1.init();
    sample[0].init( 0, 500, longbar);
    
    while(!key[KEY_ESC])
    {
        player1.move();
        
        acquire_screen();
        draw();
        blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
        clear(buffer);
        release_screen();
    }   
    
    destroy_bitmap(buffer);
    destroy_bitmap(image);
    destroy_bitmap(longbar);
    
    return 0;
}
END_OF_MAIN()


Thanks in advance! -IV [Edited by - TraderJack on May 17, 2005 9:58:26 PM]

-IVHumble Student

Advertisement
If screen is there for 1 frame and then dissapears then the problem is probably in your main loop or in your event handler,bcs initializations is good if u can see it even or a 1 frame. I've got some strange issues about sprite going into upper left corner when i press any key on keyboard and i solved it by puting break after every case command in event handler.

Maybe i have gone off question bcs i didnt understand perfectly your question.
Serbia,Zrenjanin
Hi,

I'm not exactly sure how Allegro works but I think it could be a problem with your main game loop having no delay. I can see the timer being initialised but I can't see anything in your main loop using the timer. You could try throwing a Sleep(<num of milliseconds>); in the main loop just to test if it is that.

Hope that helped.

acquire_screen()
release_screen()

What exactly do those two functions do? If they do what they're names might imply, then they are quite likely the culprits.
Quote:Original post by nilkn
acquire_screen()
release_screen()

What exactly do those two functions do? If they do what they're names might imply, then they are quite likely the culprits.


Oh, that's just allegro's way of double-buffering. Here, take a look at a program that works just fine (this was the prototype to this program):

#include <allegro.h>#include "player.h"BITMAP *image;BITMAP *buffer;player player1;/***********8*******Timer*************8*******/volatile long speed_counter = 0;void increment_speed_counter(){     speed_counter ++;}END_OF_FUNCTION(increment_speed_counter);/******************LoadGFX********************/void loadgfx(){     buffer = create_bitmap(800, 600);     image = load_bitmap("C:/Program Code/Bitmaps/Bucky/Bucky1.bmp", NULL);}/***********Draw**************/void draw(){     player1.draw();     line( buffer, 0, 500, 800, 500, makecol(255,0,255) );}/**********Main****************/int main (int argc, char *argv[]){    /**********Initialization*********/        allegro_init();    install_keyboard();    install_timer();        LOCK_VARIABLE(speed_counter);    LOCK_FUNCTION(increment_speed_counter);     install_int_ex(increment_speed_counter, BPS_TO_TIMER(30));        set_color_depth(16);    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);    /*********Load graphics**********/        loadgfx();/*********Game Loop***********/        player1.init();        while(!key[KEY_ESC])    {        player1.move();                acquire_screen();        draw();        blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);        clear(buffer);        release_screen();    }           destroy_bitmap(buffer);    destroy_bitmap(image);        return 0;}END_OF_MAIN()


Ugh, I bet this all comes down to forgetting a variable or some simple line of code...

-IV

-IVHumble Student

what is in object::draw() ?
--Riley
Here's object.h followed by .cpp

// .h#ifndef __OBJECT_H__#define __OBJECT_H__#include "allegro.h"#include "player.h"class player;extern BITMAP* image;extern BITMAP* buffer;class object{public:               float x;        float y;        bool isactive;                float top;        float bottom;        float left;        float right;                object();        ~object();               BITMAP* graphic;                void init(int, int, BITMAP*);        void move();        void setsides();        void draw();        };#endif// .cpp#include "object.h"#include "player.h"object::object(){}object::~object(){}void object::setsides(){     top = y;     bottom = (y + graphic->h);     left = x;     right = (x + graphic->w);}void object::init(int _x, int _y, BITMAP* grafik){     x = _x;     y = _y;     setsides();     isactive = true;     graphic = grafik;}void object::draw(){     draw_sprite(buffer, graphic, x, y);}

-IVHumble Student

Heh, in Object::init you are calling Object::setsides before you are assigning the object its graphic. Your init function should be changed to:

void object::init(int _x, int _y, BITMAP* grafik){     x = _x;     y = _y;         //moved!     graphic = grafik;     setsides();     isactive = true;}


I hope that solves your problem.
Holy christ, that DOES work! Wtf!? That's suprising!

Alright, now, my probelm is he jumps and falls right through the bar, but I can fix that on my own... I think....

Thanks everyone!

-IV

-IVHumble Student

This topic is closed to new replies.

Advertisement