Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90

    Game Programming with Allegro


    My contribution to this forum. This tutorial is written in a fashion that you should have a good understanding of C programming.

    So you decided to use C/C++ as your programming language. And Allegro as your library. This tutorial will lead you through
    the steps using Allegro and using the mouse and keyboard. A lot of the information you will read here was gathered while reading Game Programming All in One. And from the Allegro manual.

    You will want to get:
    Allegro for DMC
    Allegro for MinGW
    Allegro for Microsoft Visual C++ 6.0
    Allegro for Microsoft Visual C++ 7.0
    Allegro for Microsoft Visual C++ 7.1
    Allegro for Microsoft Visual C++ 8.0
    Allegro Tools & Examples

    Compiling your programs you will want to link with lilalleg.a or -lalleg.

    Lets get started in our first section.

    In this section we will make sure you have allegro installed properly.

    Sample 1:
    Code:
    #include <stdio.h>
    //Allegro library
    #include <allegro.h>
    
    int main()
    {
        //Macro which initialises the Allegro library.
        allegro_init();
    
        //Sets the color depth 8,16,32
        set_color_depth(16);
    
        //Set the screen to Windowed 640x480 with 0x0 virtual screen
        if ((set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0))
        {
            //On sucess video initilizing allegro returns 0
            //but on fail this handles the error
            allegro_message("Error initilizing the Video");
            //exit the program safely
            exit(-1);
        }
    
        //Installs the Allegro keyboard interrupt handler
        install_keyboard();
    
        //Sets your window title
        set_window_title("Test Allegro");
    
        // While Escape is not being pressed
        while (!key[KEY_ESC]);
    
        //Removes the Allegro system
        allegro_exit();
    
        return 0;
    }
    END_OF_MAIN() //note there is no ; here
    If this compiles for you then you installed allegro properly and you are ready for the next part.

    Drawing primitives:
    Drawing with Allegro is very easy. makecol(r, g, b) defines a color used for drawing.

    Writes a pixel to the specified position in the bitmap, using the current drawing mode and the bitmap's clipping rectangle.
    Code:
    void putpixel(BITMAP *bmp, int x, int y, int color);
    Example:
    Code:
    putpixel(screen, 10, 30, some_color);
    Post #6 Example 1.1 Using putpixel
    Draws a line onto the bitmap, from point (x1, y1) to (x2, y2).
    Code:
    void line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color);
    Draws an outline rectangle with the two points as its opposite corners.
    Code:
    void rect(BITMAP *bmp, int x1, int y1, int x2, int y2, int color);
    Draws a solid, filled rectangle with the two points as its opposite corners.
    Code:
    void rectfill(BITMAP *bmp, int x1, int y1, int x2, int y2, int color);
    Draws a filled triangle between the three points.
    Code:
    void triangle(BITMAP *bmp, int x1, y1, x2, y2, x3, y3, int color);
    Draws a circle with the specified centre and radius.
    Code:
    void circle(BITMAP *bmp, int x, int y, int radius, int color);
    Draws a filled circle with the specified centre and radius.
    Code:
    void circlefill(BITMAP *bmp, int x, int y, int radius, int color);
    Draws an ellipse at x, y with the specified radius x and radius y.
    Code:
    void ellipse(BITMAP *bmp, int x, int y, int rx, int ry, int color);
    Draws a filled ellipse with the specified centre and radius.
    Code:
    void ellipsefill(BITMAP *bmp, int x, int y, int rx, int ry, int color);
    Draws a circular arc with centre x, y and radius r, in an anticlockwise direction starting from the angle a1 and ending when it reaches a2. These values are specified in 16.16 fixed point format, with 256 equal to a full circle, 64 a right angle, etc. Zero is to the right of the centre point, and larger values rotate anticlockwise from there.
    Code:
    void arc(BITMAP *bmp, int x, y, fixed ang1, ang2, int r, int color);
    Example:
    Code:
    /* Draw a black arc from 4 to 1 o'clock. */
    arc(screen, SCREEN_W/2, SCREEN_H/2, 
        itofix(-21), itofix(43), 50, makecol(0, 0, 0));
    Clears the bitmap to the specified color.
    Code:
    void clear_to_color(BITMAP *bitmap, int color);
    Example:
    Code:
    /* Clear the screen to red. */
    clear_to_color(screen, makecol(255, 0, 0));
    Flood fills an enclosed area, starting at point (x, y), with the specified color.
    Code:
    void floodfill(BITMAP *bmp, int x, int y, int color);
    Reads a pixel from point (x, y) in the bitmap.
    Return value: Returns -1 if the point lies outside the bitmap (ignoring the clipping rectangle), otherwise the value of the pixel in the color format of the bitmap.
    Code:
    int getpixel(BITMAP *bmp, int x, int y);
    Draws a filled polygon with an arbitrary number of corners. Pass the number of vertices's and an array containing a series of x, y points (a total of vertices's*2 values).
    Code:
    void polygon(BITMAP *bmp, int vertices, const int *points, int color);
    Example:
    Code:
    int points[12] = { 50,  50,100, 100,100, 150, 
                       50, 200,  0, 150,  0, 100 };
          ...
          clear_to_color(screen, makecol(255, 255, 255));
          polygon(screen, 6, points, makecol(0, 0, 0));
    Displaying Text to the screen:
    Formatted text output, using a printf() style format string.
    Code:
    void textprintf_ex(BITMAP *bmp, const FONT *f, int x, int y, int color,
                        int bg, const char *fmt, ...);
    Like textprintf_ex(), but interprets the x coordinate as the centre rather than the left edge of the string.
    Code:
    void textprintf_centre_ex(BITMAP *bmp, const FONT *f, int x, int y, int color,
                               int bg, const char *fmt, ...);
    Like textprintf_ex(), but interprets the x coordinate as the right rather than the left edge of the string.
    Code:
    void textprintf_right_ex(BITMAP *bmp, const FONT *f, int x, y, color,
                             int bg, const char *fmt, ...);
    Programming the keyboard, mouse:

    These are the keyboard scan codes
    Code:
    extern volatile char key[KEY_MAX];
    Code:
    KEY_A ... KEY_Z,
    KEY_0 ... KEY_9,
    KEY_0_PAD ... KEY_9_PAD,
    KEY_F1 ... KEY_F12,
    
    KEY_ESC, KEY_TILDE, KEY_MINUS, KEY_EQUALS,
    KEY_BACKSPACE, KEY_TAB, KEY_OPENBRACE, KEY_CLOSEBRACE,
    KEY_ENTER, KEY_COLON, KEY_QUOTE, KEY_BACKSLASH,
    KEY_BACKSLASH2, KEY_COMMA, KEY_STOP, KEY_SLASH,
    KEY_SPACE,
    KEY_INSERT, KEY_DEL, KEY_HOME, KEY_END, KEY_PGUP,
    KEY_PGDN, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN,
    
    KEY_SLASH_PAD, KEY_ASTERISK, KEY_MINUS_PAD,
    KEY_PLUS_PAD, KEY_DEL_PAD, KEY_ENTER_PAD,
    
    KEY_PRTSCR, KEY_PAUSE,
    
    KEY_ABNT_C1, KEY_YEN, KEY_KANA, KEY_CONVERT, KEY_NOCONVERT,
    KEY_AT, KEY_CIRCUMFLEX, KEY_COLON2, KEY_KANJI,
    
    KEY_LSHIFT, KEY_RSHIFT,
    KEY_LCONTROL, KEY_RCONTROL,
    KEY_ALT, KEY_ALTGR,
    KEY_LWIN, KEY_RWIN, KEY_MENU,
    KEY_SCRLOCK, KEY_NUMLOCK, KEY_CAPSLOCK
    KEY_EQUALS_PAD, KEY_BACKQUOTE, KEY_SEMICOLON, KEY_COMMAND
    and key_shifts
    Code:
    KB_SHIFT_FLAG
    KB_CTRL_FLAG
    KB_ALT_FLAG
    KB_LWIN_FLAG
    KB_RWIN_FLAG
    KB_MENU_FLAG
    KB_COMMAND_FLAG
    KB_SCROLOCK_FLAG
    KB_NUMLOCK_FLAG
    KB_CAPSLOCK_FLAG
    KB_INALTSEQ_FLAG
    KB_ACCENT1_FLAG
    KB_ACCENT2_FLAG
    KB_ACCENT3_FLAG
    KB_ACCENT4_FLAG
    Example:
    Code:
    if ((key_shifts & KB_CTRL_FLAG) && key[KEY_ENTER])
    printf("You pressed CTRL+Enter\n");
    You can clear the key buffer if needed.
    Code:
    clear_keybuf();
    Returns the next character from the keyboard buffer, in ASCII format. If the buffer is empty, it waits until a key is pressed. You can see if there are queued key presses with keypressed(). The low byte of the return value contains the ASCII code of the key, and the high byte the scan code. The scan code remains the same whatever the state of the shift, Ctrl and alt keys, while the ASCII code is affected by shift and Ctrl in the normal way (shift changes case, Ctrl+letter gives the position of that letter in the alphabet, eg. Ctrl+A = 1, Ctrl+B = 2, etc).

    Pressing alt+key returns only the scan code, with a zero ASCII code in the low byte.
    Code:
    int readkey();
    Example:
    Code:
    int val;
          ...
          val = readkey();
          if ((val & 0xff) == 'd')     /* by ASCII code */
             allegro_message("You pressed 'd'\n");
    
          if ((val >> 8) == KEY_SPACE) /* by scancode */
             allegro_message("You pressed Space\n");
    
          if ((val & 0xff) == 3)       /* ctrl+letter */
             allegro_message("You pressed Control+C\n");
    
          if (val == (KEY_X << 8))     /* alt+letter */
             allegro_message("You pressed Alt+X\n");
    Returns the next character from the keyboard buffer, in Unicode format. If the buffer is empty, it waits until a key is pressed. You can see if there are queued key presses with keypressed(). The return value contains the Unicode value of the key, and if not NULL, the pointer argument will be set to the scan code. Unlike readkey(), this function is able to return character values greater than 255.
    Code:
    int ureadkey(int *scancode);
    You can also use a support function provided by allegro to convert the scan code to an ASCII values.
    Code:
    scancode_to_ascii(int scancode);
    One more support function you might want to use is set_keyboard_rate, which changes the key repeat rate of the keyboard (in milliseconds). You can disable the key repeat by passing zeros to this function.
    Code:
    void set_keyboard_rate(int delay, int repeat);
    Stuffs a key into the keyboard buffer, just as if the user had pressed it. The parameter is in the same format returned by

    readkey().
    Code:
    void simulate_keypress(int key);
    Stuffs a key into the keyboard buffer, just as if the user had pressed it. The parameter is in the same format returned by

    ureadkey().
    Code:
    void simulate_ukeypress(int key, int scancode);
    Example 1 KeyTest Program:
    Code:
    #include <conio.h>
    #include <stdlib.h>
    #include <allegro.h>
    #define WHITE makecol(255, 255, 255)
    
    void main (void)
    {
        int k, x, y;
        int scancode, ascii;
        
        //initialize program
        allegro_init();
        set_color_depth(16);
        set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
        install_keyboard();
    
        //display title
        textprintf_ex(screen, font, 10, 10, WHITE, 0, "KeyTest Program");
        textprintf_ex(screen, font, 10, 10, WHITE, 0, "Press a key(ESC to quit)");
        x = SCREEN_W / 2 - 60;
        y = SCREEN_H / 2 - 20;
        
        while (!key[KEY_ESC])
        {
            //get and convert scan code
            k = readkey();
            scancode = (k >> 8);
            ascii = scancode_to_ascii(scancode);
    
            //display key values
            textprintf_ex(screen, font, x, y, makecol(255, 100, 200),
                          0, "Key Value = %-6d", k);
            textprintf_ex(screen, font, x, y + 15, makecol(255, 100, 200),
                          0, "Scan code = %-6d", scancode);
            textprintf_ex(screen, font, x, y + 30, makecol(255, 100, 200),
                          0, "ASCII code = %-6d", ascii);
            textprintf_ex(screen, font, x, y + 45, makecol(255, 100, 200),
                          0, "Character = %-6c", (char)ascii);
        }
        allegro_exit();
    }
    END_OF_MAIN()
    The Mouse Handler:

    Installs the Allegro mouse handler. You must do this before using any other mouse functions.
    Return value: Returns -1 on failure, zero if the mouse handler is already installed (in which case this function does nothing) and the number of buttons on the mouse if the mouse handler has successfully been installed (ie. this is the first time a handler is installed or you have removed the previous one).
    Code:
    int install_mouse();
    Wherever possible, Allegro will read the mouse input asynchronously (ie. from inside an interrupt handler), but on some platforms that may not be possible, in which case you must call this routine at regular intervals to update the mouse state variables.
    Code:
    int poll_mouse();
    You can check the polled state using mouse_needs_poll. It's a good idea to call this function to determine whether polling is needed.
    Code:
    int mouse_needs_poll();
    Global variables containing the current mouse position and button state. Wherever possible these values will be updated asynchronously, but if mouse_needs_poll() returns TRUE, you must manually call poll_mouse() to update them with the current input state. The `mouse_x' and `mouse_y' positions are integers ranging from zero to the bottom right corner of the screen.

    The `mouse_z' and `mouse_w' variables hold the current vertical and horizontal wheel position, when using an input driver that supports wheel mice. The `mouse_b' variable is a bitfield indicating the state of each button: bit 0 is the left button, bit 1 the right, and bit 2 the middle button. Additional non standard mouse buttons might be available as higher bits in this variable.
    Code:
    extern volatile int mouse_x;
    extern volatile int mouse_y;
    extern volatile int mouse_z;
    extern volatile int mouse_w;
    extern volatile int mouse_b;
    extern volatile int mouse_pos;
    Usage example:
    Code:
    if (mouse_b & 1)
        printf("Left button is pressed\n");
    if (!(mouse_b & 2))
        printf("Right button is not pressed\n");
    The `mouse_pos' variable has the current X coordinate in the upper 16 bits and the Y in the lower 16 bits. This may be useful in tight polling loops where a mouse interrupt could occur between your reading of the two separate variables, since you can copy this value into a local variable with a single instruction and then split it up at your leisure.
    Code:
       int pos, x, y;
       
       pos = mouse_pos;
       x = pos >> 16;
       y = pos & 0x0000ffff;
    You don't like Allegro's mouse pointer? No problem. Use this function to supply an alternative of your own. If you change the pointer and then want to get Allegro's lovely arrow back again, call set_mouse_sprite(NULL).
    Code:
    void set_mouse_sprite(BITMAP *sprite);
    The mouse focus is the bit of the pointer that represents the actual mouse position, ie. the (mouse_x, mouse_y) position. By default this is the top left corner of the arrow, but if you are using a different mouse pointer you might need to alter it.
    Code:
    void set_mouse_sprite_focus(int x, int y);
    Tells Allegro to display a mouse pointer on the screen. This will only work if the timer module has been installed. The mouse pointer will be drawn onto the specified bitmap, which should normally be `screen' (see later for information about bitmaps). To hide the mouse pointer, call show_mouse(NULL).
    Warning: if you draw anything onto the screen while the pointer is visible, a mouse movement interrupt could occur in the middle of your drawing operation. If this happens the mouse buffering and graphics drawing code will get confused and will leave 'mouse droppings' all over the screen. To prevent this, you must make sure you turn off the mouse pointer whenever you draw onto the screen. This is not needed if you are using a hardware cursor.
    Code:
    void show_mouse(BITMAP *bmp);
    Helper for hiding the mouse pointer prior to a drawing operation. This will temporarily get rid of the pointer, but only if that is really required (ie. the mouse is visible, and is displayed on the physical screen rather than some other memory surface, and it is not a hardware or OS cursor). The previous mouse state is stored for subsequent calls to unscare_mouse().
    Code:
    void scare_mouse();
    Like scare_mouse(), but will only hide the cursor if it is inside the specified rectangle. Otherwise the cursor will simply be frozen in place until you call unscare_mouse(), so it cannot interfere with your drawing.
    Code:
    void scare_mouse_area(int x, int y, int w, int h);
    Undoes the effect of a previous call to scare_mouse() or scare_mouse_area(), restoring the original pointer state.
    Code:
    void unscare_mouse();
    Moves the mouse to the specified screen position. It is safe to call even when a mouse pointer is being displayed.
    Code:
    void position_mouse(int x, int y);
    There are two helper functions that you will likely never use, but which are available. The set_mouse_range function limits the mouse pointer to a specified rectangular region on the screen.
    Code:
    void set_mouse_range(int x1, int y1, int x2, int y2);
    The second helper function is set_mouse_speed, this overrides the default mouse pointer speed set by the operating system.
    Code:
    void set_mouse_speed(int xspeed, int yspeed);
    Often, games will need to track the mouse movements without regard to the position of a pointer on the screen. Many games (first-person shooters) don't even use a mouse pointer, rather, they use the mouse to adjust the view-point of the player in the game world. This is called relative mouse motion because you can continue to move the mouse to the left (lifting the mouse and dragging it to the left again) over and over again, resulting in the game would spinning around the player continuously. The mouse need not be limited to the boundaries of the screen, it can return an infinite range of mouse movement.
    Code:
    void set_mouse_sprite_focus(int x, int y);
    To use the mickeys returned by this function, you will want to create two integer variables to keep track of the last values, and then compare them to the new values returned by get_mouse_mickeys. You can then determine whether the mouse has moved up, down, left, or right, with the result having some effect in the game.
    Sets the mouse wheel position variable to the specified value.
    Code:
    void position_mouse_z(int z);
    Sets the horizontal mouse wheel position to the specified value.
    Code:
    void position_mouse_w(int w);

    Comments on this post

    • Viper_SB agrees : Nicly done I like it thanks
    • aitken325i agrees
    • ElijaTheGold agrees : Nice Job
    • B-Con agrees
    • LinuxPenguin agrees : Wow
    • codergeek42 agrees : Woah. O_o
    • dotancohen agrees : Very nice, nice, nice
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    This is the 2nd part of this tutorial. You will learn to draw/load/blit bitmaps onto the screen/buffer and move them around the screen. You will also get a small collision tutorial in here. I will also teach you double buffering to automatically vsync() your game. I will teach you about solid/alpha blending sprites.
    Lets take a look at the screen
    Code:
    extern BITMAP *screen
    Code:
    typedef struct BITMAP
    {
        int w, h;
        int clip;
        int cl, cr, ct, cb;
        GFX_VTABLE *vtable;
        void *write_bank;
        void *read_bank;
        unsigned long id;
        void *extra;
        int x_ofs;
        int y_ofs;
        int seg;
        ZERO_SIZE_ARRAY(unsigned char *, line);
    }BITMAP;
    The information in the BITMAP structure is not really useful to you as a programmer because it is almost entirely used by Allegro internally. Some of the values are useful, such as w and h (width and height) and perhaps the clipping variables.

    Creating Bitmaps:
    you can use create_bitmap function to create a memory bitmap.
    Code:
    BITMAP *create_bitmap(int width, int height);
    By default, this function creates a bitmap using the current color depth. It is a good idea if you want to use your artwork is to call set_color_depth after set_gfx_mode when your program starts. The bitmap created with create_bitmap has clipping enabled by default, so if you draw outside the boundary of the bitmap, no memory will be corrupted. There is actually a related version of this function you can use if you want to use a specific color depth.
    Code:
    BITMAP *create_bitmap_ex(int color_depth, int width, int height);
    You can retrieve the color depth of a bitmap using this function:
    Code:
    int bitmap_color_depth(BITMAP *bmp);
    Any bitmap you create you must also clear it because a new bitmap has random pixels on it based on the contents of memory at the space where the bitmap is now located.
    You can clear a bitmap using this function:
    Code:
    void clear_bitmap(BITMAP *bmp)
    You can fill a specified bitmap with a certain color.
    Code:
    void clear_to_color(BITMAP *bmp, int color);
    Example:
    Code:
    //clears the screen to black
    clear_to_color(screen, makecol(0, 0, 0));
    Code:
    BITMAP *create_sub_bitmap(BITMAP *parent, int x, y, width, height);
    This function creates a sub-bitmap of an existing bitmap that actually shares the memory of the parent bitmap. Any changes you make to the sub-bitmap will be instantly visible on the parent and vice versa.

    Cleaning the house:
    It's is important to destroy any bitmap that you create as to eliminate memory leaks in your program.
    Code:
    void destroy_bitmap(BITAMP *bitmap);
    Acquiring and releasing bitmaps:
    You can lock a bitmap by calling
    Code:
    void acquite_bitmap(BITMAP *bmp);
    locking a bitmap allows you to draw everything you need to it. If you lock a bitmap you must also unlock it. You can unlock a bitmap by calling
    Code:
    void release_bitmap(BITMAP *bmp);
    Allegro give you a shortcut function if you want to lock and unlock the screen
    Code:
    void acquire_screen();
    void release_screen();
    If you do not unlock a bitmap before locking it again your program will most likely crash because of repeated locking of a bitmap.
    You can also clip a bitmap. Clipping is the process of ensuring that drawing to a bitmap or the screen does not occur beyond the boundary of that object.
    Code:
    void set_clip(BITMAP *bitmap, int x1, int y1, int x2, int y2);
    By default every bitmap has clipping turned on but if you wanted to turn it off you can use
    Code:
    set_clip(bmp, 0 , 0, 0, 0);
    Although removing the clipping is not generally a good idea if your careful with you bitmaps never leaving the screen you can do this to gain a slight performance boost to your program.

    Now lets get into loading bitmaps from the disk(that you/someone made).
    The easiest way to load a bitmap file is to call the load_bitmap function
    Code:
    BITMAP *load_bitmap(const char *filename, RGB *pal);
    Example:
    Code:
    load_bitmap("Mysprite.bmp", 0);
    You can also save bitmaps that you may have created in your program by calling
    Code:
    int save_bitmap(const char *filename, BITMAP *bmp, const RGB *pal);
    Example:
    Code:
    //Lets take a screenshot and save it
    BITMAP *bmp
    bmp = create_sub_bitmap(screen, 0, 0, SCREEN_W, SCREEN_H);
    save_bitmap("screenshot.bmp", bmp, NULL);
    destroy_bitmap(bmp);
    Now lets take these bitmaps that we loaded and draw then to the screen.
    Code:
    void blit(BITMAP *source, BITMAP *destination, int source_x, int source_y,
              int dest_x, int dest_y, int width, int height);
    Here are the parameters for the blit function
    Code:
    BITMAP *source          The source bitmap (copy from)
    BITMAP *destination     The destination bitmap (copy to)
    int source_x            The x location on the source bitmap to copy from
    int source_y            The y location on the source bitmap to copy from
    int dest_x              The x location on the destination bitmap to copy to
    int dest_y              The y location on the destination bitmap to copy to
    int width               The width of the source rectangle to be copied
    int height              The height of the source rectangle to be copied
    You can also scale your blit with stretch_blit which is very much like the blit function.
    Code:
    void blit(BITMAP *source, BITMAP *destination, int source_x, int source_y,
              source_width, source_height, int dest_x, int dest_y, int width, 
              int height,dest_width, dest_height);
    The only changes are
    Code:
    int source_width        The width of the source rectangle
    int source_height       The height of the source rectangle
    int dest_width          The width of the destination rectangle(scaled into)
    int dest_height         The height of the destination rectangle(scaled into)
    stretch_blit can be very useful in simulating zooming in and out. One thing though is that the stretch_blit function unlike the blit function is that stretch_blit source and destination bitmaps must be the same color depth and it can only be stretched to a memory bitmap(screen is not a memory bitmap).

    I do not use blit myself because it doesn't usually have the functionality that I like.
    Lets get into the functions that I find easier to use and more portable.
    Code:
    void draw_sprite(BITMAP *bmp, BITMAP *sprite, int x, int y);
    This function is much like blit in that it draws the sprite image using transparency. A transparent pixel in a bitmap is a pixel with the color 255 Red 0 Blue 255 Green (Bright Pink).
    For this next example you will need to create a drawing doesn't matter of what or how big but you must save it to the same directory as your source code for this example. Make and save this sprite with the name "mysprite.bmp" without the quotes.
    Code:
    #include <conio.h>
    #include <stdlib.h>
    #include "allegro.h"
    
    #define WHITE makecol(255, 255, 255)
    
    int main()
    {
        BITMAP *mysprite;  //creates a pointer for a bitmap
        int x, y;
        //initialize the program
        allegro_init();
        install_keyboard();
        set_color_depth(16);
        set_window_title("Draw Sprite Demo");
        if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
        {
            allegro_message("Error setting the screen");
            exit(-1);
        }
        if (!(mysprite = load_bitmap("mysprite.bmp", NULL)))
        {
            allegro_message("Can not find the bitmap mysprite.bmp"
            " Please recreate the bitmap and save it in the same" 
            " area as this source code file");
            exit(-1);
        }
        x = SCREEN_W / 2 - mysprite->w / 2;
        y = SCREEN_H / 2 - mysprite->h / 2;
    
        //Main loop
        while (!key[KEY_ESC])
        {
            //erase the sprite
            rectfill(screen, x, y, x + mysprite->w, y + mysprite->h, 0);
    
            //move the sprite
            if (x-- < 2)
                x = SCREEN_W - mysprite->w;
            
            //draw the sprite
            draw_sprite(screen, mysprite, x, y);
     
            rest(1);
        }
        //delete the bitmap
        destroy_bitmap(mysprite);
        return 0;
    }
    END_OF_MAIN() //note there is no ; here
    Try changing your bitmap if you haven't already done so to make the background of your sprite the color (255, 0, 255) bright pink. And then rerun the program. That is how transparency works with draw_sprite();

    Sometimes you may want to scale/flip/rotate/pivoted sprites this is give you an example on how to do just that.
    [CODE=Scaled Sprites]void stretch_sprite(BITMAP *bmp, BITMAP *sprite, int x, int y, int w, int h);[/CODE]
    If your sprite is 64x64 and you want to display it as a 32x32 you would just define w = 32 and h = 32 which will shrink the sprite. You can also make that same 64x64 sprite into a 128x128 sprite. This function is really good if you want to give the appearence of zooming in and out in your program.

    There are three different kinds of flipping you can do to sprites.
    [CODE=Flipping sprites]void draw_sprite_v_flip(BITMAP *bmp, BITMAP *source, int x, int y);
    void draw_sprite_h_flip(BITMAP *bmp, BITMAP *source, int x, int y);
    void draw_sprite_vh_flip(BITMAP *bmp, BITMAP *source, int x, int y);[/CODE]
    As you probably guessed the v is for vertical the h is for horizontal.

    Rotating sprites is a little more difficult because in allegro there is not 360 degrees in a circle rather there is 256. Good for us there are functions called itofix() and ftofix() in the c language.
    [CODE=Rotating sprites]void rotate_sprite(BITMAP *bmp, BITMAP *source, int x, int y, fixed angle);[/CODE]
    Here is an example of this to give a better understanding on this function.
    Code:
    int angle;
    BITMAP mysprite;
    ...
    angle++;
    if (angle > 256)
        angle = 0;
    rotate_sprite(screen, mysprite, 320, 240, itofix(angle));
    That example will give you 256 rotation degrees before completing a full circle. Usually you will not need that many and just by increasing the angle value more will allow you to rotate faster.

    As you might have guessed you can also flip and/or stretch sprites while rotating.
    Code:
    void rotate_sprite_v_flip(BITMAP *bmp, BITMAP *sprite, int x, int y, 
                              fixed angle);
    void rotate_scaled_sprite(BITMAP *bmp, BITMAP *sprite, int x, int y, 
                              fixed angle, fixed scale);
    void rotate_scalled_sprite_v_flip(BITMAP *bmp, BITMAP *sprite, int x, int y, 
                                      fixed angle, fixed scale);
    These three are exactly like the above so I wont give an example of these. You may even want to pivot your sprite. The pivot point is the location on the image where rotation occurs. If a sprite is 64x64 pixels, then the default pivot point is at 31x31(accounting for zero). The pivot functions allow you to change the position of the pivot where rotation takes place.
    Code:
    void pivot_sprite(BITMAP *bmp, BITMAP *sprite, int x, int y, int cx, 
                            int cy, fixed angle);
    The x and y values specify where the sprite is drawn, while cx and cy specify the pivot within the sprite. If you had a 32x32 sprite cx and cy should have a value between 0 and 31. Just like rotating sprites can flip and scale so can pivoted sprites. These functions are as follows.
    Code:
    void pivot_sprite_v_flip(BITMAP *bmp, BITMAP *sprite, int x, int y, 
                             int cx, int cy, fixed angle);
    void pivot_scaled_sprite(BITMAP *bmp, BITMAP *sprite, int x, int y, 
                             int cx, int cy, fixed angle, fixed scale);
    void pivot_scaled_sprite_v_flip(BITMAP *bmp, BITMAP *sprite, int x, int y, 
                             int cx, int cy, fixed angle, fixed scale);
    Transparecy and Patterened Drawing:
    Code:
    void drawing_mode(int mode, BITMAP *pattern, int x_anchor, int y_anchor);
    Sets the graphics drawing mode. This only affects the geometric routines like putpixel, lines, rectangles, circles, polygons, floodfill, etc, not the text output, blitting, or sprite drawing functions. The mode should be one of the following constants:
    Code:
    DRAW_MODE_SOLID               - the default, solid color
                                    drawing
    DRAW_MODE_XOR                 - exclusive-or drawing
    DRAW_MODE_COPY_PATTERN        - multicolored pattern fill
    DRAW_MODE_SOLID_PATTERN       - single color pattern fill
    DRAW_MODE_MASKED_PATTERN      - masked pattern fill
    DRAW_MODE_TRANS               - translucent color blending
    This next is directly from the Allegro Manual

    In DRAW_MODE_SOLID, pixels of the bitmap being drawn onto are simply replaced by those produced by the drawing function.

    In DRAW_MODE_XOR, pixels are written to the bitmap with an exclusive-or operation rather than a simple copy, so drawing the same shape twice will erase it. Because it involves reading as well as writing the bitmap memory, xor drawing is a lot slower than the normal replace mode.

    With the patterned modes, you provide a pattern bitmap which is tiled across the surface of the shape. Allegro stores a pointer to this bitmap rather than copying it, so you must not destroy the bitmap while it is still selected as the pattern. The width and height of the pattern must be powers of two, but they can be different, eg. a 64x16 pattern is fine, but a 17x3 one is not. The pattern is tiled in a grid starting at point (x_anchor, y_anchor). Normally you should just pass zero for these values, which lets you draw several adjacent shapes and have the patterns meet up exactly along the shared edges. Zero alignment may look peculiar if you are moving a patterned shape around the screen, however, because the shape will move but the pattern alignment will not, so in some situations you may wish to alter the anchor position.

    When you select DRAW_MODE_COPY_PATTERN, pixels are simply copied from the pattern bitmap onto the destination bitmap. This allows the use of multicolored patterns, and means that the color you pass to the drawing routine is ignored. This is the fastest of the patterned modes.

    In DRAW_MODE_SOLID_PATTERN, each pixel in the pattern bitmap is compared with the mask color, which is zero in 256-color modes or bright pink for truecolor data (maximum red and blue, zero green). If the pattern pixel is solid, a pixel of the color you passed to the drawing routine is written to the destination bitmap, otherwise a zero is written. The pattern is thus treated as a monochrome bitmask, which lets you use the same pattern to draw different shapes in different colors, but prevents the use of multicolored patterns.

    DRAW_MODE_MASKED_PATTERN is almost the same as DRAW_MODE_SOLID_PATTERN, but the masked pixels are skipped rather than being written as zeros, so the background shows through the gaps.

    In DRAW_MODE_TRANS, the global color_map table or truecolor blender functions are used to overlay pixels on top of the existing image. This must only be used after you have set up the color mapping table (for 256 color modes) or blender functions (for truecolor modes). Because it involves reading as well as writing the bitmap memory, translucent drawing is very slow if you draw directly to video RAM, so wherever possible you should use a memory bitmap instead.

    This is a shortcut for toggling xor drawing mode on and off. Calling xor_mode(TRUE) is equivalent to drawing_mode(DRAW_MODE_XOR, NULL, 0, 0). Calling xor_mode(FALSE) is equivalent to drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0).
    Code:
    void xor_mode(int on);
    This is a shortcut for selecting solid drawing mode. It is equivalent to calling drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0).
    Code:
    void solid_mode();
    256-color transparency:
    In paletted video modes, translucency and lighting are implemented with a 64k lookup table, which contains the result of combining any two colors c1 and c2. You must set up this table before you use any of the translucency or lighting routines. Depending on how you construct the table, a range of different effects are possible. For example, translucency can be implemented by using a color halfway between c1 and c2 as the result of the combination. Lighting is achieved by treating one of the colors as a light level (0-255) rather than a color, and setting up the table appropriately. A range of specialised effects are possible, for instance replacing any color with any other color and making individual source or destination colors completely solid or invisible. Color mapping tables can be precalculated with the colormap utility, or generated at runtime. Read chapter "Structures and types defined by Allegro" for an internal description of the COLOR_MAP structure.

    Code:
    extern COLOR_MAP *color_map;
    Global pointer to the color mapping table. You must allocate your own COLOR_MAP either statically or dynamically and set color_map to it before using any translucent or lit drawing functions in a 256-color video mode!
    Code:
    void create_trans_table(COLOR_MAP *table, const PALETTE pal,
                                  int r, g, b, void (*callback)(int pos));
    Fills the specified color mapping table with lookup data for doing translucency effects with the specified palette. When combining the colors c1 and c2 with this table, the result will be a color somewhere between the two. The r, g, and b parameters specify the solidity of each color component, ranging from 0 (totally transparent) to 255 (totally solid). For 50% solidity, pass 128.
    This function treats source color #0 as a special case, leaving the destination unchanged whenever a zero source pixel is encountered, so that masked sprites will draw correctly. This function will take advantage of the global rgb_map variable to speed up color conversions. If the callback function is not NULL, it will be called 256 times during the calculation, allowing you to display a progress indicator.
    Example:
    Code:
    COLOR_MAP trans_table;
    ...
    /* Build a color lookup table for translucent drawing. */
    create_trans_table(&trans_table, pal, 128, 128, 128, NULL);
    Code:
    void create_light_table(COLOR_MAP *table, const PALETTE pal, 
                                  int r, g, b, void (*callback)(int pos));
    Fills the specified color mapping table with lookup data for doing lighting effects with the specified palette. When combining the colors c1 and c2 with this table, c1 is treated as a light level from 0-255. At light level 255 the table will output color c2 unchanged, at light level 0 it will output the r, g, b value you specify to this function, and at intermediate light levels it will output a color somewhere between the two extremes. The r, g, and b values are in the range 0-63.
    This function will take advantage of the global rgb_ap variable to speed up color conversions. If the callback function is not NULL, it will be called 256 times during the calculation, allowing you to display a progress indicator.
    Example:
    Code:
    COLOR_MAP light_table;
    ...
    /* Build a color lookup table for lighting effects. */
    create_light_table(&light_table, pal, 0, 0, 0, NULL);
    Code:
    void create_color_table(COLOR_MAP *table, const PALETTE pal, 
                                  void (*blend)(PALETTE pal, int x, int y, 
                                  RGB *rgb), void (*callback)(int pos));
    Fills the specified color mapping table with lookup data for doing customised effects with the specified palette, calling the blend function to determine the results of each color combination.
    Your blend routine will be passed a pointer to the palette and the two indices of the colors which are to be combined, and should fill in the RGB structure with the desired result in 0-63 format. Allegro will then search the palette for the closest match to the RGB color that you requested, so it doesn't matter if the palette has no exact match for this color.

    If the callback function is not NULL, it will be called 256 times during the calculation, allowing you to display a progress indicator.
    Example:
    Code:
    COLOR_MAP greyscale_table;
    ...
    void return_grey_color(const PALETTE pal,
                           int x, int y, RGB *rgb)
    {
        ...
    }
    ...
    /* Build a color lookup table for greyscale effect. */
    create_color_table(&greyscale_table, pal,
                       return_grey_color, NULL);
    Code:
    void create_blender_table(COLOR_MAP *table, const PALETTE pal, 
                                    void (*callback)(int pos));
    Fills the specified color mapping table with lookup data for doing a paletted equivalent of whatever truecolor blender mode is currently selected. After calling set_trans_blender(), set_blender_mode(), or any of the other truecolor blender mode routines, you can use this function to create an 8-bit mapping table that will have the same results as whatever 24-bit blending mode you have enabled.

    Truecolor transparency:
    In truecolor video modes, translucency and lighting are implemented by a blender function of the form:
    Code:
     unsigned long (*BLENDER_FUNC)(unsigned long x, y, n);
    For each pixel to be drawn, this routine is passed two color parameters x and y, decomposes them into their red, green and blue components, combines them according to some mathematical transformation involving the interpolation factor n, and then merges the result back into a single return color value, which will be used to draw the pixel onto the destination bitmap.

    The parameter x represents the blending modifier color and the parameter y represents the base color to be modified. The interpolation factor n is in the range [0-255] and controls the solidity of the blending.

    When a translucent drawing function is used, x is the color of the source, y is the color of the bitmap being drawn onto and n is the alpha level that was passed to the function that sets the blending mode (the RGB triplet that was passed to this function is not taken into account).

    When a lit sprite drawing function is used, x is the color represented by the RGB triplet that was passed to the function that sets the blending mode (the alpha level that was passed to this function is not taken into account), y is the color of the sprite and n is the alpha level that was passed to the drawing function itself.

    Since these routines may be used from various different color depths, there are three such callbacks, one for use with 15-bit 5.5.5 pixels, one for 16 bit 5.6.5 pixels, and one for 24-bit 8.8.8 pixels (this can be shared between the 24 and 32-bit code since the bit packing is the same).
    Code:
    void set_trans_blender(int r, int g, int b, int a);
    Enables a linear interpolator blender mode for combining translucent or lit truecolor pixels.
    Code:
    void set_alpha_blender();
    Enables the special alpha-channel blending mode, which is used for drawing 32-bit RGBA sprites. After calling this function, you can use draw_trans_sprite() or draw_trans_rle_sprite() to draw a 32-bit source image onto any hicolor or truecolor destination. The alpha values will be taken directly from the source graphic, so you can vary the solidity of each part of the image. You can't use any of the normal translucency functions while this mode is active, though, so you should reset to one of the normal blender modes (eg. set_trans_blender()) before drawing anything other than 32-bit RGBA sprites.
    Code:
    void set_write_alpha_blender();
    Enables the special alpha-channel editing mode, which is used for drawing alpha channels over the top of an existing 32-bit RGB sprite, to turn it into an RGBA format image. After calling this function, you can set the drawing mode to DRAW_MODE_TRANS and then write draw color values (0-255) onto a 32-bit image. This will leave the color values unchanged, but alter the alpha to whatever values you are writing. After enabling this mode you can also use draw_trans_sprite() to superimpose an 8-bit alpha mask over the top of an existing 32-bit sprite.

    You can also directly effect how your transparent sprite is displayed on the screen with one and only of of the twelve blender functions
    Code:
    //Enables an additive blender mode for combining translucent or lit 
    //truecolor pixels.
    
    void set_add_blender(int r, int g, int b, int a);
    
    //Enables a burn blender mode for combining translucent or lit 
    //truecolor pixels. Here the lightness values of the colours of the 
    //source image reduce the lightness of the destination image, darkening 
    //the image. 
    
    void set_burn_blender(int r, int g, int b, int a);
    
    //Enables a color blender mode for combining translucent or lit truecolor 
    //pixels. Applies only the hue and saturation of the source image to the 
    //destination image. The luminance of the destination image is not affected.
    
    void set_color_blender(int r, int g, int b, int a);
    
    //Enables a difference blender mode for combining translucent or lit 
    //truecolor pixels. This makes an image which has colours calculated 
    //by the difference between the source and destination colours. 
    
    void set_difference_blender(int r, int g, int b, int a);
    
    //Enables a dissolve blender mode for combining translucent or lit truecolor 
    //pixels. Randomly replaces the colours of some pixels in the destination 
    //image with those of the source image. The number of pixels replaced depends 
    //on the alpha value (higher value, more pixels replaced; you get the idea :).
     
    void set_dissolve_blender(int r, int g, int b, int a);
    
    //Enables a dodge blender mode for combining translucent or lit truecolor 
    //pixels. The lightness of colours in the source lighten the colours of the 
    //destination. White has the most effect; black has none.
    
    void set_dodge_blender(int r, int g, int b, int a);
    
    //Enables a hue blender mode for combining translucent or lit truecolor pixels. 
    //This applies the hue of the source to the destination.
    
    void set_hue_blender(int r, int g, int b, int a);
    
    //Enables an invert blender mode for combining translucent or lit truecolor 
    //pixels. Blends the inverse (or negative) colour of the source with the 
    //destination. 
    
    void set_invert_blender(int r, int g, int b, int a);
    
    //Enables a luminance blender mode for combining translucent or lit truecolor 
    //pixels. Applies the luminance of the source to the destination. The colour 
    //of the destination is not affected. 
    
    void set_luminance_blender(int r, int g, int b, int a);
    
    //Enables a multiply blender mode for combining translucent or lit truecolor 
    //pixels. Combines the source and destination images, multiplying the colours 
    //to produce a darker colour. If a colour is multiplied by white it remains unchanged; 
    //when multiplied by black it also becomes black. 
    
    void set_multiply_blender(int r, int g, int b, int a);
    
    //Enables a saturation blender mode for combining translucent or lit truecolor pixels.
    //Applies the saturation of the source to the destination image.
    
    void set_saturation_blender(int r, int g, int b, int a);
    
    //Enables a screen blender mode for combining translucent or lit truecolor pixels. 
    //This blender mode lightens the colour of the destination image by multiplying 
    //the inverse of the source and destination colours. Sort of like the opposite of 
    //the multiply blender mode. 
    
    void set_screen_blender(int r, int g, int b, int a);
    Code:
    void set_blender_mode(BLENDER_FUNC b15, b16, b24, int r, g, b, a);
    Specifies a custom set of truecolor blender routines, which can be used to implement whatever special interpolation modes you need. This function shares a single blender between the 24 and 32-bit modes.
    Code:
    void set_blender_mode_ex(BLENDER_FUNC b15, b16, b24, 
                                   b32, b15x, b16x, b24x, int r, g, b, a);
    Like set_blender_mode(), but allows you to specify a more complete set of blender routines. The b15, b16, b24, and b32 routines are used when drawing pixels onto destinations of the same format, while b15x, b16x, and b24x are used by draw_trans_sprite() and draw_trans_rle_sprite() when drawing RGBA images onto destination bitmaps of another format. These blenders will be passed a 32-bit x parameter, along with a y value of a different color depth, and must try to do something sensible in response.

    You can quickly use transparency in your program
    Example:
    Code:
    //Variables you will need
    COLOR_MAP transtable;
    PALETTE pal;
    //After you have set your screen up 
    get_palette(pal);
    create_light_table(&transtable, pal, 10, 10, 60, NULL);
    //Then you can use any one of the blenders that you might want.
    set_screen_blender(0, 0, 0, 255);
    //Now you can draw a transparent sprite
    draw_trans_sprite(BITMAP *bmp, BITMAP *sprite, int x, int y);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    ANIMATION in it's simple form.
    Code:
    BITMAP player[32]; //makes a bitmap array that can hold 32 frames
    ...
    BITMAP *grabframe(BITMAP *source, int width, int height, int startx, 
                      int starty, int columns, int frame) 
    { 
        BITMAP *temp = create_bitmap(width, height);   
        int x = startx + (frame % columns) * width;
        int y = starty + (frame / columns) * height;  
        blit(source, temp, x, y, startx, starty, width, height);
        return temp;
    } 
    ...
    temp = load_bmp("img\\player.bmp", 0);
        for (n = 0; n < 32; n++)
            player[n] = grabframe(temp, 64, 48, 0, 0, 8, n);
    This says that the sprite frame is 64x by 48y and that the start location of each sprite is 0x0 and there is 8 frames per column and 4 rows. n's max value should never be more then the amount of frames in your animation. Remember this function and how to use it cause it is the basis of animation.

    Once we have the sprite loaded using the grabframe function we are ready to show it animated on the screen
    Code:
    n = 0;
    ...
    n++;
    if (n > 31) //thats 32 frames including 0
        n = 0;
    draw_sprite(activepage, player[n], x, y);
    ..
    //draw activepage to the screen and clear activepage
    Yes it is just that easy. For this next example you will need to get a file from me or make your own bitmap of 'x' amount of frames.

    Example: NOTE you will need my star bitmap or make your own here. To use mine that I provided just save the picture below as stars.bmp.
    Code:
    #include "allegro.h"
    
    BITMAP *star[36], *temp;
    BITMAP *buffer, *buffer2, *activepage;
    int n;
    volatile start, counter, frame, framerate, resting, rested;
    
    void timer1(void)
    {
        //this keeps track of the framerate
        counter++;
        framerate = frame;
        frame = 0;
        rested = resting;
    }
    
    void dotimer()
    {
        //Locks all the variables used for the timer
        LOCK_VARIABLE(counter);
        LOCK_VARIABLE(framerate);
        LOCK_VARIABLE(ticks);
        LOCK_FUNCTION(timer1);
        install_int(timer1, 1000);
    }
    
    void rest1(void)
    {
        //while resting we are doing this.
        resting++;
    }
    
    void flip()
    {
         show_video_bitmap(activepage);
          //Page flipping :) fun stuff
         if (activepage == buffer)
             activepage = buffer2;
         else
             activepage = buffer;            
         clear_to_color(activepage, makecol(0, 0, 0));  
    }
    //This is our function for splitting up each sprite in an animation
    BITMAP *grabframe(BITMAP *source, int width, int height, int startx, 
                      int starty, int columns, int frame) 
    {
        
        BITMAP *temp = create_bitmap(width, height);   
        int x = startx + (frame % columns) * width;
        int y = starty + (frame / columns) * height;  
        blit(source, temp, x, y, startx, starty, width, height);
        return temp;
    }
    
    void main()
    {
         allegro_init();
         set_color_depth(16);
         if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
         {
             allegro_message("Can not set window");
             exit(-1);
         }
         install_keyboard();
         install_mouse();
         //installs timers so we can regulate game speed
         install_timer();
         srand(time(NULL));
         //starts the timer to regulate game speed
         dotimer();
         buffer = create_video_bitmap(SCREEN_W, SCREEN_H);
         buffer2 = create_video_bitmap(SCREEN_W, SCREEN_H);
         if (!buffer && !buffer2)
         {
             allegro_message("Can not create 2 pages in memory");
             exit(-1);          
         }
         activepage = buffer2;
         //Loads the bitmap to temp
         temp = load_bmp("stars.bmp", 0);
         for (n = 0; n < 36; n++)
             star[n] = grabframe(temp, 48, 48, 0, 0, 6, n);
         while (!key[KEY_ESC])
         {
             //control the timer to keep track of each frame
             frame++;
             //Animation begins here
             n++;
             //if the last frame is displayed return the sprite to the beginning
             if (n > 35)
                 n = 0;
             draw_sprite(activepage, star[n], SCREEN_W / 2 - (star[n]->w / 2), 
                         SCREEN_H / 2 - (star[n]->h / 2));
             //Display the frame rate.  Should stay at a steady 30 fps
             textprintf_ex(activepage, font, 0, 0, makecol(255,255,255), 0, 
                           "Press Esc to Quit FPS: %i", framerate);
             flip();
             rest_callback(17, rest1);
         }
         //release our bitmaps from memory
         for (n = 0; n > 36; n++)
             destroy_bitmap(star[n]);
         destroy_bitmap(buffer);
         destroy_bitmap(buffer2);
         //exit allegro
         allegro_exit();
         return;
    }
    END_OF_MAIN() //Note no ; here
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    Saved for using mappy and creating vertical and horizontal scrollers and tile based scrolling.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    Saved for audio handling and using datafiles with allegro.

    Comments on this post

    • Axweildr agrees : Nice contribution, and welcome
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    [CODE=putpixel demo]
    #include "allegro.h"

    void main()
    {
    int x, y;
    int red, green, blue, color;

    //initialize Allegro
    allegro_init();
    install_keyboard();
    srand(time(NULL));
    //Windowed 640x480 0x0 virtual
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    set_window_title("Pixel demo Press Esc to quit");

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x = 10 + rand() % (SCREEN_W - 20);
    y = 10 + rand() % (SCREEN_H - 20);

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red, green, blue);

    //draw the pixel
    putpixel(screen, x, y, color);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN() //Note no ; here[/CODE]
    [CODE=line demo]
    #include "allegro.h"

    void main()
    {
    int x1,y1,x2,y2;
    int red,green,blue,color;

    //initialize Allegro
    allegro_init();
    install_keyboard();
    srand(time(NULL));

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "Lines Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x1 = 10 + rand() % (SCREEN_W-20);
    y1 = 10 + rand() % (SCREEN_H-20);
    x2 = 10 + rand() % (SCREEN_W-20);
    y2 = 10 + rand() % (SCREEN_H-20);

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the pixel
    line(screen, x1,y1,x2,y2,color);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
    [CODE=rect demo]
    #include "allegro.h"

    void main()
    {
    int x1,y1,x2,y2;
    int red,green,blue,color;

    //initialize Allegro
    allegro_init();
    install_keyboard();

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "Rect Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x1 = 10 + rand() % (SCREEN_W-20);
    y1 = 10 + rand() % (SCREEN_H-20);
    x2 = 10 + rand() % (SCREEN_W-20);
    y2 = 10 + rand() % (SCREEN_H-20);

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the rect
    rect(screen,x1,y1,x2,y2,color);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
    [CODE=rectfill demo]
    #include "allegro.h"

    void main()
    {
    int x,y,x1,y1,x2,y2;
    int red,green,blue,color;

    //initialize Allegro
    allegro_init();
    install_keyboard();

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "RectFill Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x1 = 10 + rand() % (SCREEN_W-20);
    y1 = 10 + rand() % (SCREEN_H-20);
    x2 = 10 + rand() % (SCREEN_W-20);
    y2 = 10 + rand() % (SCREEN_H-20);

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the pixel
    rectfill(screen,x1,y1,x2,y2,color);
    rest(25);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
    [CODE=triangle demo]
    #include "allegro.h"

    void main()
    {
    int x1,y1,x2,y2,x3,y3;
    int red,green,blue,color;

    //initialize Allegro
    allegro_init();
    install_keyboard();
    install_timer();

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "Triangles Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x1 = 10 + rand() % (SCREEN_W-20);
    y1 = 10 + rand() % (SCREEN_H-20);
    x2 = 10 + rand() % (SCREEN_W-20);
    y2 = 10 + rand() % (SCREEN_H-20);
    x3 = 10 + rand() % (SCREEN_W-20);
    y3 = 10 + rand() % (SCREEN_H-20);

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the pixel
    triangle(screen,x1,y1,x2,y2,x3,y3,color);

    rest(100);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
    [CODE=circle demo]
    #include "allegro.h"

    void main()
    {
    int x,y,radius;
    int red,green,blue,color;

    //initialize some stuff
    allegro_init();
    install_keyboard();
    install_timer();
    srand(time(NULL));

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "Circles Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x = 30 + rand() % (SCREEN_W-60);
    y = 30 + rand() % (SCREEN_H-60);
    radius = rand() % 30;

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the pixel
    circle(screen, x, y, radius, color);

    sleep(25);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
    [CODE=circlefill]
    #include "allegro.h"

    void main()
    {
    int x,y,radius;
    int red,green,blue,color;

    //initialize some stuff
    allegro_init();
    install_keyboard();
    install_timer();
    srand(time(NULL));

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "CircleFill Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x = 30 + rand() % (SCREEN_W-60);
    y = 30 + rand() % (SCREEN_H-60);
    radius = rand() % 30;

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the pixel
    circlefill(screen, x, y, radius, color);

    sleep(25);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
    [CODE=elipse demo]
    #include "allegro.h"

    void main()
    {
    int x,y,radiusx,radiusy;
    int red,green,blue,color;

    //initialize everything
    allegro_init();
    install_keyboard();
    install_timer();
    srand(time(NULL));

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "Ellipses Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x = 30 + rand() % (SCREEN_W-60);
    y = 30 + rand() % (SCREEN_H-60);
    radiusx = rand() % 30;
    radiusy = rand() % 30;

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the ellipse
    ellipse(screen, x, y, radiusx, radiusy, color);

    sleep(25);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
    [CODE=elipsefill demo]
    #include "allegro.h"

    void main()
    {
    int x,y,radiusx,radiusy;
    int red,green,blue,color;

    //initialize everything
    allegro_init();
    install_keyboard();
    install_timer();
    srand(time(NULL));

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "EllipseFill Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    x = 30 + rand() % (SCREEN_W-60);
    y = 30 + rand() % (SCREEN_H-60);
    radiusx = rand() % 30;
    radiusy = rand() % 30;

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the ellipse
    ellipsefill(screen, x, y, radiusx, radiusy, color);

    sleep(25);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
    [CODE=polygon demo]
    #include "allegro.h"

    void main()
    {
    int vertices[8];
    int red,green,blue,color;

    //initialize everything
    allegro_init();
    install_keyboard();
    install_timer();
    srand(time(NULL));

    //initialize video mode to 640x480
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0)
    {
    allegro_message(allegro_error);
    return;
    }

    //display screen resolution
    textprintf_ex(screen, font, 0, 0, makecol(255,255,255), -1,
    "Polygons Program - %dx%d - Press ESC to quit",
    SCREEN_W, SCREEN_H);

    //wait for keypress
    while(!key[KEY_ESC])
    {
    //set a random location
    vertices[0] = 10 + rand() % (SCREEN_W-20);
    vertices[1] = 10 + rand() % (SCREEN_H-20);
    vertices[2] = vertices[0] + (rand() % 30)+50;
    vertices[3] = vertices[1] + (rand() % 30)+50;
    vertices[4] = vertices[2] + (rand() % 30)-100;
    vertices[5] = vertices[3] + (rand() % 30)+50;
    vertices[6] = vertices[4] + (rand() % 30);
    vertices[7] = vertices[5] + (rand() % 30)-100;

    //set a random color
    red = rand() % 255;
    green = rand() % 255;
    blue = rand() % 255;
    color = makecol(red,green,blue);

    //draw the pixel
    polygon(screen,4,vertices,color);

    sleep(50);
    }

    //end program
    allegro_exit();
    return;
    }

    END_OF_MAIN()[/CODE]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    Saved for "Your first game" project. Examples and tutorials on making you first game.

    This is the project I am working on now. I am actually coding this game while writing this tutorials Basics.

    The game featured will be called Sapce. I got the name from misspelling space which is the theme of the game. A space shooter.

    I wanted to give a tutorial about path finding but decided it was not a good idea for a learning project. The bad thing is sapce is full of useless functions that don't do anything realistic yet. Like a pre path finding function for updating the map variables. And smart AI for the enemies(again just started). There is just to much going into this small game it just isn't a good idea to use it as an example. I will upload a quick stop and go game that is incomplete that you will be able to finish on your own with the commands you have learned.
  14. #8
  15. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    Just FYI after about 1 month I believe, you can no longer edit your posts , if after that time you still want to edit your place holders just let me know.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    I plan on having this done within the next week. Well at least the 'Basic' tutorial. In case it does. Will do thanks.
  18. #10
  19. המבין יבין
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2001
    Location
    Haifa
    Posts
    2,085
    Rep Power
    1485
    I could have PM'ed this, 0CIRCLE0, but I'd like to put it up here where everyone could see it. Nice, nice job. I'm a DS lurker now, and not much of a poster (other than the Jokes thread), but it's the jewels like this thread that keep me lurking. Thanks.
    . . . What is Firefox?
    . . . . . . What is Linux?
    . . . . . . . . . . . What is Love?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    Glad to keep you here. Hopefully I will finish before my saved threads lock out. I think the hardest part will be making all the examples. Haven't decided if I want to post source for the scrolling tutorial or just link to a file. Only problem is I don't have a sure way to keep a file going for more then 30 days since I use savefile.com unless people download the examples at least 1 time every 30 days.

    Programming with Allegro is my favorite(well only) pass time. Figured since this forum has a specific spot for the game programming I would change it up a little bit and finally make my own tutorial.

    I am glad to say I have finished the dealing with sprites section of the tutorial in post #2 there are still a bunch of functions I passed up but they are less likely needed and I will follow up with the more advanced allegro functions I did not mention later.
  22. #12
  23. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    I think there may be some people here who would be willing to give you some free hosting space. If my site was still up I'd offer you space but alas Godaddy is horrible and they locked up my site and I can't reset my password.

    Can anyone step up?
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  24. #13
  25. המבין יבין
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2001
    Location
    Haifa
    Posts
    2,085
    Rep Power
    1485
    No problem, send me the file[s] and I'll host [it||them] at dotancohen.com. I'm PMing 0circle0 now.
    . . . What is Firefox?
    . . . . . . What is Linux?
    . . . . . . . . . . . What is Love?
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    Thank I appreciate the help. Some of this tutorial is going to get long(the examples at post #6) with out hosting the examples and linking to them directly.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    65
    Rep Power
    90
    Animation in post #3 has been updated. It gives you an idea on how to animate a sprite.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo