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.


Messages - GunnDawg

Pages: [1] 2
1
Graphics / Re: Sprite movement/animation question.
« on: December 30, 2014, 12:41:35 pm »
I'll look into cleaning the code up. And you're right that the movement function isn't doing what I think it is doing, hence why I created this post. I added a cout for each one and the console output shows that's the source.y IS changing, but the sprite it self, IS NOT.

2
Graphics / Sprite movement/animation question.
« on: December 30, 2014, 11:52:13 am »
Before I re-factored my code and created a player class, I had this working correctly. Now for whatever reason I cannot seem to get this working. You'll see in my Player.cpp file that based on players movement it will change the source.y to either Left, Right, Up, or Down which will/should call for the correct sprite in the sheet. Left would be left facing, Right would be right facing, etc, etc. I have not included the actual animation code yet, so that I am not worried about. Just curious as to why it's not properly changing and displaying the source.y. Player moves fine with the right mouse button but he stays facing right. Code is below. Thanks.

Player.cpp
#include "Player.h"

enum Direction { Up, Left, Down, Right };
float frameCounter = 0;
float switchFrame = 100;
float frameSpeed = 800;
sf::Vector2i source(0, Right);
sf::Vector2f totalMovement = sf::Vector2f();
sf::Vector2f mousePoint = sf::Vector2f();
sf::RenderWindow window;
bool totalMovementSet = false;

Player::Player()
{
        if (!playerTexture.loadFromFile("Sprites/player_sheet.png"))
        {
                std::cout << "Cannot find player Texture PNG file" << std::endl;
        }

        playerSprite.setTexture(playerTexture);
        playerSprite.setOrigin(sf::Vector2f(32, 64));
        playerSprite.setPosition(sf::Vector2f(400, 300));
        playerSprite.setTextureRect(sf::IntRect(source.x * 64, source.y * 64, 64, 64));
}

Player::~Player()
{

}

//Get class data.
void Player::update(float dt)
{

}

void Player::movement(sf::RenderWindow &window)
{
        if (sf::Mouse::isButtonPressed(sf::Mouse::Right))
        {
                totalMovementSet = true;
                totalMovement.x = sf::Mouse::getPosition(window).x - playerSprite.getPosition().x;
                totalMovement.y = sf::Mouse::getPosition(window).y - playerSprite.getPosition().y;

                mousePoint.x = sf::Mouse::getPosition(window).x;
                mousePoint.y = sf::Mouse::getPosition(window).y;
        }

        if (totalMovementSet == true)
        {
                float tanResult = atan2(totalMovement.y, totalMovement.x);
                double speed = 0.03;
                const double pi = 3.1415926535897;

                if ((tanResult > 3 * pi / 4 && tanResult < pi) || (tanResult < -3 * pi / 4 && tanResult > -pi))
                        source.y = Left;
                else if (tanResult > -pi / 4 && tanResult < pi / 4)
                {
                        source.y = Right;
                }
                else if (tanResult > -3 * pi / 4 && tanResult < -pi / 4)
                {
                        source.y = Up;
                }
                else if (tanResult > pi / 4 && tanResult < 3 * pi / 4)
                {
                        source.y = Down;
                }

                playerSprite.move(cos(tanResult)*speed, sin(tanResult)*speed);
        }

        if (playerSprite.getPosition().x > mousePoint.x - 10 && playerSprite.getPosition().x < mousePoint.x + 10 && playerSprite.getPosition().y > mousePoint.y - 10 &&
                playerSprite.getPosition().y < mousePoint.y + 10)
        {
                totalMovementSet = false;
        }
}

void Player::draw(sf::RenderWindow &window)
{
        window.draw(playerSprite);
}

std::string Player::getName()
{
        return Name;
}

int Player::getHealth()
{
        return newHealth;
}

int Player::getMana()
{
        return newMana;
}

//Set class data.
void Player::setName(std::string p_Name)
{
        Name = p_Name;
}

void Player::setHealth(int p_Health)
{
        newHealth = p_Health;
}

void Player::setMana(int p_Mana)
{
        newMana = p_Mana;
}

 

Player.h
#pragma once
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <string>
#include "SFML/Graphics.hpp"

class Player
{
public:
        Player();

        ~Player();

        std::string getName();
        int getHealth();
        int getMana();
        void update(float dt);
        void draw(sf::RenderWindow &window);
        void setName(std::string);
        void setHealth(int);
        void setMana(int);
        void movement(sf::RenderWindow &window);

private:
        std::string Name;
        int newHealth;
        int newMana;
        sf::Texture playerTexture;
        sf::Sprite playerSprite;
};

#endif
 

main.cpp
#include "SFML/Graphics.hpp"
#include "SFML/Window.hpp"
#include "Player.h"
#include <conio.h>
#include <iostream>
#include <string>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "Meet Ed");

        Player player1;

        sf::Clock clock;

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

                /*sf::Time time = clock.getElapsedTime();
                player1.update(time.asMilliseconds());
                clock.restart().asMilliseconds();*/


                window.clear(sf::Color(255, 255, 255));

                player1.draw(window);
                if (sf::Mouse::isButtonPressed(sf::Mouse::Right))
                {
                        player1.movement(window);
                }

                window.display();
        }
       
        return 0;
}
 

3
Graphics / Re: best way to handle this? (Player class question)
« on: December 26, 2014, 03:26:10 am »
Now I'm just confused. Are there examples out of there that I'm not finding that shows a standard player class for simple things like name, health, and sprite and/or texture use that I can look and to try and implement myself?

4
Graphics / Re: best way to handle this? (Player class question)
« on: December 26, 2014, 03:12:08 am »

2) The Player simply does not own a texture in any way, shape or form, instead relying on the client code to load, own and set the player's texture.  I believe this is the more commonly recommended solution, and it probably is the more architecturally sound option because it avoids the mutliple players problem I pointed out above, and managing resources is non-trivial but logically separate from the Player class.  Someday you may want a "proper" resource manager class for your textures that does the same thing for all Player/Enemy/Background textures.

So with this option I would simply not handle setting the player Texture and Sprite in the class but instead creating the sprite and assigning its texture outside of the scope of the class? All events after that would need to interact with the sprite itself, correct? So there would be no, Player1.move(); (Player1 being the object name), but instead would call the sprite it self to move? Then handle all health, mana, etc changes using the class functions (setHealth, setMana, etc)? Was trying to go for a full on way to have all events reference to the player object and not have to juggle between independent data outside of the class, but if this is the preferred method (without the use of a resource manager), then I suppose it'll do.

5
Graphics / best way to handle this? (Player class question)
« on: December 26, 2014, 02:55:26 am »
So I'm creating a player class that holds the players name, health, and mana. I figured this would be a good class to also hold the players sprite and texture to that sprite as well, though I'm starting to see where that might be an issue. Trying to wrap my head around the logical way to go about this. I'll leave my code here so you can see what I'm talking about.

Player.cpp
#include "Player.h"
#include "SFML/Graphics.hpp"

Player::Player(std::string p_Name, int p_Health, int p_Mana, sf::Texture p_Texture)
{
        Name = p_Name;
        newHealth = p_Health;
        newMana = p_Mana;
        newTexture = p_Texture;
}

Player::~Player()
{

}

//Get class data.
std::string Player::getName()
{
        return Name;
}

int Player::getHealth()
{
        return newHealth;
}

int Player::getMana()
{
        return newMana;
}

//Set class data.
void Player::setName(std::string p_Name)
{
        Name = p_Name;
}

void Player::setHealth(int p_Health)
{
        newHealth = p_Health;
}

void Player::setMana(int p_Mana)
{
        newMana = p_Mana;
}

void Player::setTexture(sf::Texture pTexture)
{
        newTexture = pTexture;
}

void Player::setSprite(sf::Sprite pSprite)
{
        Sprite = pSprite;
}
 

Player.h
#pragma once
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <string>
#include "SFML/Graphics.hpp"

class Player
{
public:
        Player();

        Player(std::string, int, int, sf::Texture);

        ~Player();

        std::string getName();
        int getHealth();
        int getMana();


        void setName(std::string);
        void setHealth(int);
        void setMana(int);
        void setSprite(sf::Sprite);
        void setTexture(sf::Texture);

private:
        std::string Name;
        int newHealth;
        int newMana;
        sf::Texture newTexture;
        sf::Sprite Sprite;
};


#endif
 

main.cpp
#include "SFML/Graphics.hpp"
#include "Player.h"
#include <conio.h>
#include <iostream>
#include <string>

int main()
{
        std::string name;
        int health = 0, mana = 10;
        sf::Texture DarthVader;
        sf::Texture pTexture;
        sf::Sprite mainChar;

        Player Player1(name, health, mana, pTexture);
        std::cin >> name;
        Player1.setName(name);
        Player1.setTexture(DarthVader);
        Player1.setSprite(mainChar);
       
        std::cout << Player1.getMana();
        std::cout << Player1.getName() << std::endl;
       
        return 0;
}
 


Now obviously having a player class function "setTexture" is going to cause a conflict (at least a think). Secondly, do I even need to set the sprite and the texture with a class function? The player will be able to choose to play from a few different characters, but does that require setting the sprite and texture for each one or should I just have a setTexture for the sprite? I'm a little confused on how to best go about this.

Any input is appreciated. Thank you and Merry Christmas!

6
General / Visual Studio 2015
« on: December 22, 2014, 02:28:43 am »
Currently using Visual Studio 2013 with SFML 2.2 and was wondering if it's possible to use Visual Studio 2015 Preview with SFML yet, and how.

Thanks.

7
General / SFML 2.2 change log question.
« on: December 21, 2014, 09:43:03 am »
In the change log for SFML 2.2 it says this...

"+* Added support for Visual Studio 2013 and proper support for the TDM builds (#482)"

What exactly does this mean? I've been using VS 2013 and SFML just fine before 2.2. Am I missing something here?

8
General / Re: Regarding structure.
« on: December 20, 2014, 12:32:25 pm »
Very well. I shall keep my mouse commands within the main.cpp and carry on. Thanks folks.

9
General / Regarding structure.
« on: December 20, 2014, 06:49:26 am »
Hello folks, I am pretty new to SFML and had a few questions in regards to the most common techniques for setting up games. I kind of made the mistake of not starting with a player class and am at the point where I want to roll a lot of my code into classes. Player and Input being two of them. So my question is do most people have a class just for input, and another class for player functions? Here is how I have my player class looking at the moment and am starting to second guess my self into making an input class for just mouse and keyboard input.

Player.cpp
Code: [Select]
#pragma once

#include "Player.h"
#include <SFML/Graphics.hpp>


Player::Player(const sf::Texture& imagePath) :
mSprite(imagePath),
mSource(1, Player::Down)

{
mSprite.setScale(1.5f, 1.5f);
}

Player::~Player()
{
// TODO Auto-generated destructor stub
}

void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(mSprite, states);
}

void Player::moveUp()
{
//mainChar.move(sf::Vector2f(0, -10));
}

void Player::moveDown()
{

}

void Player::moveLeft()
{

}

void Player::moveRight()
{

};

It's obviously ready to start going with player input but if making a separate class just for input is the more appropriate way to go, then I'll adjust accordingly.

Thanks for any and all input folks.

10
Graphics / Re: Problem with sprite movement.
« on: December 16, 2014, 12:03:24 am »
Simply removing the "&& totalMovementSet == false" should accomplish that.

Ah yes, that did the trick. Sorry for the beginner oversights. I still have issues comprehending what some stuff is doing and why. Appreciate the help guys. I've accomplished (with your guys help of course) what this thread was designed for :)

11
Graphics / Re: Problem with sprite movement.
« on: December 15, 2014, 10:45:46 pm »
You could use events instead to respond to mouse clicks if you wanted to only change the target when clicked but if you want the target to follow the mouse when dragging with the right button, your approach looks great (although you technically can use events for that too).

I'm glad you got it to do what you were looking for and didn't just give up. It sounded like you were going to do that when you wrote "I'll scrap this"  :P

I would rather it not follow the mouse on right click but instead go to where the mouse cursor is at when right clicking, which is does fine at the moment. I am now trying to get it to move to a different mouse location in the middle of its movement if case the player decides before he gets to the destination that he wants to go somewhere else. Take an RTS game for example. Lets say you accidentally start sending your army/units into the enemy base, you would be able to go "wow wait, no", and right click somewhere else. Trying to do that. At the moment once you right click somewhere, you're committed to moving there, not good!

12
Graphics / Re: Problem with sprite movement.
« on: December 15, 2014, 08:47:00 am »
Correct that is not what I want to happen. I'd like for them to be able to move where and when they want and not be locked out while moving from point A to point B


EDIT: I do use that event further down for like you said, to close. I only provided essential code for this.

13
Graphics / Re: Problem with sprite movement.
« on: December 15, 2014, 08:32:33 am »
So I've come up with this solution that seems to do the trick. I'll post my solution/code for you to see. Like I said "it works" though I'm not sure if this is the proper or most efficient way to do something like this. Can it be done easier? Will this bite me in the rear later on? Any input is appreciated.

        while (window.isOpen())
        {
                sf::Event event;

                if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && totalMovementSet == false)
                {
                        totalMovementSet = true;
                        totalMovement.x = sf::Mouse::getPosition(window).x - darthVader.getPosition().x;                       
                        totalMovement.y = sf::Mouse::getPosition(window).y - darthVader.getPosition().y;

                        mousePoint.x = sf::Mouse::getPosition(window).x;
                        mousePoint.y = sf::Mouse::getPosition(window).y;
                }

                if (totalMovementSet == true)
                {
                        darthVader.move(totalMovement * (1.f / 8000.f));
                }

                if (darthVader.getPosition().x > mousePoint.x - 10 && darthVader.getPosition().x < mousePoint.x + 10 && darthVader.getPosition().y > mousePoint.y - 10 &&
                        darthVader.getPosition().y < mousePoint.y + 10)
                {
                        totalMovementSet = false;
                }
 

the sprite now moves to the location that I right click and stops until I click somewhere else.

14
Graphics / Re: Problem with sprite movement.
« on: December 15, 2014, 03:35:34 am »
Thanks. I suppose I'll scrap this and start over with a new approach.

15
Graphics / Re: Problem with sprite movement.
« on: December 15, 2014, 02:22:40 am »
so because  "sf::Mouse::isButtonPressed" isn't what I am after for this example then can you point me in another direction?

Pages: [1] 2
anything