CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2012
    Posts
    1

    Allegro Issue ( Access Violation )

    Today I started playing with Allegro and came into an issue...

    main.cpp
    Code:
    #include <allegro.h>
    #include "sprite.h"
    
    BITMAP *background;
    
    int score = 0;
    sprite ship( "images//ship.bmp", 5, 5 );
    
    void getMovement()
    {
    
    }
    
    void draw()
    {
        textprintf_ex( screen, font, 175, 10, makecol( 255, 255, 255 ), -1, "Score: %d", score );
        ship.draw();
    }
    
    void setup()
    {
        allegro_init();
        install_keyboard();
        background = load_bitmap( "images//background.bmp", NULL );
        blit( background, screen, 0, 0, 0, 0, background->w, background->h );    
    }
    
    int main()
    {
        set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 600, 0, 0 );        //windowed
        setup();
        while ( !key[KEY_ESC] )
        {
            draw();
            getMovement();
            rest( 10 );
        }
        return 0;
    }   
    END_OF_MAIN();
    sprites.h...
    Code:
    //sprites.h
    #include <allegro.h>
    
    class sprite
    {
      private:
        int x, y;
      public:
        BITMAP *image;
        sprite( char path[], int, int );
        ~sprite();
        void draw();
    };
    
    sprite::sprite( char path[], int startingX, int startingY )
    {             
       image = load_bitmap( path, NULL );
       x = startingX;
       y = startingY;  
    }
    
    sprite::~sprite()
    {
         
    }
    
    void sprite::draw()
    {
        draw_sprite( screen, image, x, y );
    }
    However the image will never get drawn and the following error will result "An Access Violation (Segmentation Fault) raised in your program."

    If anyone could help it would be greatly appreciated. Thanks in advanced.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Allegro Issue ( Access Violation )

    Quote Originally Posted by nicholas.odo View Post
    Today I started playing with Allegro and came into an issue...
    I have never used Allegro.

    However, do you not check for errors when you call Allegro functions?
    Code:
       image = load_bitmap( path, NULL );
    How do you know that the image loaded correctly? You cannot just assume the function worked -- you must check all error codes that third-party functions provide.

    Possibly that is your problem -- you're assuming that all of those functions worked successfully without checking the return value, and possibly one or more of those functions fail. You then go and do things in your program on bogus pointer values, causing an access violation.

    That is my guess -- even if that isn't the issue, you should change your code so that you're checking the return value from those functions.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured