Server + One that allows input:
Code:
#include <SDL.h>
#include <socket.cpp>
#include <iostream>
#include <sstream>
SDL_Event event;
SDL_Surface *screen;
ServerSocket server;
void apply_surface( int x, int y, SDL_Surface* source)
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, screen, &offset );
SDL_Flip(screen);
}
void clearscreen(){
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
}
class Dot
{
private:
//The X and Y offsets of the dot
int x, y;
public:
//Initializes the variables
Dot();
//Takes key presses and adjusts the dot's velocity
void handle_input();
//Moves the dot
void move();
//Shows the dot on the screen
void show();
int getx();
int gety();
};
Dot::Dot()
{
//Initialize the offsets
x = 0;
y = 0;
}
void Dot::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: y -= 5; break;
case SDLK_DOWN: y += 5; break;
case SDLK_LEFT: x -= 5; break;
case SDLK_RIGHT: x += 5; break;
}
}
}
void Dot::show()
{
SDL_Rect basd;
basd.w = 10;
basd.h = 10;
basd.y = y;
basd.x = x;
SDL_FillRect( screen, &basd, SDL_MapRGB( screen->format, 0x00, 0x99, 0x00 ) );
}
char axw[11];
int Dot::getx(){
return x;
}
int Dot::gety(){
return y;
}
int main(int argc, char* args[])
{
server.StartHosting(4753);
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(500,500,32,0);
clearscreen();
Dot mydot;
Dot hisdot;
bool quit = false;
while(quit == false){
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
quit = true;
}
mydot.handle_input();
clearscreen();
mydot.show();
SDL_Flip(screen);
int xz = mydot.getx();
int yz = mydot.gety();
/*
char* sxz,*syz;
itoa(xz,sxz,10);
itoa(yz,syz,10);
string az = sxz;
string bz = syz;
string hae = az + " BA " + bz;
char *ha;
strcpy(ha,hae.c_str());
server.SendData(ha);
*/
stringstream oout;
oout << xz;
stringstream ooutz;
ooutz << yz;
string az = oout.str();
string bz = ooutz.str();
string zzz = az + " BA " + bz;
memcpy(axw," ",11);
strcpy(axw,zzz.c_str());
}
SDL_Delay(100);
server.SendData(axw);
SDL_Delay(100);
}
}
Client / one that shows what the guy with the server did:
Code:
#include <Socket.h>
#include <SDL.h>
#include <iostream>
#include <sstream>
SDL_Event event;
SDL_Surface *screen;
ClientSocket client;
void apply_surface( int x, int y, SDL_Surface* source)
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, screen, &offset );
SDL_Flip(screen);
}
void clearscreen(){
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
}
class Dot
{
private:
//The X and Y offsets of the dot
int x, y;
public:
//Initializes the variables
Dot();
//Takes key presses and adjusts the dot's velocity
void handle_input(int xx,int yy);
//Moves the dot
void move();
//Shows the dot on the screen
void show();
int getx();
int gety();
};
Dot::Dot()
{
//Initialize the offsets
x = 0;
y = 0;
}
void Dot::handle_input(int xx,int yy)
{
y = yy;
x = xx;
}
void Dot::show()
{
SDL_Rect basd;
basd.w = 10;
basd.h = 10;
basd.y = y;
basd.x = x;
SDL_FillRect( screen, &basd, SDL_MapRGB( screen->format, 0x00, 0x99, 0x00 ) );
}
int Dot::getx(){
return x;
}
int Dot::gety(){
return y;
}
int main(int argc, char* args[])
{
string aaw;
char coords[11];
client.ConnectToServer("127.0.0.1",4753);
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(500,500,32,0);
clearscreen();
int zx,zy;
Dot mydot;
Dot hisdot;
bool quit = false;
while(quit == false){
zx = 50;
zy = 50;
for(int p = 1; p<500; p++){
aaw = coords;
if (aaw.size() > 4){
// 100 BA 100
int pos = aaw.find("BA");
string px1 = aaw.substr(0,3);
string px2 = aaw.substr(pos+3,3);
zx = atoi(px1.c_str());
zy = atoi(px2.c_str());
}
if(zx < 3){
zx = 150;
}
if(zy < 3){
zy = 150;
}
}
mydot.handle_input(zx,zy);
clearscreen();
mydot.show();
SDL_Flip(screen);
SDL_Delay(100);
client.RecvData(coords,11);
SDL_Delay(100);
}
}