void ProcessInput(Game_Input *input) { input->UP.KeyDown = IsKeyDown(&Keys, input->UP.Button); input->UP.KeyUp = IsKeyUp(&Keys, input->UP.Button); input->DOWN.KeyDown = IsKeyDown(&Keys, input->DOWN.Button); input->DOWN.KeyUp = IsKeyUp(&Keys, input->DOWN.Button); input->RIGHT.KeyDown = IsKeyDown(&Keys, input->RIGHT.Button); input->RIGHT.KeyUp = IsKeyUp(&Keys, input->RIGHT.Button); input->LEFT.KeyDown = IsKeyDown(&Keys, input->LEFT.Button); input->LEFT.KeyUp = IsKeyUp(&Keys, input->LEFT.Button); }
void JoystickSystick(void) { s8 pos = 0; s8 ad; if(IsKeyLeft()){ pos-=127;} if(IsKeyRight()){ pos+=127; } JoyMouseBuffer[IDX_X] = pos; pos = 0; if(IsKeyUp()){ pos-=127;} if(IsKeyDown()){ pos+=127;} JoyMouseBuffer[IDX_Y] = pos; ad = (2048-ADCResult.ADY)/16; JoyMouseBuffer[IDX_RX] = ad; ad = (2048-ADCResult.ADX)/16; JoyMouseBuffer[IDX_RY] = ad; ad = (ADCResult.ADZ - 2048)/16; JoyMouseBuffer[IDX_RZ] = ad; pos = 0; if(IsKey1()){ pos |= 1;} if(IsKey2()){ pos |= 2;} if(IsKey3()){ pos |= 4;} if(IsKey4()){ pos |= 8;} if(IsKeyL1()){pos |= 16;} if(IsKeyR1()){pos |= 32;} if(IsKeyL2()){pos |= 64;} if(IsKeyR2()){pos |= 128;} JoyMouseBuffer[IDX_BTN] = pos; pos = 0; if(IsKeySelect()){ pos|=1; } if(IsKeyPause()){ pos|=2; } JoyMouseBuffer[IDX_BTN+1] = pos; PostMessage(MSG_DUMMY); }
int key_released(int key) { #ifdef _WIN32 if(IsKeyUp(key)) { return 1; } else { return 0; } #else XNextEvent(display, &event); if(event.type == KeyRelease && event.xkey.keycode == key) { return 1; } else { return 0; } #endif return 0; }
void MouseSystick(void) { s8 pos = 0; s8 x = 0; s8 y = 0; s8 delta = mux; if(IsKey4()){ pos |= 1;} if(IsKey2()){ pos |= 2;} if(IsKey3()){ pos |= 4;} JoyMouseBuffer[IDX_MOUSE_BTN] = pos; if(IsKeyL1()){ delta = MUX_MAX; } if(IsKeyL2()){ delta = 1; } if(IsKeyLeft()){ x-=delta; } if(IsKeyRight()){ x+=delta; } if(IsKeyUp()){ y-=delta;} if(IsKeyDown()){ y+=delta;} adX = ADCResult.ADX; adY = ADCResult.ADY; if(x ==0 && y==0 /*&& bMode*/){ if(IsKey1()){ // Air mouse mode, if(adX > ADC_RESOLUTION/2 + ADC_THRESHOLD){ y-= delta; }else if(adX<ADC_RESOLUTION/2 - ADC_THRESHOLD){ y+= delta; } if(adY > ADC_RESOLUTION/2 + ADC_THRESHOLD){ x-= delta; }else if(adY<ADC_RESOLUTION/2 - ADC_THRESHOLD){ x+= delta; } } } if(x || y){ mux++; if(mux >MUX_MAX)mux = MUX_MAX; }else{ mux = 1; } JoyMouseBuffer[IDX_MOUSE_X] = x; JoyMouseBuffer[IDX_MOUSE_Y] = y; pos = 0; if(IsKeyR1()){ pos+=1; } if(IsKeyR2()){ pos-=1; } JoyMouseBuffer[IDX_MOUSE_WHEEL] = pos; if(IsKeyPause()){ KeyPause++; }else{ if(KeyPause > 1){ PostMessage(KEY_PAUSE); } KeyPause = 0; } PostMessage(MSG_DUMMY); }
//Our main loop which should continue running as long as we don't quite the game static void MainLoop() { char DLLFilePath[MAX_PATH]; char *onePastLastSlash; DWORD pathSize = GetModuleFileNameA(NULL, DLLFilePath, sizeof(DLLFilePath)); onePastLastSlash = DLLFilePath; for (char *scan = DLLFilePath; *scan; scan++) { if (*scan == '\\') { onePastLastSlash = scan + 1; } } char DLLFullPath[MAX_PATH]; BuildFileFullPath(&state, "playground game.dll", DLLFullPath, sizeof(DLLFullPath)); char tempDLLFullPath[MAX_PATH]; BuildFileFullPath(&state, "playground game_temp.dll", tempDLLFullPath, sizeof(tempDLLFullPath)); char PDBFullPath[MAX_PATH]; BuildFileFullPath(&state, "playground game.pdb", PDBFullPath, sizeof(PDBFullPath)); char tempPDBFullPath[MAX_PATH]; BuildFileFullPath(&state, "playground game_temp.pdb", tempPDBFullPath, sizeof(tempPDBFullPath)); Input = {}; Input.UP.Button = VK_UP; Input.DOWN.Button = VK_DOWN; Input.RIGHT.Button = VK_RIGHT; Input.LEFT.Button = VK_LEFT; LARGE_INTEGER performanceFrequency; QueryPerformanceFrequency(&performanceFrequency); TicksPerSecond = performanceFrequency.QuadPart; int monitorRefreshHZ = 60; HDC deviceContext = GetDC(Window.Window); int refreshHz = GetDeviceCaps(deviceContext, VREFRESH); ReleaseDC(Window.Window, deviceContext); if (refreshHz > 1) { monitorRefreshHZ = refreshHz; } float gameUpdateHZ = (float)(monitorRefreshHZ); float targetSecondsPerFrame = 1.0f / gameUpdateHZ; UINT desiredSchedulerTime = 1; bool sleepIsSmaller = true;//timeBeginPeriod(desiredSchedulerTime) == TIMERR_NOERROR; LARGE_INTEGER lastTick = GetTicks(); float updateTime = 0; int updates = 0; double frames = 0; double frameTime = 0; while (IsRunning) { /* start_loop = clock(); */ FILETIME newWriteTimeDLL = GetLastWriteTime(DLLFullPath); FILETIME newWriteTimePDB = GetLastWriteTime(PDBFullPath); if (CompareFileTime(&newWriteTimeDLL, &Game.LastWriteTimeDLL) != 0 && CompareFileTime(&newWriteTimeDLL, &Game.LastWriteTimePDB) != 0) { UnloadGameCode(&Game); CopyFile(PDBFullPath, tempPDBFullPath, FALSE); Game = LoadGameCode(DLLFullPath, tempDLLFullPath); Game.Game_Init(Dimensions); } LARGE_INTEGER gameTimerStart = GetTicks(); ProcessPendingMessages(&Keys); ProcessInput(&Input); //Update everything //Update the game Game.Game_Update(&Input); /*NOTE(kai): TEST ONLY*/ //Testing if A button is pressed if (IsKeyDown(&Keys, 'A')) { OutputDebugString("Key: a is pressed\n"); } //Testing if A button is released if (IsKeyUp(&Keys, 'A')) { OutputDebugString("Key: a is released\n"); } //Render everything //Clear the window ClearWindow(); //Render the game Game.Game_Render(); LARGE_INTEGER gameTimerEnd = GetTicks(); frameTime += (double)(1000.0f * GetSecondsElapsed(gameTimerStart, gameTimerEnd)); frames++; //frames += 1000.0f / (double)(1000.0f * GetSecondsElapsed(gameTimerStart, gameTimerEnd)); //PrintTimeElapsed(lastTick, gameTimerEnd); float secondsElapsedForFrame = GetSecondsElapsed(lastTick, GetTicks()); if (secondsElapsedForFrame < targetSecondsPerFrame) { if (sleepIsSmaller) { DWORD sleepTime = (DWORD)(1000.0f * (targetSecondsPerFrame - secondsElapsedForFrame)); if (sleepTime > 0) { Sleep(sleepTime); } } while (secondsElapsedForFrame < targetSecondsPerFrame) { secondsElapsedForFrame = GetSecondsElapsed(lastTick, GetTicks()); } updates++; } updateTime += GetSecondsElapsed(lastTick, GetTicks()); if (updateTime >= 1.0f) { double avgFPS = 1000.0f / ((frameTime) / frames); std::cout << "UPS: " << updates << ", average FPS: " << avgFPS << ", average work/frame: " << (frameTime) / frames << "\n"; frames = 0; frameTime = 0; updates = 0; updateTime = 0; } LARGE_INTEGER endTick = GetTicks(); //PrintTimeElapsed(lastTick, endTick); lastTick = endTick; //Render the window RenderWindow(Window.Window); /* //calc fps calcfps(); static int framecount = 0; framecount++; if (framecount == 10) { framecount = 0; std::cout << "frame per second is : " << (fps) << std::endl; } //QueryPerformanceCounter(&t_current_loop); end_loop = clock(); //float frameticks = (t_current_loop.QuadPart - t_previous_loop.QuadPart) / ((frequency_loop.QuadPart) / 1000.0); float frameticks = ((float)(end_loop - start_loop) / CLOCKS_PER_SEC) * 1000.0f; //print the current fps // std::cout << 1000/frameticks << std::endl; if (1000.0f / max_fps > frameticks){ Sleep(1000.0f / max_fps - frameticks); } */ } //Release resources (if there is any) and destory the window Release(); }
bool Keyboard::WasKeyReleasedThisFrame(byte key) const { return (IsKeyUp(key) && WasKeyDown(key)); }
int AddIn_main(int isAppli, unsigned short OptionNum) { int xMap, yMap, newXMap, newYMap; unsigned char *light_buffer, *dark_buffer, *light_swap, *dark_swap, *tmp_swap; Map mapTest; //On initialise les buffer par calloc (plus propre que par allocation automatique!) : light_buffer = calloc(1024, sizeof(unsigned char)); dark_buffer = calloc(1024, sizeof(unsigned char)); //Les deux buffers ci-dessous sont les "sawp" qui permettent de faire du double buffering // afin d'obtenir une meilleure fluiditée. light_swap = calloc(1024, sizeof(unsigned char)); dark_swap = calloc(1024, sizeof(unsigned char)); tmp_swap = 0; Bdisp_AllClr_DDVRAM(); Change_Contrast (166); //On initialise le grayscale de revolution-fx : GrayLinkBuffers(light_buffer, dark_buffer); GrayInit(6987, 3269); //Allocation de la map : mapTest = aMap(m_votre_map); //Boucle principale d'execution : while(IsKeyUp (KEY_CTRL_EXIT)){ if (IsKeyDown (KEY_CTRL_LEFT)) xMap-=2; if (IsKeyDown (KEY_CTRL_RIGHT)) xMap+=2; if (IsKeyDown (KEY_CTRL_UP)) yMap-=2; if (IsKeyDown (KEY_CTRL_DOWN)) yMap+=2; //On vérifie si on ne sort pas de la map : if (xMap<0) xMap=0; else if (xMap > (mapTest.width-128/mapTest.tileset.tileWidth)*mapTest.tileset.tileWidth) xMap = (mapTest.width-128/mapTest.tileset.tileWidth)*mapTest.tileset.tileWidth; if (yMap<0) yMap=0; else if (yMap > (mapTest.height-64/mapTest.tileset.tileHeight)*mapTest.tileset.tileHeight) yMap = (mapTest.height-64/mapTest.tileset.tileHeight)*mapTest.tileset.tileHeight; //On dessine la map sur les buffer de swap : drawMap (mapTest, xMap/mapTest.tileset.tileWidth, yMap/mapTest.tileset.tileHeight, 128/mapTest.tileset.tileWidth +1, 64/mapTest.tileset.tileWidth +1, -(xMap % mapTest.tileset.tileWidth), -(yMap % mapTest.tileset.tileHeight), light_swap, dark_swap); //On inverse les buffer de swap avec les buffer principaux : GrayLinkBuffers(light_swap, dark_swap); tmp_swap = light_swap; light_swap = light_buffer; light_buffer = tmp_swap; tmp_swap = dark_swap; dark_swap = dark_buffer; dark_buffer = tmp_swap; //On efface les nouveaux buffer de swap : memset(light_swap, 0, 1024); memset(dark_swap, 0, 1024); } CPUSpeedNormal(); Reset_Calc(); return 1; }