Graphical output suddenly quits working with allegro based header - c++

EDIT: ANSWERED. Upon examination of the g.drawLines in my drawBackground function, I was drawing a point, not a line. It was user error.
Our school has written a header that simplifies much of allegro 5. I'm writing a program that will use that header (GameEngineV4) to simply emulate checkers, however, I'm having some difficulty getting the board to display to the screen.
Here is my code:
#include "GameEngineV4.h"
#include <iostream>
using namespace std;
void drawBackground(GameEngineV4, const int, const int);
int main()
{
const int BOARD_WIDTH = 600;
const int BOARD_MARGIN = 100;
GameEngineV4 g; // instantiate graphic object
g.initializePanel(800,800); // initialize Window
g.setBackground(g.CYAN); // color the background
drawBackground(g, BOARD_MARGIN, BOARD_WIDTH);
//* cleanup
g.displayPanel();
system("Pause");
g.displayDelay(1.0); // wait 10 seconds
g.closePanel();
return 0;
}
void drawBackground(GameEngineV4 g, const int BOARD_MARGIN, const int BOARD_WIDTH){
g.setColor(g.BLACK);
int i = 0;
int offset = (BOARD_WIDTH / 8) * i; //Variable to hold the amount a line should be shifted from original.
for (i = 0; i < 9; i++){
offset = (BOARD_WIDTH / 8) * i;
g.drawLine(BOARD_MARGIN, BOARD_MARGIN + offset, BOARD_MARGIN, BOARD_MARGIN + offset); //Horizontal lines
g.drawLine(BOARD_MARGIN + offset, BOARD_MARGIN, BOARD_MARGIN + offset, BOARD_MARGIN); //Vertical lines
}
}
The difficulty lies somewhere in the drawBackground function. I had tested the drawBackground function first by replacing the drawLine code in the for loop with g.drawLine(50, 50 + offset, 450, 450 + offset); and the code displayed just fine then. Why would changing it to calculate using constants affect my code?
Coded and compiled in Visual Studio 2013 on Windows 7.
EDIT: Of course, it would be a bit simpler if I included the header and implementation for GameEngineV4
Here's GameEngineV4.h:
#ifndef _GameEngineV4
#define _GameEngineV4
#include <string>
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h> //Our primitive header file
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
using namespace std;
class GameEngineV4
{
private:
int gSleep;
int gSpeed;
bool appRunning;
ALLEGRO_COLOR colortable[32];
ALLEGRO_FONT * fonttable[32];
int keyTable[256];
int default_pen;
int default_brush;
int default_font;
int default_BGColor;
double default_thickness;
bool keyPressed;
int pressedChar;
ALLEGRO_DISPLAY *gdisplay;
public:
GameEngineV4();
//-------------------------------------------------
// Game Engine Functions
//-------------------------------------------------
// call to return last key pressed
int gameLastkey();
// call to set game speed;
void gameSetGameSpeed(int);
void gameSetSpeed(int);
// call to set game speed;
int gameGetGameSpeed();
int gameGetSpeed();
// call to set display delay;
void gameSetDisplayDelay(int);
// start game
int gameEngineMain(void);
// call to repaint window
void gameRepaint();
// creates and fills a rectangle color is index into standard color table
void fill_rect(int xl,int yl,int xr,int yr,int penColor,int brushColor);
void fill_rect(int x1,int y1,int w,int h);
// creates and fills an ellipse
void fill_ellipse(int x1 ,int y ,int w,int h,int penColor,int brushColor);
void fill_ellipse(int x1,int y1,int w,int h);
// text out string, x, y, color code
void text_out(char *,int ,int ,int);
void text_out(char *txt, int x,int y);
void text_out(LPCSTR txt,int x,int y,int clr);
void text_out(LPCSTR txt,int x,int y);
// draw line from point1(x1,y1) to point2(x2,y2)
void draw_line(int x1,int y1,int x2,int y2); // uses default color
void draw_line(int x1,int y1,int x2,int y2,int color);
// draws ellipse
void draw_ellipse(int x1,int y1,int w,int h,int penColor,int brushColor);
void draw_ellipse(int x1,int y1,int w,int h); // uses default color
// draws rectangle
void draw_rect(int x1,int y1,int w,int h,int penColor,int brushColor);
void draw_rect(int x1,int y1,int w,int h);
// sets default color
void color(int pen,int brush);
// sets default pen color
void pen_color(int pen);
// sets default brush
void brush_color(int brush);
// register call backs
void registerGameMain(void (*fun)());
void registerGameInit(void (*fun)());
void registerGameQuit(void (*fun)());
void registerGamePaint(void (*fun)());
void registerGameKeyUp(void (*fun)(int));
void registerGameKeyDown(void (*fun)(int));
void registerGameLMouse(void (*fun)(int,int,int));
void registerGameRMouse(void (*fun)(int,int,int));
void registerGameMoveMouse(void (*fun)(int,int,int));
void playBeep1();
void playBeep2();
void playBeep(float g, float s );
void playWav(char * filename, float gain, float speed, float duration);
void playWave(string filename, float gain, float speed, float duration);
int initializeAudio(int samples);
//-----------------------------------------------
// call back functions
//-----------------------------------------------
// called during initialization
void gameInit();
// called during initialization
void gameMain();
// called at exit
void gameQuit();
// called to repaint screen
void gamePaint();
// keyboard and mouse functions
void gameKeyUp(int);
void gameKeyDown(int);
void gameLMouse(int,int,int);
void gameRMouse(int,int,int);
void gameMoveMouse(int,int,int);
// initialization methods
void initColorTable();
void initFontTable();
void initKeyTable();
void GoPaint();
// Alternate Graphics functions
int initializePanel(int width, int height);
int initializePanel(int width, int height, int color);
void closePanel();
void displayPanel();
void displayDelay(double delay);
void setBackground(int color);
void setBackground(int r, int g, int b);
void setThickness(double t);
void setPenColor(int pen);
void setColor(int pen);
void setColor(int r, int g, int b);
void setBrushColor(int brush);
void setBrushColor(int r, int g, int b);
void setFont(int font);
void setFontSize(int fontSize, int flag);
void setFont(string family, int fontSize, int flag);
void drawLine(int x1, int y1, int x2, int y2);
void drawOval(int x1,int y1,int w,int h);
void drawRect(int x1,int y1,int w,int h);
void drawString(char *txt,int x,int y);
void drawString(string txt,int x,int y);
void drawARC(int x1,int y1,int r, int w,int h,float st, float dt);
void fillOval(int x1,int y1,int w,int h);
void fillRect(int x1,int y1,int w,int h);
void drawTriangle(float x1, float y1, float x2, float y2 , float x3, float y3);
void fillTriangle(float x1, float y1, float x2, float y2 , float x3, float y3);
void fillRibbon(int x1,int y1,int x2,int y2, int w);
void drawRibbon(int x1,int y1,int x2,int y2, int w);
enum color{BLACK,BLUE,CYAN,DARK_GRAY,GRAY,GREEN,
LIGHT_GRAY,MAGENTA,ORANGE,PINK,RED,WHITE,YELLOW,
MAROON,PURPLE,LIME,OLIVE,NAVY,TEAL};
};
#endif
And here's the implementation, GameEngineV4.cpp:
//-------------------------------------------------------
//-------------------------------------------------------
// GameEngine Version 4.2 - Allegro, class Based, and
// registered callbacks
// 4.1
// 4.2 Enhanced Audio
//-------------------------------------------------------
//-------------------------------------------------------
#include <iostream>
#include <string>
#include "GameEngineV4.h"
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h> //Our primitive header file
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
using namespace std;
//-------------------------------------------------------
// Definitions
//-------------------------------------------------------
#define WINDOW_WIDTH 800 // default size of game window
#define WINDOW_HEIGHT 600
#define T2SLEEP 20 // thread 2 default sleep time
#define T1SPEED 700000 // thread 1 default delay loops
//-------------------------------------------------------
// Global Variables
//-------------------------------------------------------
ALLEGRO_SAMPLE *beep1 = NULL;
ALLEGRO_SAMPLE *beep2 = NULL;
//-------------------------------------------------------
// Function pointers
//-------------------------------------------------------
void (*pGameMain) ()=NULL;
void (*pGameInit) ()=NULL;
void (*pGameQuit) ()=NULL;
void (*pGamePaint) ()=NULL;
void (*pGameKeyUp) (int)=NULL;
void (*pGameKeyDown) (int)=NULL;
void (*pGameLMouse) (int,int,int)=NULL;
void (*pGameRMouse) (int,int,int)=NULL;
void (*pGameMoveMouse) (int,int,int)=NULL;
enum KEYS{ UP, DOWN, LEFT, RIGHT};
GameEngineV4::GameEngineV4()
{
gSleep=T2SLEEP;
gSpeed=T1SPEED;
appRunning=false;
for (int i=0;i<256;i++)
keyTable[i]=0;
default_pen=0;
default_brush=0;
default_font=0;
default_BGColor=0;
default_thickness=2.0;
gdisplay = NULL;
}
int GameEngineV4::gameEngineMain(void)
{
int width = WINDOW_WIDTH;
int height = WINDOW_HEIGHT;
bool done = false;
bool redraw = true;
int pos_x = width / 2;
int pos_y = height / 2;
int FPS = 60;
bool keys[4] = {false, false, false, false};
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
if(!al_init()) //initialize Allegro
return -1;
display = al_create_display(width, height); //create our display object
if(!display) //test display object
return -1;
al_init_primitives_addon();
al_install_keyboard();
al_install_mouse();
al_init_font_addon();
al_init_ttf_addon();
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(10);
beep1 = al_load_sample("beep1.ogg");
beep2 = al_load_sample("beep2.ogg");
initColorTable();
initFontTable();
initKeyTable();
gameInit();
//CreateThread(0,0,ThreadProc1,0,0,0);
//CreateThread(0,0,ThreadProc2,0,0,0);
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / gameGetSpeed());
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_mouse_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
keyPressed=true;
pressedChar=keyTable[(int)ev.keyboard.keycode];
gameKeyDown(keyTable[(int)ev.keyboard.keycode]);
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
keyPressed=true;
pressedChar=keyTable[(int)ev.keyboard.keycode];
gameKeyUp(keyTable[(int)ev.keyboard.keycode]);
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
else if(ev.type == ALLEGRO_EVENT_TIMER)
{
gameMain();
redraw = true;
}else if (ev.type== ALLEGRO_EVENT_MOUSE_AXES)
{
gameMoveMouse(ev.mouse.x,ev.mouse.y,1);
}else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
if (ev.mouse.button & 1)
{
gameLMouse(ev.mouse.x,ev.mouse.y,1);
}
if (ev.mouse.button & 2)
{
gameRMouse(ev.mouse.x,ev.mouse.y,1);
}
}
if(redraw && al_is_event_queue_empty(event_queue))
{
redraw = false;
gamePaint();
al_flip_display();
al_clear_to_color(colortable[default_BGColor]);
}
}
al_destroy_sample(beep1);
al_destroy_sample(beep2);
al_destroy_event_queue(event_queue);
al_destroy_timer(timer);
al_destroy_display(display); //destroy our display object
return 0;
}
/*
//-------------------------------------------------------
// Additional Game Threads
//-------------------------------------------------------
DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
gameMain1();
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
while(appRunning)
{
gameMain2();
GoPaint(NULL,NULL);
Sleep(gSleep);
}
return 0;
}
*/
//-------------------------------------------------------
// register callbacks and callbacks
//-------------------------------------------------------
void GameEngineV4:: registerGameMain(void (*fun)() )
{
pGameMain = fun ;
}
void GameEngineV4::gameMain()
{
if (pGameMain !=NULL) pGameMain();
}
void GameEngineV4:: registerGameInit(void (*fun)() )
{
pGameInit = fun ;
}
void GameEngineV4::gameInit()
{
if (pGameInit !=NULL) pGameInit();
}
void GameEngineV4:: registerGameQuit(void (*fun)() )
{
pGameQuit = fun ;
}
void GameEngineV4::gameQuit()
{
if (pGameQuit !=NULL) pGameQuit();
}
void GameEngineV4:: registerGamePaint(void (*fun)() )
{
pGamePaint = fun ;
}
void GameEngineV4::gamePaint()
{
if (pGamePaint !=NULL) pGamePaint();
}
void GameEngineV4:: registerGameKeyUp(void (*fun)(int) )
{
pGameKeyUp = fun ;
}
void GameEngineV4::gameKeyUp(int x)
{
if (pGameKeyUp !=NULL) pGameKeyUp(x);
}
void GameEngineV4:: registerGameKeyDown(void (*fun)(int) )
{
pGameKeyDown = fun ;
}
void GameEngineV4::gameKeyDown(int x)
{
if (pGameKeyDown !=NULL) pGameKeyDown(x);
}
void GameEngineV4:: registerGameLMouse(void (*fun)(int,int,int) )
{
pGameLMouse = fun ;
}
void GameEngineV4::gameLMouse(int x, int y, int z)
{
if (pGameLMouse !=NULL) pGameLMouse(x, y, z);
}
void GameEngineV4:: registerGameRMouse(void (*fun)(int,int,int) )
{
pGameRMouse = fun ;
}
void GameEngineV4::gameRMouse(int x, int y, int z)
{
if (pGameRMouse !=NULL) pGameRMouse(x, y, z);
}
void GameEngineV4:: registerGameMoveMouse(void (*fun)(int,int,int) )
{
pGameMoveMouse = fun ;
}
void GameEngineV4::gameMoveMouse(int x, int y, int z)
{
if (pGameMoveMouse !=NULL) pGameMoveMouse(x, y, z);
}
//-------------------------------------------------------
// Pens and Brushes
//-------------------------------------------------------
void GameEngineV4::initColorTable()
{
colortable[0]=al_map_rgb(0,0,0); // black
colortable[1]=al_map_rgb(0,0,255); // blue
colortable[2]=al_map_rgb(0,255,255); // cyan /aqua
colortable[3]=al_map_rgb(96,96,96); // dark gray
colortable[4]=al_map_rgb(160,160,160); // gray
colortable[5]=al_map_rgb(0,160,0); // green
colortable[6]=al_map_rgb(192,192,192); // light gray /silver
colortable[7]=al_map_rgb(255,0,255); // magenta
colortable[8]=al_map_rgb(255,165,0); // orange
colortable[9]=al_map_rgb(255,160,203); // pink
colortable[10]=al_map_rgb(255,0,0); // red
colortable[11]=al_map_rgb(255,255,255); // white
colortable[12]=al_map_rgb(255,255,0); // yellow
colortable[13]=al_map_rgb(160,0,0); // maroon
colortable[14]=al_map_rgb(160,0,160); // purplE
colortable[15]=al_map_rgb(0,255,0); // lime
colortable[16]=al_map_rgb(160,160,0); // olive
colortable[17]=al_map_rgb(0,0,160); // navy
colortable[18]=al_map_rgb(0,160,160); // teal
}
void GameEngineV4::initFontTable()
{
fonttable[0] = al_load_font("arial.ttf", 16, 1);
fonttable[1] = al_load_font("arial.ttf", 32, 2);
fonttable[2] = al_load_font("arial.ttf", 48, 2);
}
void GameEngineV4::initKeyTable()
{
for (int i=0; i< 256;i++)
keyTable[i]=0; // zero
keyTable[84]=0x26; // keyup
keyTable[85]=0x28; // key down
keyTable[82]=0x25; // left
keyTable[83]=0x27; // right
for (int i=0; i< 26;i++)
keyTable[i+1]=i+0x41; // a-z
for (int i=0; i< 9;i++)
keyTable[i+27]=i+0x30; // 0-9
for (int i=0; i< 9;i++)
keyTable[i+37]=i+0x60; // 0-9 key pad
for (int i=0; i< 12;i++)
keyTable[i+47]=i+0x70; // F1-F12
keyTable[59]=0x1b; // escape
keyTable[61]=0x6d; // minus
keyTable[63]=0x08; // backspace
keyTable[64]=0x09; // tab
keyTable[75]=0x20; // space
keyTable[67]=0x0d; // return or enter
}
//-------------------------------------------------------
// Text Procssing
//-------------------------------------------------------
void GameEngineV4::text_out(char *txt,int x,int y,int clr)
{
al_draw_text(fonttable[default_font], colortable[clr], x, y, 0, txt);
}
void GameEngineV4::text_out(char *txt,int x,int y)
{
al_draw_text(fonttable[default_font], colortable[default_pen], x, y, 0, txt);
}
//-------------------------------------------------------
// Functions to Draw Lines and Shapes
//-------------------------------------------------------
void GameEngineV4::draw_line(int x1,int y1,int x2,int y2)
{
al_draw_line(x1, y1, x2, y2, colortable[default_pen],default_thickness);
}
void GameEngineV4::draw_line(int x1,int y1,int x2,int y2,int clr)
{
al_draw_line(x1, y1, x2, y2, colortable[clr] ,default_thickness);
}
void GameEngineV4::draw_ellipse(int x1,int y1,int w,int h,int penClr,int brushClr)
{
int w1=w/2;
int h1=h/2;
al_draw_ellipse(x1+w1, y1+h1, w1, h1, colortable[penClr], default_thickness);
}
void GameEngineV4::draw_rect(int x1,int y1,int w,int h)
{
al_draw_rectangle(x1,y1,x1+w,y1+h,colortable[default_pen],default_thickness);
}
void GameEngineV4::draw_rect(int x1,int y1,int w,int h,int penClr,int brushClr)
{
al_draw_rectangle(x1,y1,x1+w,y1+h,colortable[penClr],default_thickness);
}
void GameEngineV4::draw_ellipse(int x1,int y1,int w,int h)
{
int w1=w/2;
int h1=h/2;
al_draw_ellipse(x1, y1, w1, h1, colortable[default_pen], default_thickness);
}
void GameEngineV4::fill_ellipse(int x1,int y1,int w,int h,int penClr,int brushClr)
{
int w1=w/2;
int h1=h/2;
al_draw_filled_ellipse (x1+w1,y1+h1,w1,h1,colortable[penClr]);
}
void GameEngineV4::fill_ellipse(int x1,int y1,int w,int h)
{
int w1=w/2;
int h1=h/2;
al_draw_filled_ellipse (x1+w1,y1+h1,w1,h1,colortable[default_brush]);
}
void GameEngineV4::fill_rect(int x1,int y1,int w,int h)
{
al_draw_filled_rectangle(x1,y1,x1+w,y1+h,colortable[default_brush]);
}
void GameEngineV4::fill_rect(int x1,int y1,int w,int h,int penClr,int brushClr)
{
al_draw_filled_rectangle(x1,y1,x1+w,y1+h,colortable[penClr]);
}
void GameEngineV4::color(int pen,int brush)
{
default_pen=pen;
default_brush=brush;
}
void GameEngineV4::pen_color(int pen)
{
default_pen=pen;
}
void GameEngineV4::brush_color(int brush)
{
default_brush=brush;
}
int GameEngineV4::gameLastkey()
{
if(!keyPressed)
{
return 0;
}
else
{
keyPressed=false;
return pressedChar;
}
}
void GameEngineV4::gameSetSpeed(int x)
{
gSpeed=x;
}
void GameEngineV4::gameSetGameSpeed(int x)
{
gSpeed=x;
}
void GameEngineV4::gameSetDisplayDelay(int x)
{
gSleep=x;
}
int GameEngineV4::gameGetSpeed()
{
return gSpeed;
}
int GameEngineV4::gameGetGameSpeed()
{
return gSpeed;
}
// graphics interface
int GameEngineV4::initializePanel(int width, int height)
{
if(!al_init()) //initialize Allegro
return -1;
gdisplay = al_create_display(width, height); //create our display object
if(!gdisplay) //test display object
return -1;
al_init_primitives_addon();
al_install_keyboard();
al_install_mouse();
al_init_font_addon();
al_init_ttf_addon();
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(100);
initColorTable();
initFontTable();
initKeyTable();
return 0;
}
void GameEngineV4::closePanel()
{
al_destroy_display(gdisplay); //destroy our display object
return;
}
void GameEngineV4::displayPanel()
{
al_flip_display();
}
void GameEngineV4::setBackground(int color)
{
default_BGColor =color;
al_clear_to_color(colortable[default_BGColor]);
}
void GameEngineV4::setBackground(int r, int g, int b)
{
colortable[30] =al_map_rgb(r,g,b);
default_BGColor =30;
al_clear_to_color(colortable[default_BGColor]);
}
void GameEngineV4::setThickness(double t)
{
default_thickness=t;
}
void GameEngineV4::setPenColor(int pen)
{
default_pen=pen;
}
void GameEngineV4::setColor(int clr)
{
default_pen=clr;
default_brush=clr;
}
void GameEngineV4::setColor(int r, int g, int b)
{
colortable[31] =al_map_rgb(r,g,b);
default_pen=31;
}
void GameEngineV4::setBrushColor(int brush)
{
default_brush=brush;
}
void GameEngineV4::setBrushColor(int r, int g, int b)
{
colortable[30] =al_map_rgb(r,g,b);
default_brush=30;
}
void GameEngineV4::setFont(int font)
{
default_font=font;
}
void GameEngineV4::setFontSize(int fontSize, int flag)
{
fonttable[31] = al_load_font("arial.ttf", fontSize, flag);
default_font=31;
}
void GameEngineV4::setFont(string family, int fontSize, int flag)
{
fonttable[31] = al_load_font(family.c_str(), fontSize, flag);
default_font=31;
}
void GameEngineV4::drawLine(int x1, int y1, int x2, int y2)
{
al_draw_line(x1, y1, x2, y2, colortable[default_pen] ,default_thickness);
}
void GameEngineV4::drawOval(int x1,int y1,int w,int h)
{
int w1=w/2;
int h1=h/2;
al_draw_ellipse(x1+w1, y1+h1, w1, h1, colortable[default_pen], default_thickness);
}
void GameEngineV4::drawRect(int x1,int y1,int w,int h)
{
al_draw_rectangle(x1,y1,x1+w,y1+h,colortable[default_pen],default_thickness);
}
void GameEngineV4::drawString(char *txt,int x,int y)
{
text_out(txt,x,y);
}
void GameEngineV4::drawString(string txt,int x,int y)
{
al_draw_text(fonttable[default_font], colortable[default_pen], x, y, 0, txt.c_str());
}
void GameEngineV4::fillOval(int x1,int y1,int w,int h)
{
int w1=w/2;
int h1=h/2;
al_draw_filled_ellipse (x1+w1,y1+h1,w1,h1,colortable[default_brush]);
}
void GameEngineV4::fillRect(int x1,int y1,int w,int h)
{
al_draw_filled_rectangle(x1,y1,x1+w,y1+h,colortable[default_brush]);
}
void GameEngineV4::displayDelay(double d)
{
al_rest(d);
}
void GameEngineV4::drawARC(int x1,int y1, int r, int w,int h,float st, float dt)
{
int w1=w/2;
int h1=h/2;
float dt1=3.14 * (dt*-1)/180.0;
float st1=3.14 * (st*-1)/180.0;
al_draw_arc(x1+w1,y1+h1,r,st1,dt1,colortable[default_brush],default_thickness);
}
void GameEngineV4::playBeep1()
{
al_play_sample(beep1, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0);
}
void GameEngineV4::playBeep2()
{
al_play_sample(beep2, 1, 0, 1 , ALLEGRO_PLAYMODE_ONCE, 0);
}
void GameEngineV4::playBeep(float g, float s )
{
al_play_sample(beep1, g, 0, s, ALLEGRO_PLAYMODE_ONCE, 0);
}
void GameEngineV4::playWav(char filename[], float gain, float speed, float duration )
{
ALLEGRO_SAMPLE *soundeffect = al_load_sample(filename);
al_play_sample(soundeffect,gain,0.0, speed, ALLEGRO_PLAYMODE_ONCE, 0);
al_rest(duration);
al_destroy_sample(soundeffect);
}
void GameEngineV4::playWave(string filename, float gain, float speed, float duration )
{
ALLEGRO_SAMPLE *soundeffect = al_load_sample(filename.c_str());
al_play_sample(soundeffect,gain,0.0, speed, ALLEGRO_PLAYMODE_ONCE, 0);
al_rest(duration);
al_destroy_sample(soundeffect);
}
// audio interface
int GameEngineV4::initializeAudio(int samples)
{
if(!al_init()) //initialize Allegro
return -1;
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(samples);
return 0;
}
void GameEngineV4::drawTriangle(float x1, float y1, float x2, float y2 , float x3, float y3)
{
al_draw_triangle(x1,y1,x2,y2,x3,y3,colortable[default_pen],default_thickness);
}
void GameEngineV4::fillTriangle(float x1, float y1, float x2, float y2 , float x3, float y3)
{
al_draw_filled_triangle(x1,y1,x2,y2,x3,y3,colortable[default_brush]);
}
void GameEngineV4::fillRibbon(int x1,int y1,int x2,int y2, int w)
{
struct MyPoint
{
float x, y;
};
const size_t num_points1 = 2;
MyPoint points1[] =
{
{x1, y1},
{x2, y2}
};
al_draw_ribbon((float*)points1, sizeof(MyPoint), colortable[default_brush], w, num_points1);
}
void GameEngineV4::drawRibbon(int x1,int y1,int x2,int y2, int w)
{
fillRibbon(x1,y1,x2,y2, w);
}
EDIT: ANSWERED. See top for more info.

In my drawBackground function, I am supposed to provide the starting point of the line and the ending point of the line. I provided the same point for both the start and end point. End result, nothing displayed.

Related

How to create a layout of bricks for Breakout game using a data/text file in C++ SFML?

I am creating a classic breakout game in C++ using SFML library. So far I have a movable paddle and ball implemented.
Currently, my goal is to create a layout of bricks. So far I have it so that a single brick is displayed. However, I want to make it so that I can draw a layout of bricks in text file using a letter (For example using letter 'B'), read that text file's brick layout and draw that layout in game.
I am not sure how to do this so any help would be great!
Here is my code so far (Note that I did not include for Paddle and Ball as I thought it was not needed for this problem):
GameObject.h
#pragma once
#include <SFML/Graphics.hpp>
class GameObject
{
protected:
sf::Vector2f position;
float speed;
sf::RenderWindow& m_window;
public:
GameObject(float startX, float startY, sf::RenderWindow& window);
virtual ~GameObject() {};
virtual void Draw() = 0;
virtual void Update() = 0;
};
GameObject.cpp
#include "GameObject.h"
GameObject::GameObject(float startX, float startY, sf::RenderWindow& window)
: position{ startX, startY }
, speed{ 0.5f }
, m_window{ window }
{
}
Brick.h
#pragma once
#include "GameObject.h"
class Brick : public GameObject
{
private:
sf::RectangleShape brickShape;
static constexpr int shapeWidth = 50;
static constexpr int shapeHeight = 20;
int color;
int strength;
public:
Brick(float startX, float startY, sf::RenderWindow& window);
sf::FloatRect getPosition();
sf::RectangleShape getShape();
int getStrength();
void setStrength(int strengthValue);
void Draw() override;
void Update() override;
};
Brick.cpp
#include "Brick.h"
Brick::Brick(float startX, float startY, sf::RenderWindow& window)
: GameObject{ startX, startY, window }
{
brickShape.setSize(sf::Vector2f(shapeWidth, shapeHeight));
brickShape.setPosition(position);
color = (rand() % 2) + 1;
if (color == 1)
{
brickShape.setFillColor(sf::Color::Yellow);
strength = 1;
}
else
{
brickShape.setFillColor(sf::Color(255, 165, 0));
strength = 2;
}
}
sf::FloatRect Brick::getPosition()
{
return brickShape.getGlobalBounds();
}
sf::RectangleShape Brick::getShape()
{
return brickShape;
}
int Brick::getStrength()
{
return strength;
}
void Brick::setStrength(int strengthValue)
{
strength = strengthValue;
}
void Brick::Draw()
{
m_window.draw(brickShape);
}
void Brick::Update()
{
}
Game.h
#pragma once
#include <SFML/Graphics.hpp>
#include "Paddle.h"
#include "Ball.h"
#include "Brick.h"
#include <algorithm>
#include <fstream>
#include <iostream>
class Game
{
private:
sf::RenderWindow& m_window;
std::unique_ptr<Paddle> player;
std::unique_ptr<Ball> ball;
std::unique_ptr<Brick> brick;
std::vector<std::unique_ptr<Brick>>bricks;
int bricksCountX;
int bricksCountY;
public:
const unsigned int m_windowWidth;
const unsigned int m_windowHeight;
public:
Game(sf::RenderWindow& window, const unsigned int& windowWidth, const unsigned int& windowHeight);
float RandomFloat(float a, float b);
void ReadFile();
void HandleCollision();
void HandleInput();
void Draw();
void Update();
void Run();
};
Game.cpp
#include "Game.h"
Game::Game(sf::RenderWindow& window, const unsigned int& windowWidth, const unsigned int& windowHeight)
: m_window{ window }
, m_windowWidth{ windowWidth }
, m_windowHeight{ windowHeight }
{
player = std::make_unique<Paddle>(m_windowWidth/2, m_windowHeight - 70, m_window);
ball = std::make_unique<Ball>(m_windowWidth / 2, m_windowHeight / 2, m_window);
ReadFile();
for (int i = 0; i < bricksCountX; i++)
for (int j = 0; j < bricksCountY; j++)
bricks.emplace_back(std::make_unique<Brick>((i + 1.5) * ((long long)brick->getShape().getSize().x + 3) + 22,
(j + 5) * (brick->getShape().getSize().y + 3), m_window));
}
float Game::RandomFloat(float a, float b)
{
return ((b - a) * ((float)rand() / RAND_MAX)) + a;
}
void Game::ReadFile()
{
// Create a text string, which is used to output the text file
std::string bricksText;
int numOfLines = 0;
// Read from the text file
std::ifstream MyReadFile("Bricks Layout.txt");
// Use a while loop together with the getline() function to read the file line by line
while (std::getline(MyReadFile, bricksText))
{
++numOfLines;
// Output the text from the file
bricksCountX = bricksText.length();
std::cout << bricksCountX;
}
bricksCountY = numOfLines;
// Close the file
MyReadFile.close();
}
void Game::HandleCollision()
{
if (ball->getShape().getPosition().x - ball->getRadius() < 0.0f)
{
ball->reboundLeft();
}
else if (ball->getShape().getPosition().x + ball->getRadius() > m_windowWidth)
{
ball->reboundRight();
}
else if (ball->getShape().getPosition().y - ball->getRadius() < 0.0f)
{
ball->reboundTop();
}
else if (ball->getShape().getPosition().y + ball->getRadius() > m_windowHeight)
{
ball->ballAngle = ball->ballAngle * -1;
}
else if (ball->getPosition().intersects(player->getPosition()))
{
ball->reboundPaddle(*player);
}
for (unsigned int i = 0; i < bricks.size(); i++)
{
if (ball->getPosition().intersects(bricks[i]->getPosition()))
{
if (bricks[i]->getStrength() == 1)
{
ball->reboundBrick(*bricks[i]);
bricks.erase(std::remove(bricks.begin(), bricks.end(), bricks[i]), bricks.end());
}
else
{
ball->reboundBrick(*bricks[i]);
bricks[i]->setStrength(1);
}
}
}
}
void Game::HandleInput()
{
player->HandleInput();
}
void Game::Draw()
{
player->Draw();
ball->Draw();
for (unsigned int i = 0; i < bricks.size(); i++)
{
bricks[i]->Draw();
}
}
void Game::Update()
{
player->Update();
ball->Update();
brick->Update();
}
void Game::Run()
{
//Game Loop
while (m_window.isOpen())
{
sf::Event event;
while (m_window.pollEvent(event))
{
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
m_window.close();
}
m_window.clear();
Draw();
HandleInput();
HandleCollision();
Update();
m_window.display();
}
}

SFML sprite is a white square when making multiple textures

I am making a SFML framework, and when I use the function loadImage one time, the image loads correctly with all colors, but if I use it two times for another texture, there is only one sprite rendered and it's all white. I read that you don't want to delete the texture or the sprite or it will be white. But in this code I'm storing all the textures in a vector. Does any one know what is wrong in this function?
FM::Image FM::graphics::loadImage(const char* fileName) {
texturesindex++;
sf::Texture texture;
texture.loadFromFile(fileName);
textures.push_back(texture);
sf::Sprite sprite(textures[texturesindex]);
Image image;
image.sprite = sprite;
return image;
}
Here's all the code:
SFFM.cpp:
#include "SFFM.h"
#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
int backgroundcolor[3] = { 0,0,0};
sf::RenderWindow Window(sf::VideoMode(800, 600), "MyGame");
std::vector<sf::Texture> textures;
int texturesindex = -1;
void FM::window::setWindowOptions(unsigned int Width, unsigned int Height, const char* Title, int FrameLimit) {
Window.setSize(sf::Vector2u(Width, Height));
Window.setFramerateLimit(FrameLimit);
Window.setTitle(Title);
}
void FM::window::setBackgroundColor(int r, int g, int b) {
backgroundcolor[0] = r;
backgroundcolor[1] = g;
backgroundcolor[2] = b;
}
FM::Image FM::graphics::loadImage(const char* fileName) {
texturesindex++;
sf::Texture texture;
texture.loadFromFile(fileName);
textures.push_back(texture);
sf::Sprite sprite(textures[texturesindex]);
Image image;
image.sprite = sprite;
return image;
}
void FM::graphics::drawImage(Image image, int x, int y, int scalex, int scaley, int rotation) {
image.sprite.setPosition(x, -y);
image.sprite.setRotation(rotation);
image.sprite.setScale(sf::Vector2f(scalex, scaley));
Window.draw(image.sprite);
}
void FM::graphics::drawImage(Image image, int x, int y, int scalex, int scaley) {
image.sprite.setPosition(x, -y);
image.sprite.setScale(sf::Vector2f(scalex, scaley));
Window.draw(image.sprite);
}
void FM::graphics::drawImage(Image image, int x, int y) {
image.sprite.setPosition(x, -y);
Window.draw(image.sprite);
}
int main()
{
FM::Start();
while (Window.isOpen())
{
sf::Event event;
while (Window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
Window.close();
else if (event.type == sf::Event::Resized)
{
Window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
}
}
Window.clear(sf::Color(backgroundcolor[0], backgroundcolor[1], backgroundcolor[2]));
FM::Update();
Window.display();
}
return 0;
}
SFFM.h:
#pragma once
#include <SFML/Graphics.hpp>
namespace FM
{
void Update();
void Start();
class window {
public:
static void setWindowOptions(unsigned int Width, unsigned int Height, const char * Title, int FrameLimit);
static void setBackgroundColor(int r, int g, int b);
};
class Image {
public:
sf::Sprite sprite;
};
class graphics {
public:
static Image loadImage(const char* fileName);
static void drawImage(Image image, int x, int y, int scalex, int scaley, int rotation);
static void drawImage(Image image, int x, int y, int scalex, int scaley);
static void drawImage(Image image, int x, int y);
};
class Input {
public:
};
};
main.cpp:
#include "SFFM.h"
#include <SFML\Graphics.hpp>
FM::Image Gangster;
FM::Image Block;
int x = 0;
int y = 0;
void FM::Start() {
window::setWindowOptions(1280, 720, "Platformer", 120);
window::setBackgroundColor(0, 127, 255);
Gangster = graphics::loadImage("assets/Gangster.png");
}
void FM::Update() {
graphics::drawImage(Gangster, x, y, 5, 5);
graphics::drawImage(Block, 100, 100, 5, 5);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
y += 1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
y -= 1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
x += 1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
x -= 1;
}
}
Here's a screenshot:
TL;DR Solutions, either:
Use a container other than std::vector<sf::Texture> - std::list<sf::Texture> or std::forward_list<sf::Texture>.
Store pointers to textures - std::vector<std::unique_ptr<sf::Texture>>.
Explanation
You store texture objects along with sprites referring them. This is correct because a spite just keeps a reference (a pointer) to its texture. This also implies that the texture address must remain unchanged during the sprite lifetime.
It looks like you did not take into account how std::vector push_back() works. A vector initally allocates some memory, and once there is no place to insert a new item, the vector allocates more memory and copies all inserted items and the new one there. So all the item addresses change.
In your case an item is a texture, its address is changed upon an isertion of another texture, so some sprite "looses" its texture. This usually results in white squares drawn.

undefined reference inheritance [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I created base Sprite class:
#ifndef SPRITE_H_
#define SPRITE_H_
#include<iostream>
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
class Sprite
{
public:
Sprite(SDL_Texture* texture, int x, int y, int w, int h, SDL_Renderer* p_renderer);
virtual ~Sprite();
SDL_Rect getRect();
SDL_Texture* loadImage(std::string file_path);
void drawSprite();
void setPosition(int x, int y);
float getXpos();
float getYpos();
private:
SDL_Renderer* gRenderer;
SDL_Texture* image;
SDL_Rect rect;
//for position update
float x_pos;
float y_pos;
};
#endif /* SPRITE_H_ */
#include "Sprite.h"
Sprite::Sprite(SDL_Texture* texture, int x, int y, int w, int h, SDL_Renderer* p_renderer)
{
gRenderer = p_renderer;
image = texture;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
x_pos = x;
y_pos = y;
}
Sprite::~Sprite()
{
SDL_DestroyTexture(image);
}
float Sprite::getXpos()
{
return x_pos;
}
float Sprite::getYpos()
{
return y_pos;
}
SDL_Texture* loadImage(std::string file_path, SDL_Renderer* p_renderer)
{
return IMG_LoadTexture(p_renderer, file_path.c_str());
}
void Sprite::drawSprite()
{
SDL_RenderCopy(gRenderer, image, NULL, &rect);
}
Next I created Bird class that is using Sprite class as a base:
/*
* Bird.h
*
* Created on: 1 maj 2016
* Author: Astral
*/
#ifndef BIRD_H_
#define BIRD_H_
#include "Sprite.h"
class Bird: public Sprite
{
public:
Bird(SDL_Texture* texture, int x, int y, int w, int h, SDL_Renderer* p_renderer, float p_speed, float p_acceleration);
virtual ~Bird();
void updateBird(int x, int y);
void handleInput();
private:
float speed, acceleration;
};
#endif /* BIRD_H_ */
#include "Bird.h"
Bird::Bird(SDL_Texture* texture, int x, int y, int w, int h, SDL_Renderer* p_renderer, float p_speed, float p_acceleration):Sprite(texture, x, y, w, h, p_renderer)
{
speed = p_speed;
acceleration = p_acceleration;
}
Bird::~Bird()
{
}
void Bird::updateBird(int x, int y)
{
Sprite::setPosition(x, y);
}
Now I'm getting error and I have no idea why:
../src/Bird.cpp:18: undefined reference to `Sprite::setPosition(int, int)'
Sprite.cpp
1.
void Sprite::setPosition(int x, int y)
{
x_pos = x;
y_pos = y;
}
2.
SDL_Texture* loadImage(std::string file_path, SDL_Renderer* p_renderer)
 ↓
SDL_Texture* Sprite::loadImage(std::string file_path)

Cannot place Collision Detection debug rectangle over game sprites

I am following along Let's Make An RPG (C++/SDL2) - Tutorials on Youtube and am stuck on Let's Make an RPG (C++/SDL2) - Part 35 Continuation on Collision (https://www.youtube.com/watch?v=DLu6g2S3Ta0&list=PLHM_A02NtaaVey-4Ezh7p6bbOsv-DKA-0).
I am trying to place a transparent rectangle over all sprites to debug collision detection. I am not getting any errors so there is no one piece of code I can show you.
Can someone please shed some light on where I went wrong. I'm very new to C++ but I'm not giving up. Thank you for your time. I'm genuinely grateful.
This is the code for both the header and cpp file of the Collision Rectangle
#include "CollisionRectangle.h"
CollisionRectangle::CollisionRectangle()
{
OffsetX = 0;
OffsetY = 0;
SetRectangle(0,0,0,0);
}
CollisionRectangle::CollisionRectangle(int x, int y, int w, int h)
{
OffsetX = x;
OffsetY = y;
SetRectangle(0,0,w,h);
}
CollisionRectangle::~CollisionRectangle()
{
//dtor
}
void CollisionRectangle::SetRectangle(int x, int y, int w, int h)
{
CollisionRect.x = x + OffsetY;
CollisionRect.y = y + OffsetY;
CollisionRect.w = w;
CollisionRect.h = h;
}
//header
#pragma once
#include "stdafx.h"
class CollisionRectangle
{
public:
CollisionRectangle();
CollisionRectangle(int x, int y, int w, int h);
~CollisionRectangle(void);
void SetRectangle(int x, int y, int w, int h);
SDL_Rect GetRectangle(){ return CollisionRect; }
void setX(int x){ CollisionRect.x = x + OffsetX; }
void setY(int y){ CollisionRect.y = y + OffsetY; }
private:
int OffsetX;
int OffsetY;
SDL_Rect CollisionRect;
};
This is the code for the both the header and the cpp file of Sprites
#include "Sprite.h"
Sprite::Sprite(SDL_Renderer* passed_renderer, std::string FilePath, int x, int y, int w, int h, float *passed_CameraX, float *passed_CameraY, CollisionRectangle passed_CollisionRect)
{
CollisionRect = passed_CollisionRect;
renderer = passed_renderer;
CollisionSDL_Rect = CollisionRect.GetRectangle();
image = NULL;
image = IMG_LoadTexture(renderer, FilePath.c_str());
if (image == NULL)
{
std::cout << "Couldn't load " << FilePath.c_str() << std::endl;
}
CollisionImage = NULL;
CollisionImage = IMG_LoadTexture(renderer, "Data/DebugImages/DebugBox.png");
if (CollisionImage == NULL)
{
std::cout << "Couldn't load " << "CollisionImage" << std::endl;
}
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_QueryTexture(image,NULL,NULL, &img_width, &img_height);
crop.x = 0;
crop.y = 0;
crop.w = img_width;
crop.h = img_height;
X_pos = x;
Y_pos = y;
Origin_X = 0;
Origin_Y = 0;
CurrentFrame = 0;
Amount_Frame_X = 0;
Amount_Frame_Y = 0;
CameraX = passed_CameraX;
CameraY = passed_CameraY;
Camera.x = rect.x + *CameraX;
Camera.y = rect.y + *CameraY;
Camera.w = rect.w;
Camera.h = rect.h;
}
void Sprite::DrawSteady()
{
SDL_RenderCopy(renderer,image, &crop, &rect);
}
void Sprite::Draw()
{
Camera.x = rect.x + *CameraX;
Camera.y = rect.y + *CameraY;
CollisionRect.setX(rect.x + *CameraX);
CollisionRect.setY(rect.y + *CameraY);
SDL_RenderCopy(renderer,image, &crop, &Camera);
SDL_RenderCopy(renderer,CollisionImage, NULL, &CollisionSDL_Rect);
}
void Sprite::SetUpAnimation(int passed_Amount_X, int passed_Amount_Y)
{
Amount_Frame_X = passed_Amount_X;
Amount_Frame_Y = passed_Amount_Y;
}
void Sprite::PlayAnimation(int BeginFrame, int EndFrame, int Row, float Speed)
{
if (animationDelay+Speed < SDL_GetTicks())
{
if (EndFrame <= CurrentFrame)
CurrentFrame = BeginFrame;
else
CurrentFrame++;
crop.x = CurrentFrame * (img_width/Amount_Frame_X);
crop.y = Row * (img_height/Amount_Frame_Y);
crop.w = img_width/Amount_Frame_X;
crop.h = img_height/Amount_Frame_Y;
animationDelay = SDL_GetTicks();
}
}
Sprite::~Sprite()
{
SDL_DestroyTexture(image);
}
void Sprite::SetX(float X)
{
X_pos = X;
rect.x = int(X_pos - Origin_X);
}
void Sprite::SetY(float Y)
{
Y_pos = Y;
rect.y = int(Y_pos - Origin_Y);
}
void Sprite::SetPosition(float X, float Y)
{
X_pos = X;
Y_pos = Y;
rect.x = int(X_pos - Origin_X);
rect.y = int(Y_pos - Origin_Y);
}
float Sprite::getX()
{
return X_pos;
}
float Sprite::getY()
{
return Y_pos;
}
void Sprite::SetOrigin(float X, float Y)
{
Origin_X = X;
Origin_Y = Y;
SetPosition(getX(),getY());
}
void Sprite::SetWidth(int W)
{
rect.w = W;
}
void Sprite::SetHeight(int H)
{
rect.h = H;
}
int Sprite::GetWidth()
{
return rect.w;
}
int Sprite::GetHeight()
{
return rect.h;
}
//header
#pragma once
#include "stdafx.h"
#include "SDL_Setup.h"
#include "CollisionRectangle.h"
class Sprite
{
public:
Sprite(SDL_Renderer* passed_renderer, std::string FilePath, int x, int y, int w, int h, float *CameraX, float *CameraY, CollisionRectangle passed_CollisionRect);
~Sprite();
void Draw();
void DrawSteady();
void SetX(float X);
void SetY(float Y);
void SetPosition(float X, float Y);
float getX();
float getY();
void SetOrigin(float X, float Y);
int GetWidth();
int GetHeight();
void SetHeight(int H);
void SetWidth(int W);
void PlayAnimation(int BeginFrame, int EndFrame, int Row, float Speed);
void SetUpAnimation(int passed_Amount_X, int passed_Amount_Y);
private:
CollisionRectangle CollisionRect;
SDL_Rect Camera;
SDL_Rect CollisionSDL_Rect;
float *CameraX;
float *CameraY;
float Origin_X;
float Origin_Y;
float X_pos;
float Y_pos;
SDL_Texture* image;
SDL_Texture* CollisionImage;
SDL_Rect rect;
SDL_Rect crop;
SDL_Renderer* renderer;
int img_width;
int img_height;
int CurrentFrame;
int animationDelay;
int Amount_Frame_X;
int Amount_Frame_Y;
};

Allegro 5 drawing to buffer

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:
BallEngine Class - Manage all the Allegro functions/objects
BallManager Class - Manages all of the balls
Ball Class - Hold information about a ball
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?
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.