title: Allegro 5 drawing to buffer
body: <p>I am making a program that will draw balls bouncing around the screen. I am currently working on the part that draws the balls.</p>
  
  <p>My code consists of the following:</p>
  
  <ol>
  <li>BallEngine Class - Manage all the Allegro functions/objects    </li>
  <li>BallManager Class - Manages all of the balls</li>
  <li>Ball Class - Hold information about a ball</li>
  <li>main.cpp - Game loop, etc.</li>
  </ol>
  
  <p>BallEngine.h:</p>
  
  <pre><code>#pragma once
  #include &lt;allegro5\allegro.h&gt;
  #include &lt;allegro5\allegro_font.h&gt;
  #include &lt;allegro5\allegro_ttf.h&gt;
  #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);
  };
  </code></pre>
  
  <p>BallEngine.cpp:</p>
  
  <pre><code>#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);
      }
  }
  </code></pre>
  
  <p>BallManager.h:</p>
  
  <pre><code>#pragma once
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "Ball.h"
  #include &lt;allegro5\allegro.h&gt;
  class BallManager
  {
  private:
      std::vector&lt;Ball*&gt; 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);
  };
  </code></pre>
  
  <p>BallManager.cpp:</p>
  
  <pre><code>#include "BallManager.h"
  
  
  BallManager::BallManager(void)
  {
  }
  
  
  BallManager::~BallManager(void)
  {
  }
  //Get Functions:
  int BallManager::getBall_X(int id)
  {
      return List[id]-&gt;getPos_X();
  }
  int BallManager::getBall_Y(int id)
  {
      return List[id]-&gt;getPos_Y();
  }
  int BallManager::getBall_Velocity(int id)
  {
      return List[id]-&gt;getVelocity();
  }
  int BallManager::getBall_Angle(int id)
  {
      return List[id]-&gt;getAngle();
  }
  int BallManager::getBall_Radius(int id)
  {
      return List[id]-&gt;getRadius();
  }
  ALLEGRO_COLOR BallManager::getBall_Color(int id)
  {
      return List[id]-&gt;getColor();
  }
  
  //Set functions:
  void BallManager::setBall_X(int id, int value)
  {
      List[id]-&gt;setPos_X(value);
  }
  void BallManager::setBall_Y(int id, int value)
  {
      List[id]-&gt;setPos_Y(value);
  }
  void BallManager::setBall_Velocity(int id, int value)
  {
      List[id]-&gt;setVelocity(value);
  }
  void BallManager::setBall_Angle(int id, int value)
  {
      List[id]-&gt;setAngle(value);
  }
  void BallManager::setBall_Radius(int id, int value)
  {
      List[id]-&gt;setRadius(value);
  }
  void BallManager::setBall_Color(int id, ALLEGRO_COLOR value)
  {
      List[id]-&gt;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) &gt; List.size() || id &lt; 0)
      {
          return false;
      }
      return true;
  }
  void BallManager::drawBall(int id)
  {
      List[id]-&gt;Draw();
  }
  void BallManager::drawBalls()
  {
      int total = List.size();
      for(int index = 0; index &lt; total; index++)
      {
          List[index]-&gt;Draw();
      }
  
  }
  void updateBalls()
  {
      //TODO
  }
  </code></pre>
  
  <p>Ball.h:</p>
  
  <pre><code>#pragma once
  #include &lt;allegro5\allegro.h&gt;
  #include &lt;allegro5\allegro_primitives.h&gt;
  
  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);
  };
  </code></pre>
  
  <p>Ball.cpp:</p>
  
  <pre><code>#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);
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;allegro5\allegro.h&gt;
  #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, &amp;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;
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>The error reads:
  Debug Error!
  R6010 -abort() has been called</p>
  
  <p>I also get some information the command prompt:
  Assertion failed: addon_initialized, file allegro-git\addons\primitives\primitives.c, line 79</p>
  
  <p>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?</p>