void CSvrSyncPlug::DeleteGame(LPCWSTR dir) { wchar_t filename[MAX_PATH] = {0}; wchar_t find[MAX_PATH] = {0}; swprintf(find, L"%s*.*", dir); WIN32_FIND_DATAW wfd = {0}; HANDLE hFinder = FindFirstFile(find, &wfd); if (hFinder == INVALID_HANDLE_VALUE) return ; do { if (WAIT_OBJECT_0 == WaitForSingleObject(m_hExited, 0)) break; if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { //filter dot directory. if (!((wfd.cFileName[0] == L'.') && ((wfd.cFileName[1] == 0) || (wfd.cFileName[1] == L'.' && wfd.cFileName[2] == 0)))) { swprintf(filename, L"%s%s\\", dir, wfd.cFileName); DeleteGame(filename); } } else { swprintf(filename, L"%s%s", dir, wfd.cFileName); SetFileAttributesW(filename, FILE_ATTRIBUTE_NORMAL); DeleteFileW(filename); } }while (FindNextFileW(hFinder, &wfd)); FindClose(hFinder); }
bool CGameUpdate::UpdateBefore(__int64 qUpdateBytes) { //check disk free room. wchar_t szLog[1024] = {0}; qUpdateBytes += SAVE_DISK_ROOMSIZE; LARGE_INTEGER liSize = {0}; hy_GetDiskRoomSize(m_strCliPath[0], &liSize); if (liSize.QuadPart >= qUpdateBytes) return true; //删除游戏,保证空间足够。 if (m_dwUptFlag & UPDATE_FLAG_DELETE_GAME) { SetUpdateStatus(UPDATE_STATUS_DELETE_GAME); std::vector<tagGameInfo*> list; GetAllGameList(list); for (size_t idx=0; idx<list.size(); idx++) { if (!CheckState()) { std::for_each(list.begin(), list.end(), Delete_Pointer<tagGameInfo>); return false; } tagGameInfo* pGame = list[idx]; if (PathFileExistsA(pGame->CliPath) && pGame->CliPath[0] == m_strCliPath[0] && lstrcmpiW(_bstr_t(pGame->CliPath), m_strCliPath.c_str()) != 0) { std::wstring str(_bstr_t(pGame->CliPath)); std::wstring str2 = hy_ConvertPath(str); SetUpdateInfo(INFO_STR_DELETEGAME, str.c_str()); DeleteGame(str2.c_str()); hy_DyncaRefreDriver(str[0]); hy_GetDiskRoomSize(m_strCliPath[0], &liSize); if (liSize.QuadPart >= qUpdateBytes) { std::for_each(list.begin(), list.end(), Delete_Pointer<tagGameInfo>); return true; } else { swprintf_s(szLog, L"i8desk: free size:%dM, need size:%dM.", (DWORD)(liSize.QuadPart / 1000 / 1000), (DWORD)(qUpdateBytes / 1000 / 1000)); OutputDebugStringW(szLog); } } } std::for_each(list.begin(), list.end(), Delete_Pointer<tagGameInfo>); } //空间不足,返回错误。 SetErrorInfo(UPT_ERR_DISKNOROOM, (DWORD)(liSize.QuadPart / 1000 / 1000), (DWORD)(qUpdateBytes / 1000 / 1000)); return false; }
int main(void) { printf("Welcome to championship whack-a-mole!\nType \"help\" for more information.\n"); char cmd[NAME_SIZE+1]={0}; Team* selectedTeam; ScoreboardList = NULL; ReadTeams(); while(true) { printf("\n>>> "); do { fflush(stdout); } while ( !readString(cmd, NAME_SIZE) ); printf("\n"); if ( ! strcmp(cmd, "q") || ! strcmp(cmd, "quit") ) { printf("Bye bye!"); break; } else if ( !strncmp(cmd, "SHOW ", 5) ) { if ( !strcmp(&cmd[5], "TEAMS") ) { // 5 = strlen("SHOW "); LastGameList = NULL; ShowGames(); } else if ( (selectedTeam = getTeamFromInput(&cmd[5])) != NULL ){ TeamUpdateGameListCache(selectedTeam, NULL); LastGameList = selectedTeam->gameList; printf("TEAM: %s\n", selectedTeam->name); // FIXME: TESTING ShowGames(); } else goto wrong_command; } else if ( !strcmp(cmd, "teams") ) { ListTeams(); } else if ( !strncmp(cmd, "add", 3) ) { NewGame(&cmd[4]); // 4 = strlen("add ") } else if ( !strncmp(cmd, "del", 3) ) { DeleteGame(&cmd[3]); // 3 = strlen("del") } else if ( !strcmp(cmd, "scores") ) { ShowScoreboard(); } else if ( !strcmp(cmd, "help") ) { showHelp(); } else { wrong_command: printf("Unrecognized command.\n"); } } return 0; }
size_t GameDatabase::UpdateGame(const size_t index, const StringPairArray &rulesets, const StringPairArray& options, const ISerialize& stats, const Game &game) { // If this is the last game in the list, no need to reload the database. bool reload = (index != mGames.size() - 1); // Delete the game and then resave it. DeleteGame(index, false); return SaveGame(game, rulesets, options, stats, reload); }
/* ======================== idMenuScreen_Shell_Save::HandleAction ======================== */ bool idMenuScreen_Shell_Save::HandleAction( idWidgetAction& action, const idWidgetEvent& event, idMenuWidget* widget, bool forceHandled ) { if( menuData == NULL ) { return true; } if( menuData->ActiveScreen() != SHELL_AREA_SAVE ) { return false; } widgetAction_t actionType = action.GetType(); const idSWFParmList& parms = action.GetParms(); switch( actionType ) { case WIDGET_ACTION_JOY4_ON_PRESS: { return true; } case WIDGET_ACTION_JOY3_ON_PRESS: { if( options == NULL ) { return true; } DeleteGame( options->GetViewIndex() ); return true; } case WIDGET_ACTION_GO_BACK: { menuData->SetNextScreen( SHELL_AREA_ROOT, MENU_TRANSITION_SIMPLE ); return true; } case WIDGET_ACTION_PRESS_FOCUSED: { if( options == NULL ) { return true; } if( session->GetSaveGameManager().IsWorking() ) { return true; } if( parms.Num() == 1 ) { int selectionIndex = parms[0].ToInteger(); if( selectionIndex != options->GetFocusIndex() ) { options->SetViewIndex( options->GetViewOffset() + selectionIndex ); options->SetFocusIndex( selectionIndex ); return true; } } SaveGame( options->GetViewIndex() ); return true; } case WIDGET_ACTION_SCROLL_VERTICAL: { return true; } } return idMenuWidget::HandleAction( action, event, widget, forceHandled ); }
/* ======================== idMenuScreen_Shell_Load::HandleAction h ======================== */ bool idMenuScreen_Shell_Load::HandleAction( idWidgetAction& action, const idWidgetEvent& event, idMenuWidget* widget, bool forceHandled ) { if( menuData != NULL ) { if( menuData->ActiveScreen() != SHELL_AREA_LOAD ) { return false; } } widgetAction_t actionType = action.GetType(); const idSWFParmList& parms = action.GetParms(); switch( actionType ) { case WIDGET_ACTION_JOY4_ON_PRESS: { return true; } case WIDGET_ACTION_JOY3_ON_PRESS: { if( options == NULL ) { return true; } int selectionIndex = options->GetViewIndex(); DeleteGame( selectionIndex ); return true; } case WIDGET_ACTION_GO_BACK: { if( menuData != NULL ) { if( game->IsInGame() ) { menuData->SetNextScreen( SHELL_AREA_ROOT, MENU_TRANSITION_SIMPLE ); } else { menuData->SetNextScreen( SHELL_AREA_CAMPAIGN, MENU_TRANSITION_SIMPLE ); } } return true; } case WIDGET_ACTION_PRESS_FOCUSED: { if( options == NULL ) { return true; } if( sortedSaves.Num() == 0 ) { return true; } int selectionIndex = options->GetViewIndex(); if( parms.Num() == 1 ) { selectionIndex = parms[0].ToInteger(); if( selectionIndex != options->GetFocusIndex() ) { options->SetViewIndex( options->GetViewOffset() + selectionIndex ); options->SetFocusIndex( selectionIndex ); } else { LoadGame( options->GetViewOffset() + selectionIndex ); } } else { LoadGame( options->GetViewIndex() ); } return true; } case WIDGET_ACTION_SCROLL_VERTICAL: { return true; } } return idMenuWidget::HandleAction( action, event, widget, forceHandled ); }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEXA wc = {0}; MSG Msg = {0}; // Create a window with these properties wc.cbSize = sizeof(WNDCLASSEX); wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW); wc.lpszClassName = "WindowClass"; wc.hIcon = LoadIcon(hInstance,"A"); // laad projecticoon wc.hIconSm = LoadIcon(hInstance,"A"); // Say hi to Windows if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } // Set up a window with 1024x768 usable pixels RECT result = {0,0,1024,768}; AdjustWindowRect(&result,WS_VISIBLE|WS_OVERLAPPEDWINDOW,false); // Create a window with a border and 'client rect' of 1024x768 hwnd = CreateWindow("WindowClass","DotsEnBoxes",WS_VISIBLE|WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, // x CW_USEDEFAULT, // y result.right - result.left, // width result.bottom - result.top, // height NULL,NULL,hInstance,NULL); if(hwnd == NULL) { MessageBox(NULL,"Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } // Init render loop InitEngine(); InitGame(); // Handle user input. If done, render a frame. Goto 1 while(Msg.message != WM_QUIT) { while(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } if(!renderer->paused) { renderer->Begin(false); renderer->DrawScene(scene); renderer->End(); } else { Sleep(100); } } DeleteGame(); // Render loop stopped due to Alt+F4 etc? Delete everything DeleteEngine(); return Msg.wParam; }
int main( int pArgc, const char** pArgs ) { const char* lQueryPtr; int lLock; int lMemId; char lQuery[4096]; char lOp[12]; BOOL lPrintTitle; #ifndef _NO_IPC_ union semun lNULL; #endif #ifdef _NO_IPC_ gGlobalState.Clear(); if( InitLogFile() ) { fprintf( gLogFile, "IMR Init version 0.13.13b %s\n", GetTime( time(NULL)) ); } #endif #ifdef _FAST_CGI_ while( FCGI_Accept() >= 0 ) { #endif lPrintTitle = TRUE; /* Send the header required by the server */ printf("Content-type: text/plain%c%c", 10, 10); lQueryPtr = getenv( "QUERY_STRING" ); if( lQueryPtr == NULL ) { printf( "No parameters\n" ); } else { StrMaxCopy( lQuery, lQueryPtr, 4096 ); UnpadQuery( lQuery ); if( sscanf( lQuery, "=%11s", lOp ) == 1 ) { #ifndef _FAST_CGI_ /* Change the local directory */ char* lPathEnd = strrchr( pArgs[0], '/' ); if( lPathEnd != NULL ) { *lPathEnd = 0; chdir( pArgs[0] ); } #endif if( !strcmp( lOp, "RESET" ) ) { printf( "RESET OK\n\n" ); #ifdef _NO_IPC_ /* // gGlobalState.Clear(); // sleep( 2 ); // return 0; // break; */ #else lLock = semget( IR_SEM_KEY, 1, 0777 ); lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666 ); if( lLock != -1 ) { semctl( lLock, 0, IPC_RMID, lNULL ); } if( lMemId != -1 ) { shmctl( lMemId, IPC_RMID, NULL ); } #endif if( InitLogFile() ) { fprintf( gLogFile, "IMR Reset %s\n", GetTime( time(NULL)) ); } #ifdef _FAST_CGI_ break; #endif } else if( !strcmp( lOp, "SET_MESSAGE" ) ) { } /* else if( !strcmp( lOp, "STICK" ) ) { // Fork and leave a child running for 1 hour int lCode = fork(); if( lCode == 0 ) { if( InitLogFile() ) { fprintf( gLogFile, "STICK Start %s\n", GetTime( time(NULL)) ); CloseLogFile(); } close( 0 ); close( 1 ); close( 2 ); sleep( 3600 ); if( InitLogFile() ) { fprintf( gLogFile, "STICK End %s\n", GetTime( time(NULL)) ); CloseLogFile(); } } else if( lCode == -1 ) { printf( "ERROR\n" ); } else { printf( "SUCCESS\n" ); } } */ else { IRState* lState = NULL; #ifdef _NO_IPC_ lState = &gGlobalState; #else int lLock; /* Semaphore */ int lMemId; /* Shared memory */ struct sembuf lSemOp; lSemOp.sem_flg = 0; /*Avoid corruption but must not core-dump SEM_UNDO; // Risky but prevents dead-lock */ lSemOp.sem_num = 0; /* First try to create the structure for the first time */ /* Lock the data struct */ lLock = semget( IR_SEM_KEY, 1, 0777|IPC_CREAT|IPC_EXCL ); if( lLock != -1 ) { union semun lArg; /* Initialize the newly created semaphore */ lArg.val = 1; semctl( lLock, 0, SETVAL, lArg ); } else { lLock = semget( IR_SEM_KEY, 1, 0777 ); } if( lLock == -1 ) { printf( "Unable to get semaphore\n" ); } else { lSemOp.sem_op = -1; if( semop( lLock, &lSemOp, 1 ) == -1 ) { printf( "Unable to decrement semaphore\n" ); lLock = -1; } else { /* From here we can work safely */ lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666|IPC_CREAT|IPC_EXCL ); if( lMemId != -1 ) { lState = (IRState*)shmat( lMemId, NULL, 0 ); if( (int)lState == -1 ) { lState = NULL; } if( lState == NULL ) { printf( "Unable to attach shmem\n" ); } else { Clear( lState ); if( InitLogFile() ) { fprintf( gLogFile, "IMR Init %s\n", GetTime( time(NULL)) ); } } } else { lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666 ); if( lMemId == -1 ) { printf( "Unable to get shmem\n" ); } else { lState = (IRState*)shmat( lMemId, NULL, 0 ); if( (int)lState == -1 ) { lState = NULL; } if( lState == NULL ) { printf( "Unable to attach shmem\n" ); } } } } } #endif if( lState != NULL ) { lPrintTitle = FALSE; VerifyExpirations( lState ); if( !strcmp( lOp, "REFRESH" ) ) { int lUserIndex; int lUserId; int lTimeStamp; if( sscanf( lQuery, "%*s %d-%u %d", &lUserIndex, &lUserId, &lTimeStamp )==3 ) { PrintStateChange( lState, lUserIndex, lUserId, lTimeStamp ); } } else if( !strcmp( lOp, "ADD_CHAT" ) ) { int lUserIndex; int lUserId; char lChatMessage[200]; if( sscanf( lQuery, "%*s %d-%u %200s", &lUserIndex, &lUserId, lChatMessage )==3 ) { Unpad( lChatMessage ); AddChatMessage( lState, lUserIndex, lUserId, lChatMessage ); } } /* URL?=ADD_USER MAJOR-MINORID VERSION KEY2 KEY3 ALIAS */ else if( !strcmp( lOp, "ADD_USER" ) ) { int lMajorID; int lMinorID; int lVersion; unsigned int lKey2; unsigned int lKey3; char lUserName[40]; #ifdef _EXPIRED_ AddUser( lState, "User", 1,-1, -1, 0, 0 ); #else if( sscanf( lQuery, "%*s %d-%d %d %d %d %40s", &lMajorID, &lMinorID, &lVersion, &lKey2, &lKey3, lUserName )==6 ) { Unpad( lUserName ); AddUser( lState, lUserName, lVersion,lMajorID, lMinorID, lKey2, lKey3 ); } #endif } /* URL?=ADD_GAME USER_ID GAME_NAME TRACK_NAME NBLAP WEAPON PORT */ else if( !strcmp( lOp, "ADD_GAME" ) ) { int lUserIndex; int lUserId; int lNbLap; char lGameName[40]; char lTrackName[40]; int lWeapon; unsigned lPort; if( sscanf( lQuery, "%*s %d-%u %40s %40s %d %d %u", &lUserIndex, &lUserId, lGameName, lTrackName, &lNbLap, &lWeapon, &lPort )==7 ) { const char* lRemoteAddr = getenv( "REMOTE_ADDR" ); if( (lRemoteAddr != NULL)&&(strlen(lRemoteAddr) != 0) ) { Unpad( lTrackName ); Unpad( lGameName ); AddGame( lState, lGameName, lTrackName, lNbLap, lUserIndex, lUserId, lRemoteAddr, lPort, lWeapon ); } } } else if( !strcmp( lOp, "JOIN_GAME" ) ) { int lUserIndex; int lUserId; int lGameIndex; int lGameId; if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 ) { JoinGame( lState, lGameIndex, lGameId, lUserIndex, lUserId ); } } else if( !strcmp( lOp, "DEL_GAME" ) ) { int lUserIndex; int lUserId; int lGameIndex; int lGameId; if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 ) { DeleteGame( lState, lGameIndex, lGameId, lUserIndex, lUserId ); } } else if( !strcmp( lOp, "LEAVE_GAME" ) ) { int lUserIndex; int lUserId; int lGameIndex; int lGameId; if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 ) { LeaveGame( lState, lGameIndex, lGameId, lUserIndex, lUserId ); } } else if( !strcmp( lOp, "DEL_USER" ) ) { int lUserIndex; int lUserId; if( sscanf( lQuery, "%*s %d-%u", &lUserIndex, &lUserId )==2 ) { DeleteUser( lState, lUserIndex, lUserId ); } } else if( !strcmp( lOp, "START_GAME" ) ) { int lUserIndex; int lUserId; int lGameIndex; int lGameId; if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 ) { StartGame( lState, lGameIndex, lGameId, lUserIndex, lUserId ); } } else { lPrintTitle = TRUE; } } #ifdef _NO_IPC_ lState = NULL; #else /* Release lock */ if( lLock != -1 ) { lSemOp.sem_op = 1; semop( lLock, &lSemOp, 1 ); /* Release memory */ if( lState != NULL ) { shmdt( (char*)lState ); } } #endif } } } CloseLogFile(); if( lPrintTitle ) { printf( "Internet Meeting Room (c)1996,97 GrokkSoft inc.\n" ); } #ifdef _FAST_CGI_ } #endif return 0; }
int main(int argc, char **argv) { MemStartCheck(); { char* test = new char[16]; delete[] test; } grinliz::TestContainers(); { grinliz::GLString releasePath; GetSystemPath(GAME_SAVE_DIR, "release_log.txt", &releasePath); SetReleaseLog(fopen(releasePath.c_str(), "w")); } GLOUTPUT_REL(("Altera startup. version'%s'\n", VERSION)); SDL_version compiled; SDL_version linked; SDL_VERSION(&compiled); SDL_GetVersion(&linked); GLOUTPUT_REL(("SDL version compiled: %d.%d.%d\n", compiled.major, compiled.minor, compiled.patch)); GLOUTPUT_REL(("SDL version linked: %d.%d.%d\n", linked.major, linked.minor, linked.patch)); GLASSERT((linked.major == compiled.major && linked.minor == compiled.minor)); // SDL initialization steps. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_EVENTS) < 0) { fprintf(stderr, "SDL initialization failed: %s\n", SDL_GetError()); exit(1); } // OpenGL 4.3 provides full compatibility with OpenGL ES 3.0. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); #ifdef DEBUG #if 0 // I was hoping to get to Angle on intel - may still be able to. But // as is this gets HW mode, which crashes in a function that should // be supported. The Intel drivers are so terrible. As of this writing, // you can't specify the DX version of ES: // http://forums.libsdl.org/viewtopic.php?t=9770&highlight=angle+opengl SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // driver supports 2 and 3. Both crash. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); #endif #if 0 // 3.0 context. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); #endif #if 0 // The trickier 3.2 context. // No GL_QUADs anymore. // All the attributes need to be floats. // No ALPHA textures. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); #endif #if 0 // In theory the minimum supported version: // has instancing, and modern shader syntax SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); #endif #if 0 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); #endif #endif if (multisample) { SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multisample); } SDL_DisplayMode displayMode; SDL_GetCurrentDisplayMode(0, &displayMode); int screenX = displayMode.w / 8; int screenY = displayMode.h / 8; int screenWidth = displayMode.w * 3 / 4; int screenHeight = displayMode.h * 3 / 4; if (argc == 3) { screenWidth = atoi(argv[1]); screenHeight = atoi(argv[2]); if (screenWidth <= 0) screenWidth = SCREEN_WIDTH; if (screenHeight <= 0) screenHeight = SCREEN_HEIGHT; } restoreWidth = screenWidth; restoreHeight = screenHeight; SDL_Window *screen = SDL_CreateWindow("Altera", screenX, screenY, screenWidth, screenHeight, /*SDL_WINDOW_FULLSCREEN | */ SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); GLASSERT(screen); SDL_GL_CreateContext(screen); int stencil = 0; int depth = 0; CHECK_GL_ERROR; SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencil); SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth); glGetError(); // the above stencil/depth query sometimes does throw an error. glGetError(); // 2 queries, 2 errors. CHECK_GL_ERROR; GLOUTPUT_REL(("SDL screen created. stencil=%d depthBits=%d\n", stencil, depth)); /* Verify there is a surface */ if (!screen) { fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError()); exit(1); } CHECK_GL_ERROR; glewExperimental = GL_TRUE; int r = glewInit(); GLASSERT(r == GL_NO_ERROR); (void)r; while (glGetError() != GL_NO_ERROR) { // around again } CHECK_GL_ERROR; const unsigned char* vendor = glGetString(GL_VENDOR); const unsigned char* renderer = glGetString(GL_RENDERER); const unsigned char* version = glGetString(GL_VERSION); GLOUTPUT_REL(("OpenGL vendor: '%s' Renderer: '%s' Version: '%s'\n", vendor, renderer, version)); CHECK_GL_ERROR; bool done = false; bool zooming = false; SDL_Event event; grinliz::Vector2I mouseDown = { 0, 0 }; grinliz::Vector2I rightMouseDown = { 0, 0 }; int zoomX = 0; int zoomY = 0; // Used to compute fingers close, but problems: // - really to the OS to do that, because of all the tuning // - the coordinates are in windows normalized, so can't get the physical distance reliably. //bool fingersClose = true; int nFingers = 0; void* game = NewGame(screenWidth, screenHeight, 0); int modKeys = SDL_GetModState(); U32 tickTimer = 0, lastTick = 0, thisTick = 0; #ifdef OUTPUT_MOUSE_AND_TOUCH int value = GetSystemMetrics(SM_DIGITIZER); if (value & NID_INTEGRATED_TOUCH) GLOUTPUT(("NID_INTEGRATED_TOUCH\n")); if (value & NID_MULTI_INPUT) GLOUTPUT(("NID_MULTI_INPUT\n")); if (value & NID_READY) GLOUTPUT(("NID_READY\n")); #endif grinliz::Vector2F multiTouchStart = { 0, 0 }; // ---- Main Loop --- // while (!done) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_WINDOWEVENT: if (event.window.event == SDL_WINDOWEVENT_RESIZED) { screenWidth = event.window.data1; screenHeight = event.window.data2; GameDeviceLoss(game); GameResize(game, screenWidth, screenHeight, 0); } break; case SDL_KEYUP: switch (event.key.keysym.scancode) { case SDL_SCANCODE_LCTRL: modKeys = modKeys & (~KMOD_LCTRL); break; case SDL_SCANCODE_RCTRL: modKeys = modKeys & (~KMOD_RCTRL); break; case SDL_SCANCODE_LSHIFT: modKeys = modKeys & (~KMOD_LSHIFT); break; case SDL_SCANCODE_RSHIFT: modKeys = modKeys & (~KMOD_RSHIFT); break; default: break; } break; case SDL_KEYDOWN: { // sym or scancode? I used a dvorak keyboard, so appreciate // every day the difference. However, AWSD support is the // primary thing so scancode is hopefully the better choice. switch (event.key.keysym.scancode) { case SDL_SCANCODE_LCTRL: modKeys = modKeys | KMOD_LCTRL; break; case SDL_SCANCODE_RCTRL: modKeys = modKeys | KMOD_RCTRL; break; case SDL_SCANCODE_LSHIFT: modKeys = modKeys | KMOD_LSHIFT; break; case SDL_SCANCODE_RSHIFT: modKeys = modKeys | KMOD_RSHIFT; break; case SDL_SCANCODE_F4: { int sdlMod = SDL_GetModState(); if (sdlMod & (KMOD_RALT | KMOD_LALT)) done = true; } break; case SDL_SCANCODE_ESCAPE: GameHotKey(game, GAME_HK_ESCAPE); break; case SDL_SCANCODE_SPACE: GameHotKey(game, GAME_HK_TOGGLE_PAUSE); break; case SDL_SCANCODE_RETURN: GameHotKey(game, GAME_HK_DEBUG_ACTION); break; case SDL_SCANCODE_F1: GameHotKey(game, GAME_HK_TOGGLE_DEBUG_TEXT); break; case SDL_SCANCODE_F2: GameHotKey(game, GAME_HK_TOGGLE_DEBUG_UI); break; // F3: screenshot case SDL_SCANCODE_TAB: GameHotKey(game, GAME_HK_CAMERA_TOGGLE); break; case SDL_SCANCODE_HOME: GameHotKey(game, GAME_HK_CAMERA_CORE); break; case SDL_SCANCODE_END: GameHotKey(game, GAME_HK_CAMERA_AVATAR); break; case SDL_SCANCODE_PAGEUP: GameHotKey(game, GAME_HK_TELEPORT_AVATAR); break; //case SDLK_a: reserved case SDL_SCANCODE_B: GameHotKey(game, GAME_HK_CHEAT_GOLD); break; case SDL_SCANCODE_C: GameHotKey(game, GAME_HK_ATTACH_CORE); break; //case SDLK_d: reserved case SDL_SCANCODE_E: GameHotKey(game, GAME_HK_CHEAT_ELIXIR); break; case SDL_SCANCODE_H: GameHotKey(game, GAME_HK_TOGGLE_PATHING); break; case SDL_SCANCODE_I: GameHotKey(game, GAME_HK_TOGGLE_AI_DEBUG); break; case SDL_SCANCODE_K: GameHotKey(game, GAME_HK_CHEAT_CRYSTAL); break; case SDL_SCANCODE_M: GameHotKey(game, GAME_HK_MAP); break; case SDL_SCANCODE_P: GameHotKey(game, GAME_HK_TOGGLE_PERF); break; case SDL_SCANCODE_Q: GameHotKey(game, GAME_HK_CHEAT_HERD); break; //case SDLK_s: reserved case SDL_SCANCODE_T: GameHotKey(game, GAME_HK_CHEAT_TECH); break; case SDL_SCANCODE_U: GameHotKey(game, GAME_HK_TOGGLE_UI); break; //case SDLK_w: reserved case SDL_SCANCODE_1: GameHotKey(game, GAME_HK_TOGGLE_GLOW); break; case SDL_SCANCODE_2: GameHotKey(game, GAME_HK_TOGGLE_PARTICLE); break; case SDL_SCANCODE_3: GameHotKey(game, GAME_HK_TOGGLE_VOXEL); break; case SDL_SCANCODE_4: GameHotKey(game, GAME_HK_TOGGLE_SHADOW); break; case SDL_SCANCODE_5: GameHotKey(game, GAME_HK_TOGGLE_BOLT); break; case SDL_SCANCODE_F3: GameDoTick(game, SDL_GetTicks()); SDL_GL_SwapWindow(screen); ScreenCapture(); break; case SDL_SCANCODE_F11: { if (fullscreen) { // SDL_RestoreWindow doesn't seem to work as I expect. SDL_SetWindowFullscreen(screen, 0); SDL_SetWindowSize(screen, restoreWidth, restoreHeight); fullscreen = false; } else { restoreWidth = screenWidth; restoreHeight = screenHeight; SDL_SetWindowFullscreen(screen, SDL_WINDOW_FULLSCREEN_DESKTOP); fullscreen = true; } } break; default: break; } } break; case SDL_MOUSEBUTTONDOWN: { int x = event.button.x; int y = event.button.y; int mod = 0; if (modKeys & (KMOD_LSHIFT | KMOD_RSHIFT)) mod = GAME_TAP_MOD_SHIFT; else if (modKeys & (KMOD_LCTRL | KMOD_RCTRL)) mod = GAME_TAP_MOD_CTRL; if (nFingers > 1) { // do nothing } else if (event.button.button == SDL_BUTTON_LEFT) { #ifdef OUTPUT_MOUSE_AND_TOUCH GLOUTPUT(("Left mouse down %d %d\n", x, y)); #endif mouseDown.Set(event.button.x, event.button.y); GameTap(game, GAME_TAP_DOWN, x, y, mod); } else if (event.button.button == SDL_BUTTON_RIGHT) { #ifdef OUTPUT_MOUSE_AND_TOUCH GLOUTPUT(("Right mouse down %d %d\n", x, y)); #endif GameTap(game, GAME_TAP_CANCEL, x, y, mod); rightMouseDown.Zero(); if (mod == 0) { rightMouseDown.Set(event.button.x, event.button.y); GameCameraPan(game, GAME_PAN_START, float(x), float(y)); } else if (mod == GAME_TAP_MOD_CTRL) { zooming = true; //GameCameraRotate( game, GAME_ROTATE_START, 0.0f ); SDL_GetRelativeMouseState(&zoomX, &zoomY); } } } break; case SDL_MOUSEBUTTONUP: { int x = event.button.x; int y = event.button.y; if (event.button.button == SDL_BUTTON_RIGHT) { #ifdef OUTPUT_MOUSE_AND_TOUCH GLOUTPUT(("Right mouse up %d %d\n", x, y)); #endif zooming = false; if (!rightMouseDown.IsZero()) { GameCameraPan(game, GAME_PAN_END, float(x), float(y)); rightMouseDown.Zero(); } } if (event.button.button == SDL_BUTTON_LEFT) { if (!mouseDown.IsZero()) { // filter out mouse events that become finger events. #ifdef OUTPUT_MOUSE_AND_TOUCH GLOUTPUT(("Left mouse up %d %d\n", x, y)); #endif int mod = 0; if (modKeys & (KMOD_LSHIFT | KMOD_RSHIFT)) mod = GAME_TAP_MOD_SHIFT; else if (modKeys & (KMOD_LCTRL | KMOD_RCTRL)) mod = GAME_TAP_MOD_CTRL; GameTap(game, GAME_TAP_UP, x, y, mod); } } } break; case SDL_MOUSEMOTION: { SDL_GetRelativeMouseState(&zoomX, &zoomY); int state = event.motion.state; int x = event.motion.x; int y = event.motion.y; int mod = 0; if (modKeys & (KMOD_LSHIFT | KMOD_RSHIFT)) mod = GAME_TAP_MOD_SHIFT; else if (modKeys & (KMOD_LCTRL | KMOD_RCTRL)) mod = GAME_TAP_MOD_CTRL; if (nFingers > 1) { // Do nothing. // Multi touch in progress. } else if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) { if (!mouseDown.IsZero()) { #ifdef OUTPUT_MOUSE_AND_TOUCH GLOUTPUT(("Left Mouse move %d %d\n", x, y)); #endif GameTap(game, GAME_TAP_MOVE, x, y, mod); // mouseMoveCount = 0; } } else if (!rightMouseDown.IsZero()) { GLASSERT(state & SDL_BUTTON(SDL_BUTTON_RIGHT)); GameCameraPan(game, GAME_PAN_END, float(x), float(y)); } else if (zooming && (state & SDL_BUTTON(SDL_BUTTON_RIGHT))) { float deltaZoom = 0.01f * (float)zoomY; GameZoom(game, GAME_ZOOM_DISTANCE, deltaZoom); GameCameraRotate(game, (float)(zoomX)*0.5f); } else if (state ==0) { GameTap(game, GAME_MOVE_WHILE_UP, x, y, mod); } } break; case SDL_MOUSEWHEEL: { if (event.wheel.y) { float deltaZoom = -0.1f * float(event.wheel.y); GameZoom(game, GAME_ZOOM_DISTANCE, deltaZoom); } } break; case SDL_FINGERUP: { const SDL_TouchFingerEvent* tfe = &event.tfinger; nFingers = SDL_GetNumTouchFingers(tfe->touchId); if (nFingers < 2 && !multiTouchStart.IsZero()) { #ifdef OUTPUT_MOUSE_AND_TOUCH GLOUTPUT(("2 finger END.\n")); #endif multiTouchStart.Zero(); } } break; case SDL_FINGERDOWN: { const SDL_TouchFingerEvent* tfe = &event.tfinger; nFingers = SDL_GetNumTouchFingers(tfe->touchId); if (nFingers > 1) { if (!mouseDown.IsZero()) { // Wrap up the existing action. #ifdef OUTPUT_MOUSE_AND_TOUCH GLOUTPUT(("Switch to gesture.\n")); #endif mouseDown.Zero(); } } } break; case SDL_MULTIGESTURE: { const SDL_MultiGestureEvent* mge = &event.mgesture; nFingers = SDL_GetNumTouchFingers(mge->touchId); if (nFingers > 1 && multiTouchStart.IsZero()) { #ifdef OUTPUT_MOUSE_AND_TOUCH GLOUTPUT(("2 finger START.\n")); #endif multiTouchStart.Set(mge->x, mge->y); } else if (!multiTouchStart.IsZero()) { // The Pan interacts badly with zoom and rotated. So instead of a continuous, // multi-event action do a bunch of "mini pans". GameCameraPan(game, GAME_PAN_START, multiTouchStart.x * float(screenWidth), multiTouchStart.y*float(screenHeight)); multiTouchStart.Set(mge->x, mge->y); GameCameraPan(game, GAME_PAN_MOVE, multiTouchStart.x * float(screenWidth), multiTouchStart.y*float(screenHeight)); GameCameraPan(game, GAME_PAN_END, multiTouchStart.x * float(screenWidth), multiTouchStart.y*float(screenHeight)); } if (nFingers == 2) { GameZoom(game, GAME_ZOOM_DISTANCE, -mge->dDist * 10.f); GameCameraRotate(game, -mge->dTheta * 100.0f); //GLOUTPUT(("MultiGestureEvent dTheta=%.4f dDist=%.4f x=%.4f y=%.4f nFing=%d\n", mge->dTheta, mge->dDist, mge->x, mge->y, mge->numFingers)); } } break; case SDL_DOLLARGESTURE: { GLOUTPUT(("DollarGestureEvent\n")); } break; case SDL_QUIT: { done = true; } break; default: break; } } U32 delta = SDL_GetTicks() - tickTimer; if (delta < TIME_BETWEEN_FRAMES) { SDL_Delay(1); continue; } tickTimer = SDL_GetTicks(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); const U8* keys = SDL_GetKeyboardState(0); U32 tickDelta = thisTick - lastTick; if (tickDelta > 100) tickDelta = 100; float keyMoveSpeed = KEY_MOVE_SPEED * float(tickDelta) / float(TIME_BETWEEN_FRAMES); float keyZoomSpeed = KEY_ZOOM_SPEED * float(tickDelta) / float(TIME_BETWEEN_FRAMES); float keyRotatepeed = KEY_ROTATE_SPEED * float(tickDelta) / float(TIME_BETWEEN_FRAMES); if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S]) { if (modKeys & KMOD_CTRL) GameZoom(game, GAME_ZOOM_DISTANCE, keyZoomSpeed); else GameCameraMove(game, 0, -keyMoveSpeed); } if (keys[SDL_SCANCODE_UP] || keys[SDL_SCANCODE_W]) { if (modKeys & KMOD_CTRL) GameZoom(game, GAME_ZOOM_DISTANCE, -keyZoomSpeed); else GameCameraMove(game, 0, keyMoveSpeed); } if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) { if (modKeys & KMOD_CTRL) GameCameraRotate(game, keyRotatepeed); else GameCameraMove(game, -keyMoveSpeed, 0); } if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) { if (modKeys & KMOD_CTRL) GameCameraRotate(game, -keyRotatepeed); else GameCameraMove(game, keyMoveSpeed, 0); } if (game) { lastTick = thisTick; thisTick = SDL_GetTicks(); PROFILE_BLOCK(GameDoTick); GameDoTick(game, thisTick); } { PROFILE_BLOCK(Swap); SDL_GL_SwapWindow(screen); } } GameSave(game); DeleteGame(game); for (int i = 0; i < nModDB; ++i) { delete databases[i]; } SDL_Quit(); #if SEND_CRASH_LOGS // Empty the file - quit went just fine. { FILE* fp = FOpen( "UFO_Running.txt", "w" ); if ( fp ) fclose( fp ); } #endif MemLeakCheck(); return 0; }