int main(int argc, char** argv) { const char *inPath = argv[0]; // Get application directory int i, j; for (i = 0, j = 0; inPath[i] != '\0'; i++) { if ((inPath[i] == '\\') || (inPath[i] == '/')) j = i + 1; } strncpy(g_AppDirectory, inPath, j); g_AppDirectory[j] = '\0'; // Get application INI file path const char *inPathDot = strrchr(inPath, '.'); int c = (inPathDot == NULL) ? strlen(inPath) : inPathDot - inPath; strncpy(g_AppIniPath, inPath, c); strncpy(g_AppIniPath + c, ".ini", 4); #if defined(_DINGOO) SDL_putenv("DINGOO_IGNORE_OS_EVENTS=1"); //HACK to fix "push long time on X" problem #endif Settings_ParseIniFile(g_AppIniPath); // Init SDL video if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) return 255; // Unable to initialize SDL #ifdef _WIN32 SDL_putenv("SDL_VIDEO_WINDOW_POS=300,200"); SDL_WM_SetCaption("BKBTL SDL", "BKBTL SDL"); #else SDL_ShowCursor(SDL_DISABLE); #endif // Prepare screen surface g_Screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BITPERPIXEL, 0); if (g_Screen == NULL) return 254; // Unable to set video mode g_Keyboard = SDL_CreateRGBSurface(0, KEYBOARD_WIDTH, KEYBOARD_HEIGHT, 32, 0,0,0,0); SDL_SetAlpha(g_Keyboard, SDL_SRCALPHA, 180); SDL_SetColorKey(g_Keyboard, SDL_SRCCOLORKEY, SDL_MapRGB(g_Keyboard->format, 0,0,0)); SDL_Delay(1000); if (SDL_NumJoysticks()) g_Joystick = SDL_JoystickOpen(0); Fonts_Initialize(); if (!Emulator_Init()) return 255; if (!Emulator_InitConfiguration(DEFAULT_BK_CONF)) return 255; Main_ClearScreen(); Main_SetScreenMode(0); Emulator_Start(); Uint32 ticksLast; Uint32 ticksLastFps = SDL_GetTicks(); int frames = 0; while (!g_okQuit) { ticksLast = SDL_GetTicks(); // Time at frame start SDL_Event evt; while (SDL_PollEvent(&evt)) { if (evt.type == SDL_QUIT) { g_okQuit = TRUE; break; } else { if (evt.type == SDL_KEYDOWN || evt.type == SDL_KEYUP || evt.type == SDL_JOYBUTTONDOWN || evt.type == SDL_JOYBUTTONUP) { Main_OnKeyJoyEvent(evt); } } } if (g_okEmulatorRunning) { Emulator_SystemFrame(); Main_DrawScreen(); frames++; } // Delay Uint32 ticksNew = SDL_GetTicks(); Uint32 ticksElapsed = ticksNew - ticksLast; g_LastDelay = 0; if (ticksElapsed < FRAME_TICKS) { g_LastDelay = FRAME_TICKS - ticksElapsed; SDL_Delay(FRAME_TICKS - ticksElapsed); } ticksLast = ticksNew; if (ticksLast - ticksLastFps > 1000) //DEBUG: FPS calculation { g_LastFps = frames; frames = 0; ticksLastFps += 1000; } } Emulator_Stop(); Emulator_Done(); Main_SetScreenMode(-1); if (g_Joystick != NULL) { SDL_JoystickClose(g_Joystick); } // Free memory SDL_FreeSurface(g_Keyboard); SDL_FreeSurface(g_Screen); Fonts_Finalize(); SDL_Quit(); return 0; }
// Initialize the application bool InitInstance() { // Parse INI file Settings_ParseIniFile(g_ApplicationIniPath); // Retrieve general settings g_ScreenWidth = Settings_GetValueInt(SETTINGS_SECTION_VIDEO, SETTINGS_KEY_WIDTH, 800); if (g_ScreenWidth < 320) g_ScreenWidth = 320; g_ScreenHeight = Settings_GetValueInt(SETTINGS_SECTION_VIDEO, SETTINGS_KEY_HEIGHT, 600); if (g_ScreenHeight < 240) g_ScreenHeight = 240; g_ScreenBitsPerPixel = Settings_GetValueInt(SETTINGS_SECTION_VIDEO, SETTINGS_KEY_BITSPERPIXEL, 0); if (g_ScreenBitsPerPixel < 0) g_ScreenBitsPerPixel = 0; g_FullScreen = Settings_GetValueBool(SETTINGS_SECTION_VIDEO, SETTINGS_KEY_FULLSCREEN, false); g_ScreenMode = Settings_GetValueInt(SETTINGS_SECTION_VIDEO, SETTINGS_KEY_SCREENMODE, 0); Screen_GetScreenSize(g_ScreenMode, &g_UkncScreenWid, &g_UkncScreenHei); if (g_UkncScreenWid == 0 || g_UkncScreenHei == 0) // Unallowed mode, reset to default { g_ScreenMode = 0; Screen_GetScreenSize(g_ScreenMode, &g_UkncScreenWid, &g_UkncScreenHei); } // Init SDL video if (SDL_Init(SDL_INIT_VIDEO) < 0) return false; // Unable to initialize SDL #ifdef _WIN32 SDL_WM_SetCaption("UKNCBTL SDL", "UKNCBTL SDL"); #else SDL_ShowCursor(SDL_DISABLE); #endif // Apply SDL settings from the "SDL" section of the INI file // See http://www.libsdl.org/docs/html/sdlenvvars.html for the list of options const TSETTINGSKEYVALUEMAP* pMapSdl = Settings_GetKeyValues(SETTINGS_SECTION_SDL); if (pMapSdl != 0) { for (TSETTINGSKEYVALUEMAP::const_iterator itsdl = pMapSdl->begin(); itsdl != pMapSdl->end(); ++itsdl) { TCHAR buffer[256]; _sntprintf(buffer, 256, _T("%s=%s"), itsdl->first.c_str(), itsdl->second.c_str()); SDL_putenv(buffer); } } // Prepare screen surface int flags = SDL_HWSURFACE | SDL_DOUBLEBUF; if (g_FullScreen) flags |= SDL_FULLSCREEN; g_Screen = SDL_SetVideoMode(g_ScreenWidth, g_ScreenHeight, g_ScreenBitsPerPixel, flags); if (g_Screen == NULL) return false; // Unable to set video mode g_UkncScreen = SDL_CreateRGBSurface(0, g_UkncScreenWid, g_UkncScreenHei, 32, 0,0,0,0); if (g_Screen == NULL) return false; if (!Emulator_Init()) return false; // Restore settings: cartridges for (int slot = 1; slot < 2; slot++) { TCHAR buffer[11]; _tcscpy(buffer, SETTINGS_KEY_CARTRIDGE1); buffer[_tcslen(buffer) - 1] = _T('0') + slot; TSTRING sFileName = Settings_GetValue(SETTINGS_SECTION_UKNCBTL, buffer); if (! sFileName.empty()) Emulator_LoadROMCartridge(slot, sFileName.c_str()); } // Restore settings: floppies for (int slot = 0; slot < 4; slot++) { TCHAR buffer[11]; _tcscpy(buffer, SETTINGS_KEY_FLOPPY0); buffer[_tcslen(buffer) - 1] = _T('0') + slot; TSTRING sFileName = Settings_GetValue(SETTINGS_SECTION_UKNCBTL, buffer); if (! sFileName.empty()) Emulator_AttachFloppy(slot, sFileName.c_str()); } // Restore settings: hard drives for (int slot = 1; slot < 2; slot++) { TCHAR buffer[11]; _tcscpy(buffer, SETTINGS_KEY_HARD1); buffer[_tcslen(buffer) - 1] = _T('0') + slot; TSTRING sFileName = Settings_GetValue(SETTINGS_SECTION_UKNCBTL, buffer); if (! sFileName.empty()) Emulator_AttachHardDrive(slot, sFileName.c_str()); } ////DEBUG: SDL key names //std::map<const char*, int> keys; //for (int code = 0; code < 512; code++) //{ // const char * keyname = SDL_GetKeyName((SDLKey)code); // if (_stricmp(keyname, "unknown key") != 0) // keys[keyname] = code; //} //for (std::map<const char*, int>::iterator it = keys.begin(); it != keys.end(); ++it) //{ // printf("%04x\t%s\n", it->second, it->first); //} //printf("TOTAL: %d\n", keys.size()); return true; }