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 - MrSnappingTurtle

Pages: [1]
1
Graphics / Subtract Polygons From Polygons(Create Concave Shape)
« on: February 18, 2015, 08:40:18 pm »
Hi,
I am trying to create a concave shape. As SFML does not support concave shapes, I can create a "hull" that encompasses the concave shape. I then look at the points that are not on the hull and create a triangle consisting of the point, a point on the hull to the left, and a point on the hull to the right. This will make a triangle that is the hole (the concave part of the polygon). Once I have all the holes, I want to subtract them from the "hull" and get my final polygon. How can I do the subtracting step? Is that even possible?

As a side note, I would then draw that shape to a sf::RenderTexture which  I would apply a light/glow fragment shader in order to create dynamic shadow light. Is that the proper way to do such a thing?

If anyone is wondering, this is what I am trying to create the polygon for: http://ncase.me/sight-and-light/

Thanks!

2
Graphics / [SOLVED] "True" global bounds (or position) of text
« on: December 24, 2014, 08:01:58 am »
Hey guys!

Hope you don't find my recent flood of questions.  :P

Anyways, I was wondering if there is a way to get the "true" global bounds of an sf::Text. What I mean is that when I use the following code, the rectangle doesn't cover the text completely:

playText_.setOrigin(sf::Vector2f(playText_.getLocalBounds().width / 2.0f, playText_.getLocalBounds().height / 2.0f));

selectionRect_.setSize(sf::Vector2f(playText_.getGlobalBounds().width, playText_.getGlobalBounds().height));
selectionRect_.setOrigin(sf::Vector2f(selectionRect_.getSize().x / 2.0f, selectionRect_.getSize().y / 2.0f));
selectionRect_.setPosition(playText_.getPosition());




Do you guys see what I mean? Is there any way around this issue?

EDIT: Now that I look at it, it looks like it is just offset to the top left... However, the origins are at the center so is there any way that can happen?

3
Graphics / Access violation on certain attributes of sf::Text [C++]
« on: December 24, 2014, 03:30:46 am »
Hey,

So this time hopefully my issue isn't as stupid as me adding an extra semi-colon, but when I try to get the global bounds of an sf::Text object, I get an access violation:

First-chance exception at 0x0FD2E41F (sfml-graphics-d-2.dll) in Survive.exe: 0xC0000005: Access violation reading location 0xCCCCCD24.

The violation happens in the if statement when I use the .contains method(). I know it happens because of the .getGlobalBounds() method because if I try std::cout'ing that value's width, height, etc, it will crash there instead.

Here are my .h and .cpp files. The relevent .h portion is the  //------Menu---- and //=====Main Screen==== section. As for the .cpp, the menu constructor is the one that loads. As an added note, even though I change the character size, Visual Studio is reporting it as '0' in the debug window at the time of the crash. Thanks!

.h

#ifndef GUIMANAGER_H
#define GUIMANAGER_H
#include <SFML/Graphics.hpp>
#include "Player.h"
class GUIManager
{
public:
        GUIManager(sf::RenderWindow*, Player*);
        GUIManager(sf::RenderWindow*);
        GUIManager();
        void update(const sf::Time&);

        enum menuType
        {
                main,
                settings,
                gameplay,
                audio,
                controls
        };

        //Getters
        sf::RectangleShape getHealthOutOf() const;
        sf::RectangleShape getHealthCurrent() const;
        sf::RectangleShape getReloadOutOf() const;
        sf::RectangleShape getReloadCurrent() const;
        sf::Text getTotalAmmo() const;
        bool isMenu() const;
        menuType getCurrentMenu() const;
        sf::RectangleShape getSelectionRect() const;
        sf::Text getSurviveTitleText() const;
        sf::Text getPlayText() const;
        sf::Text getSettingsText() const;
        sf::Text getQuitText() const;

       


private:
        sf::RenderWindow* pWindow_;
        sf::Font font_;
        bool menu_;
       

        //-----------------------------------MENU------------------------------------
        //The type of menu that is currently visible
        menuType currentMenu_ = main;
        //Rectangle that appears below selected button/text
        sf::RectangleShape selectionRect_;
        //Text that is shown at the bottom to describe what the setting does
        sf::Text descriptionText_;
        //The sizes of selectable buttons and title texts in percent
        int titleSize_ = 0.3f;
        int buttonSize_ = 0.1f;

        float titleOffset_ = 0.15f;
        //==================================Main Screen===================================

        //Text
        sf::Text surviveTitleText_;
        sf::Text playText_;
        sf::Text settingsText_;
        sf::Text quitText_;

        //====================================Settings====================================

        sf::Text settingsTitleText_;
        sf::Text audioText_;
        sf::Text gameplayText_;
        sf::Text fullscreenText_;

        //====================================GAMEPLAY====================================
        sf::Text gameplayTitleText_;
        sf::Text mapSizeText_;
        sf::Text difficultyMultiplierText_;



        //--------------------------------------Game--------------------------------------
        Player* pPlayer_;

        //Health
        sf::RectangleShape healthOutOf_;
        sf::RectangleShape healthCurrent_;
        sf::Vector2f healthOffset_ = sf::Vector2f(0.01f, 0.01f); //In percent of window size
        sf::Vector2f healthSize_ = sf::Vector2f(0.075f, 0.01f); //In percent of window size

        //Weapon
        sf::Text ammo_;
        sf::Vector2f ammoOffset_ = sf::Vector2f(0.015f, 0.015f);
        float ammoSize_ = 0.035f; //In percent

        //Reload
        sf::RectangleShape reloadOutOf_;
        sf::RectangleShape reloadCurrent_;
        sf::Vector2f reloadOffset_ = sf::Vector2f(0.01f, 0.01f);
        sf::Vector2f reloadSize_ = sf::Vector2f(0.075f, 0.01f);

};

#endif 

.cpp

#include "GUIManager.h"
#include <iostream>
#include "Humanoid.h"
#include "Player.h"
#include "Gun.h"

//HUD constructor
GUIManager::GUIManager(sf::RenderWindow* pWindow, Player* pPlayer)
        :pWindow_(pWindow), pPlayer_(pPlayer)
{


        if (!font_.loadFromFile("arial.ttf"))
                std::cout << "Failed to load font." << std::endl;

        //Health
        healthOutOf_.setFillColor(sf::Color(214, 34, 34));
        healthCurrent_.setFillColor(sf::Color(78, 213, 116));
        healthOutOf_.setSize(sf::Vector2f(3.0f, 3.0f));
        healthCurrent_.setSize(sf::Vector2f(3.0f, 3.0f));
        healthOutOf_.setOrigin(0.0f, 3.0f);
        healthCurrent_.setOrigin(0.0f, 3.0f);

        //Ammo
        ammo_.setFont(font_);
        ammo_.setColor(sf::Color::Black);

        //Reload
        reloadOutOf_.setFillColor(sf::Color(23, 24, 25));
        reloadCurrent_.setFillColor(sf::Color(141, 191, 215));
        reloadCurrent_.setSize(sf::Vector2f(3.0f, 3.0f));
        reloadOutOf_.setSize(sf::Vector2f(3.0f, 3.0f));
        reloadCurrent_.setOrigin(sf::Vector2f(3.0f, 3.0f));
        reloadOutOf_.setOrigin(sf::Vector2f(3.0f, 3.0f));
}

//Menu constructor
GUIManager::GUIManager(sf::RenderWindow* pWindow)
        :menu_(true), pWindow_(pWindow)
{


        if (!font_.loadFromFile("arial.ttf"))
                std::cout << "Failed to load font." << std::endl;
        selectionRect_.setFillColor(sf::Color(200, 200, 200, 100));
        selectionRect_.setPosition(sf::Vector2f(-10000.0f, -10000.0f));

        surviveTitleText_.setFont(font_);
        playText_.setFont(font_);
        settingsText_.setFont(font_);
        quitText_.setFont(font_);

        //Assigns all the strings to the texts
        surviveTitleText_.setString("Survive");
        playText_.setString("Play");
        settingsText_.setString("Settings");
        quitText_.setString("Quit");
        settingsTitleText_.setString("Settings");
        audioText_.setString("Audio");
        gameplayText_.setString("Gameplay");
        fullscreenText_.setString("Fullscreen: No");
        gameplayTitleText_.setString("Gameplay");
        mapSizeText_.setString("Map Size: 257");
        difficultyMultiplierText_.setString("Difficulty: Easy");

       
}

//Default - do not use
GUIManager::GUIManager()
{
}

void GUIManager::update(const sf::Time& dT)
{
        sf::Vector2f windowSize = pWindow_->getView().getSize();

        if (menu_)
        {
                if (currentMenu_ == main)
                {
                        surviveTitleText_.setCharacterSize((int)(titleSize_ * windowSize.y));
                        playText_.setCharacterSize((int)(buttonSize_ * windowSize.y));
                        settingsText_.setCharacterSize((int)(buttonSize_ * windowSize.y));
                        quitText_.setCharacterSize((int)(buttonSize_ * windowSize.y));
                        std::cout << settingsText_.getPosition().x;

                        surviveTitleText_.setPosition(windowSize.x / 2.0f, windowSize.y * titleOffset_);
                        playText_.setPosition(windowSize.x / 2.0f, windowSize.y * titleOffset_ + windowSize.y * 0.15f * 2);
                        settingsText_.setPosition(windowSize.x / 2.0f, windowSize.y * titleOffset_ + windowSize.y * 0.15f * 3);
                        quitText_.setPosition(windowSize.x / 2.0f, windowSize.y * titleOffset_ + windowSize.y * 0.15f * 4);

               
                        if (playText_.getGlobalBounds().contains((sf::Vector2f)sf::Mouse::getPosition(*pWindow_)))
                        {
                                selectionRect_.setSize(sf::Vector2f(playText_.getGlobalBounds().width, playText_.getGlobalBounds().height));
                                selectionRect_.setPosition(playText_.getPosition());
                        }
                        else if (settingsText_.getGlobalBounds().contains((sf::Vector2f)sf::Mouse::getPosition(*pWindow_)))
                        {
                                selectionRect_.setSize(sf::Vector2f(settingsText_.getGlobalBounds().width, settingsText_.getGlobalBounds().height));
                                selectionRect_.setPosition(settingsText_.getPosition());
                        }
                        else if (quitText_.getGlobalBounds().contains((sf::Vector2f)sf::Mouse::getPosition(*pWindow_)))
                        {
                                selectionRect_.setSize(sf::Vector2f(quitText_.getGlobalBounds().width, quitText_.getGlobalBounds().height));
                                selectionRect_.setPosition(quitText_.getPosition());
                        }
                }

        }
        else
        {
                sf::Vector2f currentWindowPos(pWindow_->getView().getCenter().x - windowSize.x / 2.0f, pWindow_->getView().getCenter().y - windowSize.y / 2.);
                //Health
                healthOutOf_.setScale(windowSize.x * healthSize_.x, windowSize.y * healthSize_.y);
                healthCurrent_.setScale(windowSize.x * healthSize_.x * (pPlayer_->getHealth() / 100.0f), windowSize.y * healthSize_.y);
                healthOutOf_.setPosition(currentWindowPos.x + windowSize.x * healthOffset_.x, currentWindowPos.y + windowSize.y - windowSize.y * healthOffset_.y);
                healthCurrent_.setPosition(currentWindowPos.x + windowSize.x * healthOffset_.x, currentWindowPos.y + windowSize.y - windowSize.y * healthOffset_.y);

                //Reload
                Gun gun = pPlayer_->getGuns().at(pPlayer_->getCurrentGunIndex());
                if (gun.isReloading())
                        reloadCurrent_.setScale(windowSize.x * reloadSize_.x * (gun.getCurrentReloadTime().asSeconds() / gun.getReloadTime()), windowSize.y * reloadSize_.y);
                else
                        reloadCurrent_.setScale(windowSize.x * reloadSize_.x * (gun.getCurrentBullets() / (float)gun.getBulletsPerMag()), windowSize.y * reloadSize_.y);

                reloadOutOf_.setScale(windowSize.x * reloadSize_.x, windowSize.y * reloadSize_.y);
                reloadCurrent_.setPosition(currentWindowPos.x + windowSize.x - windowSize.x * reloadOffset_.x, currentWindowPos.y + windowSize.y - windowSize.y * reloadOffset_.y);
                reloadOutOf_.setPosition(currentWindowPos.x + windowSize.x - windowSize.x * reloadOffset_.x, currentWindowPos.y + windowSize.y - windowSize.y * reloadOffset_.y);

                //Ammo
                ammo_.setString(std::to_string(pPlayer_->getGuns().at(pPlayer_->getCurrentGunIndex()).getCurrentBullets()) + " / " + std::to_string(pPlayer_->getGuns().at(pPlayer_->getCurrentGunIndex()).getTotalAmmo()));
                ammo_.setOrigin(ammo_.getLocalBounds().width / 2, ammo_.getLocalBounds().height / 2.0f);
                ammo_.setCharacterSize(0.75f * reloadOutOf_.getGlobalBounds().height);
                ammo_.setPosition(reloadOutOf_.getPosition().x - reloadOutOf_.getGlobalBounds().width / 2.0f, reloadOutOf_.getPosition().y - reloadOutOf_.getGlobalBounds().height / 2.0f);
        }
}

//Getters
sf::RectangleShape GUIManager::getHealthOutOf() const { return healthOutOf_; }
sf::RectangleShape GUIManager::getHealthCurrent() const { return healthCurrent_; }
sf::RectangleShape GUIManager::getReloadOutOf() const { return reloadOutOf_; }
sf::RectangleShape GUIManager::getReloadCurrent() const { return reloadCurrent_; }
sf::Text GUIManager::getTotalAmmo() const { return ammo_; }
bool GUIManager::isMenu() const { return menu_; }
sf::RectangleShape GUIManager::getSelectionRect() const { return selectionRect_; }
sf::Text GUIManager::getSurviveTitleText() const { return surviveTitleText_; }
sf::Text GUIManager::getPlayText() const { return playText_; }
sf::Text GUIManager::getSettingsText() const { return settingsText_; }
sf::Text GUIManager::getQuitText() const { return quitText_; }

4
Graphics / Font is failing to load
« on: December 23, 2014, 03:15:28 am »
Hey!

I have the following code:
if(!font_.loadFromFile("arial.ttf"));
        std::cout << "Failed to load font." << std::endl;

I have the following file structure:


The end result is one of pain and suffering(look at the console):


Does anyone know why this is happening? The font is in the same directory as the exe. Thanks!

5
Audio / Audio cuts out or doesn't play at all [c++]
« on: December 20, 2014, 10:02:54 pm »
Hi,
I am trying to set up my audio system, however it seems to cut out early or sometimes not play at all. Do you guys see anything wrong with my set up?

#ifndef SOUNDMANAGER_H
#define SOUNDMANAGER_H
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <vector>
class SoundManager
{
public:
    SoundManager();
    void playSound(const std::string&);
    void update(const sf::Time&);
private:
   
    //------------------Buffers-----------------------
    //Pistol
    sf::SoundBuffer bufferPistolOne_;
    sf::SoundBuffer bufferPistolTwo_;
    sf::SoundBuffer bufferPistolThree_;
   
    //Rifle
    sf::SoundBuffer bufferRifleOne_;
    sf::SoundBuffer bufferRifleTwo_;
    sf::SoundBuffer bufferRifleThree_;
    sf::SoundBuffer bufferRifleFour_;
   
    //Grass footsteps
    sf::SoundBuffer bufferGrassOne_;
    sf::SoundBuffer bufferGrassTwo_;
    sf::SoundBuffer bufferGrassThree_;
    sf::SoundBuffer bufferGrassFour_;
    sf::SoundBuffer bufferGrassFive_;
    sf::SoundBuffer bufferGrassSix_;
    sf::SoundBuffer bufferGrassSeven_;
   
    std::vector<sf::Sound> vSounds_;
   
   
   
   
};

#endif

 


#include "SoundManager.h"
#include <iostream>
SoundManager::SoundManager()
{
    //----------------Loads all of the sounds--------------
    //Pistol
    if(!bufferPistolOne_.loadFromFile("sounds/gun/pistol/pistol_1.ogg"))
        std::cout << "Failed to load: sounds/gun/pistol/pistol_1.ogg" << std::endl;
   
    if(!bufferPistolTwo_.loadFromFile("sounds/gun/pistol/pistol_2.ogg"))
        std::cout << "Failed to load: sounds/gun/pistol/pistol_2.ogg" << std::endl;

    if(!bufferPistolThree_.loadFromFile("sounds/gun/pistol/pistol_3.ogg"))
        std::cout << "Failed to load: sounds/gun/pistol/pistol_3.ogg" << std::endl;

    //Rifle
    if(!bufferRifleOne_.loadFromFile("sounds/gun/rifle/rifle_1.ogg"))
        std::cout << "Failed to load: sounds/gun/rifle/rifle_1.ogg" << std::endl;

    if(!bufferRifleTwo_.loadFromFile("sounds/gun/rifle/rifle_2.ogg"))
        std::cout << "Failed to load: sounds/gun/rifle/rifle_2.ogg" << std::endl;
   
    if(!bufferRifleThree_.loadFromFile("sounds/gun/rifle/rifle_3.ogg"))
        std::cout << "Failed to load: sounds/gun/rifle/rifle_3.ogg" << std::endl;

    if(!bufferRifleFour_.loadFromFile("sounds/gun/rifle/rifle_4.ogg"))
        std::cout << "Failed to load: sounds/gun/rifle/rifle_4.ogg" << std::endl;

   
    //Grass
    if(!bufferGrassOne_.loadFromFile("sounds/footstep/grass/grass_1.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_1.ogg" << std::endl;

    if(!bufferGrassTwo_.loadFromFile("sounds/footstep/grass/grass_2.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_2.ogg" << std::endl;

    if(!bufferGrassThree_.loadFromFile("sounds/footstep/grass/grass_3.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_3.ogg" << std::endl;

    if(!bufferGrassFour_.loadFromFile("sounds/footstep/grass/grass_4.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_4.ogg" << std::endl;

    if(!bufferGrassFive_.loadFromFile("sounds/footstep/grass/grass_5.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_5.ogg" << std::endl;

    if(!bufferGrassSix_.loadFromFile("sounds/footstep/grass/grass_6.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_6.ogg" << std::endl;

    if(!bufferGrassSeven_.loadFromFile("sounds/footstep/grass/grass_7.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_7.ogg" << std::endl;


}
void SoundManager::update(const sf::Time& dT)
{
    for(auto iSound = vSounds_.begin(); iSound != vSounds_.end();)
        if(iSound->getStatus() == sf::Sound::Stopped)
            iSound = vSounds_.erase(iSound);
        else
            ++iSound;
   
}
void SoundManager::playSound(const std::string& type)
{
    if(type == "pistol")
    {
        sf::Sound sound;
        int random = std::rand() % 3;
        if(random == 0)
            sound = sf::Sound(bufferPistolOne_);
        else if(random == 1)
            sound = sf::Sound(bufferPistolTwo_);
        else
            sound = sf::Sound(bufferPistolThree_);
       
        vSounds_.push_back(sound);
        vSounds_.at(vSounds_.size() - 1).play();
    }
    else if(type == "rifle")
    {
        sf::Sound sound;
        int random = std::rand() % 4;
        if(random == 0)
            sound = sf::Sound(bufferRifleOne_);
        else if(random == 1)
            sound = sf::Sound(bufferRifleTwo_);
        else if(random == 2)
            sound = sf::Sound(bufferRifleThree_);
        else
            sound = sf::Sound(bufferRifleFour_);
       
        vSounds_.push_back(sound);
        vSounds_.at(vSounds_.size() - 1).play();
    }
    else if(type == "grass")
    {

        sf::Sound sound;
        int random = std::rand() % 7;
        if(random == 0)
            sound = sf::Sound(bufferGrassOne_);
        else if(random == 1)
            sound = sf::Sound(bufferGrassTwo_);
        else if(random == 2)
            sound = sf::Sound(bufferGrassThree_);
        else if(random == 3)
            sound = sf::Sound(bufferGrassFour_);
        else if(random == 4)
            sound = sf::Sound(bufferGrassFive_);
        else if(random == 5)
            sound = sf::Sound(bufferGrassSix_);
        else
            sound = sf::Sound(bufferGrassSeven_);
   
       
        vSounds_.push_back(sound);
        vSounds_.at(vSounds_.size() - 1).play();
    }
}
 


Thanks! :D

6
Graphics / Help with white texture sprite issue
« on: December 03, 2014, 04:50:34 am »
Hi, I am trying to draw certain sprites, but they end up being drawn as white squares or even distorted. Everything that has been drawn as a white rectangle follows the following format:

Object
-Texture
-Sprite

Then the level has a list of objects.

It then returns the list by copy to the engine which draws it.

Here is the code for the trees:
http://pastebin.com/UmsapXJj

The end result:


Other items are having the same issue such as the gun. The tiles aren't and they are the only ones stored in a regular array. Could that have something to do with it?

Thanks!

7
Window / Viewport not changing and stays at (0, 0)
« on: August 16, 2014, 07:05:12 am »
Hi, I have the following code:

view.reset(sf::FloatRect(mainLevel->rooms[i]->roomSprite.getPosition().x, mainLevel->rooms[i]->roomSprite.getPosition().y, window.getSize().x, window.getSize().y));
window.setView(view);
std::cout << "Room Position: (" << mainLevel->rooms[i]->roomSprite.getPosition().x << ", " << mainLevel->rooms[i]->roomSprite.getPosition().y << ")" << std::endl;
std::cout << "Viewport: (" << window.getView().getViewport().left << ", " << view.getViewport().top << ")" << std::endl;
 

However, when I have the viewport position log, it is at (0, 0) when the room position is at (-75, -75). This happens even if I hardcode the (-75, -75) into it. Does anyone know why this happens? Thanks!

8
General / Does anyone else have massive frame drops for no reason?
« on: August 10, 2014, 02:14:23 am »
EDIT: So I just realized the way that I was calculating FPS was wrong :u However, I still get stutter even with the high framerates. I am using deltaTime, does anyone know what could be causing this?

Hi,

I am working on a game and am having an issue with my framerate dropping massively for no reason. And I'm not talking about just any framerate drop, I mean it will constantly go from 2000+ to 20 for a frame.This happens when something is happening or when nothing is happening at all. I'm pretty sure this is what is causing issues with jittery movement when using delta time, as well. Has anyone else had this issue?

Thanks!

9
Graphics / setTextureRect() not changing the sprite
« on: May 30, 2014, 07:42:02 pm »
Hi,

So in my header file for the player I have this code:

const std::array<sf::IntRect, 3> INTERACT
        {{
                sf::IntRect(11, 0, 11, 10),
                sf::IntRect(22, 0, 11, 10),
                sf::IntRect(11, 0, 11, 10)
        }};

And then in the source file I have this inside the update function:
 if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && animationState != interact)
        {
                 animationState = interact;

                 for (int i = 0; i < INTERACT.size(); i++)
                 {
                         sf::Clock frameTimer;
                         sprite.setTextureRect(INTERACT[i]);
                         std::cout << "Sprite: " << i << "should have changed..." << std::endl;
                         //Waits to iterate the next frame
                         while (frameTimer.getElapsedTime().asMilliseconds() < 100) {}
                 }

                 animationState = idle;
                 std::cout << "done!";
        }

Now, the problem is that the sprite.setTextureRect(INTERACT(i)) part does not work. The cout does run so I know that the function is being called. If I hold down the spacebar, however, it will eventually be showing just the last frame. Is there something obvious that I'm missing?

10
Graphics / Jittery Movement Even With deltaTime
« on: May 30, 2014, 04:49:01 am »
Hi,

I was creating the code to move my sprite and implemented deltaTime into it, yet the sprite still jitters often when moving around. Does anyone know what is wrong? Comment if you need more detail.

main.cpp
#include <SFML/Graphics.hpp>
#include "player.h"

int main()
{
        sf::RenderWindow window(sf::VideoMode(1280, 720), "Incremental");

        sf::Clock clock;
        player player;

        //Game Loop
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();
                player.update(clock.restart().asSeconds());
                window.draw(player.sprite);
                window.display();
                clock.restart();
        }

        return 0;
}

player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Window/Keyboard.hpp>


class player
{
private:

        // Clock and timer used to iterate between positions on the sprite sheet.
        sf::Clock fpsClock;
        sf::Time fpsTimer;

        sf::Texture spriteSheet;


        //Animation States
        enum AnimationState
        {
                idle,
                run,
                interact
        };

        AnimationState animationState = idle;
       
        //Used to check if sprite scale is already inversed when going left.
        bool spriteInvertable = true;
        //Movement
        sf::Vector2f position;
        float speed = 10000;

        //Scale Properties
        float spriteScaleX = 10, spriteScaleY = 10;

        /*Animation positions in the sprite sheet...
        Arrays are static and initialized elsewhere
        cause VS is a POS and I had to use a workaround.
        */

        const sf::IntRect IDLE = sf::IntRect(0, 0, 8, 10);
        static const sf::IntRect RUN[2];
        static const sf::IntRect INTERACT[4];

public:

        sf::Sprite sprite;
       
        //Animation Functions
        void setAnimationState(const AnimationState&);
        void animate();
        void update(const double&);

        player();
};

#endif
 

player.cpp
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <iostream>
#include "player.h"

const sf::IntRect player::RUN[2] =
{
        sf::IntRect(33, 0, 8, 10),
        sf::IntRect(44, 0, 8, 10)
};
const sf::IntRect player::INTERACT[4] =
{
        sf::IntRect(1, 0, 8, 10),
        sf::IntRect(9, 0, 8, 10),
        sf::IntRect(17, 0, 8, 10),
        sf::IntRect(9, 0, 8, 10)
};

void player::setAnimationState(const AnimationState& state)
{
        animationState = state;
}
void player::update(const double& deltaTime)
{
        //Debugz
        std::cout << deltaTime << std::endl;

        //Movement Update
        if
                (
                sf::Keyboard::isKeyPressed(sf::Keyboard::D) &&
                !sf::Keyboard::isKeyPressed(sf::Keyboard::A) &&
                animationState != interact
                )
        {
                //Moves the sprite
                sprite.move(speed * deltaTime, 0);

                setAnimationState(run);
                fpsTimer = fpsClock.getElapsedTime();

                //Animates by going through the texture (probably a better way to do this...)
                if (fpsTimer.asMilliseconds() <= 30 * 3)
                {
                        sprite.setTextureRect(RUN[0]);
                }
                else if (fpsTimer.asMilliseconds() <= 60 * 3 && fpsTimer.asMilliseconds() > 30 * 3)
                {
                        sprite.setTextureRect(RUN[1]);
                }
                else if (fpsTimer.asMilliseconds() >= 60 * 3)
                {
                        fpsClock.restart();
                }

                //Re-enables spriteInvertable and uninverts the image if it was inverted.
                if (!spriteInvertable)
                {
                        sprite.setScale(spriteScaleX, spriteScaleY);
                        spriteInvertable = true;
                }
        }
        else if
                (
                sf::Keyboard::isKeyPressed(sf::Keyboard::A) &&
                !sf::Keyboard::isKeyPressed(sf::Keyboard::D) &&
                animationState != interact
                )
        {
                //Moves the sprite
                sprite.move(-1 * speed * deltaTime, 0);
               
                setAnimationState(run);
                fpsTimer = fpsClock.getElapsedTime();

                //Animates by going through the texture (probably a better way to do this...)
                if (fpsTimer.asMilliseconds() <= 30 * 3)
                {
                        sprite.setTextureRect(RUN[0]);
                }
                else if (fpsTimer.asMilliseconds() <= 60 * 3 && fpsTimer.asMilliseconds() > 30 * 3)
                {
                        sprite.setTextureRect(RUN[1]);
                }
                else if (fpsTimer.asMilliseconds() >= 60 * 3)
                {
                        fpsClock.restart();
                }
               
                //Inverts Sprite
                if (spriteInvertable)
                {
                        sprite.setScale(-spriteScaleX, spriteScaleY);
                        spriteInvertable = false;
                }

        }
        else
        {
                sprite.setTextureRect(IDLE);
        }
}

player::player()
{
        //Loads up the texture and sets the sprite to it.
        spriteSheet.loadFromFile("../art/final/player.png");
        sprite.setTexture(spriteSheet);
        sprite.setOrigin(5, 8);
        //Default state
        sprite.move(0, 100);
        sprite.setTextureRect(IDLE);
        sprite.setScale(spriteScaleX, spriteScaleY);
}

THANKS!  ;)

Pages: [1]
anything