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

Pages: [1]
1
General / How to check if key is double-tapped?
« on: April 10, 2017, 01:39:45 pm »
Hello. In my game I want the player's character to sprint if I double tap the movement keys. (WASD)

Here is my current movement logic...

void PlayerCharacter::handleInputCurrent(sf::Event& event) {
    int speed = DefaultSpeed;

    setVelocity(sf::Vector2f(0, 0));

    if (event.type == sf::Event::KeyReleased) {
        switch (event.key.code) {
        case sf::Keyboard::W:
            m_state = IdleUp;
            break;

        case sf::Keyboard::S:
            m_state = IdleDown;
            break;

        case sf::Keyboard::A:
            m_state = IdleLeft;
            break;

        case sf::Keyboard::D:
            m_state = IdleRight;
            break;
        }
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
        setVelocity(sf::Vector2f(getVelocity().x, -speed));
        m_state = WalkUp;
    }
   
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
        setVelocity(sf::Vector2f(getVelocity().x, speed));
        m_state = WalkDown;
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
        setVelocity(sf::Vector2f(-speed, getVelocity().y));
        m_state = WalkLeft;
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
        setVelocity(sf::Vector2f(speed, getVelocity().y));
        m_state = WalkRight;
    }
}

By the way, m_state is used for animation purposes and setVelocity(x, y) is used to set the velocity of an entity.

Thanks for reading!  :)

2
General / Re: Pass common objects around classes with struct
« on: April 03, 2017, 08:39:34 am »
The resource holder is a template class that stores an std::map of whatever typename you pass into it and a second typename which is called IdentifierT. "resource_identifiers" looks like this:


#pragma once
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Font.hpp>
#include "resource_holder.hpp"

enum class TextureIdentifiers {
    Player,
    Tiles,
     . . .
}

enum class Fonts {
    CodersCrux;
    . . .
}

typedef ResourceHolder<sf::Texture, TextureIdentifiers> TextureHolder;

typedef ResourceHolder . . .
 

Thanks for the reply!

3
General / Pass common objects around classes with struct
« on: April 02, 2017, 08:16:39 pm »
Hi everyone!  :) It's my first post here so I apologize if I get anything wrong. Anyway, on my game project, I want to pass objects that I commonly use around using a custom struct named Context. My question is, am I doing it correctly? Please tell me if I am copying things that I shouldn't be and that everything is properly referenced. Here's the code.

// context.hpp

#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include "resource_holder/resource_holder.hpp"
#include "resource_holder/resource_identifiers.hpp"

struct Context {
    Context(sf::RenderWindow& window, TextureHolder& textures, FontHolder& fonts);

    sf::RenderWindow* window;
    TextureHolder* textures;
    FontHolder* fonts;
}

// context.cpp

#include "context.hpp"

Context::Context(sf::RenderWindow& window, TextureHolder& textures, FontHolder& fonts)
: window(&window)
, textures(&textures)
, fonts(&fonts) {
   
}

 

And then in game.cpp


Context Game::createContext() {
    return Context(m_window, m_textures, m_fonts);
}

. . .

m_player(createContext);

 

And here is Player:


// player.hpp

#include <SFML/Graphics.hpp>
#include "world/entity.hpp"
#include "context/context.hpp"

class Player : public Entity {
public:
    Player(Context context);

    virtual void update(const float deltaTime);

private:
   virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

   Context m_context;
   sf::Sprite m_sprite;
}

// player.cpp

Player::Player(Context context)
: m_context(context)
, m_sprite(context.textures->get(TextureIdentifiers::Player)) {
    . . .
}

// Rest of code omitted for clarity.

 

Coming from a Java/C# background, I don't know much about pointers or references so I apologize if I seem really dumb.  If you spot any mistakes (which you most likely will) please point them out!  ;)

Pages: [1]