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

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

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

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

4
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++ ).

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

Pages: [1]