Atlas - SDL_sysmain_main.cpp
Home / ext / SDL / src / main / ngage Lines: 1 | Size: 5109 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#ifdef __cplusplus 22extern "C" { 23#endif 24 25#include "SDL_internal.h" 26 27extern SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]); 28extern SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event); 29extern SDL_AppResult SDL_AppIterate(void *appstate); 30extern void SDL_AppQuit(void *appstate, SDL_AppResult result); 31 32#ifdef __cplusplus 33} 34#endif 35 36#include <e32std.h> 37#include <estlib.h> 38#include <stdio.h> 39#include <stdlib.h> 40 41#include "../../audio/ngage/SDL_ngageaudio.hpp" 42#include "../../render/ngage/SDL_render_ngage_c.hpp" 43#include "SDL_sysmain_main.hpp" 44 45CRenderer *gRenderer = 0; 46 47GLDEF_C TInt E32Main() 48{ 49 // Get args and environment. 50 int argc = 1; 51 char *argv[] = { "game", NULL }; 52 char **envp = NULL; 53 54 // Create lvalue variables for __crt0 arguments. 55 char **argv_lvalue = argv; 56 char **envp_lvalue = envp; 57 58 CTrapCleanup *cleanup = CTrapCleanup::New(); 59 if (!cleanup) { 60 return KErrNoMemory; 61 } 62 63 TRAPD(err, 64 { 65 CActiveScheduler *scheduler = new (ELeave) CActiveScheduler(); 66 CleanupStack::PushL(scheduler); 67 CActiveScheduler::Install(scheduler); 68 69 TInt posixErr = SpawnPosixServerThread(); 70 if (posixErr != KErrNone) { 71 SDL_Log("Error: Failed to spawn POSIX server thread: %d", posixErr); 72 User::Leave(posixErr); 73 } 74 75 __crt0(argc, argv_lvalue, envp_lvalue); 76 77 // Increase heap size. 78 RHeap *newHeap = User::ChunkHeap(NULL, 7500000, 7500000, KMinHeapGrowBy); 79 if (!newHeap) { 80 SDL_Log("Error: Failed to create new heap"); 81 User::Leave(KErrNoMemory); 82 } 83 CleanupStack::PushL(newHeap); 84 85 RHeap *oldHeap = User::SwitchHeap(newHeap); 86 87 TInt targetLatency = 225; 88 InitAudio(&targetLatency); 89 90 // Wait until audio is ready. 91 while (!AudioIsReady()) { 92 User::After(100000); // 100ms. 93 } 94 95 // Create and start the rendering backend. 96 gRenderer = CRenderer::NewL(); 97 CleanupStack::PushL(gRenderer); 98 99 // Create and start the SDL main runner. 100 CSDLmain *mainApp = CSDLmain::NewL(); 101 CleanupStack::PushL(mainApp); 102 mainApp->Start(); 103 104 // Start the active scheduler to handle events. 105 CActiveScheduler::Start(); 106 107 CleanupStack::PopAndDestroy(gRenderer); 108 CleanupStack::PopAndDestroy(mainApp); 109 110 User::SwitchHeap(oldHeap); 111 112 CleanupStack::PopAndDestroy(newHeap); 113 CleanupStack::PopAndDestroy(scheduler); 114 }); 115 116 if (err != KErrNone) { 117 SDL_Log("Error: %d", err); 118 } 119 120 return err; 121} 122 123CSDLmain *CSDLmain::NewL() 124{ 125 CSDLmain *self = new (ELeave) CSDLmain(); 126 CleanupStack::PushL(self); 127 self->ConstructL(); 128 CleanupStack::Pop(self); 129 return self; 130} 131 132CSDLmain::CSDLmain() : CActive(EPriorityStandard) {} 133 134void CSDLmain::ConstructL() 135{ 136 CActiveScheduler::Add(this); 137} 138 139CSDLmain::~CSDLmain() 140{ 141 Cancel(); 142} 143 144void CSDLmain::Start() 145{ 146 SetActive(); 147 TRequestStatus *status = &iStatus; 148 User::RequestComplete(status, KErrNone); 149} 150 151void CSDLmain::DoCancel() {} 152 153static bool callbacks_initialized = false; 154 155static void ShutdownApp(SDL_AppResult result) 156{ 157 DeinitAudio(); 158 SDL_AppQuit(NULL, result); 159 SDL_Quit(); 160 CActiveScheduler::Stop(); 161} 162 163void CSDLmain::RunL() 164{ 165 if (callbacks_initialized) { 166 SDL_Event event; 167 168 while (SDL_PollEvent(&event)) { 169 iResult = SDL_AppEvent(NULL, &event); 170 if (iResult != SDL_APP_CONTINUE) { 171 ShutdownApp(iResult); 172 return; 173 } 174 } 175 176 iResult = SDL_AppIterate(NULL); 177 if (iResult != SDL_APP_CONTINUE) { 178 ShutdownApp(iResult); 179 return; 180 } 181 182 Start(); 183 } else { 184 SDL_SetMainReady(); 185 SDL_AppInit(NULL, 0, NULL); 186 callbacks_initialized = true; 187 Start(); 188 } 189} 190[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.