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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - The Terminator

Pages: 1 [2] 3
16
Window / Checking if keys aren't being pressed
« on: February 06, 2013, 11:56:59 pm »
Hi there, I'm trying to implement inertia in my game (character speeds up after a certain amount of time). I'm trying to make a if statement that checks to see if any of the following keys are being pressed: W, A, S, D. If none are being pressed, restart the clock that measures the amount of time the player has been moving.

    if (!sf::Keyboard::isKeyPressed(sf::Keyboard::W) &&
        !sf::Keyboard::isKeyPressed(sf::Keyboard::A) &&
        !sf::Keyboard::isKeyPressed(sf::Keyboard::S) &&
        !sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        std::cout << "The user is pressing a key." << std::endl;
        inertiaClock.restart();
    }

Whenever a move function that houses this if statement gets called, the if statement isn't even executed. It automatically goes that fastest it can in this function, which would be an inertia multiplier of 5

void Player::addInertia()
{
    if (inertiaClock.getElapsedTime().asSeconds() <= 1.0f)
        inertiaValue = 1.0f;
    else if (inertiaClock.getElapsedTime().asSeconds() >= 1.0f)
        inertiaValue = 3;
    else
        inertiaValue = 5; // Speed automatically goes here
}
Any help is appreciated, thanks

17
General / std::unique_ptr on Mac
« on: January 28, 2013, 07:45:12 pm »
Hi there, I've finally been able to link SFML 2.0 on my macbook pro. I'm trying to load my code that I've written on my Windows machine, but it seems that Xcode doesn't like std::unique_ptr. In fact, that's the only thing that Xcode is reporting as wrong.

My specs:
OS X 10.8.2
Macbook Pro
Xcode 4.5.2

Is there a bug or something with SFML? I can use std::unique_ptr on regular console applications. My code runs fine on my windows machine. Help is greatly appreciated.

Thanks

18
Graphics / Switching in between backgrounds
« on: January 06, 2013, 03:11:25 pm »
Hi, I'm trying to make my program that I'm making for school change backgrounds every five seconds. My code compiles just fine, but the program crashes immediately. I've managed to debug it enough to where I know what's causing the problem, but I can't figure out how. Heres my code:

BackgroundManager.hpp:

#include <memory>
#include <iostream>
#include <sstream>

#include "Background.hpp"
#include "GameState.hpp"

class BackgroundManager //manages the initialization and switching of different backgrounds
{
public:
    BackgroundManager() : resourceFilepath("Resources/") {}; //sets resourcefilepath equal to Resources/
    ~BackgroundManager() {};

    void initBackgrounds(); //initializes backgrounds
    void switchBackgrounds(sf::RenderWindow& screen, int state); //function to switch between backgrounds
    static std::unique_ptr<Background> createBackground() { return std::unique_ptr<Background>(new Background); }

private:
    std::unique_ptr<Background> background;

    int randBackgroundNum;

    const std::string& resourceFilepath; //const reference to a string for the resource file path

    std::ostringstream backgroundNum; //a stream used to collect background numbers and initialize them

    sf::Clock clock; //clock for measuring team in between backgrounds
};

BackgroundManager.cpp:

#include "BackgroundManager.hpp"

void BackgroundManager::initBackgrounds()
{
    srand(time(NULL)); //seed random number

    try
    {
         background = createBackground();
         std::cout << "Successfully created a background." << std::endl;
    }

    catch (std::bad_alloc& ba)
    {
        std::cerr << ba.what() << " at background manager" << std::endl;
    }

    catch (...)
    {
        std::cout << "An unknown exception occured." << std::endl;
    }
}

void BackgroundManager::switchBackgrounds(sf::RenderWindow& screen, int state)
{
    switch (state)
    {
        case 0: //Case introstate
            randBackgroundNum = rand() % 5; //get a number 0 - 5
            break;
        case 1: //Case historystate
            randBackgroundNum = rand() % 10 + 6; //get a number 6 - 10
            break;
    }

    backgroundNum << randBackgroundNum; //place the randBackgroundNum in a stream for file usage

    background->init(resourceFilepath + backgroundNum.str() + ".jpg"); //initialize background with given random filepath >>>>>>PROBLEM IS HERE THAT CAUSES CRASH <<<<<<

    clock.restart(); //restart the clock

    while (clock.getElapsedTime().asSeconds() < 5.0f) //while 5 seconds haven't passed, keep drawing current background
        screen.draw(background->getSprite());
}

19
General / Problems setting up SFML 2.0 on Xcode 4.5.2
« on: January 02, 2013, 06:32:57 pm »
Hi there, I'm trying to configure SFML 2 on the newest version of Xcode (4.5.2) on my Macbook Pro. The templates and files load fine using the .pkg executable, but when I create a new project using the template, Xcode complains that it can't find SFML/Graphics along with all the other modules. I followed the instructions of the tutorial, but I can't seem to figure out what the problem is. Any help is appreciated.

20
Audio / Keeping a single song playing throughout different states
« on: December 03, 2012, 01:25:37 am »
I'm having a bit of trouble regarding music. I want a single song to keep playing throughout my whole states. I have attempted to use getPlayingOffset to get the current time, and then pausing it temporarily until it switches states, in which another method will resume the music. However, when I attempt to do this, the music refuses to play and spits out "Failed to play audio stream: sound parameters have not been initialized (call initialized first). Any help explaining the error is much appreciated.

21
General / [SOLVED]Having lots of trouble with sf::Clock and sf::Time
« on: December 01, 2012, 03:35:11 pm »
I'm trying to measure a certain amount of time to be used by a recoil method with doesn't allow a gun to shoot again until the time is past. When I try to do this, it prints out odd numbers to the console and doesn't work at all.

bool Gun::recoil(float time)
{
    sf::Clock recoilTimeClock;
    sf::Time Timer;

    Timer = recoilTimeClock.getElapsedTime();

    std::cout << Timer.asSeconds() << std::endl;

    recoilTimeClock.restart();
}

When I do this, it prints out extremely weird numbers that don't correlate with the program in any way. A picture is attached below.

[attachment deleted by admin]

22
Graphics / 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

23
Feature requests / Special keyboard events
« on: November 11, 2012, 03:25:38 pm »
I was working on my game, and I thought special keyboard events might be a good idea for SFML. For example, let's say we had a character on a map. This character walked normally using wasd keys. Now, if we wanted to make the character run, we had to double click either of the wasd keys within a certain amount of time. I think it would be nice if SFML handled this :). Thanks for hearing me out

24
I was wondering if there was a way to be able to delete a single sprite from the screen instead of clearing the whole screen then retexturing it with everything except that specific sprite. Let's say we were to have a shotgun with 8 shells and every time it was shot a shell would disappear from the ammo menu. It would seem awfully wasteful to clear the screen EVERY single time a shot went off.

25
SFML projects / Survive and Thrive
« on: November 06, 2012, 03:02:46 am »
Intro:
Hello all, I'm here to introduce one of my big projects called Survive and Thrive. Survive and Thrive is a 2D zombie game that is based on waves. Every wave zombies are spawned in increments depending on the wave. The objective of the game is to survive and thrive  :P. You must kill zombies in order to gain cash to further advance your character.

When are updates coming?
Updates will be shoveled out depending on how much time I have. Expect to see more updates during the weekend than the week. (high school for you)

Shout outs:
A huge shout out to eXpl0it3r as I use some of his code for the state engine.

Conclusion:
Anyways, thanks for looking at my up and coming project! I welcome CONSTRUCTIVE criticism and bug reports. A heads up for the people who download this version, it's only v0.1 so most of the main menu doesn't work. (yet) If you are interested in becoming a part in this project, message me as I need an artist for sprites and images. Thanks once again for reading!

Download Link:
http://www.mediafire.com/?z8wzlptuj1js5kz

26
Graphics / Trouble detecting sprite's global position
« on: November 05, 2012, 11:32:09 pm »
Hi there, I'm having some difficulties trying to detect if the mouse is hovered over and clicking on the sprite. I want it to change sprites once it's hovered over, then change states once it is clicked on. The only thing I'm having trouble on is the mouse. Here's my code that compiles but doesn't seem to work:

void IntroState::update() {
    sf::Event event;

        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_game.quit();

                    break;
                }

                break;

            break;

            case sf::Event::MouseButtonPressed:
                if (s_playButton.getGlobalBounds().contains(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y))
                {
                    m_game.screen.draw(s_playButtonActive); //replace current button with a highlighted one since it is being hovered over

                    if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) // if the playbutton is being hovered over AND the left mouse button is being clicked
                    {
                        //do stuff
                    }
                }

                break;
                }
        }
}

I appreciate any help. Thanks!

27
General / What would be most efficient?
« on: October 19, 2012, 07:52:00 pm »
I'm creating some tilemap support for my engine, and I can't really decide which two ways to go about it. Here's two ways I thought of: (P.S. I plan to load the tilemap from an external file such as a .txt)

1:
Each tilemap would be represented as a child class that would be derived from a base class called TileMap. To draw this tilemap when the character does something such as reaches the edge of the map, you would pass a reference to the object of the child class and it would draw that specific class. A camera would not be needed as the certain tilemap would be seen as a whole on the screen.

2:
Have 1 huge tilemap that periodically draws itself when the character moves in a certain direction. Once the character has reached the edge, it will collide and stop. A camera will be needed because the tilemap would need to scroll in the direction that the character is moving. There would only be one class called TileMap with methods such as void drawMap(), etc.

It would be helpful for some advice to pursue this task. Thanks in advance!

28
Audio / Music won't work at all
« on: September 23, 2012, 10:40:26 pm »
Hi, on my game the sf::Music class doesn't seem to be working at all. I get no errors, and my path to the file is correct, but no music is being played. Here's some of my code:

//Main music
sf::Music m_mainMusic;

//Load mainMusic into memory and set some propertie    
m_mainMusic.openFromFile("Resources/japanesemusic.ogg");    
m_mainMusic.setVolume(50);  
m_mainMusic.setLoop(true);    
m_mainMusic.play();

It won't work at all, and I tried using sf::Sound and soundbuffer as well and they didn't work either. Any suggestions?

29
General / Problems with Entity Manager using std::unique_ptr
« on: August 22, 2012, 10:59:24 pm »
Ok, I'm trying to create an entity manager for my engine, and I'm encountering a problem that I can't seem to figure out. Here's my code for EntityManager.hpp:

#ifndef ENTITYMANAGER_H
#define ENTITYMANAGER_H

#include <memory>
#include <vector>

class Entity;

class EntityManager {
public:
    void createEntity(std::unique_ptr<Entity>);
    void destroyEntity(std::unique_ptr<Entity>);
    void destroyAllEntities() { entities.clear(); }

private:
    std::vector<std::unique_ptr<Entity> > entities;
};

#endif // ENTITYMANAGER_H

And it's corresponding cpp file:

#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>

#include "EntityManager.hpp"
#include "Entity.hpp"

void EntityManager::createEntity(std::unique_ptr<Entity> entity) {
    entities.push_back(entity);
}

void EntityManager::destroyEntity(std::unique_ptr<Entity> entity) {
    bool entityFound = binary_search(entities.begin(), entities.end(), entity);

    if (entityFound) {
        auto iter = find(entities.begin(), entities.end(), entity);
        entities.erase(iter, entities.end());
    }

    else
        std::cout << "Could not find entity to destroy!" << std::endl;
}

When I try to compile this, it gives me an error in actual c++ files like unique_ptr.h, etc. I can't really post the errors because it's reaaaaally long but what I do know is that the problem is due to me trying to push a unique_ptr into my entities vector. I have also done the same thing with raw pointers to my Entity class (Entity*) and it worked fine, but I don't like dealing with raw pointers because they can get really annoying sometimes (destroying them correctly, etc) and it's just easier to use smart pointers. I hope you guys can help me and thank you for your time  :D

30
General / Does SFML support MySQL?
« on: August 15, 2012, 01:19:06 am »
I need to use a MySQL database to check username and passwords for my game, does sfml 2 support this?

Pages: 1 [2] 3