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

Pages: [1] 2 3 ... 12
SFML wiki / Re: XBox 360 Controller support for SFML games
« on: August 08, 2013, 12:48:06 am »
Thanks for the comments!
I'm planning to implement joystick force feedback support for Linux too  8)

General / Re: Angle between two vectors
« on: July 19, 2013, 04:37:37 pm »
The goal is to be able to aim a bullet from the player to the mouse.

You need a direction vector (mouse position - player position)
Then normalize that vector (make it a unit vector)
Now, add this normalized vector to your bullet velocity and multiply by the bullet movement speed (a float) and the delta time (as float)

General discussions / Re: GLFW 3
« on: July 15, 2013, 02:06:10 am »
Another cool feature:

The window title is a regular C string using the UTF-8 encoding. This means for example that, as long as your source file is encoded as UTF-8, you can use any Unicode characters.

glfwSetWindowTitle(window, "さよなら絶望先生");

Graphics / Re: View wont move.
« on: July 11, 2013, 12:38:40 am »
Why dont you use aggregation instead of inheritance?

Inside your class:
    sf::View m_view;

SFML wiki / XBox 360 Controller support for SFML games
« on: July 09, 2013, 12:08:44 am »
Hi there!
I´m writing a wrapper around XInput, trying to keep the interface as similar to the SFML API as possible.
If you guys like this code and find it usefull, I will add it to the wiki page  8)

The code compiles and works fine with MinGW
It works with my wired X360 controller, I don´t know if it will work with the wireless controller

Of course you will need the DirectX SDK installed  :'(
Note that this will work only with XBox 360 compatible devices, for another kind of gamepads you still need to use the SFML Joystick API

Ok, here is the code:


#include <SFML/System/Vector2.hpp>

namespace xb {

class Joystick {


        // Typedefs
        typedef unsigned int    t_joyNum;
        typedef unsigned short  t_buttonNum;

        // Enums
        enum {
            Count = 4       // Player 0-3

        enum {
            DPAD_UP       = 0x0001,
            DPAD_DOWN = 0x0002,
            DPAD_LEFT    = 0x0004,
            DPAD_RIGHT  = 0x0008,
            START            = 0x0010,
            BACK              = 0x0020,
            LEFT_THUMB   = 0x0040,
            RIGHT_THUMB = 0x0080,
            LB                   = 0x0100,
            RB                  = 0x0200,
            A                   = 0x1000,
            B                  = 0x2000,
            X                  = 0x4000,
            Y                  = 0x8000,

        // Functions (similar to SFML API)
        static bool isConnected (t_joyNum joyNum);
        static unsigned int getButtonCount (t_joyNum joyNum) { return 14; }
        static bool isButtonPressed (t_joyNum joyNum, t_buttonNum buttonNum);

        // X360 specific functions
        static bool isAnyXBox360ControllerConnected();

        static void getTriggers (t_joyNum joyNum, float &left, float &right);
        static void getSticksPosition (t_joyNum joyNum, sf::Vector2f &left, sf::Vector2f &right);

        static void setVibration (t_joyNum, float leftMotor = 0.0f, float rightMotor = 0.0f);

}; // class

} // ns

#endif // X360_CONTROLLER_HPP

#include "X360Controller.hpp"

#ifdef __MINGW32__
    // Useless MS defines (needed to compile under MinGW)
    #define __in
    #define __out
    #define __reserved

// This define makes your program compile faster by excluding things we are not using

#include <windows.h>
#include <XInput.h>

namespace xb {

// Returns true if the joystick is connected (and is an XBox 360 Controller)
bool Joystick::isConnected (t_joyNum joyNum)

    XINPUT_STATE state;
    ZeroMemory (&state, sizeof (XINPUT_STATE));

    auto result = XInputGetState (joyNum, &state);
    return  (result == ERROR_SUCCESS);


// Convenience function: Returns true if there is at least one X360 controller connected
bool Joystick::isAnyXBox360ControllerConnected()

    return  (isConnected(0) || isConnected(1) || isConnected(2) || isConnected(3));


// Returns true if the device supports audio capabilities (headset)
// Uncomment if you need this function
bool Joystick::voiceSupported (t_joyNum joyNum)

    ZeroMemory (&caps, sizeof (XINPUT_CAPABILITIES));

    auto result = XInputGetCapabilities (joyNum, XINPUT_FLAG_GAMEPAD, &caps);

    if (result != ERROR_SUCCESS)
        return false;

    return  (caps.Flags & XINPUT_CAPS_VOICE_SUPPORTED);


// Returns true if the specified button is pressed
// Note that the triggers are NOT recognized as buttons.. You must use
// the getTriggers function for reading the triggers state
bool Joystick::isButtonPressed (t_joyNum joyNum, t_buttonNum buttonNum)

    XINPUT_STATE state;
    ZeroMemory (&state, sizeof (XINPUT_STATE));

    XInputGetState (joyNum, &state);
    return  (state.Gamepad.wButtons & buttonNum);


// This function returns nothing
// It fills the variables left and right with the current state of the triggers (LT and RT)
// The values will always be in the range 0..1
// TODO: TAKE CARE OF THE DEAD ZONE ??????????????????????????????????
void Joystick::getTriggers (t_joyNum joyNum, float &left, float &right)

    XINPUT_STATE state;
    ZeroMemory (&state, sizeof (XINPUT_STATE));

    XInputGetState (joyNum, &state);

    // Normalize and take care of the Dead Zone
    left  = static_cast <float> (state.Gamepad.bLeftTrigger)  / 255;
    right = static_cast <float> (state.Gamepad.bRightTrigger) / 255;


// This function returns nothing
// It fills the vectors left and right with the stick positions,
// wich are in the range -100..100, similar to the SFML function
// getAxisPosition
void Joystick::getSticksPosition (t_joyNum joyNum, sf::Vector2f &left, sf::Vector2f &right)

    XINPUT_STATE state;
    ZeroMemory (&state, sizeof (XINPUT_STATE));

    XInputGetState (joyNum, &state);

    // Check for the "DEAD ZONE"
    // Left Stick
    if ( (state.Gamepad.sThumbLX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE &&
              state.Gamepad.sThumbLX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) &&
             (state.Gamepad.sThumbLY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE &&
              state.Gamepad.sThumbLY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) ) {

        state.Gamepad.sThumbLX = 0;
        state.Gamepad.sThumbLY = 0;


    // Right Stick
    if ( (state.Gamepad.sThumbRX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE &&
              state.Gamepad.sThumbRX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) &&
             (state.Gamepad.sThumbRY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE &&
              state.Gamepad.sThumbRY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) ) {

        state.Gamepad.sThumbRX = 0;
        state.Gamepad.sThumbRY = 0;


    // Convert values to SFML style (-100..100)
    left.x  = static_cast <float> (state.Gamepad.sThumbLX / 327);
    left.y  = static_cast <float> (state.Gamepad.sThumbLY / 327);
    right.x = static_cast <float> (state.Gamepad.sThumbRX / 327);
    right.y = static_cast <float> (state.Gamepad.sThumbRY / 327);


// Set vibration (0.0 to 1.0)
// 0 stops the vibration
void Joystick::setVibration (t_joyNum joyNum, float leftMotor, float rightMotor)

    ZeroMemory (&vib, sizeof (XINPUT_VIBRATION));

    vib.wLeftMotorSpeed  = static_cast <WORD> (leftMotor  * 65535.0f);
    vib.wRightMotorSpeed = static_cast <WORD> (rightMotor * 65535.0f);

    XInputSetState (joyNum, &vib);


} // ns

Test (main.cpp)
#include <iostream>
using std::cout;
using std::endl;

#include <string>
#include <sstream>

#include <SFML/Graphics.hpp>

// CodeBlocks:
// Linker settings, link libraries, add "XInput" without ".lib"
#include "X360Controller.hpp"

template <class T>
std::string numberToString (const T& t) {

    std::stringstream ss;
    ss << t;

    return ss.str();


int main ()

    // If not X360 controllers found, exit
    if (!xb::Joystick::isAnyXBox360ControllerConnected()) {
        cout << "Error: XBox 360 controllers not detected!" << endl;
        return 1;

    // Create the main window
    sf::RenderWindow window (sf::VideoMode (800, 600), "XBox 360 Controller for SFML");

    // Load font
    sf::Font myFont;
    if (!myFont.loadFromFile("steelfish_rg.ttf")) {
        cout << "Error loading font file!" << endl;
        return 1;

    sf::Text buttonText ("Press any button", myFont);
    sf::Text triggerText ("", myFont);
    sf::Text stickText ("", myFont);

    triggerText.setPosition(0.0f, 50.0f);
    stickText.setPosition(0.0f, 150.0f);

    // Start the game loop
    while (window.isOpen()) {

        // Process events
        sf::Event event;

        while (window.pollEvent(event)) {

            // Close window : exit
            if (event.type == sf::Event::Closed)

        // Check for buttons
        if (xb::Joystick::isButtonPressed (0, xb::Joystick::A))
            buttonText.setString ("A");
        if (xb::Joystick::isButtonPressed (0, xb::Joystick::B))
            buttonText.setString ("B");
        if (xb::Joystick::isButtonPressed (0, xb::Joystick::X))
            buttonText.setString ("X");
        if (xb::Joystick::isButtonPressed (0, xb::Joystick::Y))
            buttonText.setString ("Y");
        if (xb::Joystick::isButtonPressed (0, xb::Joystick::LB))
            buttonText.setString ("LB");
        if (xb::Joystick::isButtonPressed (0, xb::Joystick::RB))
            buttonText.setString ("RB");

        // Check for the triggers (LT and RT)
        float lt, rt;
        xb::Joystick::getTriggers(0, lt, rt);
        triggerText.setString("Left trigger: " + numberToString(lt) + "\nRight trigger: " + numberToString(rt));

        // Triggers controls the vibration
        xb::Joystick::setVibration(0, lt, rt);

        // Get sticks positions
        sf::Vector2f ls, rs;
        xb::Joystick::getSticksPosition(0, ls, rs);
        stickText.setString("Left stick: " + numberToString(ls.x) + "," + numberToString(ls.y) +
                            "\nRight stick: " + numberToString(rs.x) + "," + numberToString(rs.y));

        // Clear screen

        // Dibujar

        // Update the window


    // Stop vibration

    return 0;


Graphics / Re: How to reduce draw calls? (and some questions)
« on: July 02, 2013, 11:54:19 am »
Have you tryed using std::array as Sprite container?

#include <array>
std::array <sf::Sprite, 30> spriteContainer;

Also try to reduce the number of textures to the minimum (Use texture atlas)

Very insteresting proyect!  8)
If you need spanish traslation write me a pm  ;D

You cant

Windows uses .dll libraries
Mac uses dylib ? I guess  :-\
Linux uses .so files

You must compile the libraries for the specific OS (and architecture)

SFML projects / Re: Config Parser
« on: March 29, 2013, 03:18:46 am »
- why do you include <cstring> everywhere?
- why do you declare and implement empty destructor?

Graphics / Re: Optimization while using SFML
« on: March 26, 2013, 01:07:12 pm »
Ok, so i'm going to have all the potential states of the thing on a single texture, and when I change what the object looks like, i'm actually just changing what part of the texture is being displayed?
^^ Is that correct? ^^

Correct. You can take a look at the AnimatedSprite class in the wiki.

System / Re: String doesn't display accents under Linux
« on: March 19, 2013, 02:24:39 am »
Thanks for the example code, I will test it.

But if you're only using literal strings, why do you make it so complicated? Just use wide strings.

Because I thought that it was not necessary (at least under Linux)
Ok, thanks again Laurent!

System / Re: String doesn't display accents under Linux
« on: March 18, 2013, 10:40:10 am »
Ok, let me see if I understand

- Should I call Utf8::decode on each character of the ANSI string?
- "Iterator pointing to the beginning of the input sequence" means something like std::string::begin?
- Is this a heavy process?
- Do I need to get/change the C/C++ locale?

System / Re: String doesn't display accents under Linux
« on: March 17, 2013, 03:41:22 pm »
Portability, cross-platform interoperability and simplicity are more important than interoperability with existing platform APIs. So, the best approach is to use UTF-8 narrow strings everywhere and convert them back and forth on Windows before calling APIs that accept strings.

UTF-8 Everywhere

I'm using UTF-8 encoding under Linux, however the SFML text doesn't work as expected:

#include <iostream>
#include <string>
#include <locale>

#include <SFML/Graphics.hpp>

using std::cout;
using std::endl;

int main (int argc, char **argv)
        std::locale myLocale ("spanish"); // spanish
        std::locale::global (myLocale); // needed for console output only???
        cout << "Locale name = " << myLocale.name() << endl;

        const char *myANSI = "Hola áéíóú";
        cout << "C style ANSI string = " << myANSI << endl;    
        sf::String mySFML_String (myANSI, myLocale);
        std::string standardString = mySFML_String.toAnsiString();
        cout << "From SFML to std::string = " << standardString.c_str() << endl;
        // Graphics Mode xD
        sf::RenderWindow window (sf::VideoMode(600, 400), "Hello Strings!");
        sf::Font myFont;
        if (!myFont.loadFromFile("./DYST.ttf")) {
                cout << "Error loading fntfile" << endl;
                return -1;
        sf::Text myText (mySFML_String, myFont, 50);
        // Loop
        while (window.isOpen()) {
                sf::Event myEvents;
                while (window.pollEvent(myEvents)) {
                        if (myEvents.type == sf::Event::Closed) {
                } // pollEvent
        } // isOpen
        return 0;


(Font file supports accents)

I don't know what else should I do...  :-\
Please help

System / Re: AW: String doesn't display accents under Linux
« on: March 14, 2013, 12:36:33 am »
Personally I wouldn't expect the ANSI (couldn't it be also just ASCII?) version to work. ;)

Well, using this code (same Win7 OS):
    // Create the main window
    sf::RenderWindow window (sf::VideoMode (800, 600), "String Test");

    // Load font
    sf::Font myFont;
    if (!myFont.loadFromFile("./steelfish_rg.ttf")) {
        cout << "error loading font" << endl;
        return -1;

    // ANSI
    const char *myANSIstring = "Hola áéíóú";
    sf::Text myANSItext (myANSIstring, myFont, 50);

    // Start the game loop
    while (window.isOpen()) {

        // Process events
        sf::Event event;
        while (window.pollEvent(event)) {

            // Close window : exit
            if (event.type == sf::Event::Closed)

        // Clear screen

        // Draw

        // Update the window


    return EXIT_SUCCESS;

with codeblocks default system native encoding:

works fine  :o

System / Re: String doesn't display accents under Linux
« on: March 14, 2013, 12:20:51 am »
My goal is to support:
- Windows and Linux platforms
- English and Spanish languages (maybe more)

Testing the same code, Win7 x64 SP1, Region: Spanish (Argentina), IDE: Codeblocks 12, file encoding: UTF-8 (if I leave the default codeblocks encoding, I can´t compile the code)
Inserting the code posted by Laurent at the beginning of the main function

Something interesting I found:
std::wstring VS std::string

1. When I should use std::wstring over std::string?

On Linux? Almost never

On Windows? Almost always

I know there is a lot of info about encodings. I try to understand but its very confusing  ???
Should I use wide strings and wide file streams to read them from files?
What kind of encoding should I use for my source code files?

Pages: [1] 2 3 ... 12