segunda-feira, 7 de Janeiro de 2013

Game Making : The Game Loop Continued


As I said in my previous post a game is just a software piece with distinct behavior. The way I presented the game loop before was actually a very simplified way of making a game loop. Usually a game loop is more complex than a simple three instruction loop. Most game have a frame limited Game Loop, this means that the game loop will only run a certain number of times per frame, usually 60 FPS.

 There are even more complex architectures than run the Game Loop in multi-threaded way, this means that the Logic Part of the game (Update, Physics, Collision) runs at a different pace than the Rendering loop and on diferent CPU threads.

For example while I can be drawing at 60 FPS (Frame per second), my physics engine can be updating at 120 FPS, this means that for each rendered frame I calculate twice the Logic, this is useful in simulation intensive games (Need for Speed, other vehicle Simulators).

 How can we insert this new information on our game loop?
I won't go in detail of multi-thread Game Loops but the principles are the same. In case you are wondering multi thread game loops are extremely complicate to do as a begginner, you need to be very clear on the way the update and render cycle work to be able to multi thread a game.

public int GameLoop()
{
    long time = GetTime(); //This just retuns  the current system Time
    while(GameIsRunning)
    {
      long deltaT = GetTime() - time;
       long GameLoopTime = 1/DesiredFrameRate * 1000; 
     
      Wait(deltaT - GameLoopTime);
      time = GetTime();
      ReadPlayerInput(deltaT) //Keyboard,Mouse,Joystick
      Update(deltaT); //Update the game world (Physics,Animation,Etc)
      Render(deltaT); //Draw Everything to the screen
    }
}
Explanation:
Most of the code is the same from the previous post we only have a few instructions that are new,


  • GetTime() is just a function that gets the current system time in miliseconds.
  • GameLoopTime is the desired loop speed, for a 60 FPS game it is around 16 miliseconds. This is calculate by deviding 1/FPS * 1000, in our case 1/60  * 1000 = 16.666. So each Game Loop Cycle will take at least 16.66ms.
  • The Wait(waitTime) instruction is just to stop the loop from progressing, it could be implement a Sleep(uint time) in most language. Making the calling thread to sleep for a few milliseconds.
One question that can be lingering, why are we passing the deltaT down to the other methods? Right now, it doesn't really matter that we pass it or not, but it is a good practice that will help solve a lot of nightmares later on.


As again this is just a example, don't take this without a grain of salt, or as the true way to implement, as you can see this is just simple way to explain how to create a simple Game Loop for starting purposes.

As always, if you have any suggestions, comments, or questions, don't hesitate to comment below, and I will try to answer them as soon as possible.

sexta-feira, 4 de Janeiro de 2013

Game Making : The Basics



Well, if you are here one thing is certain, you want to create a game. Well, this is a heads up on what it takes to create a game. The concepts that I am going to talk on this post are generic, and don't relate to any specific type (3D,2D) or even Genre (FPS,RTS,etc). 

So first thing first, what is a game? 

Well, a game is a program,a piece of software, So the first step to create a game is to learn how to program. Any language is suitable to program games, there are other more accepted in the market, C++,plain old C, java, etc. There is no correct language, but most AAA games (think Mass Effect, Age of Empire and all the other games released by big companies) are mostly made in C++, so if you are thinking in going in a professional way this may be a good choice to pick.In the end is mostly down to your personal preference.

There are also Engines that ease this process, Unity, RPG Maker, Game Maker, and a bunch of others, but those are out of scope of what I am trying to teach.
These engines do most of the ground work for you, but I feel that to use these engines without knowing the basics is really not worth it. Also, there will be a time that to create your vision of that amazing world, the engine you picked won't suffice without some extra coding, either via scripting, or changing to another engine altogether.

I am talking with experience here, I learned how to program via scripting for RPG Maker XP, and soon realized the limitations of the engine. It was after this that I decided to move toward learning how a game worked behind the  pretty GUI (Graphic User Interface) that these engines have for us to use.

One last word, don't feel like I am saying that rolling your own game is better than using a self established engine, that is not what I am trying to transmit. Using a engine, simplifies and makes it faster to develop your game most of the times, but you still need to learn how it works to take the best out of it. Keep that in mind.

How do games work?

Games are program like I said, but they are very specific programs with a set of rules. For you to understand the next section is advised that you do know some concepts about programming (Loops,Ifs and other basic information).

All the games have something that is called the "Game Loop". This is what makes the game run, and by the name you can see that it is something that loops, by this it means its constantly repeats the same set of actions over and over again.

What I am going to explain next, is a very crude  way of describing the game loop in pseudo code:

while(GameIsRunning)
{
   ReadPlayerInput(;) //Keyboard,Mouse,Joystick
   Update(); //Update the game world (Physics,Animation,Etc)
   Render(); //Draw Everything to the screen
}

What it does is easy, while our game is running (aka not closed, on background) it should call those functions.
That's the skeleton of a game right there, its as simple as that, the complicated parts comes after this, what should each function do? And more importantly how to do it?

Well those question will come further on the chain of these series of short tutorials, so keep posting back and comment if you have any doubts, need better explanation or have any suggestion.

quarta-feira, 2 de Janeiro de 2013

Game Programming : A Start

First of all welcome to my blog.

My name is Morphex, a web-developer,programmer, hobbyist game programmer, and student at a university in Portugal.

This blog will focus mostly on my programming journey, and will feature articles,tutorials, rants and other aspects that I feel to discuss around here. Here I will talk about my upcoming projects, either games or software I develop.

Most of my tutorials/articles with code related subjects will be in C#, since I love the language syntax and Visual Studio as editor (yes I know many of you will start throwing rocks and stones at me, but its my choice  ).

I am thinking in writing at least once per week, but occasionally it might be sooner or later. Stay tuned. And don't forget to leave your feedback.

One last thing, I am active under GameDev Forums with the same nick name, so you can find me around there.

Hopefully you will enjoy this as much as I do, and in the way you and I might learn something.

Thank you for your time,
Morphex