Page 1 of 1

No image displayed.

Posted: Sat Sep 11, 2010 1:57 pm
by Norbo
I cant see anything on the screen when i draw an image to it. The image is properly loaded and blitted, but it does not show on the screen. Im trying to blit a bitmap called title to buffer, and then buffer to screen. This is written in c++ and allegro. Can anybody help?

My code:

Code: Select all

#include <allegro.h>
#include <fstream>
#include <string>

// define some run-states for the game
#define STATE_TITLE     0
#define STATE_MENU      1
#define STATE_GAME      2
#define STATE_SHUTDOWN  255

const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 800;

//quit flag
bool quit=false;
bool fullscreen=false;
//game state changer
int game_state = STATE_TITLE;

//buffer and bg
BITMAP *buffer;
BITMAP *bg;

//game elements
BITMAP *ball;
BITMAP *p1;
BITMAP *p2;

//things
BITMAP *title;
BITMAP *help;

//menu
BITMAP *play1;
BITMAP *play2;
BITMAP *help1;
BITMAP *help2;
BITMAP *exit1;
BITMAP *exit2;

//File stream for the game log.
std::ofstream logger( "log.txt" );

void log( std::string message )
{
    //Write message to file
    logger << message << std::endl;
    //Flush the buffer
    logger.flush();
}

void Init()
{
    log("Initilizing allegro...");
    allegro_init();
    log("Initilizing keyboard...");
    install_keyboard();
    log("Initilizing mouse...");
    install_mouse();
    log("Initilizing timer...");
    install_timer();

      log("Initilizing sound...");
      if(install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL) != 0)
      {
          allegro_exit();
          log("Error setting up Sound");
          exit(1);
      }
    log("Setting color depth...");
    int cd = desktop_color_depth();
    if (cd < 15) cd = 32;
    set_color_depth(cd);
    log("                      ");
    log("Loading all bitmaps...");
    log("Buffer and bg...");
    //buffer and bg
    buffer = create_bitmap(SCREEN_WIDTH,SCREEN_HEIGHT);
    bg = load_bitmap("gfx/bg.bmp",NULL);

    log("Game Elements...");
    //game elements
    ball = load_bitmap("gfx/ball.bmp",NULL);
    if (ball==NULL)
    {
        log("ball is null");
    }
    p1 = load_bitmap("gfx/red.bmp",NULL);
    if (p1==NULL)
    {
        log("tp1 is null");
    }
    p2 = load_bitmap("gfx/blue.bmp",NULL);
    if (p2==NULL)
    {
        log("p2 is null");
    }

    log("Title and help screen...");
    //things
    title = load_bitmap("gfx/title.bmp",NULL);
    if (title==NULL)
    {
        log("title is null");
    }
    help = load_bitmap("gfx/help.bmp",NULL);

    log("Menu...");
    //menu
    play1 = load_bitmap("gfx/play1.bmp",NULL);
    play2 = load_bitmap("gfx/play2.bmp",NULL);
    help1 = load_bitmap("gfx/help1.bmp",NULL);
    help2 = load_bitmap("gfx/help2.bmp",NULL);
    exit1 = load_bitmap("gfx/exit1.bmp",NULL);
    exit2 = load_bitmap("gfx/exit2.bmp",NULL);
    log("Bitmap loading completed.");
    log("                         ");
    if (fullscreen==true)
    {
        log("Making fullscreen window...");
        if (set_gfx_mode(GFX_AUTODETECT,SCREEN_WIDTH,SCREEN_HEIGHT, 0, 0) != 0)
        {
         allegro_exit();
         log("Error setting graphics mode\n%s\n\n");
         exit(1);
        }
    }
    else
    {
        log("Making windowed window...");
        if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,SCREEN_WIDTH,SCREEN_HEIGHT, 0, 0) != 0)
        {
         allegro_exit();
         log("Error setting graphics mode\n%s\n\n");
         exit(1);
        }
    }
    log("Clearing the buffer...");
    clear(buffer);
    log("Initilization complete.");
    log("                       ");
}

void ShowScreen ()
{
        clear_bitmap(buffer);
        log("Bliting buffer to the screen...");
 // blit the whole double buffer to the screen
 blit (buffer,           // source Allegro BITMAP*
       screen,           // destination Allegro BITMAP*
       0, 0,             // source x,y
       0, 0,             // destination x, y
       SCREEN_WIDTH,  // destination width
       SCREEN_HEIGHT); // destination height
       log("Clearing the buffer...");
       log("Showing the screen complete.");
}


void TITLE_LOGIC()
{
    log("Bliting title to the buffer...");
    blit(title,buffer,0,0,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
    log("Done.");
    log("      ");
    ShowScreen();
    if(key[KEY_ESC])
    {
        quit=true;
    }
    if(key[KEY_ENTER])
    {
        game_state=STATE_MENU;
    }
}

void MENU_LOGIC()
{

}

void GAME_LOGIC()
{

}

void cleanUp()
{
    log("Cleaning up...");
    destroy_bitmap(buffer);
    destroy_bitmap(bg);
    destroy_bitmap(ball);
    destroy_bitmap(p1);
    destroy_bitmap(p2);
    destroy_bitmap(title);
    destroy_bitmap(help);
    destroy_bitmap(play1);
    destroy_bitmap(play2);
    destroy_bitmap(help1);
    destroy_bitmap(help2);
    destroy_bitmap(exit1);
    destroy_bitmap(exit2);
    log("Exiting allegro...");
    allegro_exit();
    log("Done.");
    log("      ");

}

int main()
{
    Init();
    while(quit==false)
    {
        switch(game_state)
        {
            case STATE_TITLE:
            {
                log("              ");
                log("Doing title logic.");
                TITLE_LOGIC();
            }
            case STATE_MENU:
            {
                MENU_LOGIC();
            }
            case STATE_GAME:
            {
                GAME_LOGIC();
            }
        }
    }
    cleanUp();
    return 0;
}
END_OF_MAIN();

Re: No image displayed.

Posted: Sat Sep 11, 2010 7:28 pm
by GroundUpEngine
The debug log stops at "Bliting title to the buffer...", but the code seems fine. Havent use Allegro properly in a long while sorry :|

Re: No image displayed.

Posted: Sun Sep 12, 2010 12:00 am
by wearymemory
You're clearing the buffer in the ShowScreen function before it is blitted to the screen.

Code: Select all

void ShowScreen ()
{
    clear_bitmap(buffer);
    . . .

Re: No image displayed.

Posted: Sun Sep 12, 2010 12:11 am
by wearymemory
GroundUpEngine wrote:The debug log stops at "Bliting title to the buffer...", but the code seems fine. Havent use Allegro properly in a long while sorry :|
That's because, in order to simply run the OP's code, set_gfx_mode should be called before the bitmaps are loaded, SCREEN_WIDTH and SCREEN_HEIGHT should correspond to valid dimensions that are available to your screen (1280x800 was not valid for me, so I changed it to 640x480), and the title variable cannot be NULL. In order to see the title on the screen, you must then fix the issue I noted in my previous post.

There are a few other issues with the OP's code.