Saturday, October 19, 2013

Building mobile game engine with Android NDK: 4 - timing service



all parts:
1 - installing the tools and running samples
2 - calling C++ library from Java
3 - abstracting types and logging
4 - timing service

 today we will create first from the set of services. It will be timing service. Service is here some class or set of classes that enclose some functionality and is fully reusable in future projects.

 Until now our engine run as fast as possible calling engine_tick method. In this method we set background color but so far we do not know how much time elapsed form previous call. So we have no information that would be handy in whatever you can imagine - timing animation, showing the player time spent in level, timing bombs explosion, etc.

Project structure

 On the image bellow you can see project structure. New files we are going to create today are highlighted with green. Beside this we will make changes into Main.cpp. As the timing service is the first one from set off services we will create in future we are also going to create Context class. This class works as a bag full of services which is easy to reference and handle inside game.


 As with the logging class we are preparing ourselves for future when we want to go cross-platform. ITimeService.h is abstract interface class that defines interface that will be common for all supported systems -  only Android now. TimeServiceBase is class that implements those parts of the ITimeService, that are the same for all systems. Finally, AndroidTimeService implements rest of the methods.

Implementation

 Listing for ITimeService.h is simple:
#ifndef ITIMESERVICE_H_
#define ITIMESERVICE_H_

#include "Types.h"
#include "Log.h"

namespace SBC
{
namespace System
{

class ITimeService
{
public:
 virtual ~ITimeService() {};

 virtual void reset() = 0;
 virtual void update() = 0;

 virtual f64 now() = 0;
 virtual f32 elapsed() = 0;
};

} /* namespace System */
} /* namespace SBC */
#endif /* ITIMESERVICE_H_ */

 The common base for all systems looks like this:
#ifndef TIMESERVICEBASE_H_
#define TIMESERVICEBASE_H_

#include "ITimeService.h"

namespace SBC
{
namespace System
{

class TimeServiceBase : public ITimeService
{
public:
 TimeServiceBase();
 virtual ~TimeServiceBase();
 void construct();

 void reset();
 void update();

 f64 now();
 f32 elapsed();

 static unsigned long getTickCount();

private:
 f32 mElapsed;
 f64 mLastTime;
};

} // namespace System
} // namespace SBC

#endif // TIMESERVICEBASE_H_

 We added some member variables to store elapsed time from last update of timer and also previous time. This base class also has construct method. This method is part of construction of the object. First the constructor is called and then construct method is called. It is called two-phase construction. Imagine that you are creating new object that needs to create several other objects on the heap. Next imagine that first of these objects is successfully created but you get out of memory error when constructing the second one. In such a case the original object is not constructed and thus its destructor is not called. But you already successfully created on of its child objects in constructor. Who will destruct it? Answer is no one - you will get memory leak.
 Opposite to this you can do two phase construction. First allocate the object and just initialize its member variables that do not need additional memory allocations or do thing that can not fail. Then call construct method that will do the rest. Good explanation and example of this can be found here.
 We are not allocating any memory for time service now. But what if there was system that would need it in future...

 Implementation of TimeServiceBase is here:
#include "TimeServiceBase.h"

#undef LOG_TAG
#define LOG_TAG "TimeServiceBase"

namespace SBC
{
namespace System
{

//------------------------------------------------------------------------
TimeServiceBase::TimeServiceBase() :
  mElapsed(0.0f), mLastTime(0.0f)
{
}

//------------------------------------------------------------------------
TimeServiceBase::~TimeServiceBase()
{
}

//------------------------------------------------------------------------
void TimeServiceBase::construct()
{
 LOGI("TimeService constructed");
}

//------------------------------------------------------------------------
void TimeServiceBase::reset()
{
 LOGI("TimeService reseted");
 mElapsed = 0.0f;
 mLastTime = now();
}

//------------------------------------------------------------------------
void TimeServiceBase::update()
{
 // Checks elapsed time since last frame. It is important to
 // work on double with current time to avoid losing accuracy
 // Then we can go back to float for elapsed time.
 double lCurrentTime = now();
 mElapsed = (f32)(lCurrentTime - mLastTime);
 mLastTime = lCurrentTime;
}

//------------------------------------------------------------------------
f64 TimeServiceBase::now()
{
 LOGE("Feature not implemented for current system");
 return 0;
}

//------------------------------------------------------------------------
f32 TimeServiceBase::elapsed()
{
 return mElapsed;
}

//------------------------------------------------------------------------
unsigned long TimeServiceBase::getTickCount()
{
 LOGE("Feature not implemented for current system");
 return 0;
}

} // namespace System
} // namespace SBC

 As you can see some methods just say "Feature not implemented for current system". I found this very useful when I was adding new Tizen system into my cross platform engine. As I was writing Tizen implementation of the all the services one by one I was able to run it and when I came across some that was not implemented yet I got log message about it but there was minimum of crashes. Also when see this you know there is needed some system specific implementation.

Android implementation

 Now we can implement Android specific parts. AndroidTimeService.h has this listing:
#ifndef TIMESERVICE_H_
#define TIMESERVICE_H_

#include "../TimeServiceBase.h"
#if (PLATFORM_ID == PLATFORM_ANDROID)

namespace SBC
{
namespace System
{

class TimeService: public TimeServiceBase
{
public:
 TimeService();
 virtual ~TimeService();

 f64 now();

 static unsigned long getTickCount();
};

} /* namespace System */
} /* namespace SBC */
#endif // PLATFORM_ANDROID

#endif /* TIMESERVICE_H_ */

and implementation is like this:
#include "AndroidTimeService.h"
#if (PLATFORM_ID == PLATFORM_ANDROID)

#undef LOG_TAG
#define LOG_TAG "TimeService"

#include <time.h>

namespace SBC
{
namespace System
{

//------------------------------------------------------------------------
TimeService::TimeService()
{
}

//------------------------------------------------------------------------
TimeService::~TimeService()
{
}

//------------------------------------------------------------------------
f64 TimeService::now()
{
 timespec lTimeVal;
 clock_gettime(CLOCK_MONOTONIC, &lTimeVal);
 return lTimeVal.tv_sec + (lTimeVal.tv_nsec * 1.0e-9);
}

//------------------------------------------------------------------------
unsigned long TimeService::getTickCount()
{
 timespec lTimeVal;
 clock_gettime(CLOCK_MONOTONIC, &lTimeVal);
 return lTimeVal.tv_sec * 1000 + (lTimeVal.tv_nsec * 1.0e-6);
}

} /* namespace System */
} /* namespace SBC */

#endif // PLATFORM_ANDROID

 In now method we are reading current time for monotonic clock. But, what is current time? According for example to this documentation you can read that it is "Clock that cannot be set and represents monotonic time since some unspecified starting point.". The starting point is often system boot. Fortunately it is not interested for us. We need it just to measure time leaps from one call to another and to calculate the elapsed time. Last time as well as elapsed time are in seconds.

Creating context class

 Our first service is created so let's put it into bag for services - into the context. But first we have to create this bag. This is the header for Context class. It is first class that is created in Engine directory of our engine structure:
#ifndef CONTEXT_H_
#define CONTEXT_H_

#include "../System/system.h"

namespace SBC
{
namespace Engine
{

class Context
{
public:
 Context();
 ~Context();

public:
 // application
 void setApplication(SBC::System::Application* aApplication);
 SBC::System::Application* getApplication();

 //time service
 void setTimeService(SBC::System::TimeService* aTimeService);
 SBC::System::TimeService* getTimeService();

private:
 SBC::System::Application* mApplication;
 SBC::System::TimeService* mTimeService;
};

} // namespace Context
} // namespace SBC

#endif // CONTEXT_H_

 As can be seen it is just set of getters and setters. Only interesting is destructor because ownership of all created services is handeled to context and it is then responsible for destructing them (except for application). In implementation you can see it:
#include "Context.h"

#undef LOG_TAG
#define LOG_TAG  "Context"

namespace SBC
{
namespace Engine
{

//------------------------------------------------------------------------
Context::Context()
{
 mApplication = NULL;
 mTimeService = NULL;
}

//------------------------------------------------------------------------
Context::~Context()
{
 LOGI("Deleting TimeService");
 if (mTimeService != NULL)
 {
  delete mTimeService;
  mTimeService = NULL;
 }
}

//------------------------------------------------------------------------
void Context::setApplication(SBC::System::Application* aApplication)
{
 mApplication = aApplication;
}

//------------------------------------------------------------------------
SBC::System::Application* Context::getApplication()
{
 return mApplication;
}

//------------------------------------------------------------------------
void Context::setTimeService(SBC::System::TimeService* aTimeService)
{
 mTimeService = aTimeService;
}

//------------------------------------------------------------------------
SBC::System::TimeService* Context::getTimeService()
{
 return mTimeService;
}

} // namespace Context
} // namespace SBC

 Maybe you noticed that in all .cpp files, there is defined LOG_TAG in the very top. This tag helps in debugging as all log messages from the same .cpp file are tagged with it.

 As a last step change system.h file - add TimeService to it:
// Android platform
#if (PLATFORM_ID == PLATFORM_ANDROID)
 // system
 #include "Android/AndroidSystem.h"
 // time service
 #include "Android/AndroidTimeService.h"

 As we have now also the context we can wire it into Main.cpp. We will initialize the services when the engine starts and destroy the context (and all services in it) when the engine is to be destroyed.

Wiring context

 Change top of the Main.cpp to this:
//BEGIN_INCLUDE(all)
#include "src/System/system.h"
#include "src/Engine/Context.h"

#undef LOG_TAG
#define LOG_TAG "Main"

using namespace SBC::System;
using namespace SBC::Engine;

Context* gContext = NULL;

 Adjust engine_start and engine_stop methods:
//------------------------------------------------------------------------
static void engine_start(JNIEnv* aEnv, jobject aObj, jobject aAssetManager)
{
 LOGD("Starting engine ...");

 AAssetManager* assetManager = AAssetManager_fromJava(aEnv, aAssetManager);
 
 // create context that holds particular system services
 LOGI("Creating game context");
 gContext = new Context();
 
 // SERVICES
 // time service
 LOGI("Creating and initializing TimeService");
 TimeService* tm = new TimeService();
 tm->construct();
 gContext->setTimeService(tm);

}

//------------------------------------------------------------------------
static void engine_stop(JNIEnv* aEnv, jobject aObj, jboolean aTerminating)
{
 LOGD("Stopping engine ...");
 
 LOGI ("Deleting game Context");
 if (gContext != NULL)
 {
  delete gContext;
  gContext = NULL;
 }
}

and also adjust resume_engine method. We will reset the time service in it:
//------------------------------------------------------------------------
static void engine_resume(JNIEnv* aEnv, jobject aObj)
{
 LOGI("Resuming engine - resetting time");
 gContext->getTimeService()->reset();
}

 Finally, to see some effect change engine_tick method to this:
//------------------------------------------------------------------------
static void engine_tick(JNIEnv* aEnv, jobject aObj)
{
 TimeService* time = gContext->getTimeService();
 time->update();


 // do something
 static f32 counter = 0.0f;
 f32 elapsedTime = time->elapsed();
 counter += elapsedTime;
 if (counter > 1.0f)
  counter = 0.0f;

 // Just fill the screen with a color.
 glClearColor(1.0f - counter, 1.0f, counter, 0.0f);
 glClear(GL_COLOR_BUFFER_BIT);
}

 the counter variable is based on value of counter and the screen is blinking from yellow to light blue. The complete change of color takes 1 second.

Conclusion

 Today we created first of engine services - the TimeService. It tells us how much time elapsed from last tick and we can use this information to adjust whatever is based on real time in the game. Thanks to this we can base for example our animations on real time and not on frame rate which may differ from device to device.

 Download TutGame-04.zip




No comments:

Post a Comment