Atlas - SDL_sysmain_main.cpp

Home / ext / SDL / src / main / ngage Lines: 1 | Size: 5020 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 <stdlib.h> 39#include <stdio.h> 40 41#include "SDL_sysmain_main.hpp" 42#include "../../audio/ngage/SDL_ngageaudio.hpp" 43#include "../../render/ngage/SDL_render_ngage_c.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 { 61 return KErrNoMemory; 62 } 63 64 TRAPD(err, 65 { 66 CActiveScheduler *scheduler = new (ELeave) CActiveScheduler(); 67 CleanupStack::PushL(scheduler); 68 CActiveScheduler::Install(scheduler); 69 70 TInt posixErr = SpawnPosixServerThread(); 71 if (posixErr != KErrNone) 72 { 73 SDL_Log("Error: Failed to spawn POSIX server thread: %d", posixErr); 74 User::Leave(posixErr); 75 } 76 77 __crt0(argc, argv_lvalue, envp_lvalue); 78 79 // Increase heap size. 80 RHeap *newHeap = User::ChunkHeap(NULL, 7500000, 7500000, KMinHeapGrowBy); 81 if (!newHeap) 82 { 83 SDL_Log("Error: Failed to create new heap"); 84 User::Leave(KErrNoMemory); 85 } 86 CleanupStack::PushL(newHeap); 87 88 RHeap *oldHeap = User::SwitchHeap(newHeap); 89 90 TInt targetLatency = 225; 91 InitAudio(&targetLatency); 92 93 // Wait until audio is ready. 94 while (!AudioIsReady()) 95 { 96 User::After(100000); // 100ms. 97 } 98 99 // Create and start the rendering backend. 100 gRenderer = CRenderer::NewL(); 101 CleanupStack::PushL(gRenderer); 102 103 // Create and start the SDL main runner. 104 CSDLmain *mainApp = CSDLmain::NewL(); 105 CleanupStack::PushL(mainApp); 106 mainApp->Start(); 107 108 // Start the active scheduler to handle events. 109 CActiveScheduler::Start(); 110 111 CleanupStack::PopAndDestroy(gRenderer); 112 CleanupStack::PopAndDestroy(mainApp); 113 114 User::SwitchHeap(oldHeap); 115 116 CleanupStack::PopAndDestroy(newHeap); 117 CleanupStack::PopAndDestroy(scheduler); 118 }); 119 120 if (err != KErrNone) 121 { 122 SDL_Log("Error: %d", err); 123 } 124 125 return err; 126} 127 128CSDLmain *CSDLmain::NewL() 129{ 130 CSDLmain *self = new (ELeave) CSDLmain(); 131 CleanupStack::PushL(self); 132 self->ConstructL(); 133 CleanupStack::Pop(self); 134 return self; 135} 136 137CSDLmain::CSDLmain() : CActive(EPriorityLow) {} 138 139void CSDLmain::ConstructL() 140{ 141 CActiveScheduler::Add(this); 142} 143 144CSDLmain::~CSDLmain() 145{ 146 Cancel(); 147} 148 149void CSDLmain::Start() 150{ 151 SetActive(); 152 TRequestStatus *status = &iStatus; 153 User::RequestComplete(status, KErrNone); 154} 155 156void CSDLmain::DoCancel() {} 157 158static bool callbacks_initialized = false; 159 160void CSDLmain::RunL() 161{ 162 if (callbacks_initialized) 163 { 164 SDL_Event event; 165 166 iResult = SDL_AppIterate(NULL); 167 if (iResult != SDL_APP_CONTINUE) 168 { 169 DeinitAudio(); 170 SDL_AppQuit(NULL, iResult); 171 SDL_Quit(); 172 CActiveScheduler::Stop(); 173 return; 174 } 175 176 SDL_PumpEvents(); 177 if (SDL_PollEvent(&event)) 178 { 179 iResult = SDL_AppEvent(NULL, &event); 180 if (iResult != SDL_APP_CONTINUE) 181 { 182 DeinitAudio(); 183 SDL_AppQuit(NULL, iResult); 184 SDL_Quit(); 185 CActiveScheduler::Stop(); 186 return; 187 } 188 } 189 190 Start(); 191 } 192 else 193 { 194 SDL_SetMainReady(); 195 SDL_AppInit(NULL, 0, NULL); 196 callbacks_initialized = true; 197 Start(); 198 } 199} 200
[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.