Sunday, October 31, 2010

C++ Project - Final Fantasy Battle System pt.3 - Game Loop

So this past week was spent mostly learning the ClanLib GUI system and starting development on a Scene management system that is a basic implementation of a scene graph.

But more on that later. Today I just wanted to follow up on a loose end from last week. Namely the addition of a proper timed game loop to the project.

At some point I will be looking at animations or character movement and I will need the delta time for each frame in order to have proper frame dependent movement.

So the math for this is very simple. I have setup a constant which is my targeted time for each frame:

 static const unsigned int TIME_PER_FRAME = 16; // time per frame in milliseconds  


Since I would like to target 60 fps, I have set the value to 16 milliseconds per frame.

Now in order to calculate the frame time it will look something like this:

- Get system time before game loop
- while (gameLoop)
- Get time before executing game code.
- Calculate deltaTime = (time_before_loop - time_before_executing_game_code)
- GAME CODE
- Get time after game loop.
- Calculate timeToSleep = (TARGET_FRAME_TIME) - (time_before_executing_game_code - time_after_game_loop
- Sleep(timeToSleep)
- loop


Not too bad. And now the actual implementation in our Game class:

      unsigned int lasttime = CL_System::get_time();  
while (m_currentGameState == Running)
{
unsigned int startFrameTime = CL_System::get_time();
int deltaTime = startFrameTime - lasttime;
lasttime = startFrameTime;
DO GAME STUFF HERE
startFrameTime = CL_System::get_time();
int timeToSleep = TIME_PER_FRAME - (startFrameTime - lasttime);
if (timeToSleep > 0)
{
CL_System::sleep(timeToSleep);
}
}


Now I can pass my delta time per frame to my update functions in my IGameComponent class.

 virtual void Update(unsigned int) = 0;  


On other fronts, my Game class is starting to become a little unwieldy especially with all of the GUI stuff making its way in, so I think my next entry will be a bit of a lesson in refactoring.

Sunday, October 24, 2010

C++ Project - Final Fantasy Battle System pt.2

So with any programming project, the initial question is "where do I start?" I have ClanLib installed and the compiler configured and ready build, so now it's time to start planning exactly how to approach this project.


The heart of every game is essentially a loop which is responsible for updating and rendering. Since ClanLib does not provide a standard game loop or the means to track objects in our game, this seems like a good place to start.

So, we'll start with a base class, Game which will contain our loop.

 class Game   
{
private:
enum GameState {Stopped,Running,Paused};
// Singleton instance
static Game* m_instance;
. . .
Game();
Game(const Game& a);
public:
~Game();
static Game* GetInstance();
void Start();
void Initialize();
}

Notice that I have made the class a Singleton. Since I am anticipating other objects wanting to add/remove themselves from the main game loop, I want this class to be accessible anywhere within the program. Since the Game class is guaranteed to exist during the lifetime of the game, there should be no problems with initialization that is inherent with Singleton objects.

Also, I anticipate that we might want to add some simple functions for pausing the game, etc., so I have added an enum to track the current state of the loop.

Now that we have the outline for a basic loop, we need objects to actually render and update. In order to facilitate this I created a basic interface that we can use for any class that we might want to add to our loop.
 class IGameComponent  
{
public:
virtual void Initialize() = 0;
virtual void Update() = 0;
virtual void Draw(CL_GraphicContext& graphicsContext) = 0;
};

Now, let's add a container and a couple of operations to our Game class to actually track these objects.
 vector m_gameComponents;  
void AddComponent(IGameComponent*);
void RemoveComponent(IGameComponent* const)
Here is the full update loop for the game loop.
 void Game::Start()  
{
m_currentGameState = Running;
CL_Font font(gc, "Tahoma", 30);
while (m_currentGameState == Running)
{
Update();
gc.clear(CL_Colorf::cadetblue);
Draw();
gui_manager->exec(false);
wm->draw_windows(gc);
// Make the stuff visible:
window->flip(1);
// Read messages from the windowing system message queue, if any are available:
//CL_KeepAlive::process();
// Avoid using 100% CPU in the loop:
CL_System::sleep(10);
}
}
void Game::Update()
{
// Update loop
for (int i = 0; i < m_gameComponents.size();i++)
{
m_gameComponents[i]->Update();
}
}
void Game::Draw()
{
for (int i = 0; i < m_gameComponents.size();i++)
{
m_gameComponents[i]->Draw(gc);
}
}

And there you have it! A simple system for dealing with the game loop. Next up, I will need to add some sort of Scene Manager in order to track scene transitions and to encapsulate the elements that form each scene. I will also be adding in some hooks for the GUI system and the Input system into Game as well. Once, I have those in place I will start working on the design for the actual RPG gameplay.

Sunday, October 17, 2010

C++ Project - Final Fantasy Battle System pt.1

So in order to keep my programming skills in shape, I like to take a new engine and begin to develop some game systems utilizing it. Most of these are usually stalled due to lack of time or my curiosity about something new that is more interesting.


In order to rectify this, I've decided to start a small project that is not necessarily about writing a complete game for release, but rather something I can use to practice building game systems and further my use of design patterns within these systems.


So, I've chosen to go with a 2D C++ engine called ClanLib and what I will be working to build is essentially this:


That's right, I will be working on developing the battle system from Final Fantasy I. I'm a major fan of RPG games and this was probably my second or third RPG ever, so it holds sentimental value. Also, I've limited the scope slightly so I'm not overwhelmed with large complicated AI or physics systems, as it truly is the gameplay systems that really light my game developer fire. I've also picked something that can done with simple sprites as I am not an artist either.

So, within this project, I will be implementing the following:
  • General game loop
  • Input handling
  • GUI
  • Scene Management
  • RPG system (PC/NPC stats, item handling, spell casting abilities, etc.)
  • Battle system
I will be blogging each step of the way with my goals being:
  • Improve my design capabilities
  • Improve my coding standards to develop code that is highly readable and maintainable.
  • Expand my knowledge of 3rd-party game engines.
  • Expand my knowledge of game systems.
Anyhow, it should be a blast!

Wednesday, October 13, 2010

Time Flys When You're ...

I can't believe it's been almost two years since I've updated this blog. I'm not sure where the time went, but it sure has been a busy period.


The Past

In the last two years (other than NOT writing in this blog) , I've accomplished the following:
  • Developed and released an Xbox Live Indie Game.
  • Placed in the top five of the Old Spice Dream Build Play contest.
  • Developed a complete game in 48 hours.
  • Developed a match-3, RPG with dragons.
  • Released a children's MMO.
  • Met and worked with some truly unique and awesome people.
In addition to all of that, I was taking some classes at ACC and working on and off on some personal projects that unfortunately had to be stalled due to the above.

The Future

So what's coming next? Well, concerning this blog, I do have a new C++ project I'm working on in my spare time, so I think I'd like to start blogging that just to get some of my C++ code out there.

Regarding myself personally, I'm not real sure. I'll just keep developing, reading, and learning. I love developing games and as I found out the last few years, it's something that I can't not do.