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

Pages: [1]
1
Graphics / [SOLVED]C++ - setRadius doesn't work in the new 'for' ?
« on: July 11, 2015, 11:30:09 pm »
What am I doing wrong?
Why does this work
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "wtf");
   
    window.setVerticalSyncEnabled(true);
   
    const unsigned int NumberOfPlayers = 4;

    sf::CircleShape Players[NumberOfPlayers];

    // Puttin the 'setRadius' inside this for makes it work
    for(unsigned int i = 0; i<NumberOfPlayers; i++)
    {
        Players[i].setRadius(50);
        Players[i].setPosition({i*100, 0});
    }
   
    while (window.isOpen())
    {
        window.clear(sf::Color::Black);
        for(auto player : Players)
            window.draw(player);
        window.display();
    }

    return 0;
}
 
and this doesn't?
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "wtf");
   
    window.setVerticalSyncEnabled(true);
   
    const unsigned int NumberOfPlayers = 4;

    sf::CircleShape Players[NumberOfPlayers];
   
    // This doesn't work!
    for(auto player : Players)
    {
        player.setRadius(50);
    }
   
    // Puttin the 'setRadius' inside this for makes it work
    for(unsigned int i = 0; i<NumberOfPlayers; i++)
    {
        Players[i].setPosition({i*100, 0});
    }
   
    while (window.isOpen())
    {
        window.clear(sf::Color::Black);
        for(auto player : Players)
            window.draw(player);
        window.display();
    }
   
    return 0;
}
 

The change is the location of 'setRadius' in the code. The second snippet results in a blank window.

Thanks

2
Here is the whole code:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

int main()
{
        // Create the main window
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

        sf::Texture t_grass;
        if(!t_grass.loadFromFile("M_03.png"))
                cout<<"ERROR: can't load file"<<endl;
        sf::Sprite s_grass(t_grass);
        s_grass.setPosition(0,0);
       
        // Prepare the ground
        sf::RenderTexture rt_ground;
        if(!rt_ground.create(500, 500))
                cout<<"ERROR: can't create render texture"<<endl;

        rt_ground.clear(sf::Color::White);
        rt_ground.draw(s_grass);
        rt_ground.display();
       
        sf::Sprite s_ground(rt_ground.getTexture());
       
        sf::Event event;

        // Start the game loop
        while (window.isOpen())
        {
                // Process events
                while (window.pollEvent(event))
                {
                        // Close window : exit
                        if(event.type == sf::Event::Closed)
                                window.close();
                }

                // Clear screen
                window.clear();
               
                window.draw(s_ground);
               
                // Update the window
                window.display();
        }

        return EXIT_SUCCESS;
}
 
And here is the result:

As you can see, it is very weird. It renders the source code upside down and distorted but it's the source code. It's not always the source code, I simply got lucky once and the dirstortions were easy to compare to the stuff on the screen.
I am probably doing something very wrong. What is it?

notes:
  • The M_03.png is a 10x10px png.
  • There is no console output at all.

Setup:
Archlinux
NVIDIA GeForce 7600GS
Nouveau open source driver

Thanks

EDIT: Just for completeness sake, this is how it should have looked like (it works using the nvidia blob):

3
Window / A proposal of how to improve keyboard event handling
« on: April 01, 2012, 03:05:43 pm »
Hi.
I am working on a project which uses SFML and I use the keyboard as kind of piano keys. There is a problem with keyboard event handling. For example, when you press shift + number  the produced event carries the key code 0 instead of the proper one.
Here is a bug report for reference: https://github.com/SFML/SFML/issues/187

Interestingly, this behavior in SFML is inconsistent. Here are two ways of checking the key code while pressing Shift+Num1, one works and one doesn't. I work on Linux.
In the first example the key code is '0' therefore nothing would be printed:
Code: [Select]
while(window.isOpen)
{
    window.waitEvent(event);

    if(event.type == sf::Event::KeyPressed)
    {
        if(event.key.code == sf::Keyboard::Num1)
            cout<<"1 is pressed"<<endl;
    }
}

In the second example the check correctly sees that Num1 is pressed therefore "1 is pressed" would be printed:
Code: [Select]
while(window.isOpen)
{
    window.waitEvent(event);

    if(event.type == sf::Event::KeyPressed)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num1))
            cout<<"1 is pressed"<<endl;
    }
}

The reason for the inconsistency lies in the implementation.
The first example uses the code from the file InputImpl.cpp line 63. The function gets an SFML key value, converts it to an X11 keysym (layout dependent) and then to an X11 keycode. Then the keycode is checked with XQueryKeymap. This is good because the keysym is used only internally to get the keycode (layout independent).

The second example uses the code from the file WindowImplX11 line 660. The keysym - which is layout dependent and changed by pressing shift which is undesired - is being requested from X11 with the function (line 666)
Code: [Select]
XLookupString(&windowEvent.xkey, buffer, sizeof(buffer), &symbol, &keyboard); and then converted to an SFML key code with
Code: [Select]
event.key.code    = keysymToSF(symbol);The problem with this approach is that the keysym we get from X11 is layout and shift dependent. This is good if we want the inserted character, but if we want the key, we have to request the X11 keycode which is available from XKeyEvent.keycode.

Since the problem seems to linger from the beginning of time (2008?) I assume you tried to fix it but there were problems. May I ask if you could share your opinion on the subject and the proposed change?

Thank you for the great library by the way.

Edit: Here is the output of xev (X events) when pressing Num1 and then Shift+Num1. I copied only the relevant parts:
Code: [Select]
KeyPress event, serial 42, synthetic NO, window 0x2600001,
    root 0xb6, subw 0x0, time 19059638, (309,-46), root:(1119,686),
    state 0x0, keycode 10 (keysym 0x31, 1), same_screen YES,
    XLookupString gives 1 bytes: (31) "1"
    XmbLookupString gives 1 bytes: (31) "1"
    XFilterEvent returns: False

KeyRelease event, serial 45, synthetic NO, window 0x2600001,
    root 0xb6, subw 0x0, time 19059710, (309,-46), root:(1119,686),
    state 0x0, keycode 10 (keysym 0x31, 1), same_screen YES,
    XLookupString gives 1 bytes: (31) "1"
    XFilterEvent returns: False

KeyPress event, serial 45, synthetic NO, window 0x2600001,
    root 0xb6, subw 0x0, time 19062213, (309,-46), root:(1119,686),
    state 0x0, keycode 50 (keysym 0xffe1, Shift_L), same_screen YES,
    XLookupString gives 0 bytes:
    XmbLookupString gives 0 bytes:
    XFilterEvent returns: False

KeyPress event, serial 45, synthetic NO, window 0x2600001,
    root 0xb6, subw 0x0, time 19063011, (309,-46), root:(1119,686),
    state 0x1, keycode 10 (keysym 0x21, exclam), same_screen YES,
    XLookupString gives 1 bytes: (21) "!"
    XmbLookupString gives 1 bytes: (21) "!"
    XFilterEvent returns: False

KeyRelease event, serial 45, synthetic NO, window 0x2600001,
    root 0xb6, subw 0x0, time 19063083, (309,-46), root:(1119,686),
    state 0x1, keycode 10 (keysym 0x21, exclam), same_screen YES,
    XLookupString gives 1 bytes: (21) "!"
    XFilterEvent returns: False

KeyRelease event, serial 45, synthetic NO, window 0x2600001,
    root 0xb6, subw 0x0, time 19063583, (309,-46), root:(1119,686),
    state 0x1, keycode 50 (keysym 0xffe1, Shift_L), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False
The order is: 1 press, 1 release, shift press, 1 press, 1 release, shift release.
You can see that when pressing 1 with or without shift the keycode doesn't change while the keysym does change.

Pages: [1]
anything