Pages

Tuesday, August 7, 2012

A server socket class

Hello

I'm going to describe a new class, (last one before I start with the server)

We already have a class to store persistent data, now we need a way to communicate with the world.

I need a class to start a TCP socket server then accept incoming TCP connections , I need to be able to send data to any of the clients too, and finally enqueue the incoming data so I can process it when I need to do.

What the class is going to do is:
  1. Open a server socket in a port X
  2. Start a thread that will be listening in port X
  3. When we receive connections it will save the client info (ip/port) to a vector so we can communicate with the client, every client starts a new thread to get the incoming data from each client (it is a blocking-socket server)
  4. The data is stored into two different queues depending if the client has logged in  or not, I made that because I will apply different priority for the queues , the main (client updates) must be processed with maximum priority other way clients will be lagging.. in the other hand client login request can wait a pair of seconds without big problems.
To achieve that I use two classes, first one is cNetwork, it is the main server, second one os cClient, used to store information from every connected client, the header from both classes are stored in the same file:


<network.h>
#ifndef NETWORK_H
#define NETWORK_H

#define MAXLEN 1024
#include "SDL/SDL.h"
#include "SDL/SDL_net.h"
#include "SDL/SDL_thread.h"

#include <vector>
#include <queue>
#include <iostream>
#include <sstream>

using namespace std;

class cNetwork;

class cClient //the connected clients
{
public:
//functions
cClient();//creator
virtual ~cClient();//destructor
void StartThreads();//Start listener thread
static int listener( void *data );//Listener Thread
void Send(const char* Msg);//Send data to client

//variables
cNetwork *_server;//To comunicate with main server instance
TCPsocket socket;//client socket
SDL_Thread *ListenerThread;//The network client thread
int         threadReturnValueclient;
Uint16 UID;//Unique identifier
bool Connected;//socket connected?
bool Login; //loged in?

};

class cNetwork//main network server class
{
      friend class cClient;

    public:
        //functions
        cNetwork();
        virtual ~cNetwork();
        void Start(Uint16 MAXUsers, Uint16 Port);
        void CleanClient(unsigned int Client);//clean a client so we can use it
        static int Master( void *data );//Master listener function/thread
        void ClearClients();//Clear disconnected clients
        void Finish();//close all client connections and finish server

        //variables
        queue<std::string> InputQueue;//Here we store received data
        queue<std::string> LoginQueue;//Here we store login requests
        SDL_sem *ClientsLock; //Semaphore to prevent server from accesing a variable from different threads
        cNetwork *myserver;//server pointer
        vector<cClient*> Clients;//Here I store clients
        SDL_Thread *MasterThread;//The Master thread >> accepts new clients
        int         threadReturnMaster;//return value for master thread
        bool ShutDown;//closing the server?

    private:
        //private variables
        IPaddress ip;//Server ip address
        TCPsocket ServerSock; //Server socket
};

#endif // NETWORK_H


Then I have the client implementation:

<client.cpp>
#include "network.h"

cClient::cClient()
{
    //ctor
    ListenerThread = NULL;
    Connected = false;
}

cClient::~cClient()
{
    //dtor
    //Stop the thread
    SDL_KillThread( ListenerThread );
    //close the socket
    SDLNet_TCP_Close(socket);
}

void cClient::StartThreads()
{
    //Create and run the thread +
    ListenerThread = SDL_CreateThread( &listener, this );
}

int cClient::listener( void *data )
{ //While the program is not over
    cout << "Starting client listener Thread..." << endl;
    int result;
    char in_msg[MAXLEN];
    bool quit=false;

    while( quit == false )
        {
        memset(in_msg, 0, MAXLEN); // Clear in buffer
        if ( ((cClient*)data)->Connected)//If I'm connected...
            {
                result=SDLNet_TCP_Recv(((cClient*)data)->socket,in_msg,MAXLEN);//Check for incoming data
                    if(result<=0)
                        {
                            //NO DATA
                            // TCP Connection is broken. (because of error or closure)
                            SDLNet_TCP_Close(((cClient*)data)->socket);
                            cout << "Socket closed..." << endl;
                            ((cClient*)data)->Connected = false;
                            quit=true;
                        }

                stringstream idmsg;
                idmsg << ((cClient*)data)->UID << "#" << in_msg;// I will add the UID from the client to the incoming message
                try{
                    SDL_SemWait(((cClient*)data)->_server->ClientsLock);//lock smaphore to prevent client to freeze
                    cout << idmsg.str();//only for debuging

                    if  (((cClient*)data)-> Login==true)//Am I loged in?
                        {
                            ((cClient*)data)->_server->InputQueue.push(idmsg.str());  //I'm logged in
                        }else{
                            ((cClient*)data)->_server->LoginQueue.push(idmsg.str());  //New player requests
                        }
                    SDL_SemPost(((cClient*)data)->_server->ClientsLock);//unlock smaphore
                    }
                catch(exception& e)
                    {
                        cout << "QUEUE IN ERROR: " << e.what() << endl;
                    }
            }//if connected
        }//while
    cout << "Closing client listener thread..." <<endl;
    return 0;
}

void cClient::Send(const char* Msg)
{
    if (Connected==true)
        {
            int len;
            int result;
            len=strlen(Msg+1); // add one for the terminating NULL
            result=SDLNet_TCP_Send(socket,Msg,len);
            if(result<len) //If I can't send data probably I've been disconnected so....
                {
                    cout << "SDLNet_TCP_Send: " << endl << SDLNet_GetError();
                    Connected = false;
                }
        }else{
            cout << "Not connected!!" << endl;
            }
}


Notice the client stores the data in two different queues depending if the client has loged in or not, finally we have the main socket server class:

<network.cpp>
#include "network.h"

cNetwork::cNetwork()
{
    //ctor
if(SDLNet_Init()==-1) {//Start SDL_NET
cout << "SDLNet_TCP_INIT: \n " << SDLNet_GetError();
exit(2);
}

//Clean thread variables
MasterThread = NULL;
ClientsLock = NULL;
ClientsLock = SDL_CreateSemaphore( 1 );//semafor protector, previene que mas de un thread vuelque información a la vez a la cola
}

cNetwork::~cNetwork()
{
    //dtor
SDLNet_TCP_Close(ServerSock);//Close socket
SDLNet_Quit();//Close SDL_NET
}


void cNetwork::CleanClient(unsigned int Client)//clean a client so we can use it
{
//cout << "cleaning client: " << Client << endl;
Clients[Client]->UID =Client;
Clients[Client]->socket = NULL;
Clients[Client]->_server = myserver;
Clients[Client]->Connected = false;
Clients[Client]->Login = false;
}

void cNetwork::Start(Uint16 MAXUsers, Uint16 Port)
{

//1-initialize MAXUsers clients
//2-Start sock server

unsigned int x;
for (x=1;x<=MAXUsers;x++)
{
Clients.push_back(new cClient());//insert a new client into the clients vector
CleanClient(Clients.size() -1);
}

cout << "OPENING SERVER SOCKET... " << endl;
if(SDLNet_ResolveHost(&ip,NULL,Port)==-1) {
cout << "SDLNet_TCP_ResolveHost:" << endl  << SDLNet_GetError();
exit(1);
}

ServerSock=SDLNet_TCP_Open(&ip);
if(!ServerSock) {
cout << "SDLNet_TCP_open: " << endl << SDLNet_GetError();
exit(2);
}

ShutDown = false;
//Create and run the threads
MasterThread = SDL_CreateThread( &Master, this );

}



int cNetwork::Master( void *data )//Master listener function/thread
{
TCPsocket new_tcpsock; //Temporary socket to store incoming connections
new_tcpsock=NULL;

cout << "Waiting for incoming connections... " << endl;

bool doneMain=false;
while(!doneMain)//MAIN LOOP
    {
    if(!new_tcpsock)//We have a new client incoming
    {
        new_tcpsock=SDLNet_TCP_Accept(((cNetwork*)data)->ServerSock); // accept a connection coming in on server_tcpsock
        SDL_Delay(5);//No new clients, wait a little
    }else{
        cout << "New client incoming..." << endl;
        unsigned int x;
        for(x=0;x<((cNetwork*)data)->Clients.size();x++)
        {
        if (((cNetwork*)data)->Clients[x]->Connected==false)
            {
            ((cNetwork*)data)->CleanClient(x);
            ((cNetwork*)data)->Clients[x]->socket=new_tcpsock;//asign the socket
            ((cNetwork*)data)->Clients[x]->Connected = true;
            ((cNetwork*)data)->Clients[x]->Login = false;
            ((cNetwork*)data)->Clients[x]->StartThreads();//start client listener thread
            break;
            }
    }//for
        new_tcpsock=NULL;//release temporary socket var

        }//if new data

if (((cNetwork*)data)->ShutDown==true)doneMain =true;

}//while
cout << "Exiting Main thread..." << endl;
return 0;
}

void cNetwork::ClearClients()//Clear disconnected clients
{
unsigned int x;
bool done = false;
//SDL_SemWait(ClientsLock);
if (Clients.size()>0)
{
x=0;
while (!done)
{
 if (!Clients[x]->Connected)
            {
            Clients.erase(Clients.begin()+x);
           // cout << "Number of clients:" << Clients.size() << endl;
            if (x>0)x--;
            }//if !connected
x++;
if(x>=Clients.size())done=true;
}
}//clients size

}

void cNetwork::Finish()
{
    ShutDown =true;
    SDL_WaitThread(MasterThread,&threadReturnMaster);
    unsigned int x;
    for(x=0;x<Clients.size();x++)
        {
            Clients[x]->Connected=false;//force disconnection
        }//for

    while (Clients.size()>0)
        {
            ClearClients();
        }
}


There are many things here, but I will resume it to the maximum, to do that, below there is a simple example to use the class:

#include "network.h"//socket server class 
 
cNetwork NET;
NET.myserver = &NET;
NET.Start(100,55555); //start networking, up to 100 clients on port 55555
 
std::sting text;
SDL_SemWait( NET.ClientsLock );
text = NET.InputQueue.front();//take a message out from the queue
NET.InputQueue.pop();
SDL_SemPost( NET.ClientsLock ); 
 
NET.Clients[X]->Send("data");//will send "data" to client X
 
NET.Finish();//finish socket server 

  • We include the class
  • Then instantiate it
  • Start the listener thread (at this moment it is fully functional)
  • Then I extract a message from the input queue (let's suppose we have a connected client that send it..)
  • I send a message to the server ("data")
  • Finally, I close the server
Want to test the class? basically start an empty project, add the three files I have described above and then add the example, after you compile the project you will be able to connect just using telnet (telnet localhost 55555)

That's all for now, in the next post I will mix everything to make the client login a reality, I will make a text based client too and hope everything is clear that way.


Friday, August 3, 2012

SVN UP! New code repository

Hello again

     Before going any further (and after all the things that happened with Megaupload), I knew I wont be able to continue uploading my project / tools / whatever to 4shared, and yes I was right, in a few days 4shared started to ask for "user accounts" to be able to download things...

I had to start using another away to store my project.. and I needed a way to control the versions from my project.

The solution was quite simple (basically because a good friend showed me from the advantages of using subversion aka "svn")

So I went back to Google.. again.. went to http://code.google.com and signed for a new account, created a new project (http://code.google.com/p/jbfprj/) and voila! I got my own svn server :)

So now anybody can checkout in my software repository and get the source for my projects, at the moment I just uploaded SimpleSQL:

<copied from code.google.com>
Use this command to anonymously check out the latest project source code:
# Non-members may check out a read-only working copy anonymously over HTTP.
svn checkout http://jbfprj.googlecode.com/svn/trunk/ jbfprj-read-only
Notice you will need to install subversion: 
sudo apt-get install subversion
under linux or you can use another client for windows like tortoise-svn...
Anyway, that's all you will need to get the source, and of course, type "svn up" whenever you want to check if there are updates...>

In the next post I'll be back to the server program, but now I'm going to reduce the posts to a size that anybody can handle, starting for login to the server..

Thursday, August 2, 2012

A class to use MYSQL

<After installing and setting up the MySql server ,we need to access the MySql database from code::blocks>

To achieve the objective I'm going to make a class to connect and launch SQL queries to the MySql server , and of course a test project to use the class.

So the steps to get started are:
  1. Open code::blocks
  2. Create an empty project
  3. Add a file and save it "main.cpp"
  4. Add a new class with the wizard and name it "csql"
Now we edit the header for class csql (csql.h):

#ifndef CSQL_H
#define CSQL_H
#include <mysql.h>//Needed to use MySql
#include <string.h>
#include <sstream>

using namespace std;

class csql
{
    public:
        MYSQL_ROW row; //Here we store query results in rows
        csql(); //constructor
        virtual ~csql();//destructor
        bool connect(const char * serverip, const char * username, const char * password, const char * database);//connecto to a server , with username and password and select database
        void executequery(const char *query);//execute SQL query
        bool getrow();//we get a row from the results from a query

    private:
        MYSQL mysql;//mysql class
        MYSQL_RES *res ;//mysql results for queries
};

#endif // CSQL_H


Basically there are 5 functions: the creator and destructor, a function to connect to the mysql server, a function to execute queries and finally a function to get the result from SELECT queries.

Now the implementation (csql.cpp):

#include "csql.h"

using namespace std;

csql::csql()
{
    //ctor
        mysql_init(&mysql);//start mysql
}

csql::~csql()
{
    //dtor
        mysql_close(&mysql);//close mysql
}

//We use connect() to connect to the database needed
bool csql::connect( const char * serverip, const char * username, const char * password, const char * database)
{
    if (mysql_real_connect(&mysql
  ,serverip
  ,username
  ,password
  ,database,0,NULL,0))
    {

        return true;//everything ok
    }else{
        return false;//connection failed
    }
}

void csql::executequery(const char *query)//Execute SQL query
{
mysql_real_query(&mysql,query,strlen(query));
res = mysql_store_result(&mysql);
}

bool csql::getrow()//We extract a row from the result from an SQL query (or get false if empty)
{
    if ((row = mysql_fetch_row(res)))
    {
        return true;//we have extracted a row
    }else{
        return false; // no rows to extract
    }
}



It is quite simple to follow the code:
  • csql() (constructor) just initializes the mysql class used to work with the DB
  •  ~csql() (destructor) frees the class, closing any active connection
  • connect() connect to the server, using 4 parameters: the server IP, the username, the password and the database to connect (it returns true if the it connects ok, false when there is a failure)
  • executequery() used to launch SQL queries, just plain SQL commands like "select * from table"
  • getrow() get a data row from the results of a select query, if there are rows it will return true and we can access the data columns with csql_instance.row[index], other way it returns false
And finally, to use the class I wrote a pair of examples (main.cpp):

#include <iostream>
#include "csql.h"

using namespace std;

int main(int argc, char *argv[])
{

csql sql;//we use the sql class

cout << "Connecting to database.." << endl;

if (!sql.connect("localhost","root","rpgdata","rpgdata"))//we connect using the serverip, username, password and database
{
    cout << "Error connecting to database" << endl;
    return -1;
}

cout << "DELETING.." << endl;

sql.executequery("delete from accounts");//executing delete query

sql.executequery("select * from accounts");//executing select query
while (sql.getrow())
{
    cout << sql.row[0] << " " << sql.row[1] << " " << sql.row[2] << " " << sql.row[3] << " " << sql.row[4] << endl;
}

cout << "INSERTING Jorge.." << endl;

sql.executequery("insert into accounts values (NULL, 'Jorge','jorge@hotmail.com','pass',NULL)");//executing insert query

sql.executequery("select * from accounts");//executing select query
while (sql.getrow())
{
    cout << sql.row[0] << " " << sql.row[1] << " " << sql.row[2] << " " << sql.row[3] << " " << sql.row[4] << endl;
}

cout << "INSERTING Paul.." << endl;

sql.executequery("insert into accounts values (NULL, 'Paul','paul@gmail.com','pass',NULL)");//executing insert query

sql.executequery("select * from accounts");//executing select query
while (sql.getrow())
{
    cout << sql.row[0] << " " << sql.row[1] << " " << sql.row[2] << " " << sql.row[3] << " " << sql.row[4] << endl;
}

    return 0;
}




I make an instance from the mysql class, then I connect to the server, clean the table "accounts", and then I insert a pair of data rows, I display the results from select queries too, there are other SQL commands like UPDATE which I have not used, but I think that it is quite enough to show the use from the class.

<Notice that I am using the password "rpgdata", the database "rpgdata", the table "accounts".. if you don't use the same database or table names, the example wont work(I explained how to set it up in the previous two posts)>
 
Finally there is one final thing you need to set up to make the project compile:

  • Go to "project" > "build options"

  • Then in "compiler" > "other options"
`mysql_config --cflags`

  • And in "linker" > "other linker options"

`mysql_config --libs`

Other way the compiler wont find mysql and it will refuse to work...

<So basically we have a simple class to use the freshly new installed MySql server, now we can use it in any project, but obviously I'm going to add it to the socket server, in the next post I will show another new I'm going to use to host the project development...>

See you!

MYSQL + Command Line

<In the last post I spoke about my decision to start using MySql , now I will speak about how to set up a simple test database using the command line prompt>

CONNECTING TO THE SERVER

MySql has a basic tool to set up (almost) everything you need from your SQL server.

After installing MySql you just need to use the command:
mysql -h ServerIp -u username -p

That will connect to the mysql server with ServerIp, and try to validate, if you don't write the IP it will default to localhost, the username is the username used to login into the server, and finally -p makes mysql to ask for the password of the username:
jbea@eeepc1005HA:~$ mysql -h localhost -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.5.24-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
Here we are, logged in..

CREATING DATABASES AND TABLES

Next step would be very simple, create our database:
 
mysql> CREATE DATABASE rpgdata;
Query OK, 1 row affected (0.00 sec)

Notice the semicolon ";", every instruction in the mysql command prompt must finish with ";", it's the end of sentence command

It will create a new database "rpgdata" , then we can check the databases in our server:
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| rpgdata            |
+--------------------+
3 rows in set (0.00 sec)

We can delete the databases with the command:
mysql>DROP DATABASE rpgdata; 

After creating a database and before we can actually work with it we need to "use" it with the command:
mysql>USE rpgdata; 

Next step would be creating the tables to store the data:
mysql>CREATE TABLE table (datafield1, datafield2..); 
Example:
mysql> CREATE TABLE accounts (
    -> id INT AUTO_INCREMENT,
    -> fullname VARCHAR(60),
    -> email VARCHAR(120),
    -> password VARCHAR(30),
    -> PRIMARY KEY (id));
Query OK, 0 rows affected (0.06 sec)

Now we can check the tables we have in our database:
mysql> SHOW TABLES;
+-------------------+
| Tables_in_rpgdata |
+-------------------+
| accounts          |
+-------------------+
1 row in set (0.00 sec)

And another interesting thing: we can check the database structure with the command: 
mysql> DESCRIBE accounts;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| fullname | varchar(60)  | YES  |     | NULL    |                |
| email    | varchar(120) | YES  |     | NULL    |                |
| password | varchar(30)  | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


DATA HANDLING

At this point, we can start using standard SQL sentences to handle the data:
>INSERT INTO > Insert data
>UPDATE > update data
>DELETE FROM > delete data
>SELECT FROM > Get data 

A pair of examples:
mysql> INSERT INTO accounts VALUES
    -> (NULL, 'Jorge Bea Frau','jbeafrau@gmail.com','password');
Query OK, 1 row affected (0.00 sec)

mysql>

mysql> SELECT * FROM accounts;
+----+----------------+--------------------+----------+
| id | fullname       | email              | password |
+----+----------------+--------------------+----------+
|  1 | Jorge Bea Frau | jbeafrau@gmail.com | password |
+----+----------------+--------------------+----------+
1 row in set (0.00 sec)

mysql>

SQL Language is almost a programming language itself so it's way beyond my hand to teach the language, anyway I will explain the sentences when I use them so you don't have to worry about it.

BACKUPS AND RESTORES

Finally, to make a backup from a database to a plain text file and restore it later we have two commands:


mysqldump -h localhost -u root -p DATABASE > backupfile.sql
To make a backup from a database to a file...

mysql -h localhost -u root -p DATABASE < backupfile.sql
To restore a database from a backup file

<Notice that in both commands you have to execute the commands from the terminal window, not from the mysql prompt>

And that's all for now, that's all we are going to need to start using the mysql server, creating our database and the tables inside, we can now design our game database, and we could even insert the desired data inside (I Wont recommend that!), but instead  we are going to use c++ to connect to the mysql server and launch SQL sentences...

In the next post: Using mysql from c++ + code::blocks.

Monday, July 30, 2012

Finally, MYSQL + code::blocks!

Hello everybody, today I'll make a "postmortem analysis" and will install and set up MySql server

     <Been out for a long time <again>, had many things to do and my project got stucked again, but it didn't get forgotten! I have been reading all the code again and had time to think about it, the things I have done right and which things should I change / improve, and made an small list:>

GOOD THINGS
  1. The server actually works!
  2. Client program able to create players, connect, and "move around"
  3. Worked quite fast in a desktop computer using small amounts of RAM
BAD THINGS
  1. Ugly code, I feel really ashamed about that, but it is the result of writing code in a "fast 15 min way"
  2. Server is unstable, yeah, after fixing many, many bugs it did worked, but still had things to fix.
  3. I was not using a real scenario , data was not being loaded from server, so player creation, maps, etc was everything client-side, obviously that is not good, for many reasons (security, for example)
  4. World didn't had persistent data, players, objects, etc were not stored  after closing client program
In other words I just did a network demo, I was so anxious to make the server work with thousands of clients that I didn't realized I was starting to loose the focus in my objective: MAKE THE GAME!

So I took a hard decision, dropped the code and to make a second server with all the good things the first server had + all the things it SHOULD HAVE like persistent data, and a true server-client model.

Having persistent data meant to use some kind of static storage, so I thought in using just plain files plus standard c++ io functions to write data, but I dropped the idea fast because there are already libraries to solve the "writing another text parser" like TinyXml , but after playing with that again I found that if I used that library I will be making two times the work because:
  1. First I will have to write all the data in .xml files
  2. To later rewrite the code again to switch to a real database..
I went back to my very first objectives and the name MySql, came to my mind...

<NOTE: I have completely dropped support for windows in the server part, that means I wont be posting how to install and make it run under Windows OS family, basically I made that because that's the OS I will be using to make and run the server and I cant loose more time in something I wont be using, although anybody could check how to download and install the required libraries for the OS you choose..>

So I installed the mysql server:
sudo apt-get install mysql-server
It just asked me for the "root" database password, it is very important because it will be used to connect to MySql and use the database, NOTE: it is not the linux root account so you can / should have different passwords .

I installed too MySql tuner:
sudo apt-get install mysqltuner
Used to get advice to tune the databases

And the client libraries:
sudo apt-get install libmysqlclient-dev
Used to connect from the client app

To check that the MySql server is running:
sudo netstat -tap | grep mysql
  
You can restart the service with:
sudo service mysql restart

 
<You can see installing MySql server is quite simple, in the next post I will write about comand-line database management, and finally start using it with code::blocks>

Tuesday, April 3, 2012

Rpg Client update: Rotate players

Welcome back!


Player's point of view is quite clear now...

Since I decided to continue the project I started to think again in all the things I could make to improve it, and the first thing was to  make it clear where players / foes were looking at, it's basic to make you know:
  1. Where are you going (the red dot did not helped a lot)
  2. Where are going all the other players / enemies
  3. Can I ambush somebody?, am I being ambushed?
To make client rotate images I just needed a function to rotate a tile certain angle, I didn't had to modify the server code because I already included angle in the players data, I thought how to rotate that and finally I decided to use a library from a good friend that was open source and based in the standard library SGE:

SPRIG: The SDL Primitive Generator

A great job from Jhonny D

To use the library you will need the subversion client, (in linux "sudo apt-get install subversion", in Windows you can use Tortoise SVN client, for example)

To download / install it under linux:
svn checkout http://sprig.googlecode.com/svn/trunk/ sprig
cd sprig
make
sudo make install

(Haven't tested it under windows yet)

I created a new function: RotateBlitTileset( x,y,image,angle), that functions draws the rotated tile (image) at x,y.
void Engine::RotateBlitTileset(int x, int y, int tile, float Angle) //Rotate and Blits a tile from our tileset to the screen
{
    SDL_Surface * tmpBlit = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY |SDL_RLEACCEL,TileSize,TileSize,32,0,0,0,0); 
    SDL_Surface * tmpBlit2 =  SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY |SDL_RLEACCEL,TileSize,TileSize,32,0,0,0,0);
    //1-fill temporary surface with pink
    SDL_FillRect(tmpBlit, 0, SDL_MapRGB(tmpBlit->format, 255, 0, 255));

    start.x= (tile%10)*TileSize; //Column = tile % 8 (0-7)
    start.y= (tile/10)*TileSize; //Row = tile / 8 (0-7)
    start.w=TileSize;
    start.h=TileSize;

    end.x =0; // X
    end.y =0; // Y
    end.w = TileSize;
    end.h = TileSize;
    //2-Blit the desired tile to the temporary surface
    SDL_BlitSurface(tileset, &start, tmpBlit, &end); 
    //3-I call sprig function SPG_Rotate() to rotate the temporary surface and get the resulting surface into a 2nd temporary surface
    tmpBlit2 = SPG_Rotate(tmpBlit, Angle -90,  SDL_MapRGB(tmpBlit-> format, 255, 0, 255)); 
    SDL_SetColorKey(tmpBlit2,SDL_SRCCOLORKEY |SDL_RLEACCEL,SDL_MapRGB(tmpBlit2-> format, 255, 0, 255));//set the color key
    start.x=0;
    start.y=0;
    start.w=TileSize;
    start.h=TileSize;

    //set the cliping for the destination surface (the screen)
    end.x =x; // X
    end.y =y; // Y
    end.w = TileSize;
    end.h = TileSize;

    SDL_BlitSurface(tmpBlit2, NULL, screen, &end); //Blits the rotated tile to the screen
    SDL_FreeSurface( tmpBlit);
    SDL_FreeSurface( tmpBlit2);
}

Notice I can't just call Sprig rotate function because I would get a rotated tile with pink background, so I have to use the tmp surface to set the color-key, that way is not optimized at all so I will think in a way to make it faster later, but at the moment is ok.

In the next post: Let's chat!

Tuesday, March 27, 2012

I'm alive!!

I could tell you I had a very bad car accident, maybe that I got into the drugs , got kidnapped or abducted but I really can't say other thing than the truth:

I have not made anything in the project for about two months :(

Got quite frustrated trying to make the project to work the non-blocking way, and in fact I made it to "work" quite easy, I was very happy because I thought I could make the server work faster and use less resources than I was using...

WRONG!, server worked fine with a low load (say less than 50 users), but when I started to add more users it just collapsed with nasty segmentation fault errors, sometimes when a client disconnected it just blocked the server, I didn't had all those problems with the "old" server and I although I tried to debug the code, errors never went away..

But that's not the worst thing, worst think is performance was poor.. I never felt the non-blocking approach was faster, basically because my blocking approach used many, many threads, a good approach if you use a multi-core processor, but the non blocking used only 3-4 threads for everything, with separate functions like accepting new clients, updating player states or sending data, so some threads have little load while other threads couldn't handle the load at all.

What happens when you have problems? usually it starts to appear more like mushrooms (Damn Murphy's law ), and I was just trying to solve other problems that were not related to the blog...
   
And then I let the blog die slowly, but even without posting more blogs many people entered and read it, and today I arrived to 3k visits and I knew I had to do something, like trying to reanimate the zombie-blog.

Then I remembered a comment I got from wise Amit Patel : How many users do I plan to have? maybe the problem is not being able to handle 10k users·

Now I can say I was distracting myself from my biggest objective, actually making the game!!! so I put my hands on the blocking sockets code again and re-started again, it has been hard, but in this week I expect to launch two updates for the game that will make it a little more user friendly and usable..

Thanks for reading and see you soon (promise)