Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Socket Server - Sending and recieving positions/ect(SDL)

$
0
0
Hi guys!
I'm quite new to this forum, But I heard it was a good one =D
So I created a small application to kind of test around with sockets.
Here is what I have so far:
#include <sdl.h>
#include <winsock2.h>

SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *player = NULL;

int player_x = 0;
int player_y = 0;

bool gamerunning = true;
bool keys[323] = {false};

void add_image(int x, int y,
               int srcX, int srcY,
               int width, int height,
               SDL_Surface* image, SDL_Surface* destination) {
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);
    
    WSAStartup(Version, &wsa);
    
    SOCKET Listen;
    SOCKET Connect;
    
    Listen = socket(AF_INET, SOCK_STREAM, 0);
    Connect = socket(AF_INET, SOCK_STREAM, 0);
    
    SOCKADDR_IN Server;
    
    Server.sin_addr.s_addr = inet_addr("192.168.2.2");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);
    
    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
    
    listen(Listen, 4);
    
    int size = sizeof(Server);
     
     SDL_Rect src;
     src.x = srcX;
     src.y = srcY;
     src.w = width;
     src.h = height;
     
     SDL_Rect dst;
     dst.x = x;
     dst.y = y;
     dst.w = width;
     dst.h = height;
     
     SDL_BlitSurface(image,NULL,destination,&dst);
}

void set_trans(SDL_Surface* image,int r,int g,int B)/> {
     SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format, r, g, B)/>);
}

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO);
    
    SDL_WM_SetCaption("Test-Server",NULL);
    
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    background = SDL_LoadBMP("background.bmp");
    player = SDL_LoadBMP("player.bmp");
    
    set_trans(player, 0, 0, 0);
    //Server Stuff
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);
    
    WSAStartup(Version, &wsa);
    
    SOCKET Listen;
    SOCKET Connect;
    
    Listen = socket(AF_INET, SOCK_STREAM, 0);
    Connect = socket(AF_INET, SOCK_STREAM, 0);
    
    SOCKADDR_IN Server;
    
    Server.sin_addr.s_addr = inet_addr("My IP address is here");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);
    
    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
    
    listen(Listen, 4);
    
    int size = sizeof(Server);
    //Server Stuff End
    
    while (gamerunning) {
          SDL_Event event;
          
          if (SDL_PollEvent(&event)) {
               if (event.type == SDL_QUIT) {
                    gamerunning = false;
               }
               if (event.type == SDL_KEYDOWN) {
                    keys[event.key.keysym.sym] = true;
               }
               if (event.type == SDL_KEYUP) {
                    keys[event.key.keysym.sym] = false;
               }
          }
          
          if (keys[SDLK_RIGHT]) {
               player_x +=1;
          }
          if (keys[SDLK_LEFT]) {
               player_x -=1;
          }
          if (keys[SDLK_UP]) {
               player_y -=1;
          }
          if (keys[SDLK_DOWN]) {
               player_y +=1;
          }
          
          add_image(0,0,0,0,800,600,background,screen);
          add_image(player_x,player_y,0,0,32,32,player,screen);
          
          SDL_Flip(screen);
    }
    SDL_Quit();
    return 0;
}

Basically, What this does is adds a window and image.
On this image, is another moveable image.
Upon pressing the left, right, up, or down key, The smaller image on top of the background moves accordingly.
I'm not POSITIVE it's connecting to the internet, But I am assuming.
So my question is:
If this is my server, What do I need to add to my client?
If I have the client and server mixed up: Feel free to let me know, as well.
I can EZ re-add the server code into the client to connect, I've made a connecting application once before.
But I'm stumped on the idea of multiple clients being able to move their own image.
For example:

Three players connect to eachother.
One moves, and it moves his character's image.
The second player moves, and it moves his character's image (But not the other players).
The third player moves, and it moves his character's image (But not the other players).

I am completely brain-washed on this idea. It's really confusing me on how to make it move one's image, not the other player's, and shows up the character moving on other people's screen.

Sorry, I'm trying to understand this while i'm typing xD

Thanks guys, for the help =D
-CPPB

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>