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

Pages: [1] 2 3 4
1
Network / Re: [SFML 2]List of all open games
« on: January 08, 2013, 07:22:23 pm »
I found a solution to my problem which is not exactly what I wanted, but the best I could get.

If anyone is interested in what I have done:

My server starts this UDP thread as in my last post.
If a client is looking for games, it will create an udp socket and send a broadcast message to all servers:
if( udp_socket_ )
        delete udp_socket_;

udp_time_ = 0.0f;

udp_socket_ = new sf::UdpSocket();
sf::Packet pack;

if( udp_socket_->send( pack, sf::IpAddress("255.255.255.255"), 12346) != sf::Socket::Done )
        return;

udp_socket_->setBlocking( false );
 

In the update-method of this game state my client is now waiting for answers from the servers, and if there is one, it will put it into a server list. This "waiting-procedure" will go on for about 10 seconds.
if( udp_socket_ )
{

        sf::IpAddress sender;
        unsigned short sender_port;    
        sf::Packet pack;

        if( (udp_socket_->receive(pack, sender, sender_port)) == sf::Socket::Done )
                games_list->addItem(sender.toString(), sender);

        udp_time_ += game_env_.render_time;

        if( udp_time_ >= 10.0 )
        {
                udp_time_ = 0.0f;
                delete udp_socket_;
                udp_socket_ = 0;
        }
}
 

By now I've tested this method only with one server. But this server is found without crashing and so I am confident, that it will also work with more than one server ;).

2
Network / Re: [SFML 2]List of all open games
« on: January 07, 2013, 12:52:01 am »
No it is not exactly my problem. It's right, that I never receive more than one message from my servers, but I don't want to receive more messages.

My client sends one message to the server, the server answers and the client connects a TCP socket to the TCP-Server. This is the way it is supposed to work. So the udp broadcast is only necessary to contact the server and to find out how many servers exist.

Here is the code, my server handles udp messages.
The server is started in one seperate thread, which handles all TCP connections. This thread starts a new internal thread, which is listenting with an UDP socket.
The code of the UDP thread:
// Create a socket to receive a message from anyone
        sf::UdpSocket socket;

        // Listen to messages on the specified port
        if (socket.bind(port_ + 1) != sf::Socket::Done)
                return;
        std::cout << "Server is listening to port " << port_ + 1 << ", waiting for a message... " << std::endl;
        while( !quit_ )
        {
                // Wait for a message
                sf::Packet packet;
                sf::IpAddress sender;
                unsigned short senderPort;
                if (socket.receive(packet, sender, senderPort) != sf::Socket::Done)
                        return;

                std::cout << "Message received from client " << sender << std::endl;

                sf::Packet outPack;
                outPack << sf::IpAddress::getLocalAddress().toString();
                socket.send( outPack, sender, senderPort );
        }

        socket.unbind();
 

I see your point, that there must be messages, which are not received by my client, but in my opinion there are no more messages from the server. Or do I mess something up?
I also tried to create and bind the socket in the while loop and unbind it at the end of the loop, so that one socket is only waiting for one connection and sends one message and is unbind after that, but it didn't help.

3
Network / [SFML 2]List of all open games
« on: January 06, 2013, 02:47:33 pm »
Hello,

currently I am working on a little network-game using SFML-network and I have a problem listing the open games.

In my current approach the server creates an own udp-thread in which it is waiting for broadcasting messages. The clients send such broadcasting messages to receive an answer from any server which is waiting for messages. This works for one server. But I want to list all open games, hence all servers which are waiting for clients. So I thought, that I need a while-loop to handle all the packets comming from the different servers.

But therefore I would have to use non-blocking udp-sockets, because a blocking one would end up waiting for packtes to receive, but none is comming in. So my code looks like:
sf::UdpSocket udp_socket;
sf::Packet pack;

if( udp_socket.send( pack, sf::IpAddress("255.255.255.255"), 12346) != sf::Socket::Done )
        return;

udp_socket.setBlocking( false );

sf::IpAddress sender;
unsigned short sender_port;    
while(udp_socket.receive(pack, sender, sender_port) == sf::Socket::Done )
{
        std::cout << "received" << std::endl;
}
 

With this code I never receive any packets, because the status of the receive-method is always 1(sf::Socket::NotReady). If I use the blocking udp_socket, I receive one packet from my existing server, but after that the next receive-command is blocking again and it is not going on.

Do you have any idea, what would be a possible solution to list all servers with a udp-broadcast?
My idea would be that the server sends a second packet after the first packet(after a short time of waiting) and if the client receives this "poisoned" packet, it will leave the loop. But I wonder, if there is a better solution.

Thanks for your help,
Fred

EDIT: Sorry I forgot to mention, that I am using SFML 2, which I downloaded a few days ago from git repository.

4
Quote from: "Joshua Flynn"
I have in mind precisely what it would do, but generally in programming, bugs occur when the assumptions of the user (IE other than me) are different from the assumptions of the program. In this case I want to see if other people instinctively have the same concepts I do before I make it part of the design.

I hope you realized that there are many different opinions and so I recommend you not to implement those operators.

Quote from: "Joshua Flynn"
Std::list requires log(n) time to get the size of the list, when it's perfectly possible to get a log(1) return, including iterator numeric position. Given how I have no idea how it operates under the covers (if it does log(n) for size returns, what else does it do inefficiently?)

I don't think that implementing size in O(n) is just for fun. Mostly it is by the way implemented in O(1). If it is not, it is mostly O(n) because of other performance issues. For example would std::list::splice be in O(n), if size is in O(1), because the new size of the list has to be computated. If size is O(n), splice would be O(1).
I'd prefer size in O(1) and splice in O(n), but there are obviously different opinions about this topic.

But there are many other different STL container.
std::deque for example can be used similar to std::list, provieds size in O(1) and individual element access in O(1).

Just have a look at all STL containers and think about using them.
http://www.cplusplus.com/reference/stl/

I think the STL containers are rather performant and I don't think that you would easily create a new one which is better than the others ;).

5
Graphics / R6025 Error, pure virtual function call
« on: September 04, 2011, 09:55:57 pm »
I don't think that you have to do many changes. Of course you can read millions of articles, but probably your game will never be finished ;).
My suggestion is: Just try to do it your way and you will see, what are the assets and drawbacks and you will maybe use another system in your next project - or not, if your system works well for you.
What you want to do is probably programming a game and not winning design contests(of course a good design is necessary, but I don't think your design is that bad).

To solve your problem there are two solutions.

First:
Do not use any pointers. Just save a copy of the objects in your list. So you can create temporarily objects and they will be copied to the list and the old object will be deleted, but your copy will still remain. But in that case: Use references to get your objects from the list.
A small example:
Code: [Select]

class object_manager
{
public:
    object& get_object( unsigned int index );
private:
    std::list<object> objects;
}


Second:
You can still use pointers, but have to use pointers, if you create an object, too.
Example:
Code: [Select]

player* tmp_player = new player( sf::Vector2<float>(10,10), sf::Sprite(sub), &world.GetWindow()->GetInput(), world.GetWindow());
world.entityList.push_bakc(tmp_player);

But you have to delete the memory, if you want to delete an object(or all objects at the end of the game). Therefore simply call delete before removing an entity from your list.

Another point you have to think about is your image-management. In your code you're creating a temporary image "sub", which is destroyed, if the scope is left. The sprite would probably displayed as a white rectangle. So you should save your images as well. I wrote a tutorial about an image-manager a few months ago in the wiki: Have a look at it.

I don't think that you should read much about design-patterns but rather read some stuff about memory-handling in C++.

6
Network / network-management with SFML 2
« on: September 03, 2011, 11:42:34 am »
What a stupid mistake, thank you :).

Currently I am thinking about a lobby system. Is there any way to search for all servers? Games can be hosted by several people with different ips. My plan was to connect to every server and get a "game-information" message wich is diplayed. But how to find out which ips are hosting a game? I don't think that it is very clever, if I just step through all 255 possible ips and try to connect. Is there an easier way?

7
Network / network-management with SFML 2
« on: August 31, 2011, 01:12:42 pm »
Hello,

I am pretty new to networking and have just read a few tutorials about it and read the SFML documentation and treid to programm a little network-manager.
It's nothing special. One user should be able to be the host and an other one should be allowed to join the "game".

I didn't want to use a seperate server-application, so the user can choose if he wants to host a game and "be" the server.
Therefore I have a network-manager class. It does all the server-stuff(selector and listener). This network-manager is run in a sperate thread, if the user hosts a game and the methode looks like this:
Code: [Select]

while( true )
{
if( selector_.Wait() )
{
if( selector_.IsReady( listener_ ) )
{
// listener is ready, so there is a pending connection
sf::TcpSocket* client = new sf::TcpSocket;
if( listener_.Accept( *client ) == sf::Socket::Done )
{
clients_.push_back( client );
selector_.Add( *client );
client->SetBlocking( false );
std::cout << "GAME_INFO: Client connected with IP " << client->GetRemoteAddress() << std::endl;

}
}
else
{
// Check whether the other objects are ready
for( std::list<sf::TcpSocket*>::iterator it = clients_.begin();
it != clients_.end(); ++it )
{
sf::TcpSocket client = **it;
if( selector_.IsReady( client ) )
{
sf::Packet packet;
sf::Socket::Status state = client.Receive( packet );
if( state == sf::Socket::Done )
{
handle_message( client, packet );
}
}
}
}
}
}

Basiclly it's taken from the documentation. But now I have a problem. A use may disconnect for any reason. In this case I am not able to remove the socket.
What I tried:
Code: [Select]

for( std::list<sf::TcpSocket*>::iterator it = clients_.begin();
it != clients_.end();)
{
sf::TcpSocket client = **it;
if( selector_.IsReady( client ) )
{
sf::Packet packet;
sf::Socket::Status state = client.Receive( packet );
if( state == sf::Socket::Done )
{
handle_message( client, packet );
++it;
continue;
}
else if( state == sf::Socket::Disconnected )
{
selector_.Remove( client );
std::cout << "Client disconnected with IP " << client.GetRemoteAddress();
it = clients_.erase( it );
}
}
}


But this is not working. If i put "++it" to the end of the loop, my socket won't receive any messages. Even if the socket is not removed, it will not catch the welcome-message which I send to the clients.

Has anyone already programmed a network-manager with SFML 2 and can give me some hint? Or would you do it in a completly other way?

Thanks for your help,
Fred

8
Network / [SFML 2.0] IPAddress::ToString is not working
« on: August 30, 2011, 11:12:39 am »
Okay, thank you.

I used an old version of SFML 2, so there must have been the problem. I upgraded to a new one and now it's working.

9
Network / [SFML 2.0] IPAddress::ToString is not working
« on: August 29, 2011, 03:56:25 pm »
The other modules are working working well. And I compiled them all together. So I don't think, that I am using a wrong library.

Is my code working for you? If it does, there must be something wrong with my libs.

10
Network / [SFML 2.0] IPAddress::ToString is not working
« on: August 28, 2011, 05:59:05 pm »
Hello,

if I use the IPAddress::ToString function I allways get a "Debug Assertion Failed"-error.
The expression is: _CrtIsValidHeapPointer( pUserData )

My very simple code is:
Code: [Select]

int main( int argc, char* argv[] )
{
sf::IpAddress ip("192.168.1.1");
std::cout << ip.ToString();
return 0;
}


Do I use this function in a wrong way or is it maybe a bug in SFML 2.0?

Thanks for your help,
Fred

11
Window / Prevent window repaint
« on: May 14, 2011, 12:53:09 pm »
The usual way is to draw the scene again and again.
but if you don't want to, just don't call RenderWindow::Clear() and RenderWindow::Draw().
But you have to implement your own draw-function, which is only called if there is movement in your scene.

12
Graphics / How to take screenshots
« on: May 13, 2011, 03:37:53 pm »
It is called "screenshot". Hence it captures what's on the screen. And the overly is on the screen, hence it is captured ;).
The easiest way would probably be not to use these FPS-applications, if you want to create a screenshot.

13
Graphics / How to take screenshots
« on: May 13, 2011, 01:26:51 pm »
If you're programming c++, you should use c++ ;).

I recommend std::stringstream to you. It should look like this:
Code: [Select]

std::stringstream filename;
filename << "screenshots/screenshot_" << screenshot_num_ << ".png";
Image.SaveToFile( filename.str() );

14
Graphics / Sprite Help
« on: April 28, 2011, 11:31:16 am »
That was my fault. I am sorry. I just saw this code and thought it would be possible to use it in this way. But actually it is logical, because KeyEvent::Shift is not a key code. At least I now know, what it is good for ;).

15
Window / That annoying little window
« on: April 28, 2011, 11:24:19 am »
As I mentioned in my last post, this would not work. At least not, if you're running a "Gui Application".
A window that does not exist will not be shown ;).

If you're using a console application, you can make your console visible again by using your code.

Pages: [1] 2 3 4
anything