SDL and Winsock - Flashing images?

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »

113 Replies - 3084 Views - Last Post: 04 July 2013 - 11:15 PM Rate Topic: -----

#31 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 23 June 2013 - 04:12 PM

Also... what is the point of having your non-blocking recv() calls if you are going to block when you call fgets() anyway?
Was This Post Helpful? 0
  • +
  • -

#32 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 25 June 2013 - 02:07 PM

What do you need to know that isn't there?
You said that I stripped out too much code, But I don't think I did :o
Anyways, Here's the send and receive part, I think I stated before:

Server:
while(true) {
     itoa(clients_Xconnected,clients_connected,10);
     send(current_client,clients_connected,sizeof(clients_connected),0);
     
     recv(current_client,player_sX,sizeof(player_sX),0);
     
     send(current_client,player_sX,sizeof(player_sX),0);
     
     recv(current_client,player_sY,sizeof(player_sY),0);
     
     send(current_client,player_sY,sizeof(player_sY),0);
     //ect...


Client:
while(gamerunning == true) {
        FD_ZERO(&fdread);
        FD_SET(sock, &fdread);
        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
                 gamerunning = false;
        }
        if (ret > 0) {
             if (FD_ISSET(sock, &fdread)) {
                  recv(sock,client_Xconnected,sizeof(client_Xconnected),0);
             }
        }
        
        if (client_connected_frame == 0) {
             client_connected = atoi(client_Xconnected);
             client_connected_frame +=1;
        }
        amount_of_players = atoi(client_Xconnected);
        
        if (amount_of_players_frame == client_connected) {
             amount_of_players_frame +=1;
        }
        if (amount_of_players_frame > amount_of_players) {
             amount_of_players_frame = 0;
        }
        
        //Moving code (Up,Down,Left,Right)

        itoa(player_x,player_sX,10);
        send(sock,player_sX,sizeof(player_sX),0);
        
        FD_ZERO(&fdread);
        FD_SET(sock, &fdread);
        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
                 gamerunning = false;
        }
        if (ret > 0) {
             if (FD_ISSET(sock, &fdread)) {
                  recv(sock,player_sX,sizeof(player_sX),0);
             }
        }
        player_rX = atoi(player_sX);
        
        itoa(player_y,player_sY,10);
        send(sock,player_sY,sizeof(player_sY),0);
        
        FD_ZERO(&fdread);
        FD_SET(sock, &fdread);
        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
                 gamerunning = false;
        }
        if (ret > 0) {
             if (FD_ISSET(sock, &fdread)) {
                  recv(sock,player_sY,sizeof(player_sY),0);
             }
        }
        player_rY = atoi(player_sY);
        
        add_image(0,0,0,0,800,600,background,screen);
        add_image(player_x,player_y,0,0,32,32,player,screen);
        
        if (player_rX != player_x && player_rY != player_y) {
             gamerunning = true;
        } else {
               gamerunning = false;
        }
        add_image(player_rX,player_rY,0,0,32,32,player,screen);

        //ect...


Anything else I may have left out, Please let me know!

Thanks,
-Fysez
Was This Post Helpful? 0
  • +
  • -

#33 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 25 June 2013 - 05:34 PM

You can simplify with a struct to send and receive data, it would not be required to convert numbers from ASCII to ints etc.:

struct mydata
{
  int connected;
  int x, y;
};



and a function to read :

bool my_receive( SOCKET &sock, struct mydata &data)
  fd_set fdread;
  FD_ZERO(&fdread);
  FD_SET(sock, &fdread);
  bool gamerunning = true;
  
  if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
    gamerunning = false;
  }

  if (ret > 0) {
    if (FD_ISSET(sock, &fdread)) {
       recv(sock, data, sizeof(struct mydata), 0);
  }

  return gamerunning;
}



perhaps one to send as well.
Was This Post Helpful? 0
  • +
  • -

#34 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 25 June 2013 - 09:16 PM

View Post#define, on 25 June 2013 - 05:34 PM, said:

You can simplify with a struct to send and receive data, it would not be required to convert numbers from ASCII to ints etc.:

struct mydata
{
  int connected;
  int x, y;
};



and a function to read :

bool my_receive( SOCKET &sock, struct mydata &data)
  fd_set fdread;
  FD_ZERO(&fdread);
  FD_SET(sock, &fdread);
  bool gamerunning = true;
  
  if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
    gamerunning = false;
  }

  if (ret > 0) {
    if (FD_ISSET(sock, &fdread)) {
       recv(sock, data, sizeof(struct mydata), 0);
  }

  return gamerunning;
}



perhaps one to send as well.


Okay thanks =P
I'll test around with this and let you know my results
(Only said this to let you know I read it).

-Fysez
Was This Post Helpful? 0
  • +
  • -

#35 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 25 June 2013 - 10:53 PM

Okay so I had a (Weird?) question.
This is making me draw a blank and I know it's a dumb question.
But how do I receive all of the things in the struct?
For example, Here's my send(...) code:
struct send_data1 {
            int send_clients_connected1;
            int send_player_x1;
            int send_player_y1;
     };
     send_data1 SD1;
     SD1.send_clients_connected1 = clients_connected;
     SD1.send_player_x1 = player_x;
     SD1.send_player_y1 = player_y;
     
     send(current_client, (char *)&SD1, sizeof(send_data1), 0);


First of all... Is this even correct?

Second,
How do I receive this?

Here's what I'm having troubles on:

bool my_receive(SOCKET &sock, struct mydata &data);
        
        fd_set fdread;
        FD_ZERO(&fdread);
        FD_SET(sock, &fdread);
        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
             gamerunning = false;
        }
        if (ret > 0) {
             if (FD_ISSET(sock, &fdread)) {
                  recv(sock, data, sizeof(struct mydata), 0);
             }
        }

And I get the following errors:
cannot convert `data' from type `WSADATA' to type `char*'
and
invalid application of `sizeof' to incomplete type `mydata'

My mind is losing me when I think of this.

Question:
After I do the recv(...)
how do I add each 'int' individually like int receivedX = player_xInsideOfStruct?

Sorry for my "noob-ish" reply.
But thank you for letting me know what else I could do:)

-Fysez

P.S.
Would adding all of this info into a structure an sending it that way fix my problem? Or is this just to fix up my code a bit and use less lines?
Thanks again

This post has been edited by fysez: 25 June 2013 - 10:54 PM

Was This Post Helpful? 0
  • +
  • -

#36 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 26 June 2013 - 05:25 AM

You need to perform a cast:
recv(..., static_cast<char *>(&data), ...)


Was This Post Helpful? 0
  • +
  • -

#37 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 26 June 2013 - 05:38 AM

Changing to a struct will help you fix the race condition that your old code was running into. This will guarantee that all the related pieces are sent at one time instead of trying to assemble the pieces yourself.

Consider what happens in this timeline when you used to send things separately
you               server
:                 :
                  send connected
                  receive X
try get connected
receive connected
try get X                         <-- no X available because server is still blocked
                  send X
try get Y                         <-- Y now contains X.
recieve Y
:                 :


Was This Post Helpful? 0
  • +
  • -

#38 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 26 June 2013 - 09:32 AM

To differentiate it you could call the struct, Package. The Package will be used to send and receive data. It is in global space outside of main and included in both the client and server - it could be in a header file.

struct Package
{
  int clients_connected;
  int player_x;
  int player_y;
};


#define PACKAGE_SIZE sizeof(struct Package)


int main()
{
  ...
}



The struct Package can be updated with minimal effect on already written code.

Using the struct.

int main()
{
  ...

  struct Package package_receive;
  struct Package package_send;
  int player_x_variable;

  // update package
  package_send.clients_connected = 1;

  // send package
  ret = send(client, 
      reinterpret_cast<char *> (&package_send),
      PACKAGE_SIZE , 0);

  ...

  // receive package
  ret = recv(client, 
      reinterpret_cast<char *> (&package_receive),
      PACKAGE_SIZE , 0);

  // update variable
  player_x_variable = package_receive.player_x; 
}




View Postfysez, on 26 June 2013 - 06:53 AM, said:

Would adding all of this info into a structure an sending it that way fix my problem? Or is this just to fix up my code a bit and use less lines?
Thanks again


I'm not sure why the graphics are flashing. It looks as if the x and y could be swapping (x and y getting read at wrong time - the struct will help there), it could be something to do with your code for SDL, or both. You could try using SDL_Delay (maybe 500ms to 1000ms) temporarily after the flip, to slow the graphics down which might give a clue to what's happening.

The struct simplifies code and functions simplify code making it easier to write and debug. The struct helps you to be certain which piece of data you are receiving.

~

This post has been edited by #define: 26 June 2013 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#39 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 26 June 2013 - 12:41 PM

View Post#define, on 26 June 2013 - 09:32 AM, said:

To differentiate it you could call the struct, Package. The Package will be used to send and receive data. It is in global space outside of main and included in both the client and server - it could be in a header file.

struct Package
{
  int clients_connected;
  int player_x;
  int player_y;
};


#define PACKAGE_SIZE sizeof(struct Package)


int main()
{
  ...
}



The struct Package can be updated with minimal effect on already written code.

Using the struct.

int main()
{
  ...

  struct Package package_receive;
  struct Package package_send;
  int player_x_variable;

  // update package
  package_send.clients_connected = 1;

  // send package
  ret = send(client, reinterpret_cast<char *>(&package_send),
             PACKAGE_SIZE , 0);

  ...

  // receive package
  ret = recv(client, reinterpret_cast<char *>(&package_receive),
             PACKAGE_SIZE , 0);

  // update variable
  player_x_variable = package_receive.player_x; 
}



Hi! Thanks for your awesome reply!
Although I am not using the "Package" structure, I am trying to do just the regular send and receive struct.
I do have another question on this (Yes, I tried to look it up first: No results).
Here's my...

Send Code:
struct send_data1 {
            int send_clients_connected1;
            int send_player_x1;
            int send_player_y1;
     };
     send_data1 SD1;
     SD1.send_clients_connected1 = clients_connected;
     SD1.send_player_x1 = player_x;
     SD1.send_player_y1 = player_y;
     
     send(current_client, (char *)&SD1, sizeof(send_data1), 0);


Receive Code:
bool my_receive(SOCKET &sock, struct mydata &data);
        
        fd_set fdread;
        FD_ZERO(&fdread);
        FD_SET(sock, &fdread);
        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
             gamerunning = false;
        }
        if (ret > 0) {
             if (FD_ISSET(sock, &fdread)) {
                  recv(sock, reinterpret_cast<char *>(&data), sizeof(&my_receive), 0);
             }
        }
        int RC1 = data.send_clients_connected1;//This is my problem


I beleive that my sending and receiving works correctly (Not tested - But I don't get errors)
However, My last line for receiving; I get the following error:
'struct WSADATA' has no member named 'send_clients_connected1'


I'm obviously doing SOMETHING wrong here xD

Thanks again for your help, you two.
I don't say it often, But I seriously appreciate the help you're giving me.
Without you two, I'd be COMPLETELY lost.

-Fysez
Was This Post Helpful? 0
  • +
  • -

#40 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 26 June 2013 - 01:04 PM

Is the receive to the same struct object SD1? Is the socket current_client?

11        recv (current_client, (char *)&SD1, 
                sizeof(send_data1), 0);




If you are creating a function it needs to be outside of main. The structure name has changed.

bool my_receive(SOCKET &sock, struct send_data1 &data)
{
}

int main()
{
  ...
}


Was This Post Helpful? 1
  • +
  • -

#41 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 26 June 2013 - 10:20 PM

Omg I just wrote a long reply about everything I tried and failed, and then I press submit and get sent to "Internet Explorer problems" or whatever.
So I'm going to try to sum up what I said:

I'm stressing out over these problems, It's taking me SO long and it's SO hard to understand this simple problem..
But I'm still getting errors.
I tried this:
bool recv_data1(SOCKET &sock, struct send_data1 &RD1) {
     int recv_clients_connected1;
     int recv_player_x1;
     int recv_player_y1;
}

int main(int argc, char* argv[]) {
...
     while (gamerunning) {
          fd_set fdread;
        FD_ZERO(&fdread);
        FD_SET(sock, &fdread);
        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
             gamerunning = false;
        }
        if (ret > 0) {
             if (FD_ISSET(sock, &fdread)) {
                  recv(sock, reinterpret_cast<char *>(&RD1), sizeof(&recv_data1), 0);
             }
        }
     }
}

and got the error:
`RD1' undeclared (first use this function)

So then I tried adding:

int main() {
...
struct RD1;


before all of that, And got the error:
expected primary-expression before ')' token

Again, I'm stressing out over this, I just want to rip my hair out lol.

Sorry if I seemed to ignore any parts of your reply,
I'm over-complicating this for myself.

I feel like I've tried everything and it seems to fail.

Thanks, Again, for the help. Sorry i'm so difficult.
-Fysez
Was This Post Helpful? 0
  • +
  • -

#42 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 26 June 2013 - 10:59 PM

I think you need to learn the C and C++ language a bit more before you get too engrossed in networking code. Most C/C++ students would have known to declare RD1 as:

C:
struct send_data1 RD1;



C++:
send_data1 RD1;



I recommend this tutorial to try to catch yourself up: http://www.cplusplus...ial/structures/

I already see that your recv_data1 is being seen by the compiler as a function, but you seem to be writing it almost like a structure.
Was This Post Helpful? 0
  • +
  • -

#43 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 27 June 2013 - 09:44 AM

View PostSkydiver, on 26 June 2013 - 10:59 PM, said:

I think you need to learn the C and C++ language a bit more before you get too engrossed in networking code. Most C/C++ students would have known to declare RD1 as:

C:
struct send_data1 RD1;



C++:
send_data1 RD1;



I recommend this tutorial to try to catch yourself up: http://www.cplusplus...ial/structures/

I already see that your recv_data1 is being seen by the compiler as a function, but you seem to be writing it almost like a structure.


Really? Because once I try
send_data1 RD1

I get the error:
`send_data1 RD1' has incomplete type and cannot be defined
Structures are easy, I can do them fine.
RECEIVING THEM on the other hand, Is not the same.
Everything I look up, Do what you guys tell me to do, Ect. gives me errors.
Doesn't seem to work.

-Fysez

This post has been edited by fysez: 27 June 2013 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#44 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 27 June 2013 - 10:29 AM

Here you have created a function when I think you wanted to create a structure called recv_data1.

bool recv_data1(SOCKET &sock, struct send_data1 &RD1) {
     int recv_clients_connected1;
     int recv_player_x1;
     int recv_player_y1;
}


Was This Post Helpful? 0
  • +
  • -

#45 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 27 June 2013 - 02:50 PM

View Post#define, on 27 June 2013 - 10:29 AM, said:

Here you have created a function when I think you wanted to create a structure called recv_data1.

bool recv_data1(SOCKET &sock, struct send_data1 &RD1) {
     int recv_clients_connected1;
     int recv_player_x1;
     int recv_player_y1;
}



So should I do this?:
bool my_receives(SOCKET &sock, struct send_data1 &RD1) {
     struct recv_data1 {
          int recv_clients_connected1;
          int recv_player_x1;
          int recv_player_y1;
     };
}

Was This Post Helpful? 0
  • +
  • -

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »