#ifdef __cplusplus
extern "C" {
#endif
#include "SDL_internal.h"
extern SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]);
extern SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event);
extern SDL_AppResult SDL_AppIterate(void *appstate);
extern void SDL_AppQuit(void *appstate, SDL_AppResult result);
#ifdef __cplusplus
}
#endif
#include <e32std.h>
#include <estlib.h>
#include <stdlib.h>
#include <stdio.h>
#include "SDL_sysmain_main.hpp"
#include "../../audio/ngage/SDL_ngageaudio.hpp"
#include "../../render/ngage/SDL_render_ngage_c.hpp"
CRenderer *gRenderer = 0;
GLDEF_C TInt E32Main()
{
int argc = 1;
char *argv[] = { "game", NULL };
char **envp = NULL;
char **argv_lvalue = argv;
char **envp_lvalue = envp;
CTrapCleanup *cleanup = CTrapCleanup::New();
if (!cleanup)
{
return KErrNoMemory;
}
TRAPD(err,
{
CActiveScheduler *scheduler = new (ELeave) CActiveScheduler();
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);
TInt posixErr = SpawnPosixServerThread();
if (posixErr != KErrNone)
{
SDL_Log("Error: Failed to spawn POSIX server thread: %d", posixErr);
User::Leave(posixErr);
}
__crt0(argc, argv_lvalue, envp_lvalue);
RHeap *newHeap = User::ChunkHeap(NULL, 7500000, 7500000, KMinHeapGrowBy);
if (!newHeap)
{
SDL_Log("Error: Failed to create new heap");
User::Leave(KErrNoMemory);
}
CleanupStack::PushL(newHeap);
RHeap *oldHeap = User::SwitchHeap(newHeap);
TInt targetLatency = 225;
InitAudio(&targetLatency);
while (!AudioIsReady())
{
User::After(100000); }
gRenderer = CRenderer::NewL();
CleanupStack::PushL(gRenderer);
CSDLmain *mainApp = CSDLmain::NewL();
CleanupStack::PushL(mainApp);
mainApp->Start();
CActiveScheduler::Start();
CleanupStack::PopAndDestroy(gRenderer);
CleanupStack::PopAndDestroy(mainApp);
User::SwitchHeap(oldHeap);
CleanupStack::PopAndDestroy(newHeap);
CleanupStack::PopAndDestroy(scheduler);
});
if (err != KErrNone)
{
SDL_Log("Error: %d", err);
}
return err;
}
CSDLmain *CSDLmain::NewL()
{
CSDLmain *self = new (ELeave) CSDLmain();
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
CSDLmain::CSDLmain() : CActive(EPriorityLow) {}
void CSDLmain::ConstructL()
{
CActiveScheduler::Add(this);
}
CSDLmain::~CSDLmain()
{
Cancel();
}
void CSDLmain::Start()
{
SetActive();
TRequestStatus *status = &iStatus;
User::RequestComplete(status, KErrNone);
}
void CSDLmain::DoCancel() {}
static bool callbacks_initialized = false;
void CSDLmain::RunL()
{
if (callbacks_initialized)
{
SDL_Event event;
iResult = SDL_AppIterate(NULL);
if (iResult != SDL_APP_CONTINUE)
{
DeinitAudio();
SDL_AppQuit(NULL, iResult);
SDL_Quit();
CActiveScheduler::Stop();
return;
}
SDL_PumpEvents();
if (SDL_PollEvent(&event))
{
iResult = SDL_AppEvent(NULL, &event);
if (iResult != SDL_APP_CONTINUE)
{
DeinitAudio();
SDL_AppQuit(NULL, iResult);
SDL_Quit();
CActiveScheduler::Stop();
return;
}
}
Start();
}
else
{
SDL_SetMainReady();
SDL_AppInit(NULL, 0, NULL);
callbacks_initialized = true;
Start();
}
}