Thread: Allegro Programming Question

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37

    Question Allegro Programming Question

    Okay I have a two files here. A header file called types.hpp and main.cpp. Here is the contents of types.hpp:
    Code:
    /*
    types.hpp
    header file that defines all the types used in the game
    */
    
    #include <allegro.h>
    #pragma once
    
    typedef BITMAP bmp;
    typedef MIDI mid;
    typedef DATAFILE dat;
    
    //this is because i can't include the windows.h header because of the BITMAP
    //redefinition
    struct point{
        int x;
        int y;
    };
    The contents of main.cpp file are as follows:

    Code:
    /*
    main.cpp
    main file for the program
    */
    
    #include <iostream>
    #include <cstdlib>
    #include <list>
    #include "types.hpp"
    #include "sprites.h"
    
    //bat class
    class bat{
        private:
            point position;
            int lSide, rSide, middle, bbTop, bbLeft, bbRight, bbBottom;
            dat *temp;
            bmp *sprite;
        public:
            bat();
            ~bat();
            void setX(int x){position.x = x;}
            void setY(int y){position.y = y;}
            void incX(int inc){position.x += inc;}
            void incY(int inc){position.y += inc;}
            void decX(int dec){position.x -= dec;}
            void decY(int dec){position.y -= dec;}
            int getX(){return position.x;}
            int getY(){return position.y;}
            int getLSide(){return lSide;}
            int getRSide(){return rSide;}
            int getMiddle(){return middle;}
            int getBBTop(){return bbTop;}
            int getBBLeft(){return bbLeft;}
            int getBBRight(){return bbRight;}
            int getBBBottom(){return bbBottom;}
            bmp *getSprite(){return sprite;}
    };
    
    //ball class
    class ball{
        private:
            point position;
            int lSide, rSide, middle, bbTop, bbLeft, bbRight, bbBottom;
            bmp *sprite;
            dat *temp;
        public:
            ball();
            ~ball();
            void setX(int x){position.x = x;}
            void setY(int y){position.y = y;}
            void incX(int inc){position.x += inc;}
            void incY(int inc){position.y += inc;}
            void decX(int dec){position.x -= dec;}
            void decY(int dec){position.y -= dec;}
            int getX(){return position.x;}
            int getY(){return position.y;}
            int getLSide(){return lSide;}
            int getRSide(){return rSide;}
            int getMiddle(){return middle;}
            int getBBTop(){return bbTop;}
            int getBBLeft(){return bbLeft;}
            int getBBRight(){return bbRight;}
            int getBBBottom(){return bbBottom;}
            bmp *getSprite(){return sprite;}
    };
    
    //brick class
    class brick{
        private:
            point position;
            int lSide, rSide, middle, bbTop, bbLeft, bbRight, bbBottom, id;
            static int idCounter;
        public:
            brick();
            ~brick();
            void setX(int x){position.x = x;}
            void setY(int y){position.y = y;}
            void incIDCounter(){idCounter++;}
            int getX(){return position.x;}
            int getY(){return position.y;}
            int getLSide(){return lSide;}
            int getRSide(){return rSide;}
            int getMiddle(){return middle;}
            int getBBTop(){return bbTop;}
            int getBBLeft(){return bbLeft;}
            int getBBRight(){return bbRight;}
            int getBBBottom(){return bbBottom;}
            int getID(){return id;}
    };
    
    int main(){
        allegro_init();
        install_keyboard();
        set_color_depth(24);
        set_gfx_mode(GFX_AUTODETECT, 800, 600, 0, 0);
        
        //create the image back buffer
        bmp *imgBackBuffer = create_bitmap(800, 600);
        if(!imgBackBuffer){
            allegro_message("Error creating the image back buffer. Program terminating.");
            exit(1);
        }
        
        //create an isntance of the bat class
        bat playerBat;
        
        //main game loop
        while(!key[KEY_ESC]){
            //draw the bat sprite to the image back buffer
            draw_sprite(screen, playerBat.getSprite(), playerBat.getX(), playerBat.getY());
            //copy the contents of the image back buffer to the screen
            blit(imgBackBuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
            //clear the contents of the image back buffer
            clear(imgBackBuffer);
        }
        
        //destroy the bitmaps and unload datafiles
        destroy_bitmap(imgBackBuffer);
    }
    END_OF_MAIN();
    
    //bat class constructor
    bat :: bat(){
        //load the datafile into memory so that the sprites can be used
        temp = load_datafile("spritesDat.dat");
        
        //load the bat sprite into the sprite private member
        sprite = static_cast<bmp*>(temp[batSprite].dat);
        
        //set the position of the bat
        position.x = 50;
        position.y = 50;
        
        //set the other attributes of the bat class
        //this allows for the appropriate information to be used
        lSide = 0;
        rSide = sprite->w;
        middle = sprite->w / 2;
        bbLeft = position.x;
        bbTop = position.y;
        bbRight = (bbLeft + sprite->w);
        bbBottom = (bbTop + sprite->h);
        
        //unload the datafile from memory
        unload_datafile(temp);
    }
    
    //bat class destructor
    bat :: ~bat(){
        //destroy the sprites and set all vars to null
        destroy_bitmap(sprite);
        position.x = 0;
        position.y = 0;
        lSide = 0;
        rSide = 0;
        middle = 0;
        bbLeft = 0;
        bbTop = 0;
        bbRight = 0;
        bbBottom = 0;
    }
    
    //ball class constructor
    ball :: ball(){
        //use the bat constructor for a template
    }
    
    //ball class destructor
    ball :: ~ball(){
        //use the bat destructor for a template
    }
    
    //brick class constructor
    brick :: brick(){}
    
    //brick class destructor
    brick :: ~brick(){}
    The main problem isn't that the program compiles. The problem is that the bat sprite doesn't appear on the screen. Can someone help me with this?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I've actually never programmed with Allegro, but maybe you have to call a function like updateScreen()?

    What happens if you just try to bit a simple surface at (0,0)?

    Other than that, try printing the position of the bat, so you can see where you're trying to paint it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    It is because you are drawing your image directly to the screen then blitting the buffer to the screen therefore overwriting you image.
    It should be:
    Code:
    draw_sprite(buffer, spriteImg, spx, spx)
    And just a little design tip make your x,y public there is no need to make them have all those member functions to set and get the values
    Last edited by prog-bman; 08-24-2006 at 02:48 PM.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  2. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  3. Allegro Question
    By gnu-ehacks in forum Game Programming
    Replies: 2
    Last Post: 12-23-2001, 11:57 PM
  4. ALLEGRO Question
    By Unregistered in forum Game Programming
    Replies: 10
    Last Post: 10-22-2001, 01:27 PM
  5. ALLEGRO Question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-14-2001, 11:06 AM