SFML community forums

Help => General => Topic started by: Dannehood on April 21, 2012, 06:01:24 pm

Title: Help needed: Creating "pointer array objects" (*object[])
Post by: Dannehood on April 21, 2012, 06:01:24 pm
In my game I want a canon to fire bullets once every second, im half way there but it's getting too complicated for me so I need some help. I made a small example of the bullet code. The code that has been commented causes the program to crash.

Here is the code

Declarations.h
Code: [Select]
#ifndef DECLARATIONS_H
#define DECLARATIONS_H

unsigned char level_1
(sf::RenderWindow &App,
sf::Image &Bullet_Bill,
class bullet *bulletlist[],
class bullet &bulletMember,
int &counter,
unsigned char &level);

class bullet
{
public:
    bullet(){shoot = true;}
    sf::Clock clock;
    sf::Sprite sprite;
    void setShoot(bool setShoot){shoot = setShoot;}
    bool getShoot(){return shoot;}
private:
    bool shoot;
};

#endif

Main.cpp

Code: [Select]
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"

using namespace std;

int main ()
{
    unsigned char level = 1;

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Bullet");
    App.SetFramerateLimit(90);

    sf::Image Bullet_Bill;
    Bullet_Bill.LoadFromFile("Bullet_Bill.png");

    int counter = 0;
    bullet *bulletlist[100];

    bullet bulletMember;

    while(App.IsOpened())
    {
        sf::Event Event;

        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed)
            App.Close();
            if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
            App.Close();
        }
        switch (level)
        {
            case 1:level_1 (App, Bullet_Bill, &bulletlist[counter], bulletMember, counter, level);
        }
    }
}

level_1.cpp

Code: [Select]
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"

using namespace std;

unsigned char level_1(sf::RenderWindow &App,
sf::Image &Bullet_Bill,
class bullet *bulletlist[],
class bullet &bulletMember,
int &counter,
unsigned char &level)
{
    if (bulletMember.clock.GetElapsedTime() > 1.0 && bulletMember.getShoot() == true)
    {
        cout << "Bang!" << counter << endl;
        bulletMember.setShoot(false);
        bulletlist[counter] = new bullet;
        bulletlist[counter]->sprite.SetImage(Bullet_Bill);
        bulletlist[counter]->sprite.SetPosition(App.GetWidth()/2, App.GetHeight()/2);
        counter ++;
    }
    if (bulletMember.getShoot() == false)
    {
        bulletMember.clock.Reset();
        bulletMember.setShoot(true);
    }
    /*if (bulletlist[counter]->sprite.GetPosition().x < 0 - bulletlist[counter]->sprite.GetSize().x)
    {
        delete bulletlist[counter];
        counter --;
    }*/
    //bulletlist[counter]->sprite.Move(-150 * App.GetFrameTime(), 0);

    App.Clear(sf::Color(255,255,255,255));
    //App.Draw(bulletlist[counter]->sprite);
    App.Display();
    return level;
}

I appreciate all the help I get becuase right now this is frying my brain.
Title: Re: Help needed: Creating "pointer array objects" (*object[])
Post by: Nexus on April 22, 2012, 04:26:24 am
Forget arrays and use STL containers like std::vector instead. You should also learn how to work with iterators.

Have you got a good book like the C++ primer? Because this things are more difficult to learn on the internet, often important background knowledge is omitted.
Title: Re: Help needed: Creating "pointer array objects" (*object[])
Post by: Dannehood on April 22, 2012, 09:25:04 am
I have watched a few videos of the stl containers, vector, map, list, set but I nevered used any of them before so I would be very happy if you could give me an example,