C++ (Arduino wrapper) question: I'm writing a shoot em up game on an Arduino which has a LCD connected -
I have a base class (Sprite), and from this other classes are derived - Alien, Missile and Player. The constructor of the Alien class also has private member pMissile (a pointer to a Missile class) - 'an object within an object' would be a way to describe this I think.
[when an Alien fires a missile, it passes its own (x,y) coordinates to the missile, and the missile has its own method of moving starting from the Alien's coordinates]
My question is: How can I access the coordinates of the missile through the Alien object?
Streamlined code is below and I have also drawn a representation of the classes:
// Bass class - has a form/shape, x and y position
class Sprite
{
public:
Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit);
virtual void Move() = 0;
void Render() { display.drawBitmap(x,y, spacePtr, 5, 6, BLACK); }
unsigned int getX() const { return x; }
unsigned int getY() const { return y; }
protected:
unsigned char *spacePtr;
unsigned int x, y;
};
// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
class Missile : public Sprite
{
public:
Missile(): Sprite(&spaceMissile[0], 0, 0) {}
virtual void Move(); // its own method of moving
};
// Derived class "Alien" - has a specific form/shape, and specific (x,y) position
class Alien : public Sprite
{
public:
Alien();
virtual void Move(); // its own method of moving
private:
Missile *pMissile;
};
Alien::Alien(): Sprite(&spaceAlien[0], random(5, 75), random(4, 10))
{
Missile MissileArray[MaxAmmoSize];
pMissile = &MissileArray[0];
}
void Alien::Move()
{
if( random(10) % 2 == 0 )
x += 1;
if( random(10) % 3 == 0 )
y += 1;
if( (pMissile != NULL) && (random(10) == 1) )
{
pMissile->setCoord(x, y);
pMissile->Move(); // move the missile
pMissile++; // move onto the next missile in the array
}
Render();
}
/*****************************************************************************************/
Alien MONSTER;
Player HERO;
Alien *pMONSTER = &MONSTER;
void loop()
{
display.clearDisplay();
MONSTER.Move();
HERO.Move();
pMONSTER->getX(); // this is x location of MONSTER
**// how does pMONSTER access pMissile(x,y) within MONSTER.**
delay(100);
display.display();
}
Embedded C++ Class interaction
The common way is to add a getter function to Alien:
class Alien {
public:
Missile* getMissile() { return pMissile; }
}
To use it:
Alien* a = getAlienFromSomewhere();
auto pMissile = a.GetMissile();
if (pMissile != NULL) {
x = pMissile->getX();
y = pMissile->getY();
}
I imagine that you want to access your missile position through the alien to test the collision with your hero entity, but if you need to keep the track of your missiles you should not "walk" with your pointer to the next missile as shown in the Alien::Move(). Doing this you will lose the reference of the beginning of the array.
IMHO, I would do something like this in your alien class:
// Bass class - has a form/shape, x and y position
class Sprite
{
public:
Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit);
virtual void Move() = 0;
void Render() { display.drawBitmap(x,y, spacePtr, 5, 6, BLACK); }
unsigned int& getX() const { return x; }
unsigned int& getY() const { return y; }
protected:
unsigned char *spacePtr;
unsigned int x, y;
};
// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
class Missile : public Sprite
{
public:
Missile(): Sprite(&spaceMissile[0], 0, 0) {}
virtual void Move(); // its own method of moving
};
// Derived class "Alien" - has a specific form/shape, and specific (x,y) position
class Alien : public Sprite
{
public:
Alien();
~Alien(); // a destructor to cleanup your missiles - arduino have almost no memory to handle leaks ;-)
virtual void Move(); // its own method of moving
inline Missile& getMissile(unsigned char n) { return pMissile[n]; }
inline Missile& operator[](unsigned char n) { return getMissile(n); }
inline unsigned int& getX(unsigned char n) { return getMissile(n).getX(); }
inline unsigned int& getY(unsigned char n) { return getMissile(n).getY(); }
private:
Missile *pMissile;
// adding the code to handle the count
unsigned char missileCount;
};
Alien::Alien():
Sprite(&spaceAlien[0], random(5, 75), random(4, 10)),
missileCount(0)
{
// this way of allocation creates a local object that is destroyed by the end of this scope
//Missile MissileArray[MaxAmmoSize];
//pMissile = &MissileArray[0];
// so you should do somethin like this
pMissile = new Missile[MaxAmmoSize];
}
Alien()::~Alien()
{
delete[] pMissile;
}
void Alien::Move()
{
if( random(10) % 2 == 0 )
x += 1;
if( random(10) % 3 == 0 )
y += 1;
if( (pMissile != NULL) && (random(10) == 1) )
{
// my proposal to fire it up
Missile& missile = pMissile[missileCount];
missile->setCoord(x, y);
missile->Move(); // move the missile
missileCount++; // move onto the next missile in the array
}
Render();
}
Using the code like this you could access the locations of your missiles by using:
MONSTER.getX(0) += 1;
MONSTER[0].getY() +=1;
MONSTER.getMissile(1).getX() = 10;
To have some clarity I recommend also the refactoring of the getX() and getY() methods to x() and y(), since they are returning references to the class contents (and doing this, you should also rename your x and y members to something else, or you can get crazy with name conflicts).
Related
I am using an SDL Template and I am trying to implement gravity into my code so I am trying to set a Y as the strength of my gravity in update but when I try to use a pointer to setY I always get an error "a pointer to a bound function may only be used to call the function using a setter" the code below is from my GameScene.cpp
#include "GameScene.h"
GameScene::GameScene()
{
// Register and add game objects on constructor
player = new Player();
this->addGameObject(player);
floor = new Floor();
this->addGameObject(floor);
}
GameScene::~GameScene()
{
delete player;
}
void GameScene::start()
{
Scene::start();
// Initialize any scene logic here
}
void GameScene::draw()
{
Scene::draw();
}
void GameScene::update()
{
Scene::update();
if (player->getOnFloor() == false) {
player->setY -= 1;
}
else {
player->setY = 0;
}
}
This code is from my Player.h where the setters and getters are located
#pragma once
#include "GameObject.h"
#include "common.h"
#include "draw.h"
class Player :
public GameObject
{
public:
void start();
void update();
void draw();
//X Setter
void setX(int x) {
x = x;
}
//X Getter
int getX() {
return x;
}
//Y Setter
void setY(int y) {
y = y;
}
//Y Getter
int getY() {
return y;
}
//Height Setter
void setHeight(int height) {
height = height;
}
//Height Getter
int getHeight() {
return height;
}
//Width Setter
void setWidth(int width) {
width = width;
}
//Width Getter
int getWidth() {
return width;
}
//OnFloor Setter
void setOnFloor(int onFloor) {
onFloor = onFloor;
}
//OnFloor Getter
int getOnFloor() {
return onFloor;
}
private:
SDL_Texture* texture;
int x;
int y;
int height;
int width;
int speed;
bool onFloor;
};
I have tried putting () beside the setY like this
if (player->getOnFloor() == false) {
player->setY() -= 1;
}
else {
player->setY)( = 0;
}
But it still did not work, I was expecting it to make my player fall down but VS studio kept showing me the error
this may be a noobie question but I am still a noobie programmer so please bear with me
EDITED:
The player is now falling upwards, I tried doing -= 1 to make him fall downwards
void GameScene::update()
{
Scene::update();
if (player->getOnFloor() == false) {
player->setY(player->getY() -= 1);
}
else {
player->setY(0);
}
}
but I get an error on player in "player->getY()" saying that expression must be a modifiable lvalue
The error you are getting is because you are trying to use the setY setter function as a variable. In order to use the setter function to set the y property of the player object, you need to call the setY function and pass in the new value for y as an argument. Here is how you would use the setY setter function in your GameScene::update function:
void GameScene::update()
{
Scene::update();
if (player->getOnFloor() == false) {
player->setY(player->getY() + 1);
}
else {
player->setY(0);
}
}
Note that in this example, we are calling the getY getter function to get the current value of y, and then subtracting 1 from it before passing the result to the setY setter function. This will update the y property of the player object and make it fall down by 1 unit each time the update function is called.
I am looking to make this more efficient, how would I Re-write the Enemy class to use inheritance and virtual functions? Including any new child classes.
class Enemy
{
public:
int type; // 0 = Dragon, 1 = Robot
int health; // 0 = dead, 100 = full
string name;
Enemy();
Enemy(int t, int h, string n);
int getDamage(); // How much damage this enemy does
};
Enemy::Enemy() : type(0), health(100), name("")
{ }
Enemy::Enemy(int t, int h, string n) :
type(t), health(h), name(n)
{ }
int Enemy::getDamage() {
int damage = 0;
if (type == 0) {
damage = 10; // Dragon does 10
// 10% change of extra damage
if (rand() % 10 == 0)
damage += 10;
}
else if (type == 1) {
// Sometimes robot glitches and does no damage
if (rand() % 5 == 0)
damage = 0;
else
damage = 3; // Robot does 3
}
return damage;
}
This calculates how much total damage the band will dish out.
int calculateDamage(vector<Enemy*> bandOfEnemies)
{
int damage = 0;
for (int i = 0; i < bandOfEnemies.size(); i++)
{
damage += bandOfEnemies[i]->getDamage();
}
return damage;
}
That's a good start, but with inheritance, you don't need to be so specific. For example, in the enemy class you have an attribute type. If you want to use inheritance, you don't need to specify the type, because the derived class would be the type.
As for your function getDamage(), you can leave it blank and turn it into a virtual function. Putting all of this together, your code should look something like this:
class Enemy
{
public:
int health; // 0 = dead, 100 = full
string name;
Enemy();
Enemy(int t, int h, std::string n);
virtual int getDamage() = 0; // pure virtual function
};
Enemy::Enemy()
: type(0), health(100), name("") {}
Enemy::Enemy(int t, int h, std::string n)
: type(t), health(h), name(n) {}
// class 'Dragon' inherits from class 'Enemy'
class Dragon : public Enemy
{
public:
Dragon() {}
int getDamage()
{
// dragon's damage
}
};
Notice how if you want to create another enemy, you just inherit from the Enemy class. And this way, you can store your characters in an array like this:
vector<Enemy> enemies = {
Dragon(),
Dragon(),
Robot()
};
So, again, making a pong clone, and run into a bump. I'm creating the game class gamestate for my state machine and in the definition, I instantiate two classes of the class I've created for the the different player paddles and I give them two different arguments that will decide if the class becomes the player one or player two object.
But it seems like the program won't allow me to pass an argument in the class definition. I've tried to put the class instantiation in the game class constructor but then the events/logic/render functions don't have access to the paddle classes. That leads me to think it has to be in the game class definition. Here's the code presenting the problem.
class Ball
{
public:
void events();
void logic();
void render();
Ball(int server);
~Ball();
};
class Paddle
{
private:
SDL_Rect Paddle_Bound;
int yVel;
SDL_Rect *clip;
int player_key;
public:
void events();
void logic();
void render();
Paddle(int player);
~Paddle();
};
class Game : public GameState
{
private:
int server;
TTF_Font *score = NULL;
Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball Ball(2);
public:
void events();
void logic();
void render();
Game();
~Game();
};
The lines that say
Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball Ball(2);
Are the ones raising errors that say "expected a type specifier". Now I know that means it wants something like int player but I need to pass a number so that the constructor can decide which player it'll be, which ultimately decides what keys move the class up and down the screen. So to be perfectly clear, I want to be able to pass an argument to a class instantiation so that the class constructor can turn the class into the left OR the right paddle. So how do I go about instantiating a class, inside another class, using arguments?
Thanks!
EDIT: For the constructor problem I have another example that is fully coded where I can't put the class initializer in the constructor. So basically in the title screen there is one button, and it has properties stored in a Button class that I create to manage buttons.
class GameState
{
public:
virtual void events() = 0;
virtual void logic() = 0;
virtual void render() = 0;
virtual ~GameState(){};
};
class Button
{
public:
SDL_Rect button_clip[2];
SDL_Rect button;
SDL_Surface *button_sprite = NULL;
Button();
};
class Title : public GameState
{
private:
SDL_Surface *Title_Message = NULL;
SDL_Rect *clip;
// THIS IS WHERE I HAVE TO PLACE THIS LINE
//Button Title_Button;
public:
void events();
void logic();
void render();
Title();
~Title();
};
Title::Title()
{
//THIS IS WHERE IT SHOULD BE ACCORDING TO WHAT YOU'RE TELLING ME
Button Title_Message;
//text/sprites
Title_Message = TTF_RenderText_Solid(font, "PONG", color);
Title_Button.button_sprite = load_image("Start.png");
//region for button
Title_Button.button.x = 200;
Title_Button.button.y = 350;
Title_Button.button.w = 100;
Title_Button.button.h = 50;
//clips not hover
Title_Button.button_clip[0].x = 0;
Title_Button.button_clip[0].y = 0;
Title_Button.button_clip[0].w = 100;
Title_Button.button_clip[0].h = 50;
//clips hover
Title_Button.button_clip[1].x = 0;
Title_Button.button_clip[1].y = 50;
Title_Button.button_clip[1].w = 100;
Title_Button.button_clip[1].h = 50;
}
Title::~Title()
{
SDL_FreeSurface(Title_Message);
SDL_FreeSurface(Title_Button.button_sprite);
}
void Title::events()
{
int x = 0;
int y = 0;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEMOTION)
{
x = event.motion.x;
y = event.motion.y;
if ((x > Title_Button.button.x) && (x < (Title_Button.button.x + Title_Button.button.w)) && (y > Title_Button.button.y) && (y < (Title_Button.button.y + Title_Button.button.h)))
{
clip = &Title_Button.button_clip[1];
}
else
{
clip = &Title_Button.button_clip[0];
}
}
if (event.type == SDL_QUIT)
{
quit = true;
}
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
if ((x > Title_Button.button.x) && (x < (Title_Button.button.x + Title_Button.button.w)) && (y > Title_Button.button.y) && (y < (Title_Button.button.y + Title_Button.button.h)))
{
set_next_state(GAME);
}
}
}
}
}
void Title::logic()
{
}
void Title::render()
{
apply_surface(Title_Button.button.x, Title_Button.button.y, Title_Button.button_sprite, screen, clip);
apply_surface((SCREEN_WIDTH - Title_Message->w) / 2, 100, Title_Message, screen);
}
The problem here is that the events/logic/render functions have no clue that Title_Message exists when I put it in the void Title::Title() constructor. But when I put it in the class definition, I can't pass an argument through it.
The values should be initialized in the constructor definiton: not in the class definition.
class Game : public GameState
{
private:
int server;
TTF_Font *score;
Paddle PlayerOne;
Paddle Playertwo;
Ball ball; // same class name and variable name won't work
public:
void events();
void logic();
void render();
Game();
~Game();
};
...
Game::Game()
: score(NULL)
, PlayerOne(1)
, PlayerTwo(2)
, ball(2)
{
...
}
EDIT: For the Title message stuff
class Title : public GameState
{
private:
SDL_Surface *Title_Message = NULL;
SDL_Rect *clip;
// THIS IS WHERE I HAVE TO PLACE THIS LINE
Button Title_Button;
Button Title_Message;
public:
void events();
void logic();
void render();
Title();
~Title();
};
Title::Title()
{
//text/sprites
Title_Message = TTF_RenderText_Solid(font, "PONG", color);
Title_Button.button_sprite = load_image("Start.png");
You can't do that outside of a method. Do that in the constructor
class Game : public GameState
{
private:
int server;
TTF_Font *score;
Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball ballObject(2);
public:
void events();
void logic();
void render();
Game(): PlayerOne(1), Playertwo(2), ballObject(2){};
~Game();
};
Edit : An answer has already been posted, I apologize for posting the same answer. I am new at using the interface. So it took time to figure out the code block generation.
Edit 2: Repsonding to your edit.
You have to declare the object in the class itself. It's just you can't instantiate it in the declaration. You need to study about how c++ classes and classes in general work.
class class1
{
private:
class class2Obj(args);
public:
class1();
~class1();
}
The above example is wrong. To make this work you should do it like this
class class1
{
private:
class2 class2Obj;
public:
class1();
~class1();
}
class1::class1()
{
class2Obj(args);
}
I have those inheritance classes :
Base Class: Entity
Derived from Entity Classes: Actor, Obj, Enemy
The Base class Entity contains an obj of a user-defined-type that i called "CollisionStuff".
When i run my program the destructor of CollisionStuff is called after every CollisionStuff constructor call and every time game-loop goes on.
so my call is: why is this happening?
As you can see below, i allocate dinamically some arrays in the setRectangle method, the programm calls the destructor, it deletes my data and when i try to use them... it calls "_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));".
Thank you in before
here my code: Entity.h
enum e_Type {tActor = 0, tObj, tEnemy, tBackg};
class Entity
{
public:
Entity(void);
~Entity(void);
float getH();
float getW();
void setWH(float W, float H);
bool CreateSprite(std::string path);
sf::Sprite& getSprite();
void setType(e_Type type);
e_Type getType();
CollisionStuff getColStuff();
static std::list<Entity*> List;
protected:
sf::Sprite m_sprite;
sf::Texture m_texture;
float m_h;
float m_w;
e_Type m_type;
CollisionStuff m_colStuff;
void addToList();
};
CollisionStuff.h
class CollisionStuff
{
public:
CollisionStuff();
~CollisionStuff(void);
void setRectangle(int W, int H);
void followTheSprite(Entity entity);
private:
sf::Vector2f* m_a;
sf::Vector2f* m_b;
sf::Vector2f* m_c;
sf::Vector2f* m_d;
/* this member data are sides of rectangle used
to manage collisions between object throughout the scenario
a
-------------
| |
c | | d
| |
-------------
b
*/
};
CollisionStuff.cpp
CollisionStuff::CollisionStuff()
{
//setRectangle(0, 0);
}
void CollisionStuff::setRectangle(int W, int H)
{
m_a = new sf::Vector2f[W];
m_b = new sf::Vector2f[W];
m_c = new sf::Vector2f[H];
m_d = new sf::Vector2f[H];
}
void CollisionStuff::followTheSprite(Entity entity)
{
entity.getSprite().setOrigin(0, 0);
sf::Vector2f UpLeftVertex = entity.getSprite().getPosition();
for(int i = 0; i < entity.getW(); i++)
{
m_a[i].x = UpLeftVertex.x + i;
m_a[i].y = UpLeftVertex.y;
m_b[i].x = UpLeftVertex.x + i;
m_b[i].y = UpLeftVertex.y + entity.getH();
}
for(int i = 0; i < entity.getH(); i++)
{
m_c[i].x = UpLeftVertex.x;
m_c[i].y = UpLeftVertex.y + i;
m_d[i].x = UpLeftVertex.x + entity.getW();
m_d[i].y = UpLeftVertex.y + i;
}
}
CollisionStuff::~CollisionStuff(void)
{
delete [] m_a;
delete [] m_b;
delete [] m_c;
delete [] m_d;
}
EDIT
Thank you for the answers.
Example of CollisionStuff use
Actor.cpp (it's a derived class of Entity)
Actor::Actor(void)
{
if(!CreateSprite("D://Sprites//MainChar.png"))
{
std::cout << "Impossibile creare sprite" << std::endl;
}
else
{
std::cout << "Creazione sprite riuscita" << std::endl;
m_sprite.setPosition(100.0f, 365.0f);
m_sprite.setOrigin(20, 35);
//m_sprite.setPosition(190.0f, 382.5f); // 200, 400
setWH(40, 70);
m_health = 100;
m_status = Good;
setType(tActor);
m_jCounter = -1;
m_action = Null;
setColStuff();
}
}
void Actor::setColStuff()
{
m_colStuff.setRectangle(m_w, m_h);
}
void Actor::physic()
{
//setColStuff();
m_colStuff.followTheSprite(*this);
}
main.cpp
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Platform");
std::list<Entity*>::iterator i;
Background BG;
Level1 FirstLev;
Actor Doodle;
while(window.isOpen())
{
sf::Event event;
if(window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
Doodle.inputEvts();
}
Doodle.act(Doodle.getAction());
Doodle.physic();
window.clear();
window.draw(BG.getSprite());
window.draw(Doodle.getSprite());
FirstLev.drawLevel(window);
window.display();
}
return 0;
}
It's really hard to tell from the bit of code that you posted, but if I had to guess I'd say it's probably related to this:
CollisionStuff getColStuff();
you're returning CollisionStuff by value, which means a new copy will be created by whoever is calling this. It'll have the same pointers that the original CollisionStuff object allocated, and it'll delete them when it goes out of scope, leaving the original one with dangling pointers.
You can try returning by reference or by pointer, but either way you should write a copy constructor and override the assignment operator for CollisionStuff (Rule of Three).
Another idea would be to use std::vector<sf::Vector2f> instead of allocating the sf::Vector2f array yourself.
I used the Fireworks init function to store a particle into the particles vector class.
And when I try to retrieve the count of the particles vector in the update() function, the particles vector is empty. Why?
Fireworks.cpp class:
void Fireworks::init(){
float x = randf();
float y = - 1 * randf(); //Going UP
float z = randf();
Particle part(position,Vector3f(x,y,z), color, randInt(1,50));
particles.push_back(part);
}
bool Fireworks::update(){
Particle particle;
int count = particles.size(); //Total num of particles in system
cout << particles.size() << " ";
}
class Fireworks: public ParticleSystem {
private:
void init();
public:
Fireworks(Vector3f pos, Vector3f col) {
position = pos;
color = col;
init();
}
virtual bool update();
};
particlesystem.h
class ParticleSystem {
protected:
vector<Particle> particles;
public:
//virtual Particle generateParticle();
virtual bool update(){return false;};
};
main.cpp
ParticleSystem *PS;
int main( int argc, char *argv[] ) {
PS = &Fireworks(Vector3f(0,0,0), Vector3f(200,0,255));
glutIdleFunc(move);
}
void move()
{
PS->update();
}
PS = &Fireworks(Vector3f(0,0,0), Vector3f(200,0,255));
This introduces undefined behavior. The right hand side creates a temporary, which will be deleted as soon as that full expression is over (i.e. right after the ;). PS will point to a deleted object after that line - doing anything with it is undefined behavior.
Use new.
PS = new Fireworks(Vector3f(0,0,0), Vector3f(200,0,255));
Also, you must return from all functions that are declared as returning something (non-void). It doesn't matter if they're virtual or not.