axmol/cocos2dx/platform/emscripten/CCApplication.cpp

108 lines
2.4 KiB
C++
Raw Normal View History

2013-04-09 09:21:53 +08:00
#include "CCApplication.h"
#include "platform/CCFileUtils.h"
#include "CCDirector.h"
#include "CCEGLView.h"
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <emscripten/emscripten.h>
#define LOGD(...) fprintf(stderr, __VA_ARGS__)
NS_CC_BEGIN;
// sharedApplication pointer
Application * Application::sm_pSharedApplication = 0;
long Application::_animationInterval = 1000;
2013-04-09 09:21:53 +08:00
// convert the timespec into milliseconds
static long time2millis(struct timespec *times)
{
return times->tv_sec*1000 + times->tv_nsec/1000000;
}
Application::Application()
2013-04-09 09:21:53 +08:00
{
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
Application::~Application()
2013-04-09 09:21:53 +08:00
{
CC_ASSERT(this == sm_pSharedApplication);
sm_pSharedApplication = NULL;
}
extern "C" void mainLoopIter(void)
{
EGLView::sharedOpenGLView()->handleEvents();
Director::getInstance()->mainLoop();
2013-04-09 09:21:53 +08:00
}
int Application::run()
2013-04-09 09:21:53 +08:00
{
struct timespec time_struct;
long update_time;
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 0;
}
clock_gettime(CLOCK_REALTIME, &time_struct);
update_time = time2millis(&time_struct);
// XXX: Set to 1FPS while debugging
emscripten_set_main_loop(&mainLoopIter, 0, 1);
return -1;
}
void Application::setAnimationInterval(double interval)
2013-04-09 09:21:53 +08:00
{
// interval in milliseconds
_animationInterval = (long)(interval * 1000);
2013-04-09 09:21:53 +08:00
}
void Application::setResourceRootPath(const std::string& rootResDir)
2013-04-09 09:21:53 +08:00
{
_resourceRootPath = rootResDir;
if (_resourceRootPath[_resourceRootPath.length() - 1] != '/')
2013-04-09 09:21:53 +08:00
{
_resourceRootPath += '/';
2013-04-09 09:21:53 +08:00
}
FileUtils* pFileUtils = FileUtils::getInstance();
2013-04-09 09:21:53 +08:00
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
searchPaths.insert(searchPaths.begin(), _resourceRootPath);
2013-04-09 09:21:53 +08:00
pFileUtils->setSearchPaths(searchPaths);
}
const std::string& Application::getResourceRootPath(void)
2013-04-09 09:21:53 +08:00
{
return _resourceRootPath;
2013-04-09 09:21:53 +08:00
}
TargetPlatform Application::getTargetPlatform()
2013-04-09 09:21:53 +08:00
{
return kTargetEmscripten;
}
//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
Application* Application::sharedApplication()
2013-04-09 09:21:53 +08:00
{
CC_ASSERT(sm_pSharedApplication);
return sm_pSharedApplication;
}
ccLanguageType Application::getCurrentLanguage()
2013-04-09 09:21:53 +08:00
{
return kLanguageEnglish;
}
NS_CC_END;