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

Pages: [1]
1
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;
}
 

2
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!

3
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.

4
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?

5
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.

6
Graphics / Problem with sprite movement.
« on: December 14, 2014, 10:47:30 pm »
I am new to SFML and am having a problem with my sprites and mouse events. I'm trying to make it so that when I right click somewhere, the sprite moves to that location slowly (not just warps). I have managed to get it partly working with only one problem. If I right click somewhere the sprite will move towards that position only a few pixels meaning I have to click over and over again until it gets there. Think of League of Legends or any RTS game for an example. You click on the map where you want the unit to go, and he goes. In my current code I must keep clicking as my sprite is only moving a few pixels per click, instead of a steady movement. Here is a snippet of what I've got so far, let me know if you need more.

                        case sf::Event::MouseButtonPressed:

                                if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right))
                                {
                                        sf::Vector2f totalMovement(sf::Mouse::getPosition(window).x - darthVader.getPosition().x, sf::Mouse::getPosition(window).y - darthVader.getPosition().y);

                                        darthVader.move(totalMovement * (1.f / 30.f));
                                }
 

7
General / LINKER ERRORS :(
« on: September 17, 2013, 05:08:24 am »
Trying to get SFML 2.1 setup for the first time on VS 2012 Express using this tutorial: http://www.sfml-dev.org/tutorials/2.1/start-vc.php

However I am getting some errors.
Error   3   error LNK1120: 2 unresolved externals   c:\users\admin\documents\visual studio 2012\Projects\SFML\Release\SFML.exe   SFML
Error   1   error LNK2001: unresolved external symbol "public: static class sf::Color const sf::Color::Green" (?Green@Color@sf@@2V12@B)   c:\Users\Admin\documents\visual studio 2012\Projects\SFML\SFML\main.obj   SFML
Error   2   error LNK2001: unresolved external symbol "public: static class sf::RenderStates const sf::RenderStates::Default" (?Default@RenderStates@sf@@2V12@B)   c:\Users\Admin\documents\visual studio 2012\Projects\SFML\SFML\main.obj   SFML


Any help is appreciated. Thanks.

Pages: [1]