Esempio n. 1
0
inline void
SDLHandleUserInput(SDL_Event *Event,
                   game_code *GameCode,
                   game_memory *GameMemory,
                   keyboard_input *KeyboardInput,
                   gamepad_input *GamePadInput) {

    int KeyCode = Event->key.keysym.sym;
    uint8 GamePadButton = Event->cbutton.button;

    const uint8 *KeyState = SDL_GetKeyboardState(NULL);
    
    switch(Event->type) {
        case SDL_KEYDOWN:

            if(KeyCode == SDLK_ESCAPE) {
                HandleButtonPress(&KeyboardInput->Escape);
                GlobalRunning = false;
            }

            if(KeyCode == SDLK_a) {
                HandleButtonPress(&KeyboardInput->A);
                                                    
                UnloadGameCode(GameCode);
                *GameCode = LoadGameCode();
                GameMemory->IsInitialized = false;
            }

            if(KeyState[SDL_SCANCODE_LALT] && KeyState[SDL_SCANCODE_RETURN] ) {
                if(!Fullscreen){
                    SDL_SetWindowFullscreen(SDL_GetWindowFromID(Event->window.windowID),
                                            SDL_WINDOW_FULLSCREEN);
                    SDL_SetRelativeMouseMode((SDL_bool)SDL_ENABLE);
                    Fullscreen = true;
                } else {
                    SDL_SetWindowFullscreen(SDL_GetWindowFromID(Event->window.windowID), 0);
                    SDL_SetRelativeMouseMode((SDL_bool)SDL_DISABLE);
                    Fullscreen = false;
                }
            }

            if(KeyCode == SDLK_b) {
                HandleButtonPress(&KeyboardInput->B);
            }

            if(KeyCode == SDLK_f) {
                HandleButtonPress(&KeyboardInput->F);
            }

            if(KeyCode == SDLK_UP) {
                HandleButtonPress(&KeyboardInput->Up);
            }

            if(KeyCode == SDLK_DOWN) {
                HandleButtonPress(&KeyboardInput->Down);
            }

            if(KeyCode == SDLK_LEFT) {
                HandleButtonPress(&KeyboardInput->Left);
            }

            if(KeyCode == SDLK_RIGHT) {
                HandleButtonPress(&KeyboardInput->Right);
            }
            break;

        case SDL_KEYUP:

            if(KeyCode == SDLK_ESCAPE) {
                HandleButtonRelease(&KeyboardInput->Escape);
            }

            if(KeyCode == SDLK_b) {
                HandleButtonRelease(&KeyboardInput->B);
            }

            if(KeyCode == SDLK_f) {
                HandleButtonRelease(&KeyboardInput->F);
            }

            if(KeyCode == SDLK_a) {
                HandleButtonRelease(&KeyboardInput->A);
            }

            if(KeyCode == SDLK_UP) {
                HandleButtonRelease(&KeyboardInput->Up);
            }

            if(KeyCode == SDLK_DOWN) {
                HandleButtonRelease(&KeyboardInput->Down);
            }

            if(KeyCode == SDLK_LEFT) {
                HandleButtonRelease(&KeyboardInput->Left);
            }

            if(KeyCode == SDLK_RIGHT) {
                HandleButtonRelease(&KeyboardInput->Right);
            }
            break;

        case SDL_CONTROLLERBUTTONDOWN:

            if(GamePadButton == SDL_CONTROLLER_BUTTON_A) {
                HandleButtonPress(&GamePadInput->AButton);
            }

            if(GamePadButton == SDL_CONTROLLER_BUTTON_B) {
                HandleButtonPress(&GamePadInput->BButton);
            }

            if(GamePadButton == SDL_CONTROLLER_BUTTON_X) {
                HandleButtonPress(&GamePadInput->XButton);
            }

            if(GamePadButton == SDL_CONTROLLER_BUTTON_Y) {
                HandleButtonPress(&GamePadInput->YButton);
            }

            break;

        case SDL_CONTROLLERBUTTONUP:

            if(GamePadButton == SDL_CONTROLLER_BUTTON_A) {
                HandleButtonRelease(&GamePadInput->AButton);
            }

            if(GamePadButton == SDL_CONTROLLER_BUTTON_B) {
                HandleButtonRelease(&GamePadInput->BButton);
                GlobalRunning = false;
            }

            if(GamePadButton == SDL_CONTROLLER_BUTTON_X) {
                HandleButtonRelease(&GamePadInput->XButton);
            }

            if(GamePadButton == SDL_CONTROLLER_BUTTON_Y) {
                HandleButtonRelease(&GamePadInput->YButton);
            }

            break;
    }

    // NOTE(Redab): Get values from gamepad joysticks and normalize.
    
    real32 LSX = (real32)SDL_GameControllerGetAxis(GamePadHandle, SDL_CONTROLLER_AXIS_LEFTX);

    if(LSX < 0) {
        GamePadInput->LeftStick.X = LSX / 32768.0f;
    } else {
        GamePadInput->LeftStick.X = LSX / 32767.0f;
    }

    real32 LSY = (real32)SDL_GameControllerGetAxis(GamePadHandle, SDL_CONTROLLER_AXIS_LEFTY);

    if(LSY < 0) {
        GamePadInput->LeftStick.Y = LSY / 32768.0f;
    } else {
        GamePadInput->LeftStick.Y = LSY / 32767.0f;
    }

    real32 RSX = (real32)SDL_GameControllerGetAxis(GamePadHandle, SDL_CONTROLLER_AXIS_RIGHTX);

    if(RSX < 0) {
        GamePadInput->RightStick.X = RSX / 32768.0f;
    } else {
        GamePadInput->RightStick.X = RSX / 32767.0f;
    }

    real32 RSY = (real32)SDL_GameControllerGetAxis(GamePadHandle, SDL_CONTROLLER_AXIS_RIGHTY);

    if(RSY < 0) {
        GamePadInput->RightStick.Y = RSY / 32768.0f;
    } else {
        GamePadInput->RightStick.Y = RSY / 32767.0f;
    }

}
Esempio n. 2
0
//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();

}