I was wondering what is wrong with my code when I try to draw an object of a class. For my drawable class Box, I have a rectangle shape and a sprite. When I declare an instance of the class to draw in main.cpp, only the Box test(1,1) is drawn properly as I wanted. However, all of the other instances such as Box test1(2,2); Box test2(3,3) were only able to draw the rectangle shape and not the sprite. Is there any way to fix it so that any other instances of object Box is drawn properly like in the case of Box test(1,1)? And the image is loaded properly as the instance test(1,1) demonstrated. Here's my code:
Box.h
#ifndef LINKEDLIST_TEMPLATE_BOX_H
#define LINKEDLIST_TEMPLATE_BOX_H
#include <SFML/Graphics.hpp>
class Box:public sf::Drawable, sf::Transformable {
protected:
int col;
int row;
sf::Color color;
sf::RectangleShape boxBackGround;
sf::Sprite queen;
sf::Texture texture;
public:
void draw(sf::RenderTarget &window, sf::RenderStates state) const;
int getRow();
int getCol();
void setRow(int row);
void setCol(int col);
Box(int row, int col);
void setBox(int x, int y);
Box();
void setTexture();
void setQueen();
};
Box.cpp
#include "Box.h"
int Box::getCol() {
return col;
}
int Box::getRow() {
return row;
}
void Box::setCol(int col) {
this->col = col;
}
void Box::setRow(int row) {
this->row = row;
}
Box::Box(){
row = 0;
col = 0;
this->setBox(this->row,this->col);
this->setTexture();
this->setQueen();
}
Box::Box(int row, int col) {
this->row = row;
this->col = col;
this->setBox(this->row,this->col);
this->setTexture();
this->setQueen();
}
void Box::setBox(int x, int y) {
boxBackGround.setSize({200.f,200.f});
boxBackGround.setFillColor(sf::Color::White);
boxBackGround.setPosition(x*210,y*210);
}
void Box::draw(sf::RenderTarget &window, sf::RenderStates state) const {
window.draw(boxBackGround);
window.draw(queen);
}
void Box::setTexture() {
texture.loadFromFile("queen.png");
}
void Box::setQueen() {
this->queen.setTexture(this->texture);
this->queen.setScale(0.08f,0.08f);
this->queen.setPosition(this->boxBackGround.getGlobalBounds().height+50,this->boxBackGround.getGlobalBounds().width+30);
}
main.cpp
#include "Box.h"
int main() {
Box test(1,1);
Box test1(2,2);
Box test2(3,3);
sf::RenderWindow window(sf::VideoMode(1980, 1080, 32), "Test");
while(window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
window.draw(test2);
window.draw(test);
window.draw(test1);
window.display();
}
return 0;
}
In void Box::setQueen
In the line:
this->queen.setPosition(this->boxBackGround.getGlobalBounds().height+50,this->boxBackGround.getGlobalBounds().width+30);
You should be adding values to the .top and .left members of the sf::Rect returned by the getGlobalBounds(). .width and .height will always be the same for each of boxes' boxBackGround, so in your code Queens are all rendered in the same place.
Related
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();
}
}
I am trying to storesf::RectangleShape's into thestd::vectorand then draw each of them into thesf::RenderWindow.
Single rectangle shape is representing 1x1 tetromino and i would like to store it into the vector each time it reaches the bottom of the window. Then I would like to reset the position of the current tetromino to the default position.
I think I'm not even to able store it correctly. Each time the tetromino reaches the bottom it gives me this error message:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Bellow please find my current code. I just started working on it and already got stuck.
Definitions.h
#pragma once
// Point structure
struct Point
{
int dim_x;
int dim_y;
};
// Field Dimensions
const int fieldRows = 10;
const int fieldColumns = 9;
const int pointSize = 50.f;
// For checkingEdges funntion within the Tetrnomino.h
enum Edge
{
leftEdge,
rightEdge,
noneEdge
};
Game.h
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Definitions.h"
#include "Tetromino.h"
class Game
{
public:
Game();
~Game();
// Game starter
void run();
// Accessors
bool running();
private:
// Updating and rendering the game window
void update();
void render();
// Initialization
void initVariables();
void initWindow();
void initBacgroundMusic();
// Polling
void pollEvents();
// Window logic stuff
sf::RenderWindow* _window;
sf::Event _event;
void drawStack();
// Bacground Music
sf::Music _ost;
// Tetromino + Its logic
Tetromino _T;
sf::Time delayTime = sf::milliseconds(300);
sf::Clock clock;
};
Tetromino.h
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include "Definitions.h"
class Tetromino
{
public:
Tetromino();
~Tetromino();
// Initialization
void initTetromino();
// Tetromonino logic
void moveTetromino();
Edge checkEdges();
// Getters & Setters
sf::RectangleShape getTetromino();
sf::RectangleShape getStackPart(int part);
int getStackSize();
void setTetromino(sf::RectangleShape &t);
private:
// The current tetromino
sf::RectangleShape _tetromino;
std::vector<sf::RectangleShape> _stack;
};
Game.cpp
#include "Game.h"
//-----Consturcotrs and Destructors-----//
Game::Game()
{
//Basic Initialization
_T.initTetromino();
initVariables();
}
Game::~Game()
{
delete _window;
}
//-----Private Functions-----//
void Game::run()
{
update();
render();
}
bool Game::running()
{
return _window->isOpen();
}
void Game::update()
{
sf::Time elapsed = clock.getElapsedTime();
pollEvents();
if (elapsed >= delayTime)
{
_T.moveTetromino();
clock.restart();
}
}
void Game::render()
{
_window->clear(sf::Color::White);
_window->draw(_T.getTetromino());
drawStack();
_window->display();
}
void Game::initVariables()
{
_window = nullptr;
initWindow();
initBacgroundMusic();
}
void Game::initWindow()
{
_window = new sf::RenderWindow(sf::VideoMode(fieldColumns * pointSize, fieldRows * pointSize), "Tetris v0.2", sf::Style::Default);
_window->setVerticalSyncEnabled(true);
_window->setFramerateLimit(60);
}
void Game::initBacgroundMusic()
{
_ost.openFromFile("../QT_SFML_Tetris/Music.ogg");
_ost.play();
_ost.setLoop(true);
_ost.setVolume(50.f);
}
void Game::pollEvents()
{
while (_window->pollEvent(_event))
{
if (_event.type == sf::Event::Closed) {_window->close();}
if (_event.type == sf::Event::KeyPressed)
{
if (_event.key.code == sf::Keyboard::Escape){_window->close();}
if (_event.key.code == sf::Keyboard::Left && _T.checkEdges() != leftEdge)
{
sf::RectangleShape t = _T.getTetromino();
t.setPosition(t.getPosition().x - pointSize, t.getPosition().y);
_T.setTetromino(t);
render();
}
if (_event.key.code == sf::Keyboard::Right && _T.checkEdges() != rightEdge)
{
sf::RectangleShape t = _T.getTetromino();
t.setPosition(t.getPosition().x + pointSize, t.getPosition().y);
_T.setTetromino(t);
render();
}
if (_event.key.code == sf::Keyboard::Down)
{
sf::RectangleShape t = _T.getTetromino();
t.setPosition(t.getPosition().x, t.getPosition().y+ pointSize);
_T.setTetromino(t);
render();
}
}
}
}
**void Game::drawStack()**
{
for (unsigned int i = _T.getStackSize(); i > 0; --i)
{
_window->draw(_T.getStackPart(i));
}
}
main.cpp
#include <Game.h>
int main()
{
Game game;
while (game.running())
{
game.run();
}
return 0;
}
Tetromino.cpp
#include "Tetromino.h"
//-----Consturcotrs and Destructors-----//
Tetromino::Tetromino()
{
}
Tetromino::~Tetromino()
{
}
//-----Public Functions-----//
void Tetromino::initTetromino()
{
_tetromino.setPosition(sf::Vector2f((fieldColumns * pointSize - pointSize) / 2, 0.f));
_tetromino.setSize(sf::Vector2f(pointSize, pointSize));
_tetromino.setFillColor(sf::Color::Red);
}
void Tetromino::moveTetromino()
{
_tetromino.move(0.f, pointSize);
if (_tetromino.getPosition().y > fieldRows * pointSize - pointSize)
{
_stack.push_back(_tetromino);
_tetromino.setPosition(sf::Vector2f((fieldColumns * pointSize - pointSize) / 2, 0.f));
}
}
Edge Tetromino::checkEdges()
{
if (_tetromino.getPosition().x == 0)
{
return leftEdge;
}
else if (_tetromino.getPosition().x == (fieldColumns * pointSize) - pointSize)
{
return rightEdge;
}
else return noneEdge;
}
sf::RectangleShape Tetromino::getTetromino()
{
return _tetromino;
}
sf::RectangleShape Tetromino::getStackPart(int part)
{
return _stack[part];
}
int Tetromino::getStackSize()
{
return _stack.size();
}
void Tetromino::setTetromino(sf::RectangleShape &t)
{
_tetromino = t;
}
I think the main issue could be within this line:
_stack.push_back(_tetromino);
In the drawStack() method you try to iterate backwards.
There is a reverse iterator doing this for you.
you have an off-by one error in your index calculation, which works only with an empty vector (exactly to prevent these errors you should use the iterator!)
You may want to read about iterators in C++ here is a small example.
According to your code it will look like (note that you need also a getStack()-method in Tetromino.hpp, returning the reference of the vector):
void Game::drawStack()
{
for (auto it = _T.getStack().rbegin(); it != _T.getStack().rend(); it++)
{
_window->draw(*it);
}
}
If you want to keep the index, I this is a fix:
void Game::drawStack()
{
for (int i = _T.getStackSize()-1; i >= 0; --i)
{
_window->draw(_T.getStackPart(i));
}
}
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.
i developped game "mario bross" with SDL and c++ to simplify things , i have two rectangle and i had to change the coordinates one of them three steps up (-3) and after three steps down(+3) where ever the two rectangles go throught collision .
the problem is when the two rectangle goes throught collision one of them move up (-3) but it does not move down(+3) i redirect the coordinates of the rectangle it change up and down but it only render the up movement.
// surprise.h
#ifndef SURPRISE_H_INCLUDED
#define SURPRISE_H_INCLUDED
#include <SDL/SDL.h>
#include <vector>
#include "base.h"
class surprise:public baseclass
{
SDL_Rect box;
int xvel,yvel;
SDL_Surface* image;
bool ground;
double frame;
SDL_Rect clips[4];
public:
surprise(SDL_Surface*,int x,int y,int xvel,int yvel);
void show(SDL_Surface*);
void setFrame(double);
void move(int xvel);
double getFrame();
SDL_Rect* getRect();
int getyvel();
void setyvel(int y);
};
#endif // SURPRISE_H_INCLUDED
//surprise.cpp
#include"surprise.h"
#include <SDL/SDL.h>
surprise::surprise(SDL_Surface* img ,int x,int y,int xVel,int yVel)
{
image=img;
box.x=x;
box.y=y;
box.w=image->w/2;
box.h=image->h;
xvel=xVel;
yvel=yVel;
ground=0;
for(int i=0;i<3;i++)
{
clips[i].x=i*30;
clips[i].y=0;
clips[i].w=30;
clips[i].h=30;
}
frame=0.0;
}
double surprise::getFrame()
{
return frame;
}
void surprise::setFrame(double fr)
{
frame=fr;
}
void surprise::show(SDL_Surface* screen)
{
SDL_Rect tmp={box.x-coord.x,box.y,30,30};
if (frame>=2)
{
frame=0.1;
SDL_BlitSurface(image,&clips[(int)(frame+0.5)],screen,&tmp);
}
else
{
SDL_BlitSurface(image,&clips[(int)(frame+0.5)],screen,&tmp);
}
frame+=0.1;
}
SDL_Rect* surprise::getRect()
{
return &box;
}
int surprise::getyvel()
{
return box.y;
}
void surprise::setyvel(int y)
{
box.y+=y;
// part of game .cpp
// the surprise is the first box the player is the second box
//logique part
// i put the surprise class into vector
for (int i=0;i<surprises.size();i++)
{
SDL_Rect tmprect ={surprises[i]->getRect()->x-baseclass::coord.x,surprises[i]->getRect()->y,30,30};
if(collision(&tmprect,player1->getRect()))
{
for (int j=0;j<3;j++)
{
surprises[i]->setyvel(-1);
std::cout<<"increase the y coordinates "<<surprises[i]->getyvel()->y<<std::endl;
}
for (int k=0;k<3;k++)
{
surprises[i]->setyvel(+1);
std::cout<<"decrease the y coordinates "<<surprises[i]->getRect()->y<<std::endl;
}
}
}
//render part
SDL_Flip(screen);
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.