Thread: Dont know what im doing wrong loading data files with allegro

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    8

    Dont know what im doing wrong loading data files with allegro

    Code:
    void loadsprites(void)
    {
         temp = (BITMAP *)data[BALL_BMP].dat;
         //temp = load_bitmap("ball.bmp",NULL);
         for (n = 0;n<6;n++)
             ball[n] = grabframe(temp,17,17,0,0,1,n);
         destroy_bitmap(temp);
         background = load_bitmap("background.bmp",NULL);
    }
    ok this is what is going on...if i // the first line and un // the second line of loadsprites then it works but as is it does not.

    i have set temp as
    BITMAP *temp
    grabframe as

    Code:
    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 ) * width;
               //int y = starty + (frame ) * height;
               blit(source,temp,x,0,0,0,width,height);
               return temp;
    }
    can any one help the only thing i dont have here is the datafile but shouldnt be need if you know whats wrong. If you need the data file ill post it on my website thanks

    Code:
    -CIRCLE

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i have set temp as
    >BITMAP *temp
    >grabframe as
    Right now I'm more interested in the type hierarchy of data[BALL_BMP].dat.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > temp = (BITMAP *)data[BALL_BMP].dat;
    Random casts are never a good idea, unless you at least show that they're safe.

    > int x = startx + (frame ) * width;
    Code:
    width is 5, height is 3
    11111
    11111
    11111
    22222
    22222
    22222
    33333
    33333
    33333
    start of any frame is width*height*frame_number

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    8

    Maybe I need to elaborate

    FallingBall.h
    Code:
    #ifndef _FallingBall_H
    #define _FallingBall_H
    #include "allegro.h"
    #define MODE GFX_AUTODETECT_FULLSCREEN
    #define WIDTH 640
    #define HEIGHT 480
    #define BallSpeed 1
    #define BallSize ((WIDTH + HEIGHT) / 10) / 10
    #define MouseSpeed 2
    #define BACKGROUND_BMP                   0        /* BMP  */
    #define BALL_BMP                         1        /* BMP  */
    
    
    struct tagBall {
        int x;
        int y;
        int speed;
        int color;
        int size;
    }  Balls[100];
    struct Floor {
        int y;
    } Floors; 
    int i;
    BITMAP *ball[9];
    BITMAP *background;
    BITMAP *active_page;
    BITMAP *temp;
    DATAFILE *data;
    int speed;
    int mouseh;
    int buffer = 0;
    int gameover = 0;
    int dead[100];
    int lives = 10;
    int score = 0;
    int n;
    int mousex;
    int mousey;
    void mousehold();
    void dropball();
    void moveball();
    void movefloor();
    void checkdead();
    void getinput();
    void setupscreen();
    #endif
    FallingBall.c
    Code:
    #include <c:\dev-cpp\source\FallingBall.h>
    void drawback(){
         rectfill(active_page,0,Floors.y,SCREEN_W,SCREEN_H,makecol(255,0,0));
         textprintf(active_page,font,0,0,makecol(255,255,255),"Lives: %i Score: %i",lives,score);
    }
    void movemouse(){
        circle(active_page,mouse_x,mouse_y,BallSize / 2,makecol(255,0,0)); 
    }
    void mousehold(){
        set_mouse_range(0 + BallSize-3,0 + BallSize-3,SCREEN_W - BallSize+3,Floors.y - BallSize+3);
        if (mouse_b & 1 && mouseh < 50){
            mouseh++;
            return;
        }    
        if (mouseh >= 50){
            mousex = mouse_x;
            mousey = mouse_y;
            while(mouse_b & 1){
                textprintf(screen,font,SCREEN_W/2 - (4*19),SCREEN_H/2,makecol(255,255,255),"Dont Hold The Mouse");
            }    
            position_mouse(mousex,mousey);
        }    
        if(!mouse_b & 1){
            mouseh = 0;
            }    
    }    
    void moveball(){
        //circlefill(active_page,Balls[n].x,Balls[n].y,BallSize,makecol(0,0,255)); 
        draw_sprite(active_page,ball[i/2],Balls[n].x,Balls[n].y);
        Balls[n].y = Balls[n].y + BallSpeed;//Balls.speed;
        return;
    }
    void checkdead(){
        if (mouse_x > Balls[n].x - BallSize && mouse_x < Balls[n].x + BallSize){
            if (mouse_y > Balls[n].y - BallSize && mouse_y < Balls[n].y + BallSize){
                if (mouse_b & 1){
                    dead[n] = 1;
                    score += 10;
                }
            }
        }       
        if (Balls[n].y > Floors.y - BallSize)
        {
            Balls[n].y = 0 + BallSize;
            Balls[n].x = rand() % (SCREEN_W - (BallSize )) + BallSize;
            lives--;
        }    
        if (dead[n] != 0){
            dropball();
            dead[n] = 0;
        }    
    }
    void movefloor(){
        Floors.y = Floors.y - 5;    
    }    
    void dropball(){
        Balls[n].y = 0 + BallSize;
        Balls[n].x = rand() % SCREEN_W;
        movefloor();
        return;
    }    
    void setupscreen()
    {
        int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
        if (ret != 0) {
            allegro_message(allegro_error);
            return;
        }
    }   
    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 ) * width;
               //int y = starty + (frame ) * height;
               blit(source,temp,x,0,0,0,width,height);
               return temp;
    }             
    void loadsprites(void)
    {
         //temp = (BITMAP *)data[BALL_BMP].dat;
         temp = load_bitmap("ball.bmp",NULL);
         for (n = 0;n<6;n++)
             ball[n] = grabframe(temp,17,17,0,0,1,n);
         destroy_bitmap(temp);
         background = load_bitmap("background.bmp",NULL);
    }
    int main()
    {
        allegro_init();
        set_color_depth(16);
        install_mouse();
        install_keyboard();
        install_timer();
        srand(time(NULL));
        loadsprites();
        setupscreen();
        set_mouse_speed(MouseSpeed,MouseSpeed);
        Floors.y = SCREEN_H;
        BITMAP *buffer = create_video_bitmap(SCREEN_W,SCREEN_H);
        BITMAP *buffer2 = create_video_bitmap(SCREEN_W,SCREEN_H);
        dropball();
        active_page = buffer2;
        data = load_datafile("falling.dat");
        while(!key[KEY_ESC] && lives > 0){
            clear_to_color(active_page, makecol(0, 0, 0));
            drawback();
            draw_sprite(active_page,data[BACKGROUND_BMP].dat,0,0);
            i++;
            if (i >= 12){
               i = 0;}
            for (n = 0;n < (score/100) + 1;n++)
            {
            checkdead();
            moveball();
            movemouse();
            moveball();
            mousehold();
            }
            show_video_bitmap(active_page);
            if (active_page == buffer)
    	       active_page = buffer2;
            else
               active_page = buffer;           
        }    
        destroy_bitmap(buffer);
        destroy_bitmap(buffer2);
        textprintf(screen,font,0,0,makecol(255,255,255),"Last Score was %i Press ESC to Exit",score);
        while(!key[KEY_ESC]);
        allegro_exit();
    }
    END_OF_MAIN();
    and the Falling.dat source and all files needed can be gotten at
    www.geocities.com/jensen_305/Qbasic.html
    FallingBall In C With Allegro.

    if you dl this you will notice the Falling.dat and Ball.bmp
    ball.bmp is in the Falling.dat under BALL_BMP and #define in the header file. may be this is what will help.
    thanks
    Code:
    -CIRCLE

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    and for prelude
    ALLEGRO's way of loading BMP's from a datfile is
    actual text out from GAME PROGRAMMING ALL IN ONE pg.547
    //grab sprite and store in separate BITMAP
    sprite = (BITMAP *)data[SHIP_BMP].dat;
    draw_sprite(screen, sprite, WIDTH/2-sprite->w/2, HEIGHT/2-sprite->h/2);

    where before the main
    #define SHIP_BMP 1 /*BMP*/ is made with dat.exe with alegro file

    inside main
    DATAFILE *data;
    BITMAP *sprite;
    Last edited by circle; 12-11-2004 at 02:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Reading large complicated data files
    By dodzy in forum C Programming
    Replies: 16
    Last Post: 05-17-2006, 04:57 PM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM