void Game::ProcessInput()// for all general inputs. {
const Uint8* state = SDL_GetKeyboardState(NULL); SDL_Event e; while (SDL_PollEvent(&e) != 0) { switch (e.type) { case SDL_QUIT: mIsRunning = false; break; case SDL_KEYUP: break; } } changeLocation.x = 0; if (state[SDL_SCANCODE_ESCAPE]) { mIsRunning = false; } if (state[SDL_SCANCODE_D]) { changeLocation.x = 1; } if (state[SDL_SCANCODE_A]) { changeLocation.x = -1; } if (state[SDL_SCANCODE_W]) { changeLocation.y = -2; } if (state[SDL_SCANCODE_S]) { changeLocation.y = 1; } if (state[SDL_SCANCODE_SPACE] && jumpReset > 0) { changeLocation.y -= 20; jumpReset = 0; }
}
void Game::UpdateGame()//For all game physics and logic. { character.x = location.x; character.y = location.y; character.h = 123; character.w = 58;
wall[0].x = 0; wall[0].y = 990; wall[0].h = 20; wall[0].w = 1000; wall[1].x = 0; wall[1].y = 700; wall[1].h = 20; wall[1].w = 200; wall[2].x = 500; wall[2].y = 500; wall[2].h = 20; wall[2].w = 150; while (!SDL_TICKS_PASSED(SDL_GetTicks(), mTicksCount + 16)); float deltaTime = (SDL_GetTicks() - mTicksCount) / 1000.0f; if (deltaTime > 0.05f) { deltaTime = 0.05f; } { character.x = location.x; character.y = character.y + changeLocation.y; if (!checkCollision()) { location.y += changeLocation.y * 50.0f * deltaTime; } else //std::cout << "Hitting something below me" << std::endl; jumpReset = 2; } if (changeLocation.x != 0) { character.x = location.x + changeLocation.x; character.y = location.y; if (!checkCollision()) { location.x += changeLocation.x * 50.0f * deltaTime; } } else changeLocation.y = 1;
}
void Game::GenerateOutput() { SDL_RenderClear(gRenderer);
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 255); if (changeLocation.y < 0) { getFrameUpAndDown(1, location.x, location.y); } else if (changeLocation.x > 0 ) { frameToDraw = ((SDL_GetTicks() - startTime) * 6 / 1000) % 4; getFrameRight(frameToDraw, location.x, location.y); } else if (changeLocation.x < 0 ) { frameToDraw = ((SDL_GetTicks() - startTime) * 6 / 1000) % 4; getFrameLeft(frameToDraw, location.x, location.y); } else getFrameUpAndDown(0, location.x, location.y); SDL_RenderFillRect(gRenderer, &wall[0]); SDL_RenderFillRect(gRenderer, &wall[1]); SDL_RenderFillRect(gRenderer, &wall[2]); SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderPresent(gRenderer);
}