Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9030833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:35:19+00:00 2026-06-16T07:35:19+00:00

I am making a program that will draw balls bouncing around the screen. I

  • 0

I am making a program that will draw balls bouncing around the screen. I am currently working on the part that draws the balls.

My code consists of the following:

  1. BallEngine Class – Manage all the Allegro functions/objects
  2. BallManager Class – Manages all of the balls
  3. Ball Class – Hold information about a ball
  4. main.cpp – Game loop, etc.

BallEngine.h:

#pragma once
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include "BallManager.h"


ALLEGRO_DISPLAY *display;

class BallEngine
{
private:

    bool fullScreen;
    int fps;
    bool running;

public:
    BallManager BManager;
    bool getFullScreen();
    bool getIsRunning();
    void updateFullScreen();
    void setFullScreen(bool value);
    void setIsRunning(bool value);
    int getFPS();

    //Allegro Objects
    ALLEGRO_FONT *deafault_font_12;
    ALLEGRO_EVENT_QUEUE *event_queue;
    ALLEGRO_TIMER *timer;



    //Colors
    ALLEGRO_COLOR RED;
    ALLEGRO_COLOR GREEN;
    ALLEGRO_COLOR BLUE;
    ALLEGRO_COLOR YELLOW;
    ALLEGRO_COLOR PINK;
    ALLEGRO_COLOR LIGHT_BLUE;
    ALLEGRO_COLOR WHITE;
    ALLEGRO_COLOR BLACK;

    //Debug
    bool showDebug;
    void drawBallInfo(int x, int y, int id); //Draws information about a certain ball

    BallEngine(int width, int height);
    ~BallEngine(void);
};

BallEngine.cpp:

#include "BallEngine.h"

BallEngine::BallEngine(int width, int height)
{
    running = true;
    showDebug = false;
    fps = 60;
    al_init();
    if(!al_init())
    {
        printf("Failed to initalize Allegro \n");
    }
    al_install_keyboard();
    if(!al_install_keyboard())
    {
        printf("Failed to initalize keyboard \n");
    }
    al_init_font_addon();

    al_init_ttf_addon();

    fullScreen = false;
    updateFullScreen();


    deafault_font_12 = al_load_font("arial.ttf", 12, 0);
    event_queue = al_create_event_queue();
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    timer = al_create_timer(1.0/fps);
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    display = al_create_display(width, height);


    //Define engine colors
    RED = al_map_rgb(255,0,0);
    GREEN = al_map_rgb(0,255,0);
    BLUE = al_map_rgb(0,0,255);
    YELLOW = al_map_rgb(255,255,0);
    PINK = al_map_rgb(255,0,255);
    LIGHT_BLUE = al_map_rgb(255,255,0);
    WHITE = al_map_rgb(255,255,255);
    BLACK = al_map_rgb(0,0,0);
}


BallEngine::~BallEngine(void)
{
}
bool BallEngine::getFullScreen()
{
    return fullScreen;
}
bool BallEngine::getIsRunning()
{
    return running;
}
void BallEngine::updateFullScreen()
{
    if ( fullScreen == true )   
    {
        al_set_new_display_flags(ALLEGRO_FULLSCREEN);
    }
    else
    {
        al_set_new_display_flags(ALLEGRO_WINDOWED);
    }
}
void BallEngine::setFullScreen(bool value)
{
    fullScreen = value;
}

int BallEngine::getFPS()
{
    return fps;
}

void BallEngine::drawBallInfo(int x, int y, int id)
{
    if(BManager.isBallExist(id))
    {
        al_draw_textf(deafault_font_12, RED, x, y, 0, "X: %i Y: %i Velocity: %i Angle: %i Radius: %i Color %ALLEGRO_COLOR ", BManager.getBall_X(id), BManager.getBall_Y(id), BManager.getBall_Velocity(id), BManager.getBall_Angle(id), BManager.getBall_Radius(id), BManager.getBall_Color(id));
    }
    else
    {
        printf("Failed to draw ball %i information: Ball selceted out of range \n", id);
    }
}

BallManager.h:

#pragma once
#include <iostream>
#include <vector>
#include "Ball.h"
#include <allegro5\allegro.h>
class BallManager
{
private:
    std::vector<Ball*> List;
public:
    //Get functions
    int getBall_X(int id);
    int getBall_Y(int id);
    int getBall_Velocity(int id);
    int getBall_Angle(int id);
    int getBall_Radius(int id);
    ALLEGRO_COLOR getBall_Color(int id);

    //Set Functions
    void setBall_X(int id, int value);
    void setBall_Y(int id, int value);
    void setBall_Velocity(int id, int value);
    void setBall_Angle(int id, int value);
    void setBall_Radius(int id, int value);
    void setBall_Color(int id, ALLEGRO_COLOR value);

    bool isBallExist(int id); //Returns true if a ball at index id exists. Else returns false.

    void CreateBall(int x, int y, int velocity, int angle, int radius, ALLEGRO_COLOR color);
    void DeleteBall(int id);

    void drawBall(int id);
    void drawBalls();

    void updateBalls(); //NOT YET DONE

    BallManager(void);
    ~BallManager(void);
};

BallManager.cpp:

#include "BallManager.h"


BallManager::BallManager(void)
{
}


BallManager::~BallManager(void)
{
}
//Get Functions:
int BallManager::getBall_X(int id)
{
    return List[id]->getPos_X();
}
int BallManager::getBall_Y(int id)
{
    return List[id]->getPos_Y();
}
int BallManager::getBall_Velocity(int id)
{
    return List[id]->getVelocity();
}
int BallManager::getBall_Angle(int id)
{
    return List[id]->getAngle();
}
int BallManager::getBall_Radius(int id)
{
    return List[id]->getRadius();
}
ALLEGRO_COLOR BallManager::getBall_Color(int id)
{
    return List[id]->getColor();
}

//Set functions:
void BallManager::setBall_X(int id, int value)
{
    List[id]->setPos_X(value);
}
void BallManager::setBall_Y(int id, int value)
{
    List[id]->setPos_Y(value);
}
void BallManager::setBall_Velocity(int id, int value)
{
    List[id]->setVelocity(value);
}
void BallManager::setBall_Angle(int id, int value)
{
    List[id]->setAngle(value);
}
void BallManager::setBall_Radius(int id, int value)
{
    List[id]->setRadius(value);
}
void BallManager::setBall_Color(int id, ALLEGRO_COLOR value)
{
    List[id]->setColor(value);
}


void BallManager::CreateBall(int x, int y, int velocity, int angle, int radius, ALLEGRO_COLOR color)
{
    Ball* ball = new Ball(x, y, velocity, angle, radius, color);
    List.push_back(ball);
}
void BallManager::DeleteBall(int id) 
{
    if(isBallExist(id))
    {
        delete List[id];
        List.erase(List.begin()+id);
    }
    else 
    {
        printf("Failed to delete ball %i information: Ball selceted out of range \n", id);
    }
}
bool BallManager::isBallExist(int id)
{
    if((id+1) > List.size() || id < 0)
    {
        return false;
    }
    return true;
}
void BallManager::drawBall(int id)
{
    List[id]->Draw();
}
void BallManager::drawBalls()
{
    int total = List.size();
    for(int index = 0; index < total; index++)
    {
        List[index]->Draw();
    }

}
void updateBalls()
{
    //TODO
}

Ball.h:

#pragma once
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h>

class Ball
{
private:    
    int x;
    int y;
    int velocity; //Positive is left side of screen, Negitive is right side of screen
    int angle; // Angle derived from the positive vertical
    int radius;
    ALLEGRO_COLOR color;

public:
    //Get Functions
    int getPos_X();
    int getPos_Y();
    int getVelocity();
    int getAngle();
    int getRadius();
    ALLEGRO_COLOR getColor();

    //Set Functions
    void setPos_X(int value);
    void setPos_Y(int value);
    void setVelocity(int value);
    void setAngle(int value);
    void setRadius(int value);
    void setColor(ALLEGRO_COLOR value);

    //Draws to screen
    void Draw();


    //Constructor
    Ball(int Start_X, int Start_Y, int Start_Velocity, int Start_Angle, int Start_Radius, ALLEGRO_COLOR Start_Color);

    //Desctructor
    ~Ball(void);
};

Ball.cpp:

#include "Ball.h"


Ball::Ball(int Start_X, int Start_Y, int Start_Velocity, int Start_Angle, int Start_Radius, ALLEGRO_COLOR Start_Color)
{
    x = Start_X;
    y = Start_Y;
    velocity = Start_Velocity;
    angle = Start_Angle;
    radius = Start_Radius;
    color = Start_Color;
}


Ball::~Ball(void)
{
}

//Get functions
int Ball::getPos_X()
{
    return x;
}
int Ball::getPos_Y()
{
    return y;
}
int Ball::getVelocity()
{
    return velocity;
}
int Ball::getAngle()
{
    return angle;
}
int Ball::getRadius()
{
    return radius;
}
ALLEGRO_COLOR Ball::getColor()
{
    return color;
}


//Set functions
void Ball::setPos_X(int value)
{
    x = value;
}
void Ball::setPos_Y(int value)
{
    y = value;
}
void Ball::setVelocity(int value)
{
    velocity = value;
}
void Ball::setAngle(int value)
{
    angle = value;
}
void Ball::setRadius(int value)
{
    radius = value;
}
void Ball::setColor(ALLEGRO_COLOR value)
{
    color = value;
}

void Ball::Draw()
{
    al_draw_filled_circle(x, y, radius, color);
}

main.cpp:

#include <allegro5\allegro.h>
#include "BallEngine.h"
int ScreenWidth = 620;
int ScreenHeight = 480;


int main()
{
    BallEngine Engine(ScreenWidth, ScreenHeight);

    //Test balls
    Engine.BManager.CreateBall(10, 20, 0, 0, 5, al_map_rgb(0,0,255));
    Engine.BManager.CreateBall(11, 21, 1, 1, 5, al_map_rgb(0,0,255));
    Engine.BManager.CreateBall(12, 22, 2, 2, 5, al_map_rgb(0,0,255));
    Engine.BManager.CreateBall(13, 23, 3, 3, 5, al_map_rgb(0,0,255));


    ALLEGRO_EVENT events;
    int selected = 0; //Used to show which ball is selected


    al_start_timer(Engine.timer);
    while(Engine.getIsRunning())
    {
        al_wait_for_event(Engine.event_queue, &events);
        if(events.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            //Keyboard Input
            switch(events.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                    Engine.setIsRunning(false);
                    break;
                case ALLEGRO_KEY_RIGHT:
                    Engine.showDebug = !Engine.showDebug; //Toggles the selected balls info
                    break;
                case ALLEGRO_KEY_UP:
                    selected++;
                    break;
                case ALLEGRO_KEY_DOWN:
                    selected--;
                    break;
                case ALLEGRO_KEY_DELETE:
                    Engine.BManager.DeleteBall(selected); //Deletes a certain ball
                    break;
            }
        }
        else if(events.type == ALLEGRO_EVENT_TIMER)
        {
            //Update

        }

        //Draw
        Engine.BManager.drawBalls();
        //Show debug
        if(Engine.showDebug == true)
        {
            Engine.drawBallInfo(10, 10, selected);
        }

        al_flip_display();
        al_clear_to_color(al_map_rgb(0,0,0));
    }
    return 0;
}

I am having trouble drawing the balls. In allegro 4, you would pass a buffer on which to draw to and then draw the buffer to the screen. With the code that I have above I am getting an error at the draw() function in the Ball class.

The error reads:
Debug Error!
R6010 -abort() has been called

I also get some information the command prompt:
Assertion failed: addon_initialized, file allegro-git\addons\primitives\primitives.c, line 79

I think I am getting an error because the draw function doesn’t have anywhere to draw to because the display was created in the BallEngine Class, but how do I fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:35:20+00:00Added an answer on June 16, 2026 at 7:35 am

    Assertion failed: addon_initialized, file allegro-git\addons\primitives\primitives.c, line 79

    That’s precisely the problem. You haven’t initialized the primitives addon.

    al_init_primitives_addon()

    Also, you should use forward slashes as part of paths (e.g., <allegro5/allegro.h>) because it is cross platform.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently making a program that will send an email if the date
i'm making a program/game that will update automatically. i have the update part down,
I am making a program that will run a persons C++ code from PHP.
I'm making a program that will move around quite huge sets of files, sometimes
I am thinking about making a program that will need to send input and
I am trying to learn python and am making a program that will output
I am looking into making a c# program that will read in the logcat
I'm making a program that will display a few images from a directory beside
I am making program where circle is flying and bouncing of screen borders. I
I have 2 questions. So i am making a python program that will backup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.