Animating an array of child class objects - c++

I am trying to create a parent class "Shape" with child classes "Circle", "Triangle", "Rectangle". The parent class holds the x pos, y pos, and fill color or all the "shapes" and then each child class holds info specific to that shape. Would anyone mind looking over my code and see why im getting the error "Shapes does not have a member 'setRadius'" when trying to set the radius in the array of objects...
P.S. right now I only have the child class "Circle" until i get it working. Then I will add the other two classes.
Also, if anyone sees any other errors in my code, I would appreciate them being pointed out.
Thanks in advance
#include <allegro.h>
#include <cstdlib>
using namespace std;
#define scrX 640
#define scrY 400
#define WHITE makecol(255,255,255)
#define GRAY makecol(60,60,60)
#define BLUE makecol(17,30,214)
int random(int low, int high);
const int numCircles = random(1,50);
class Shape{
public:
Shape(){x = scrX / 2; y = scrY / 2; fill = WHITE;}
protected:
int x, y, fill;
};
class Circle : public Shape{
public:
Circle(){radius = 0;}
Circle(int r){radius = r;}
void setRadius(int r){radius = r;}
protected:
int radius;
};
int main()
{
// Program Initialization
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, scrX, scrY, 0, 0);
// Create and clear the buffer for initial use
BITMAP *buffer = create_bitmap(scrX, scrY);
clear_to_color(buffer, GRAY);
// Set title and create label text in window
set_window_title("Bouncing Balls Ver 1.0");
textout_ex(buffer, font, "Bouncing Balls Ver 1.0", 10, 20, WHITE, GRAY);
// Draw a background box
rectfill(buffer, 50, 50, scrX-50, scrY-50, BLUE);
// Create circles
Shape **GCir;
GCir = new Shape *[numCircles];
for(int i=0;i<numCircles;i++){
GCir[i] = new Circle;
GCir[i]->setRadius(random(1,25)); // THIS IS THE ERROR
}
while(!key[KEY_ESC]){
blit(buffer, screen, 0, 0, 0, 0, scrX, scrY);
}
destroy_bitmap(buffer);
return 0;
}
END_OF_MAIN();
int random(int low, int high)
{
return rand() % (high - low) + low;
}

The type of GCir[i] is Shape* and the Shape class doesn't have a setRadius method, Circle does. So either call setRadius on the Circle object before assign it to GCir[i] or just construct the Circle with the proper radius: GCir[i] = new Circle(random(1,25));

hammer fix :
GCir[i]->setRadius(random(1,25));
should be changed to
((Circle*)GCir[i])->setRadius(random(1,25));
Deeper problems:
you need virtual destructor on BaseClass
a better way to do it is to take the radius in the Circle class constructor.
then either use Shape::draw() as a virtual function to specify shape drawing or implement Shape::getType() and use a switch case to determine drawing logic after proper casting.

Compiler said it. You have an array of Shapes on which you try to call setRadius which is only defined for Circles. You can only call shape methods without casting Shape poonter to circle.

Related

C++ SFML sprite not showing/working

I am trying to make a program so that a red block falls from the top of the screen and the player has to avoid it. When I run the program I can see and move the player(paddle) but I can't see the enemy or "thing" sprite. Please help me because I have tried everything with the code and it still doesn't work :/
things.h
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
#pragma once
class Thing
{
public:
void thingspawn(RenderWindow &gameDisplay, int &enemystartx, int &enemystarty, int &enemywidth, int &enemyheight, int &blockcolor)
{
RectangleShape thing(Vector2f(enemywidth, enemyheight));
thing.setFillColor(Color(blockcolor));
thing.setPosition(enemystartx, enemystarty);
gameDisplay.clear();
gameDisplay.draw(thing);
}
};
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "things.h"
using namespace std;
using namespace sf;
int main()
{
RenderWindow gameDisplay(VideoMode(1366, 768), "Game", Style::Fullscreen);
gameDisplay.setMouseCursorVisible(false);
gameDisplay.clear();
int enemystarty = -200;
int enemystartx = 300;
int enemyheight = 100;
int enemywidth = 100;
int enemyspeed = 0.3f;
int enemycount = 1;
int dodged = 0;
int blockcolor = (255, 0, 0);
RectangleShape player(Vector2f(300, 30));
player.setFillColor(Color(0, 0, 255));
player.setPosition(400, 728);
while (gameDisplay.isOpen())
{
Event evnt;
while (gameDisplay.pollEvent(evnt))
{
switch (evnt.type)
{
case Event::Closed:
gameDisplay.close();
case Event::KeyPressed:
if (Keyboard::isKeyPressed(Keyboard::Q))
gameDisplay.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::Right))
if (player.getPosition().x < 1000)
player.move(0.2f, 0.0f);
if (Keyboard::isKeyPressed(Keyboard::Left))
if (player.getPosition().x > 50)
player.move(-0.2f, 0.0f);
Thing thingobject;
thingobject.thingspawn(gameDisplay, enemystartx, enemystarty, enemywidth, enemyheight, blockcolor);
enemystarty += enemyspeed;
gameDisplay.draw(player);
gameDisplay.display();
}
}
The problem is that you create RectangleShape thing in function Thing::thingspawn(...) and it is destroyed after it ends.
Try to put declaration of it as member of the Thing class
The thing is rendering correctly but in just a bad habit way. like what capi1500 said,
But the real problem is that int blockcolor = (255, 0, 0); results a black color when you pass it to the sf::Color. you wont see a black drawing in a black canvas right?
You can see here on how to construct a color from an integer: How to pack ARGB to one integer uniquely?
for now change the color of the thing using the predefined colors of sf::Color like sf::Color::Green or pass each component using the another constructor Color (Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha=255)
SFML's sf::Color's documentation: http://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Color.php

How to use of "new" for polygons in C++

The definitions of Circle and Polygon are here in Graph.h and graph.cpp.
For some exercise I need to have some unnamed shapes which are made using the new keyword. Both Circle and polygon are kinds of Shape.
For example if I have a vector_ref<Circle> vc; I can using this statement add an unnamed Circle into that vector: vc.push_back(new Circle (Point (p), 50)); because I can supply parameters (which are a point and a radius) of a circle when defining it.
But for polygons the subject is different.
For having a polygon I must declare it first, e.g., Polygon poly; then add points to it, this way, poly.add(Point(p));. Now it has caused a problem for me.
Consider I have a vector of polygons, Vector_ref<Polygon> vp; Now how to add (that is push back) a polygon using the new keyword just like I did for circle please?
My code is this:
#include <GUI.h>
using namespace Graph_lib;
//---------------------------------
class Math_shapes : public Window {
public:
Math_shapes(Point, int, int, const string&);
private:
//Widgets
Menu menu;
Button quit_button;
In_box x_coor;
In_box y_coor;
Vector_ref<Circle> vc;
Vector_ref<Graph_lib::Rectangle> vr;
Vector_ref<Graph_lib::Polygon> vt;
Vector_ref<Graph_lib::Polygon> vh;
//Action fucntions
void circle_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
vc.push_back(new Circle (Point(x,y), 50));
attach(vc[vc.size()-1]);
redraw();
}
void square_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
vr.push_back(new Graph_lib::Rectangle (Point(x,y), Point(x+100,y+100)));
attach(vr[vr.size()-1]);
redraw();
}
void triangle_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
vt.push_back(new Graph_lib::Polygon); // Problem is here!!
attach(vt[vt.size()-1]);
redraw();
}
void hexagon_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
Graph_lib::Polygon h;
h.add(Point(x,y)); h.add(Point(x+50,y+50)); h.add(Point(x+50,y+80));
h.add(Point(x,y+100)); h.add(Point(x-50,y+80)); h.add(Point(x-50,y+50));
vh.push_back(h);
attach(vh[vh.size()-1]);
redraw();
}
void quit() { hide(); }
// Call-back functions
static void cb_circle (Address, Address pw) { reference_to<Math_shapes>(pw).circle_pressed(); }
static void cb_square (Address, Address pw) { reference_to<Math_shapes>(pw).square_pressed(); }
static void cb_triangle (Address, Address pw) { reference_to<Math_shapes>(pw).triangle_pressed(); }
static void cb_hexagon (Address, Address pw) { reference_to<Math_shapes>(pw).hexagon_pressed(); }
static void cb_quit (Address, Address pw) { reference_to<Math_shapes>(pw).quit(); }
};
//----------------------------------------------------------------------------------
Math_shapes::Math_shapes(Point xy, int w, int h, const string& title):
Window(xy, w, h, title),
menu (Point(x_max()-150,70),120,30,Menu::vertical, "MathShapes"),
quit_button (Point(x_max()-100, 20), 70,20, "Quit", cb_quit),
x_coor(Point(x_max()-450,30),50,20,"x coordinate: "),
y_coor(Point(x_max()-250,30),50,20,"y coordinate: ")
{
attach(x_coor);
attach(y_coor);
attach(quit_button);
menu.attach(new Button(Point(0,0),0,0,"Circle",cb_circle));
menu.attach(new Button(Point(0,0),0,0,"Square",cb_square));
menu.attach(new Button(Point(0,0),0,0,"Equilateral triangle",cb_triangle));
menu.attach(new Button(Point(0,0),0,0,"Hexagon",cb_hexagon));
attach(menu);
}
//-------------------------------------------
int main()
try {
Math_shapes M_s(Point(100,100), 800, 600, "Math Shapes");
return gui_main();
}
catch(...)
{
return 0;
}
You simply need to hold a pointer to your polygon until you've put it in the conatiner:
Circle* pPoly = new Polygon();
// ...
pPoly->add(Point(p1));
// ...
pPoly->add(Point(p2));
// ...
vc.push_back(pPoly);
you probably want to use smart pointers rather than raw ones as above but this is where you can start.
Have you tried this
void triangle_pressed() {
int x = x_coor.get_int();
int y = y_coor.get_int();
Polygon *poly = new Polygon();
poly.add(Point(x));// add your points, I don't know if these are the right points
poly.add(Point(y));// but have you tried this way? creating it, adding, then calling
vt.push_back(poly); // push back without the move
attach(vt[vt.size()-1]);
redraw();
Well, in order to use the new operator, you would need to create a Polygon constructor that takes vector<Point> or initializer_list<Point> as an argument.
The other way around would be to create a helper function, e.g.
-- note that this is really suboptimal, there may be much better solution using move semantics, etc. (or even variadic template function for the matter)
Polygon* make_polygon(initializer_list<Point>& points)
{
Polygon* poly = new Polygon();
for (auto point : points)
poly->Add(point);
return poly;
}
And then just call vp.push_back(make_polygon({p1, p2, ...});
Obviously you could change the function to work without pointers simply by removing them alongside with the new operator call, but then it wouldn't work with your vector_ref<Polygon> type. You would need to use vector<Polygon> instead, as I assume that vector_ref<T> is just a typedef for vector<T*>

C++ FLTK, creating a rounded box

The problem says:
Draw a box with rounded corners. Define a class Box, consisting of four lines and four arcs.
So I wrote the below code for that exercise:
#include <Simple_window.h>
Simple_window win(Point(100,100), 600,400, "semi-ellipse");
struct Box: Shape{
Box(Point p, int ww, int hh): w(ww), h(hh)
{ add(Point(p.x-ww,p.y-hh)); }
void d_l() const //creating 4 lines
{
Line hrz1 (Point(150,100), Point(400,100));
Line hrz2 (Point(150,300), Point(400,300));
Line ver1 (Point(507,150), Point(507,250));
Line ver2 (Point(41,150), Point(41,250));
win.attach(hrz1);
win.attach(hrz2);
win.attach(ver1);
win.attach(ver2);
}
void draw_lines() const //creating 4 arcs
{
fl_arc(point(0).x,point(0).y,w,h,30,90);
fl_arc(point(0).x,point(0).y,w,h,270,330);
fl_arc(point(0).x,point(0).y,w,h,90,150);
fl_arc(point(0).x,point(0).y,w,h,210,270);
}
private:
int w;
int h;
};
int main()
{
using namespace Graph_lib;
Box b(Point(100,100),100,50);
win.attach(b);
win.wait_for_button();
}
When I ran it I faced this exception:
Unhandled exception at 0x757FE9D7 (ole32.dll) in test.exe: 0xC0000005: Access violation reading location 0x00000004.
I know this refers to declaring Simple_window win(Point(100,100), 600,400, "semi-ellipse"); in global state. But I did that because I had to do it. The problem is that how to attach lines and also object (here b) to Simple_window win in either parts (main() function and also Box struct).
Looks like it is caused by the global creation of win. I have never run an FLTK program where anything graphical is created before main but I'm guessing that sometimes the graphics libs require some things to be in place so it is best to use them after main.
What can you do about it? If win is declared as a pointer and created inside main instead of outside main, then you won't get a crash.
...
Simple_window* win;
struct Box: Shape
{
...
win->...
...
}
int main()
{
win = new Simple_window(Point(100, 100), 600, 400, "semi-ellipse");
Box b ...
win->attach ...
win->wait ...
delete win;
}
You don't need to put anything in global space at all. You want to be passing information to the classes that need them as opposed to needing everything declared in global scope before you do anything with it.
What I write here modifies your Box class so that it also has a private member variable which is a pointer to a Simple_window class. The constructor is modified so that when you construct Box b you have to send it a pointer to the Simple_window in which it will be drawn. When you do this the pointer is assigned the pointer to the then declared Simple_Window win.
#include <Simple_window.h>
struct Box: Shape{
private:
int w;
int h;
Simple_window* window;
public:
Box(Point p, int ww, int hh,Simple_window* win_): w(ww), h(hh),window(win_)
{ add(Point(p.x-ww,p.y-hh)); }
void d_l() const //creating 4 lines
{
Line hrz1 (Point(150,100), Point(400,100));
Line hrz2 (Point(150,300), Point(400,300));
Line ver1 (Point(507,150), Point(507,250));
Line ver2 (Point(41,150), Point(41,250));
window->attach(hrz1);
window->attach(hrz2);
window->attach(ver1);
window->attach(ver2);
}
void draw_lines() const //creating 4 arcs
{
fl_arc(point(0).x,point(0).y,w,h,30,90);
fl_arc(point(0).x,point(0).y,w,h,270,330);
fl_arc(point(0).x,point(0).y,w,h,90,150);
fl_arc(point(0).x,point(0).y,w,h,210,270);
}
};
int main()
{
using namespace Graph_lib;
Simple_window* win = new Simple_window(Point(100,100), 600,400, "semi-ellipse");
Box b(Point(100,100),100,50,win);
win->attach(b);
win->wait_for_button();
}
Your lines are hard coded and independent of the constructor parameters. They could be drawn using directly the FLTK library, similarly with the arcs and placed inside the inherited from class Shape and overriden function void draw_lines() const. Then you don't need the window object to be pointer.
A possible implementation is:
Box::Box(Point p, int w, int h)
: width(w), height(h)
{
add(Point(p.x - w, p.y - h));
}
void Box::draw_lines() const
{
// draw lines with reduced length to adapt for the arcs
if (color().visibility())
{
// upper horizontal
fl_line(point(0).x + width/4, point(0).y, point(0).x + (3./4.) * width, point(0).y);
// lower horizontal
fl_line(point(0).x + width/4, point(0).y + height, point(0).x + (3./4.) * width, point(0).y + height);
// left vertical
fl_line(point(0).x, point(0).y + height/4, point(0).x, point(0).y + (3./4.)*height);
// right vertical
fl_line(point(0).x + width, point(0).y + height/4, point(0).x + width, point(0).y + (3./4.) * height);
}
// draw arcs
if(color().visibility())
{
fl_color(fill_color().as_int());
// upper left arc
fl_arc(point(0).x, point(0).y, width/2, height/2, 90, 180);
// upper right arc
fl_arc(point(0).x + width/2, point(0).y, width/2, height/2, 0, 90);
// down right arc
fl_arc(point(0).x + width/2, point(0).y + height/2, width/2, height/2, 270, 0);
// down left arc
fl_arc(point(0).x , point(0).y + height/2, width/2, height/2, 180, 270);
}
}

C calling a class function inside of another Class that is currently an Object

new here, so be gentle, I'm currently doing my Major Project for my course and, I'm not asking for homework to be done for me, i just can't wrap my head around a strange problem i am having and have not been able to find an answer for it, even on here. I'm using SDL for my Drawing.
I'm doing Object Orientated Programming with my Project or a "state Machine" (which sounds less painful in a newbies mind, believe me), and in the render part of my Class Game1.cpp i am trying to call a Draw Function of my Player Class, but for some unknown reason that i can not fathom, it just skips this function call completely.
I have no errors, i even used breakpoints to find out what was happening, but it just skipped it completely every time, it is drawing the screen black as well without fail. Any help as t why it is skipping this would be really appreciated.
I honestly feel like it's a simple rookie mistake, but any and all scrutiny is welcome of my code, anything i can do to better myself is appreciated.
Game1.cpp:
#include "Game1.h"
#include "PlayerCharacter.h"
Game1::Game1( World * worldObject )
{
//object setup
this->worldObject = worldObject;
setDone (false);
}
Game1::~Game1()
{
}
void Game1::handle_events()
{
//*******************************************
//**//////////////Call Input///////////////**
//*******************************************
//******Check for Keyboard Input*************
//******Check Keyboard Logic*****************
//******Check for Mouse Input****************
//The mouse offsets
x = 0, y = 0;
//If the mouse moved
if (SDL_PollEvent(&worldObject->event))
{
if( worldObject->event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = worldObject->event.motion.x;
y = worldObject->event.motion.y;
}
}
//******Check Mouse Logic********************
}
void Game1::logic()
{
//*******************************************
//**//////////Collision Detection//////////**
//*******************************************
//******Check Player Bullet Collision Loop***
//Check for collision with enemies
//Check for collision with bitmap mask (walls)
//******Check Enemy Bullet Collision Loop****
//Check for Collision with Player
//Check for collision with bitmap mask (walls)
}
void Game1::render()
{
//*******************************************
//**////////////////Drawing////////////////**
//*******************************************
//******Blit Black Background****************
SDL_FillRect(worldObject->Screen , NULL , 0xff000000);
//******Blit Bitmap Mask*********************
//******Blit Flashlight**********************
//******Blit Map*****************************
//******Blit Pickups*************************
//******Blit Bullets*************************
//******Blit Player**************************
&PlayerCharacter.Draw; // <----- Skips this line completely, no idea why
//******Blit Enemies*************************
//******Blit Blackened Overlay***************
//******Blit HUD*****************************
//******Flip Screen**************************
SDL_Flip(worldObject->Screen);
}
Game1.h
#ifndef __Game1_H_INLUDED__
#define __Game1_H_INLUDED__
#include "GameState.h"
#include "SDL.h"
#include "ImageLoader.h"
using namespace IMGLoader;
class Game1 : public GameState
{
private:
//Menu Image
World * worldObject;
SDL_Rect PauseMenu,Item1Tile,Item2Tile,Item3Tile;
/*bool bPauseMenu, bItem1Tile, bItem2Tile, bItem3Tile;
int ButtonSpace,ButtonSize;
float x,y;
int Alpha1,Alpha2;*/
//Clipping Window
//SDL_Rect sclip,dclip;
public:
//Loads Menu resources
Game1 (World * worldObject);
//Frees Menu resources
~Game1();
//Main loop functions
void handle_events();
void logic();
void render();
};
#endif
PlayerCharacter.cpp
#include "PlayerCharacter.h"
SDL_Rect psclip,pdclip;
PlayerCharacter::PlayerCharacter ( float X, float Y, float dX, float dY, float Angle, float Speed, bool Existance, int Height, int Width, int Health, int Shield, SDL_Surface* Player ):Characters ( X, Y, dX, dY, Angle, Speed, Existance, Height, Width, Health )
{
this->Player = Player;
this->Shield = Shield;
this->Player = load_image("image\Player1.png");
}
void PlayerCharacter::setShield ( int Shield )
{
this->Shield = Shield;
}
int PlayerCharacter::getShield ( void )
{
return Shield;
}
void PlayerCharacter::Draw( )
{
psclip.x = 0; psclip.y = 0; psclip.w = 64; psclip.h = 64;
pdclip.x = 640; pdclip.y = 318; pdclip.w = 64; pdclip.h = 64;
SDL_BlitSurface(Player, &psclip, worldObject->Screen, &pdclip);
}
PlayerCharacter.h
#ifndef __PlayerCharacter_H_INCLUDED__
#define __PlayerCharacter_H_INCLUDED__
#include "Characters.h"
class PlayerCharacter : public Characters
{
private:
int Shield;
SDL_Surface* Player;
World *worldObject;
public:
PlayerCharacter ( float X, float Y, float dX, float dY, float Angle, float Speed, bool Existance, int Height, int Width, int Health, int Shield, SDL_Surface* Player );
void setShield ( int Shield );
int getShield ( void );
void Draw ( );
};
#endif
The line
&PlayerCharacter.Draw; // <----- Skips this line completely, no idea why
is not actually a function call. It's an expression that take the address of the Draw function in the PlayerCharacter class and does nothing with it.
I'm actually kind of surprised it compiles without errors, or at least tons of warnings.
You need to create a PlayerCharacter object, and then call the function in the object.
&PlayerCharacter.Draw is not a function call. PlayerCharacter::Draw() is not a static class method, so you need a PlayerCharacter object to invoke this method on.
You have a class PlayerCharacter, which defines what a PlayerCharacter is and what can be done with it. But as far as I see, you don't have a single PlayerCharacter object, i.e. no player character. If you had one, let's call him pc, then you could draw him with pc.Draw(). For that, you would have to instantiate the class, e.g. via PlayerCharacter pc( ... ), with the ... replaced by some appropriate values for the multitude of constructor parameters you have there. (You really want a default constructor, initializing all those to zero or other appropriate "start" value...)

How to create an object inside another class with a constructor?

So I was working on my code, which is designed in a modular way. Now, one of my classes; called Splash has to create a object of another class which is called Emitter. Normally you would just create the object and be done with it, but that doesn't work here, as the Emitter class has a custom constructor. But when I try to create an object, it doesn't work.
As an example;
Emitter has a constructor like so: Emitter::Emitter(int x, int y, int amount); and needs to be created so it can be accessed in the Splash class.
I tried to do this, but it didn't work:
class Splash{
private:
Emitter ps(100, 200, 400, "firstimage.png", "secondimage.png"); // Try to create object, doesn't work.
public:
// Other splash class functions.
}
I also tried this, which didn't work either:
class Splash{
private:
Emitter ps; // Try to create object, doesn't work.
public:
Splash() : ps(100, 200, 400, "firstimage.png", "secondimage.png")
{};
}
Edit: I know the second way is supposed to work, however it doesn't. If I remove the Emitter Section, the code works. but when I do it the second way, no window opens, no application is executed.
So how can I create my Emitter object for use in Splash?
Edit:
Here is my code for the emitter class and header:
Header
// Particle engine for the project
#ifndef _PARTICLE_H_
#define _PARTICLE_H_
#include <vector>
#include <string>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "image.h"
extern SDL_Surface* gameScreen;
class Particle{
private: // Particle settings
int x, y;
int lifetime;
private: // Particle surface that shall be applied
SDL_Surface* particleScreen;
public: // Constructor and destructor
Particle(int xA, int yA, string particleSprite);
~Particle(){};
public: // Various functions
void show();
bool isDead();
};
class Emitter{
private: // Emitter settings
int x, y;
int xVel, yVel;
private: // The particles for a dot
vector<Particle> particles;
SDL_Surface* emitterScreen;
string particleImg;
public: // Constructor and destructor
Emitter(int amount, int x, int y, string particleImage, string emitterImage);
~Emitter();
public: // Helper functions
void move();
void show();
void showParticles();
};
#endif
and here is the emitter functions:
#include "particle.h"
// The particle class stuff
Particle::Particle(int xA, int yA, string particleSprite){
// Draw the particle in a random location about the emitter within 25 pixels
x = xA - 5 + (rand() % 25);
y = yA - 5 + (rand() % 25);
lifetime = rand() % 6;
particleScreen = Image::loadImage(particleSprite);
}
void Particle::show(){
// Apply surface and age particle
Image::applySurface(x, y, particleScreen, gameScreen);
++lifetime;
}
bool Particle::isDead(){
if(lifetime > 11)
return true;
return false;
}
// The emitter class stuff
Emitter::Emitter(int amount, int x, int y, string particleImage, string emitterImage){
// Seed the time for random emitter
srand(SDL_GetTicks());
// Set up the variables and create the particles
x = y = xVel = yVel = 0;
particles.resize(amount, Particle(x, y, particleImage));
emitterScreen = Image::loadImage(emitterImage);
particleImg = particleImage;
}
Emitter::~Emitter(){
particles.clear();
}
void Emitter::move(){
}
void Emitter::show(){
// Show the dot image.
Image::applySurface(x, y, emitterScreen, gameScreen);
}
void Emitter::showParticles(){
// Go through all the particles
for(vector<Particle>::size_type i = 0; i != particles.size(); i++){
if(particles[i].isDead() == true){
particles.erase(particles.begin() + i);
particles.insert(particles.begin() + i, Particle(x, y, particleImg));
}
}
// And show all the particles
for(vector<Particle>::size_type i = 0; i != particles.size(); i++){
particles[i].show();
}
}
Also here is the Splash Class and the Splash Header.
The second option should work, and I would start looking at compilation errors to see why it doesn't. In fact, please post any compilation errors you have related to this code.
In the meantime, you can do something like this:
class Splash{
private:
Emitter* ps;
public:
Splash() { ps = new Emitter(100,200,400); }
Splash(const Splash& copy_from_me) { //you are now responsible for this }
Splash & operator= (const Splash & other) { //you are now responsible for this}
~Splash() { delete ps; }
};
Well, I managed to fix it, in a hackish way though. What I did was create a default constructor, and move my normal Constructor code into a new function. Then I created the object and called the the new init function to set everything up.