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

Pages: [1]
1
Network / Re: nonblocking sockets
« on: June 02, 2014, 10:47:00 pm »
Quote
okey, thanks for help, but atm i don't know how to use non-blocking sockets

Sockets are set to block by default and have the function setBlocking() which allows you to toggle blocking.

sf::Socket socket;
socket.setBlocking(false);

Now instead of the socket waiting for when the action can and is performed, it will just return immediately thus preventing your thread from being halted. Check the detailed description of sf::Socket for a better explanation of blocking to non-blocking.

2
Network / Re: Multiple clients connecting to one server
« on: May 30, 2014, 06:51:55 pm »
Quote
SFML doesn't currently have a way to directly check if a TCP socket is connected or not. This might change in the future.

For now, you can check if a TCP socket is connected by comparing getRemoteAddress() against sf::IpAddress::None. If a TCP socket is connected it will always have a remote address and thus be unequal to sf::IpAddress::None.

Thank you, I wasn't aware getRemoteAddress() got and returned the address directly from the other machine on the connection and not from a stored sf::IpAddress when the sf::Socket was properly declared and used to connect with the client. Since it does basically check whether the other machine is still connected then it definitely serves the purpose I was wanting. Oh and I checked the documentation ( specifically the compiled HTML documentation provided in SFML's 2.1 download ) and this is what it read for getRemoteAddress():

Quote
Get the address of the connected peer.

It the socket is not connected, this function returns sf::IpAddress::None.


So I probably should have looked at that before instantly assuming all that function performed was a quick return of a stored IP address locally when the sf::Socket was binded to the peer. That was my "am I missing something here?" in this situation so now it's all cleared up  :D. By the way it seems there's a spelling error in that documentation as seen above. ( Wait I'm correcting spelling/grammar on the Internet?! Nobody does that!  ;) . )

Quote
So this is a direct way to check if it is connected, isn't it?
Quote
So isConnected() would be... more direct than direct?

A isConnected() member function for sf::Socket would be a pretty direct way to do it so I'm all for it. For my particular case I don't plan on changing this in SFML's source and compiling for myself. So I'll just stick with creating a function that takes an sf::Socket as an argument and let it return a bool value on whether or not it's connected.

Quote
I agree that it's a little obtuse as it stands (getRemoteAddress could, for instance, point to what it is set to connect to, as opposed to what it is actually connected to - making an assumption that the user in question hasn't read the documentation first, which appears to be fairly common). An isConnected method would be good, even if it does nothing more complicated than that particular check in the first instance.

Yeah that's what I figured it performed at first as seeing it in action in the sample code I figured that's exactly what it did. I really should have just checked the documentation for getRemoteAddress() in the first place and I most likely would have never needed to post this. I usually use the sample code and documentation as a way to learn what I need as it's the most direct way for me to understand the library. Tutorials are just added verbiage to me that I only rely on after I unsuccessfully couldn't figure out what I needed from the samples and documentation.


Maybe this should be presented in future tutorial revisions by providing a code example like binary did to check whether a peer is connected to a socket. Either way the forum yet again has an awesome help section so I appreciate the swift and resolving replies.

Happy coding!


EDIT:

Quote
A isConnected() member function for sf::Socket would be a pretty direct way to do it so I'm all for it. For my particular case I don't plan on changing this in SFML's source and compiling for myself. So I'll just stick with creating a function that takes an sf::Socket as an argument and let it return a bool value on whether or not it's connected.

Meant to say sf::TcpSocket, my bad  :P.

3
Network / Multiple clients connecting to one server
« on: May 29, 2014, 10:09:37 pm »
This may enter the realm of stupidity here but I wanted to make sure I'm understanding the tutorial for SFML 2.1's Networking module. The example code and accompanying text skims over how two or more triggering connections are handled using sf::Sockets. My understanding was that, if I had a TCP server-client relation program, the server would need one listener socket for incoming connections and one socket for every client that connects. This seems to be exactly what this line in the tutorial states:

Quote
On server side, more things need to be done. Multiple sockets are required: one that listens for incoming connections, and one for each connected client.

The code in the example however only provides the example for the situation of having only one server and one client connecting at a time as far as I can tell:

sf::TcpListener listener;

// bind the listener to a port
if (listener.listen(53000) != sf::Socket::Done)
{
    // error...
}

// accept a new connection
sf::TcpSocket client;
if (listener.accept(client) != sf::Socket::Done)
{
    // error...
}

// use "client" to communicate with the connected client,
// and continue to accept new connections with the listener

As I have understood it ( and the comments at the bottom of the tutorial code states this ), all I need is for the clients to know where to find the listener socket ( IP address and port it's listening from ) then the listener will accept the connection and return a successfully connected client in a sf::Socket of my choice to handle all further interactions with this unique client until they disconnect thus freeing the socket for a new client.

Going with my logic flow earlier I figured all I need to do is if I want say five clients to be able to be connected to the server all at the same time then I need six sockets; one to listen for new connections and one to handle the transfer of information to and from each client ( so five of these as this example wants five clients ). So my code might at first look like this:

sf::TcpListener listener;

// bind the listener to a port
if (listener.listen(53000) != sf::Socket::Done)
{
    // error...
}

while(running)
{

// accept a new connection
sf::TcpSocket client1;
sf::TcpSocket client2;
sf::TcpSocket client3;
sf::TcpSocket client4;
sf::TcpSocket client5;

// Check to see if client1 is being used and if not give the new client this socket.
if(client1 == openForNewClient)
{
    if (listener.accept(client1) != sf::Socket::Done)
    {
        // error...
    }
}else
// Check to see if client2 is being used and if not give the new client this socket.
if(client2 == openForNewClient)
{
    if (listener.accept(client2) != sf::Socket::Done)
    {
        // error...
    }
}else
// Check to see if client3 is being used and if not give the new client this socket.
if(client3 == openForNewClient)
{
    if (listener.accept(client3) != sf::Socket::Done)
    {
        // error...
    }
}else
// Check to see if client4 is being used and if not give the new client this socket.
if(client4 == openForNewClient)
{
    if (listener.accept(client4) != sf::Socket::Done)
    {
        // error...
    }
}else
// Check to see if client5 is being used and if not give the new client this socket.
if(client5 == openForNewClient)
{
    if (listener.accept(client5) != sf::Socket::Done)
    {
        // error...
    }
}else
{
    // Inform potential client that there is no slot ( socket ) available.
    // The client doesn't connect.
}

// See if client1 is sending data or needs data sent.
// See if client2 is sending data or needs data sent.
// See if client3 is sending data or needs data sent.
// See if client4 is sending data or needs data sent.
// See if client5 is sending data or needs data sent.

// Update server's local frame of information based on what was received by clients.

// See if client1 has disconnected and therefore has freed the socket they were using.
// See if client2 has disconnected and therefore has freed the socket they were using.
// See if client3 has disconnected and therefore has freed the socket they were using.
// See if client4 has disconnected and therefore has freed the socket they were using.
// See if client5 has disconnected and therefore has freed the socket they were using.
}

Basically I want to know how to determine if a socket is in use by a client so that I can let my listening socket assign them with the correct socket. If there's no socket available then they simply don't connect. However sf::Socket::Status doesn't have a "In-use" enum, but I figured that's what "NotReady" was for. Am I right and would checking the status for "NotReady" being true determine whether a socket is in use by another client or not? Am I missing something obvious here? Is the "Disconnected" enum not just for the action of closing one of the two connected sockets from either the client or server?

4
Graphics / sf::View leaves screen black
« on: January 26, 2012, 07:02:51 am »
Sorry for the double-post, but should I make the conversion to SFML 2.0 inside my library for my project? I have several parts of it made heavily with SFML 1.6, so it would take several hours to redo those parts to work with the rest of the library. I'd like to finish this project before making such a task done, but if there's no other way then I'll do it. Right now this is stopping progress in it's tracks, so I'm hoping to resolve this sooner rather than later ( not to sound pushy in the least, I'm referring to myself more than anything ).

I have a personal deadline to meet, with an actual deadline coming at the end of the week. So, I do want to resolve this in that it is either my mistake in design or implementation, or that it's a problem with the current version of the SFML library I'm using.

5
Graphics / sf::View leaves screen black
« on: January 25, 2012, 03:48:19 am »
I've tested it on a machine with an NVIDEA GeForce 8200, as well as several other systems with integrated Intel video chipsets, same thing happens. Though I've wanted to upgrade to SFML 2.0, I've gotten used to 1.6 and have already started the project with it. I don't mind converting my existing code for 2.0, but only if I have to due to problems with 1.6. After this project I was already going to make the switch, but was hoping to finish this one before so.

6
Graphics / sf::View leaves screen black
« on: January 24, 2012, 04:51:48 am »
I've been working with a 2D platformer, and have been trying to implement a view in the screen. I was able to do it in SDL rather easily, though in SFML I've been trying to use sf::View in order to create a view and then set the view in the window. However, whenever I try to set a view other than the default the window has, the screen is black ( when there should be a rendered character centered on the view ). I'm not sure if it's a problem with keeping the view alive, or that it's not allowing me to blit to the screen, but I've tried hacking at it for hours to no avail.

My code:

Code: [Select]

// First level of the game.
int Game::Level1()
{

// The center view of the camera.
sf::Vector2f CenterView( ( Jim_Character.GetXPosition()
                                             + (float)11.5 ),
   ( Jim_Character.GetYPosition()
                                             + (float)20 ) );

// Half the total size of the view.
sf::Vector2f HalfSizeOfView( 400, 300 );

//The camera view for the game.
sf::View Camera( CenterView, HalfSizeOfView );

// Sets the view to center Jim.
MainWindow.SetCameraView( Camera );

// Loop while MainWindow is still open.
while( MainWindow.IsOpen() )
{

// Loop and process events ( if any ) in queue.
while( MainWindow.GetEvents() )
{

// Perform if the user clicks the red 'X'.
if( MainWindow.WindowEventsType() == "Closed" )
{

// Close the main window.
MainWindow.CloseWindow();

}

// Perform if the user pressed the "Esc" key.
if( MainWindow.KeyboardKeyPressed() == "Escape" )
{

// Sets the default view on the window.
MainWindow.SetDefaultCameraView();
// Return to the main menu.
MainMenu();

}

// Perform if the user pressed the "A" key.
if( MainWindow.KeyboardKeyPressed() == "A" )
{

// If Jim is not trying to go outside the level.
if( Jim_Character.GetXPosition() > 0 ||
Jim_Character.GetXPosition() == 0 )
{

// Start moving Jim left.
Jim_Move_Left = true;

}

}

// Perform if the user pressed the "D" key.
if( MainWindow.KeyboardKeyPressed() == "D" )
{

// If Jim is not trying to go outside the level.
if( ( Jim_Character.GetXPosition() + 33 ) < Level_1_Width )
{

// Start moving Jim right.
Jim_Move_Right = true;

}

}

// Perform if the user released the "A" key.
if( MainWindow.KeyboardKeyReleased() == "A" )
{

// Start moving Jim left.
Jim_Move_Left = false;

}

// Perform if the user released the "D" key.
if( MainWindow.KeyboardKeyReleased() == "D" )
{

// Start moving Jim right.
Jim_Move_Right = false;

}

}

// If Jim is to be moved to the left.
if( Jim_Move_Left == true )
{

// If Jim is not trying to go outside the level.
if( Jim_Character.GetXPosition() > 0 ||
Jim_Character.GetXPosition() == 0 )
{

// Move him to the left 4.
Jim_Character.MovePositionX( -.50f );

}

}

// If Jim is to be moved to the right.
if( Jim_Move_Right == true )
{

// If Jim is not trying to go outside the level.
if( ( Jim_Character.GetXPosition() + 33 ) < Level_1_Width )
{

// Move him to the left 4.
Jim_Character.MovePositionX( .50f );

}

}

// Renders the load game graphic.
MainWindow.Render( Jim_Character.GetSprite() );

// Clears and updates window with new data.
MainWindow.Update();

}

// The program exits successfully, and the main window is closed.
return 0;

}


Note that I use a wrapper library I made myself that incorporates SFML 1.6 ( as well as several other smaller libraries + obvious heavy additions of my own ). Therefore, some of the code above will do things that SFML does, as well as other things too. The comments should be enough to clear up any misunderstanding though, but if not I can certainly clarify. If any more code needs to be added, just ask. I would cut some irrelevant code out of the state function, but I thought it would be better to see what it's supposed to do as a whole.

7
Window / Limit frame rate
« on: September 22, 2011, 10:07:08 pm »
Thanks for telling me that Nexus, I was under the impression that sf::Window::SetFramerateLimit() only limited the displaying on the window itself. Now I know  :D . I was almost at the point where I was going to use OS specific code for time management just for framerate limiting :twisted: .

8
Window / Limit frame rate
« on: September 22, 2011, 09:31:39 pm »
Probably a question that's answered somewhere else, but I've been trying to limit the frame rate in an application, and I think I'm having a meltdown with simple math  :oops: .

Code: [Select]
framerate = Window.GetFrameDuration();

if( framerate < (unsigned)( 1 / 60 ) )
{

Window.Wait( ((unsigned)( 1 / 60 ) - framerate) );

}


I'm wanting 60 fps, though I'm not even sure if that is correct to get it. If it is, would changing the value dividing into 1 ( 60 ) to say 30 make the fps 30? I did this fine in SDL ( using a very similar method ) though I'm still new working with SFML. I already saw the tutorial page that talks about handling time.

http://www.sfml-dev.org/tutorials/1.3/window-time.php

Which gave me a way to get the time between frames.

EDIT: I already know about V-Sync, as well as setting the frame limit for a window via SetFramerateLimit(). I'd rather not make the application rely on the refresh rate of the monitor, and I don't want to simply limit what the window displays  8) .

Hope that makes sense  :P .

9
Window / Speedup when moving mouse
« on: September 20, 2011, 04:23:04 pm »
Ah! I'm such an idiot  :lol: . It took me a bit to process what you meant, and now I see what you mean. This is my new and improved code that actually works:

Code: [Select]
// Continually update the starup window until closed.
      while( Window.IsOpen() )
      {

         // Process events.
         while ( Window.GetEvents() )
         {

            // Close window : exit
            if ( Window.WindowEventsType() == "Closed" )
            {

               // Tells that the window is closing.
               DebugMessageString( "Window is being closed..." );
             
               // Closes the window.
               Window.CloseWindow();
             
            }

            // Close window : exit when pressing Escape "ESC".
            if ( Window.KeyboardKeyPressed() == "Escape" )
            {

               // Tells that the window is closing.
               DebugMessageString( "Window is being closed..." );
             
               // Closes the window.
               Window.CloseWindow();
             
            }

            // Plays a sound when pressing "Tab".
            if ( Window.KeyboardKeyPressed() == "Tab" )
            {

               bolt.PlaySound();
             
            }

            // Moves green square to the right.
            if ( Window.KeyboardKeyPressed() == "Right" )
            {

               playerMoveRight = true;
             
            }
             
            // Moves green square to the left.
            if ( Window.KeyboardKeyPressed() == "Left" )
            {

               playerMoveLeft = true;
             
            }

            // Moves green square to the up.
            if ( Window.KeyboardKeyPressed() == "Up" )
            {

               playerMoveUp = true;
             
            }

            // Moves green square to the down.
            if ( Window.KeyboardKeyPressed() == "Down" )
            {

               playerMoveDown = true;
             
            }

            // Stops movement going to the right.
            if ( Window.KeyboardKeyReleased() == "Right" )
            {

               playerMoveRight = false;
             
            }
             
            // Stops movement going to the left.
            if ( Window.KeyboardKeyReleased() == "Left" )
            {

               playerMoveLeft = false;
             
            }

            // Stops movement going to the up.
            if ( Window.KeyboardKeyReleased() == "Up" )
            {

               playerMoveUp = false;
             
            }

            // Stops movement going to the down.
            if ( Window.KeyboardKeyReleased() == "Down" )
            {

               playerMoveDown = false;
             
            }

            // Left mouse button clicked.
            if ( Window.MouseEventsType() == "MouseButtonPressed" )
            {

               // New button click event.
               if( New.IsClicked( Window ) == true )
               {
               
                  // Tells that the "New" button has been clicked.
                  DebugMessageString( "Mouse cursor clicked the \"New\" button..." );
               
               }

               // Load button click event.
               if( Window.MouseLocationX() >= 70 && Window.MouseLocationX() <= 170 &&
                  Window.MouseLocationY() >= 360 && Window.MouseLocationY() <= 410 )
               {
               
                  // Tells that the "Load" button has been clicked.
                  DebugMessageString( "Mouse cursor clicked the \"Load\" button..." );
               
               }

               // Tells where the mouse was clicked.
               DebugMessageInt( Window.MouseLocationX() );
               DebugMessageInt( Window.MouseLocationY() );
             
            }

         }

            if( playerMoveLeft == true )
            {
             
               green.MoveX( -10 );
             
            }

            if( playerMoveRight == true )
            {
             
               green.MoveX( 10 );
             
            }

            if( playerMoveUp == true )
            {
             
               green.MoveY( -10 );
             
            }

            if( playerMoveDown == true )
            {
             
               green.MoveY( 10 );
             
            }


         // Draws the menu.
         Window.Render( menu.GetSprite() );
         // Draws the green box.
         Window.Render( green.GetSprite() );

         // Displays the updated window.
         Window.Update();
       
      }


Thanks a ton Laurent! I need to get more sleep before I start this again, in order to prevent that from happening again  8) .

The event that is triggered by pressing down the 'Tab' key does use sf::Event and is in the event loop. I simply made it where I was working with strings rather than events outside my wrapper library.

Code: [Select]
// Processes the event(s) to see what key on the keyboard is being released.
std::string Window::KeyboardKeyPressed()
{
if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Tab ) )
{

return "Tab";

}
}


It does use an sf::Event, and it is processed in the event loop ( from what i can tell ). Though then again, if you are able to elaborate on what you mean, that would be nice. I simply didn't want to have to type that out each time I wanted to work with checking for events, so I just wanted to have to check for strings being returned from that particular function  :D . I cut down all the other tested events in that function ( to only show the important one ), but that's the same thing for all the other events that could be checked by SFML.

As for the second thing you mentioned, I believe I corrected that now. The events are tested in the event loop, and the game logic is outside with the rest in the game loop. The events change bool variables to be tested in the game loop to see if the sprite needs to be moved a certain direction.

Again, thank you for all the help! <3 SFML ( sorry SDL, it just wasn't working out  :wink:  ).

10
Window / Speedup when moving mouse
« on: September 20, 2011, 01:21:34 pm »
Code: [Select]
// Continually update the starup window until closed.
while( Window.IsOpen() )
{

// Process events.
while ( Window.GetEvents() )
{

// Close window : exit
if ( Window.WindowEventsType() == "Closed" )
{

// Tells that the window is closing.
DebugMessageString( "Window is being closed..." );

// Closes the window.
Window.CloseWindow();

}

// Close window : exit when pressing Escape "ESC".
if ( Window.KeyboardKeyPressed() == "Escape" )
{

// Tells that the window is closing.
DebugMessageString( "Window is being closed..." );

// Closes the window.
Window.CloseWindow();

}

// Plays a sound when pressing "Tab".
if ( Window.KeyboardKeyPressed() == "Tab" )
{

bolt.PlaySound();

}

// Moves green square to the right.
if ( Window.KeyboardKeyPressed() == "Right" )
{

playerMoveRight = true;

}

// Moves green square to the left.
if ( Window.KeyboardKeyPressed() == "Left" )
{

playerMoveLeft = true;

}

// Moves green square to the up.
if ( Window.KeyboardKeyPressed() == "Up" )
{

playerMoveUp = true;

}

// Moves green square to the down.
if ( Window.KeyboardKeyPressed() == "Down" )
{

playerMoveDown = true;

}

// Stops movement going to the right.
if ( Window.KeyboardKeyReleased() == "Right" )
{

playerMoveRight = false;

}

// Stops movement going to the left.
if ( Window.KeyboardKeyReleased() == "Left" )
{

playerMoveLeft = false;

}

// Stops movement going to the up.
if ( Window.KeyboardKeyReleased() == "Up" )
{

playerMoveUp = false;

}

// Stops movement going to the down.
if ( Window.KeyboardKeyReleased() == "Down" )
{

playerMoveDown = false;

}

if( playerMoveLeft == true )
{

green.MoveX( -10 );

}

if( playerMoveRight == true )
{

green.MoveX( 10 );

}

if( playerMoveUp == true )
{

green.MoveY( -10 );

}

if( playerMoveDown == true )
{

green.MoveY( 10 );

}

// Left mouse button clicked.
if ( Window.MouseEventsType() == "MouseButtonPressed" )
{

// New button click event.
if( New.IsClicked( Window ) == true )
{

// Tells that the "New" button has been clicked.
DebugMessageString( "Mouse cursor clicked the \"New\" button..." );

}

// Load button click event.
if( Window.MouseLocationX() >= 70 && Window.MouseLocationX() <= 170 &&
Window.MouseLocationY() >= 360 && Window.MouseLocationY() <= 410 )
{

// Tells that the "Load" button has been clicked.
DebugMessageString( "Mouse cursor clicked the \"Load\" button..." );

}

// Tells where the mouse was clicked.
DebugMessageInt( Window.MouseLocationX() );
DebugMessageInt( Window.MouseLocationY() );

}

}

// Draws the menu.
Window.Render( menu.GetSprite() );
// Draws the green box.
Window.Render( green.GetSprite() );

// Displays the updated window.
Window.Update();

}


That's my event loop, followed by my rendering and updating functions. I have no idea what the problem is, other than the fact that everything inside the window goes much faster if I'm moving the mouse.

11
Window / Speedup when moving mouse
« on: September 19, 2011, 01:28:54 pm »
Thanks for the swift reply  8) .

I've taken the clearing, drawing, and displaying out of the event loop, and put them right outside the event loop ( into the main game loop ). It actually became faster than before ( frame issue perhaps? ).

EDIT: From a bit more testing, I believe that the speedup is actually due to the window refreshing every time I move the mouse. Does the window do that every time I move the mouse? If so, I might like to know how to disable that ( or work around it ).

12
Window / Speedup when moving mouse
« on: September 19, 2011, 12:39:57 pm »
When I move the cursor inside the window while having other things going on ( animation, sprite movement ), everything goes much faster. I haven't limited frame rate yet, though when I attempted to I got no change in result. My problem is exactly like the person who created this thread:

http://www.dreamincode.net/forums/topic/241952-sfml-speedup-with-mouse-movement/

The Youtube video he provides has the exact same problem, and his code is structured just like mine. If my code needs to be shown, I can, though I see no difference in the design we both have.

I'm using SFML 1.6 ( stable ), compiling in Visual Studio 2008 ( C++ ).

13
Window / Speedup when moving mouse
« on: September 19, 2011, 12:39:37 pm »
When I move the cursor inside the window while having other things going on ( animation, sprite movement ), everything goes much faster. I haven't limited frame rate yet, though when I attempted to I got no change in result. My problem is exactly like the person who created this thread:

http://www.dreamincode.net/forums/topic/241952-sfml-speedup-with-mouse-movement/

The Youtube video he provides has the exact same problem, and his code is structured just like mine. If my code needs to be shown, I can, though I see no difference in the design we both have.

I'm using SFML 1.6 ( stable ), compiling in Visual Studio 2008 ( C++ ).

14
SFML projects / Clashing Cubes
« on: August 17, 2011, 03:03:36 am »
Great job! Love the basic puzzle style of it, along with the clean and professional title screen/game-play. Being able to choose the color of your cube also adds to it :).

*Thumbs up*

Pages: [1]
anything