Month: November 2012

Finally chat works in my game using TCP protocol and multithreading.

So for the past few days I have been trying to let players communicate with each other in my game. and today I finally did it.

So how it works? Well all the clients connect to the server and when client 1 type “Hello ” in the chat box, The client put that data into a packet and send it to the server. Then the server receive that packet from client 1 and will send it to all the connected clients.

Also my client use multithreading. 1 thread for the game loop and another thread for networking.

here is the video:-

enjoy 🙂

How to overcome the performance issues in a tile based engine?

So for some time now I had a big performance issue in my game. Now if you don’t know, my game is a tiles based game. I wrote my engine to take a 2D array of integers and assign a 2d texture (image) to each integer.

So if the array contains number 1 it will draw grass texture (image) in that position. If the array contains number 2 it will draw a stone texture in that position and so on.

The array would look something like



Now this is a 25X19 array which means my engine will have to draw 475 tiles (image) every frame. Now that doesn’t seem much but it’s still a problem. Now let’s say we have a 100X100 array. That means the CPU has to send 10,000 images (draw calls) to the GPU every frame. And this is a big problem since the CPU can’t send that much data to the GPU. So in other word my 3930K @ 4.5GHz in one thread can’t handle sending that much data to the GPU to process.

CPUs are just slow. Even the fastest CPU in the world is very slow.

So let’s say you have a screen resolution of 1280X720 and each tile you have is 32 pixels. And let’s also say you have a map or array size of 100X100. So 100 * 32 = 3200 pixels. So you will have a map size of 3200X3200 pixels. Now here is the question. Why draw the whole map when you are only seeing 1280X720 pixels of the actual map?

So before we were drawing 3200X3200 pixel (map size) and we were getting around 200fps. Now after only drawing the visible parts of the map which is 1280X720 we are getting ~1000 fps !!! Isn’t that great? 😀

But still if we look at our GPU usage, we will see that the GPU usage is only about 70%. Why not 100% ? Well we are still sending 880 draw calls from the CPU to the GPU. So we can see that the CPU is bottlenecking the GPU. Once again the CPU is very slow and can’t send that much data to the GPU.

How did I calculate that the CPU is sending 880 draw calls?
Well we take the screen resolution which in our case is 1280X720 pixels and divide it by the tile (image) size which is 32 pixels.

so 1280 / 32 = 40;
720 / 32 = 22.5 so let’s say 22;

40 * 22 = 880 tiles (images) or draw calls that need to be sent from the CPU to the GPU to render.

So today I came up with the best solution which will get you 100% GPU usage. But first you have to understand that the more textures (tiles, images, 3D objects) we try to send from the CPU to the GPU the lower our frame rate gets and the lower our GPU usage gets and the higher our CPU usage gets. That is because once again the CPU is very slow.

So why render 880 small images every frame when we can render one big image? Well for one if we want to change how our game level looks then we have to tell our artist / designer to draw the same level again with the same art but in a different way. And that will cost time. It’s just easier to take tiles (small images) and make our game level with them.

But why not use the best of both worlds. Let our artist create tiles (small images) and construct the game level with them but also have the advantage of having just one big image to send to the GPU. And that is exactly what I did.

In my engine I load the game level which is basically a 2D array and then the game will draw the array (map) once. Then the engine will take all of these images which are stored in the GPU buffer and store them in one big image. A texture 2D variable. Then we will take that one big image and send it to the GPU to render it instead of rendering 880 small images.

And that is how I did it. Before my fps was 1000fps now its 2100fps 😀

Today I registered Vault 16 software

I just bought today. I’m so happy 🙂

My game support networking using TCP protocol

so for the past few days I have been trying to implement networking in my game using TCP protocol. I have been able to send and receive a small packet that contains the player name. It worked perfectly.

However my performance dropped dramatically when I used networking. Before implementing networking I was getting around ~1900 fps. After networking my fps dropped to ~95 !!!! So I decided to use threading. So now I have the main thread which runs the game update functions and drawing functions. and I have one thread for the server and another for the client and one for the login system.

Also I have created a simple in-game console to help me debug my game. I have to say my life is 100 times easier now. how did I not think of that before ?!

one last thing. I’ll update my website today and keep my server running 24/7.

this is my server specs please don’t laugh.
CPU:- Core 2 Duo E6600 2.4GHz.
RAM:- 3GB DDR 2 @ 800MHz.

Here you go guys enjoy

My game and web server are up and running.

I got my web server and my SQL server up and running for my game. I’m so so sooooo very happy. Its like I’m in heaven But hushhhhhh don’t tell my ISP that I’m running a server in my house. they might block it. And please don’t hack my database. I don’t need a PlayStation network incident in here.