Implement DeltaTime
This commit is contained in:
parent
69a957c6f6
commit
ef72acbf0b
18
src/main.cpp
18
src/main.cpp
@ -1,10 +1,12 @@
|
|||||||
#include "states/GameState.h"
|
#include "states/GameState.h"
|
||||||
#include "states/MainMenuState.h"
|
#include "states/MainMenuState.h"
|
||||||
#include "states/InGameState.h"
|
#include "states/InGameState.h"
|
||||||
|
#include "states/EndingState.h"
|
||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
TTF_Init();
|
TTF_Init();
|
||||||
@ -12,18 +14,28 @@ int main() {
|
|||||||
SDL_Window* window = SDL_CreateWindow("Obstacle Game",
|
SDL_Window* window = SDL_CreateWindow("Obstacle Game",
|
||||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||||
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
|
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
|
||||||
|
|
||||||
std::unordered_map<std::string, std::shared_ptr<GameState>> states;
|
std::unordered_map<std::string, std::shared_ptr<GameState>> states;
|
||||||
states["main_menu"] = std::make_shared<MainMenuState>();
|
states["main_menu"] = std::make_shared<MainMenuState>();
|
||||||
states["game"] = std::make_shared<InGameState>();
|
states["game"] = std::make_shared<InGameState>();
|
||||||
|
states["ending"] = std::make_shared<EndingState>();
|
||||||
|
|
||||||
std::shared_ptr<GameState> currentState = states["main_menu"];
|
std::shared_ptr<GameState> currentState = states["main_menu"];
|
||||||
|
|
||||||
|
Uint64 NOW = SDL_GetPerformanceCounter();
|
||||||
|
Uint64 LAST = 0;
|
||||||
|
double deltaTime = 0;
|
||||||
|
|
||||||
currentState->init();
|
currentState->init();
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
|
LAST = NOW;
|
||||||
|
NOW = SDL_GetPerformanceCounter();
|
||||||
|
|
||||||
|
deltaTime = (double)((NOW - LAST)*1000 / (double)SDL_GetPerformanceFrequency() );
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
if (event.type == SDL_QUIT) {
|
if (event.type == SDL_QUIT) {
|
||||||
@ -33,7 +45,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
currentState->update();
|
currentState->update();
|
||||||
currentState->render(renderer);
|
currentState->render(renderer, deltaTime);
|
||||||
|
|
||||||
if (!currentState->getNextState().empty()) {
|
if (!currentState->getNextState().empty()) {
|
||||||
std::string nextStateName = currentState->getNextState();
|
std::string nextStateName = currentState->getNextState();
|
||||||
@ -45,8 +57,6 @@ int main() {
|
|||||||
}
|
}
|
||||||
currentState->clearNextState();
|
currentState->clearNextState();
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Delay(16);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user