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

Pages: [1]
1
Network / Connected UDP
« on: October 02, 2010, 04:53:31 pm »
Thanks buddy. It really helped. Now I've got both way working transmission. :)

Bye

2
Network / Connected UDP
« on: September 24, 2010, 06:30:40 pm »
Hi,
A few days ago my friend wrote a post concerning using UDP. He was suggested that he should create Connected UDP on his own and also to start a new thread concerning that. So that's what I'm doing now.

We tried to google it, but we didn't find the solution.

This is the modified code. I combined it to a single program in hope that the variable IPClient stores some additional information about the client , but it doesn't seem so.

Code: [Select]

#include <SFML/Network.hpp>
#include <iostream>

using namespace std;
using namespace sf;

void server(int);
void client(int);


IPAddress IPClient;

int main()
{
    cout << "Type: \'s\' to run server or \'c\' to run client." <<endl;
    char input;
    cin >> input;

    if(input == 's')
    {
        server(0);
        client(1);
    }
    else if(input == 'c')
    {
        client(0);
        server(1);
    }

    system("Pause");
}

void server(int param)
{
    SocketUDP Server;

    if(param == 0)
    {
        if (!Server.Bind(10023))
        {
            cout << "Error" << endl;
        }
    }
    else if(param == 1)
    {
        if (!Server.Bind(10024))
        {
            cout << "Error" << endl;
        }
    }


    cout << "Server initialized." << endl;

    IPAddress SenderIP;
    unsigned short SenderPort;
    sf::Packet Packet;

    while (true)
    {
        sf::Socket::Status SocketStatus = Server.Receive(Packet, SenderIP, SenderPort);

        cout << endl <<endl << "Socket status: " << SocketStatus  << endl << "---------------" <<endl;

        if(SocketStatus == sf::Socket::Done)
        {
            // Extract the message and display it
            string message;
            Packet >> message;
            cout << "IP: " << SenderIP << endl << "Port: "<< SenderPort << endl << "Message: " << message<< endl;
            IPClient = SenderIP;
            return;
        }

    }

    Server.Close();
}

void client(int param)
{
    cout << "Client Initialized." << endl;

    int ServPort;
    IPAddress ServIP;

    if(param == 0)
    {    
        IPAddress ServIPBuff("here comes server IP"); // !!!
        ServIP = ServIPBuff;
        ServPort = 10023;
    }
    else if(param == 1)
    {
        ServIP = IPClient;
        ServPort = 10024;
    }

    if (!ServIP.IsValid()){
        cout << "Niepoprawny adres serwera" << endl;
    }

    SocketUDP Client;
    string Message = "Hi, I'm a client !";
    sf::Packet PacketToSend;
    PacketToSend << Message;

    cout << "Press Enter to send packet." <<endl;
    getchar();

    char input = getchar();
    while(input == 10)
    {

        if (Client.Send(PacketToSend, ServIP, ServPort) != Socket::Done)
            cout << "Pierdolony error!" << endl;
        else
            cout << "Message sent to server : \"" << Message << "\"" << endl;

        input = getchar();
        if(input == 'z') break;
    }

    Client.Close();
}



It simply listens for packets in server mode and resends a packet to the client after server receives one.
In client mode it sends a packet (or more) to a server and after you type 'z'. It starts listening for packets sent back from the server.

Everything works fine on LAN. But when I run the server mode on a real server (RPS with public ip) there occurs a problem. Server receives a packet from my computer (non-public, local ip) and than tries to send a packet back. Unfortunately it never is received by my computer.

So how should I modify it to have the effect of Connected UDP?

Bye

3
General / Some beginer's remarks
« on: September 02, 2010, 11:53:07 am »
Sorry, You're right. I must have found it through "Search", because I've never consciously opened 2.0 documentation. :oops:

4
General / Some beginer's remarks
« on: September 02, 2010, 10:42:00 am »
Well, than I'll try with the pointers. :)

I have the last one question for now. As you rightly pointed out DebugTextBlock has nothing to do with the resources. Well, that was just a temporary solution, and I wanted to put it in another structure later. But after your recent post, I'm not sure if it makes sense to have this kind of global element.

This object could be created any time it's needed. In my case, a few times per second (before each screen refresh), or it could be created only once and stored somewhere. Which option would be better? (I'm concerned about the time consumed during initialization.)

5
General / Some beginer's remarks
« on: September 01, 2010, 10:56:28 pm »
Ok, I would like it this way, but I can't find another solution.  For example textures? They are used in many modules - which means different filse.
1) Must be loaded
2) In Draw()
3) In function responsible for detecting collisions
4) Maybe some others

Do you have any ides how to handle this?

To be honest, I really dislike my idea with that structure. I used to work with c# before and there was a good solution: static class

I also don't like the idea of using extern variables, because it requires 2 "copies" of whole set of variables. One in main.cpp and another in a hearer file.

Like I said something like c# static class would be perfect :D

6
General / Some beginer's remarks
« on: September 01, 2010, 07:36:50 pm »
Thanks for fast reply.

Quote
bugy wrote:
I get an error: "error: 'class sf::RenderWindow' has no member named 'SetTitle'|"
But the documentation claim it has.
Then you're probably looking at a documentation that doesn't correspond to your SFML version.


I'm using sfml 1.6 and I'm using proper documentation. Seems there must be a bug.



I have one more problem.

I want to have some global variables, available from all *.cpp files. I created a structure with them and placed it over main():
Code: [Select]

Resources res;

int main()
{


that's the structure:
Code: [Select]
struct Resources
{
    public:
    sf::RenderWindow App;
    sf::Image Image;
    sf::Sprite Sprite;
    sf::String DebugTextBlock;
};


The problem occurs when I want to draw DebugTextBlock - it simply doesn't show up.. below there is a part of another *.cpp file:
Code: [Select]

    res.DebugTextBlock.SetFont(sf::Font::GetDefaultFont());
    res.DebugTextBlock.SetText("---------------");//buffer.str());
    res.DebugTextBlock.SetSize(50);
      res.DebugTextBlock.SetCenter(res.DebugTextBlock.GetRect().GetWidth(), 0);
    res.DebugTextBlock.SetColor(sf::Color(100, 100, 100));
    res.DebugTextBlock.SetRotation(0.f);
    res.DebugTextBlock.SetScale(1.f, 1.f);
    res.DebugTextBlock.Move(780.f, 10.f);

    res.App.Draw(res.DebugTextBlock);


If I change the code a little bit and declare the object right above this code than it works.

I think the problem is with initialization of that DebugTextBlock inside the structure, but I can't fix it. Help me please.[/quote]

7
General / Some beginer's remarks
« on: September 01, 2010, 05:33:18 pm »
Hi,
I'm new to sfml and I found some facts quite interesting.

1)
The line below is ok.
Code: [Select]
sf::RenderWindow object(sf::VideoMode(800,500), "hello world");

but when I try to call that constructor again I get an error (like it doesn't exist):
Code: [Select]
object.RenderWindow(sf::VideoMode(800,500), "hello world");

I looked it up in the documentation and there are two possibilities:
Code: [Select]
sf::RenderWindow::RenderWindow   (   VideoMode   Mode,
const std::string &   Title,
unsigned long   WindowStyle = Style::Resize | Style::Close,
const WindowSettings &   Params = WindowSettings()
)


and

Code: [Select]
sf::RenderWindow::RenderWindow   (   WindowHandle   Handle,
const WindowSettings &   Params = WindowSettings()
)

I wanted to set the title after initializing the object and I finally managed to do it by calling Create()

What's the reason?

2)
The second thing is related to the first one. I mentioned that I wanted to set a title of a window after it's already initialized. There is a special method for this: SetTitle().
But when I try:
object.SetTitle("hello");
I get an error: "error: 'class sf::RenderWindow' has no member named 'SetTitle'|"
But the documentation claim it has.

WHY?

3)
The third thing concerns mouse control
sf::Event::MouseMoved; // this is an event type and it works fine
but:
event.MouseMoveEvent.X; // this causes an error ("invalid use of struct...")
to get  cursor position I had to call:
event.MouseMove.X;

The thing is that auto completion (I use code::blocks) suggests only event.MouseMoveEvent.X
while event.MouseMove.X is "invisible"

It seems a bit strange. Why is it like that?

4)
The last thing is the sign "|" in function arguments - like here:
Code: [Select]
Text.SetStyle(sf::String::Bold | sf::String::Italic | sf::String::Underlined);

I see it has something to do with enums and I'm interested if it is possible to write such a function in pure c++.

It looks like it takes any number of arguments, which are in fact just one. It also seems that the sequence doesn't metter.

Can somebody explain this to me? ( not the use of it, but how to make it) I tried to google it but I couldn't make out good keywords and found nothing helpful.

I appreciate your help

Bye.

Pages: [1]