Thread: Can't see my error for the trees

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    15

    Can't see my error for the trees

    This is a teaching project using C and the allegro library to create a basic game console window (using the console application) and to show how to create simple structs/arrays for drawing circles and rectangles to the screen. I've written many of these yet this one seems to be crashing, especially if I increase the spawn count of the objects. I would like a second pair of eyes on this.

    I get the same issue whether I'm building on ubuntu or windows 7.

    The library I am using, Allegro 4.2.3, is available at Allegro - Download - Latest version - and the specific file I'm linking to is liballeg.a in the lib folder. For Ubuntu users you may need put under "other linking options" `allegro-config --libs`

    The code is below.

    Code:
    /** Include Header Files! */
    
    #include <stdlib.h>
    #include "allegro.h"
    
    /** Set Project Definitions */
    
    // Create some colors
    #define WHITE makecol(255,255,255)
    #define RED makecol(255,0,0)
    #define BLUE makecol(0,0,255)
    #define GREEN makecol(0,255,0)
    #define ORANGE makecol(255,165,0)
    #define YELLOW makecol(255,255,0)
    
    // define a constant for number of objects to spawn
    #define NUM 5
    
    // creating a box structure
    struct tagBOX{
        int ctrx,ctry;
        int left,top,right,bottom;
        int color;
    }mybox[NUM];
    
    // creating circle structure
    struct tagCIRCLE{
        int x,y,radius,color;
    }mycircle[NUM];
    
    // set bitmap images
    BITMAP *buffer;
    
    // initialize box function
    void setup_box()
    {
        int n;
    
        for(n=0;n<NUM;n++)
        {
            mybox[n].ctrx = 20 + rand()%SCREEN_W - 20;
            mybox[n].ctry = 50 + rand()%SCREEN_H - 20;
            mybox[n].left = 10;
            mybox[n].top = 10;
            mybox[n].right = 10;
            mybox[n].bottom = 10;
            mybox[n].color = makecol(rand()%255,rand()%255,rand()%255);
        }
    }
    
    // draw the box
    void draw_box(int n)
    {
        int x1 = mybox[n].ctrx - mybox[n].left;
        int y1 = mybox[n].ctry - mybox[n].top;
        int x2 = mybox[n].ctrx + mybox[n].right;
        int y2 = mybox[n].ctry + mybox[n].bottom;
        int col = mybox[n].color;
    
        rectfill(buffer,x1,y1,x2,y2,col);
    }
    
    // initialize circle function
    void setup_circle()
    {
        int n;
    
        for(n=0;n<NUM;n++)
        {
            mycircle[n].radius = 5 + rand()%25;
            mycircle[n].x = mycircle[n].radius + rand()%SCREEN_W - mycircle[n].radius;
            mycircle[n].y = mycircle[n].radius + rand()%SCREEN_H - mycircle[n].radius;
            mycircle[n].color = makecol(rand()%255,rand()%255,rand()%255);
        }
    }
    
    // draw the circle
    void draw_circle(int n)
    {
        int r = mycircle[n].radius;
        int x = mycircle[n].x;
        int y = mycircle[n].y;
        int col = mycircle[n].color;
    
        circle(buffer,x,y,r,col);
    }
    
    /** Our main function */
    void main(void)
    {
        int n;
    
        //initialize program
        allegro_init();
        set_color_depth(32);
        set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
        install_keyboard();
        install_mouse();
        install_timer();
        srand(time(NULL));
    
        // initialize our functions
        setup_box();
        setup_circle();
    
        //CREATE SECONDARY SCREEN BUFFER
        buffer=create_bitmap(640,480);
    
        //display title
        textprintf(buffer,font,0,1,WHITE,
                   "Welcome to Allegro, press ESC to Quit!");
    
        //draw border around screen
        /** rect(BITMAP,x1,y1,x2,y2,color); */
        rect(buffer,1,12,SCREEN_W-2,SCREEN_H-2,RED);
    
        //load the mouse cursor
        show_mouse(buffer);
    
        // main game loop
        while(!key[KEY_ESC])
        {
            // draw objects
            for(n=0;n<NUM;n++)
            {
                // draw box
                draw_box(n);
                // draw circle
                draw_circle(n);
    
                // draw box and location
                textprintf(buffer,font,10,20+n*10,mybox[n].color,
                           "Box[%d],x:%d,y:%d",n,mybox[n].ctrx,mybox[n].ctry);
    
                // draw box and location to box
                textprintf(buffer,font,mybox[n].ctrx - mybox[n].left,
                           mybox[n].ctry - mybox[n].top - 12,mybox[n].color,
                           "Box[%d],x:%d,y:%d",n,mybox[n].ctrx,mybox[n].ctry);
    
                // draw circle and location
                textprintf(buffer,font,180,20+n*10,mycircle[n].color,
                           "Cir[%d],x:%d,y:%d",n,mycircle[n].x,mycircle[n].y);
    
                // draw circle and location to circle
                textprintf(buffer,font,mycircle[n].x,mycircle[n].y,
                           mycircle[n].color,"Cir[%d],x:%d,y:%d",n,mycircle[n].x,
                           mycircle[n].y);
            }
    
            //update screen
            /** blit(BITMAP,BITMAP,x1,y1,x2,y2,image_w,image_h); */
            blit(buffer,screen,0,0,0,0,640,480);
    
            //slow the game down a bit
            rest(20);
        }
    
        //free memory before exiting
        allegro_exit();
    }
    END_OF_MAIN()

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't have allegro, and don't want to download it. That means I can't compile and test your code; the following is just from reading what you posted.

    First things first:
    • It's int main(void) and return an int at the end (0 indicates success). Read this.
    • WTH is END_OF_MAIN()?
    • Don't use magic numbers. Use constants with descriptive names. Your NUM constant does not fit this. Instead of putting a comment to describe it, call it NUM_OBJ_TO_SPAWN, then your code becomes self-documenting, and mistakes become more obvious.
    • Other constants might be BOX_WIDTH and BOX_HEIGHT (each 20 for now), CIRCLE_RADIUS_MIN which is 5 and CIRCLE_RADIUS_MAX as 30. CIRCLE_RADIUS_RANGE as (CIRCLE_RADIUS_MAX - CIRCLE_RADIUS_MIN)
    • Use those constants in your formulae: mycircle[n].radius = CIRCLE_RADIUS_MIN + rand() % CIRCLE_RADIUS_RANGE;


    Also, these lines probably don't do what you think.
    Code:
    mybox[n].ctrx = 20 + rand()%SCREEN_W - 20;
    mybox[n].ctry = 50 + rand()%SCREEN_H - 20;
    mycircle[n].x = mycircle[n].radius + rand()%SCREEN_W - mycircle[n].radius;
    mycircle[n].y = mycircle[n].radius + rand()%SCREEN_H - mycircle[n].radius;
    This line seems extra broken (mismatched numbers).
    Code:
    mybox[n].ctry = 50 + rand()%SCREEN_H - 20;
    Depending on how allegro is written, the above errors (box.ctry, circle.x and circle.y) could be the result of your crashes. Especially since increasing the number of objects increases the likelihood that your formulae using rand produce a out-of-range value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trees in C++
    By edishuman in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2012, 02:29 PM
  2. binary trees..compiler error?
    By scarlet00014 in forum C Programming
    Replies: 6
    Last Post: 11-05-2008, 01:56 PM
  3. Binary Trees MINI MAXING, probability trees
    By curlious in forum General AI Programming
    Replies: 3
    Last Post: 09-30-2005, 10:57 AM
  4. Trees
    By samtay in forum C++ Programming
    Replies: 7
    Last Post: 03-28-2004, 09:35 AM
  5. traversing binary trees or partial trees
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 09:19 PM