Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Having trouble drawing zombies  (Read 2112 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Having trouble drawing zombies
« on: November 19, 2012, 01:32:34 am »
Hi there, I'm trying to draw a certain amount of zombies to the screen during a wave. My program runs, but whenever I switch states to the playing state, the program crashes (probably due to a pointer problem). Heres my code:

PlayState.hpp:
#include "Player.hpp"
#include "Background.hpp"
#include "Button.hpp"
#include "ZombieWave.hpp"

class GameEngine;

class PlayState : public GameState
{
public:
        PlayState(GameEngine& game, bool replace = true);

        void pause();
        void resume();

        void update();
        void draw();

private:
    sf::Event event;

    Player* player;
    ZombieWave zm;

    Background map;
    Background widthBorder1, widthBorder2;
    Background heightBorder1, heightBorder2;

    bool waveDone;
};

PlayState.cpp:

#include <memory>
#include <iostream>

#include "Engine.hpp"
#include "PlayState.hpp"
#include "Player.hpp"
#include "IntroState.hpp"

PlayState::PlayState(GameEngine& game, bool replace) : GameState(game, replace) {
    zm.loadZombies(5);
        std::cout << "PlayState Init" << std::endl;

    player = player->createPlayer();
    std::string playerFilepath = "Resources/headup.png";
    player->init(playerFilepath, 475, 300);

    std::string mapFilepath = "Resources/land.png";
    map.init(mapFilepath, 0, 0);

    std::string widthBorderpath = "Resources/widthborder.png";
    widthBorder1.init(widthBorderpath, 0, 0);

    std::string heightBorderpath = "Resources/heightborder.png";
    heightBorder1.init(heightBorderpath, 0, 0);

    widthBorder2.init(widthBorderpath, 0, 747);

    heightBorder2.init(heightBorderpath, 1004, -25);

    waveDone = false;
}

void PlayState::pause() {
        std::cout << "PlayState Pause" << std::endl;
}

void PlayState::resume() {
        std::cout << "PlayState Resume" << std::endl;
}

void PlayState::update() {
    if (!waveDone)
        zm.spawnZombies();

        while(m_game.screen.pollEvent(event)) {
                switch(event.type) {
            case sf::Event::Closed:
                m_game.quit();
                break;

            case sf::Event::KeyPressed:
                switch (event.key.code)
                {
                    case sf::Keyboard::Escape:
                        m_next = m_game.build<IntroState>(true);

                        break;

                    case sf::Keyboard::W:
                        player->kc.setKeyboardEvent(W);

                        if (!player->playerCollisionTest(widthBorder1.getSprite()))
                            player->movePlayer(event);

                        break;

                    case sf::Keyboard::A:
                        player->kc.setKeyboardEvent(A);

                        if (!player->playerCollisionTest(heightBorder1.getSprite()))
                            player->movePlayer(event);

                        break;

                    case sf::Keyboard::S:
                        player->kc.setKeyboardEvent(S);

                        if (!player->playerCollisionTest(widthBorder2.getSprite()))
                            player->movePlayer(event);

                        break;

                    case sf::Keyboard::D:
                        player->kc.setKeyboardEvent(D);

                        if (!player->playerCollisionTest(heightBorder2.getSprite()))
                            player->movePlayer(event);

                        break;
                }
                break;

            while (!waveDone)
                zm.moveZombies(player);
                }
        }
}

void PlayState::draw() {
        // Clear the previous drawing
        m_game.screen.clear();

    map.draw(m_game.screen, event);
    widthBorder1.draw(m_game.screen, event);
    widthBorder2.draw(m_game.screen, event);
    heightBorder1.draw(m_game.screen, event);
    heightBorder2.draw(m_game.screen, event);
    player->draw(m_game.screen, event);

        m_game.screen.display();
}

Zombie.hpp:

#include "Drawable.hpp"
#include "Player.hpp"

class Zombie : public Drawable
{
public:
    void init(std::string& filename, float x, float y) {};
    void init(std::string& filename);

    void draw(sf::RenderWindow&, sf::Event);
    sf::Sprite& getSprite();

    void move(Player*);
    void die() { dead = true; }
    void attack(Player*);

    void setHealth(int);
    void decreaseHealth(int);
    int getHealth() { return m_health; }

    sf::Vector2f getPosition() { return sprite.getPosition(); }

    bool isDead() { return dead; }

protected:
    bool dead;

    unsigned int m_health;
    unsigned int m_damage;
};

Zombie.cpp:

#include <iostream>

#include "Zombie.hpp"

void Zombie::init(std::string& filename)
{
    srand(time(NULL));

    float randX = rand() % -20 - 1;
    float randY = rand() % -20 - 1;

    texture.loadFromFile(filename);
    sprite.setTexture(texture);
    sprite.setPosition(randX, randY);

    dead = false;
}

void Zombie::draw(sf::RenderWindow& screen, sf::Event event)
{

}

void Zombie::move(Player* player)
{
    if (dead)
        std::cout << "Zombie is dead. Cannot move" << std::endl;
    else
        sprite.move(player->getPlayerCoords().x, player->getPlayerCoords().y);
}

void Zombie::attack(Player* player)
{

}

void Zombie::setHealth(int health)
{
    m_health = health;
}

void Zombie::decreaseHealth(int health)
{
    m_health -= health;
}

sf::Sprite& Zombie::getSprite()
{
    return sprite;
}

ZombieWave.hpp:

#include <vector>

#include "Zombie.hpp"

class ZombieWave
{
public:
    ZombieWave() {};
    ~ZombieWave()
    {
        zombies.clear();
        delete tempZombie;
    }

    void loadZombies(int numZombies);
    void spawnZombies();
    void moveZombies(Player* player);

    int getWaveNum() { return waveNum; }
    int getNumZombies() { return numZombies; }

private:
    int waveNum;
    int numZombies;

    std::vector<Zombie*> zombies;
    Zombie* tempZombie;
};

ZombieWave.cpp:

#include "ZombieWave.hpp"

void ZombieWave::loadZombies(int numZombies)
{
    for (int f = 0; f < numZombies; f++)
        zombies.push_back(new Zombie);
}

void ZombieWave::spawnZombies()
{
    for (unsigned int f = 0; f < zombies.size(); f++)
    {
        auto iter = zombies.begin();

        while (iter != zombies.end())
        {
            tempZombie = *iter;

            std::string tempZombieString = "Resources/zomheadleft.png";
            tempZombie->init(tempZombieString);

            numZombies++;
        }
    }

    tempZombie = NULL;
}

void ZombieWave::moveZombies(Player* player)
{
    for (unsigned int f = 0; f < zombies.size(); f++)
    {
        auto iter = zombies.begin();

        while (iter != zombies.end())
        {
            tempZombie = *iter;
            tempZombie->move(player);
        }
    }
}

Thanks for hearing me out :D
Current Projects:
Technoport

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Having trouble drawing zombies
« Reply #1 on: November 19, 2012, 08:09:00 am »
If you solve all your crashes by posting on a forum, you'll waste a huge amount of time ;)

You should rather learn how to use your debugger.
Laurent Gomila - SFML developer

kimci86

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: Having trouble drawing zombies
« Reply #2 on: November 19, 2012, 07:55:00 pm »
what about :
    player = player->createPlayer();
(in PlayState constructor)

player is uninitialized !

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Having trouble drawing zombies
« Reply #3 on: November 19, 2012, 09:46:11 pm »
Not to mention that you should try and locate where the problem is, when seeing that much code you usually prefer not to even read it. That's what a minimal and complete code sample is for, so that people won't be discouraged by enormous walls of stranger's code for a problem that may be in a line.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Having trouble drawing zombies
« Reply #4 on: November 20, 2012, 02:44:48 am »
Actually, player is initialized within PlayState.hpp. However, I found where the problem is so no further help is needed.
Current Projects:
Technoport