aboutsummaryrefslogtreecommitdiff
path: root/gameloop/src/loopManager.cpp
blob: c439a3ea6131d8a5251bc82502e1ea6e2ea26f87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "loopManager.h"
#include "timer.h"
LoopManager::LoopManager(){
}
void LoopManager::processInput(){
	SDL_Event event;
	SDL_PollEvent(&event);
	switch(event.type){
		case SDL_QUIT:
			gameRunning = false;
			break;
		case SDL_KEYDOWN:
			if(event.key.keysym.sym == SDLK_ESCAPE){
				gameRunning = false;
			}
		break;
	}
}
void LoopManager::fixedUpdate(){
	fprintf(stderr,"fixed update\n");
}
void LoopManager::loop(){
	fprintf(stderr,"loop. \n");
	LoopTimer& timer = LoopTimer::getInstance();
	while(gameRunning){
		timer.update();
		lag += timer.getDeltaTime();
		processInput();
		while (lag >= timer.getFixedDeltaTime())
		{
			fixedUpdate();
			lag -= timer.getFixedDeltaTime();
		}
		//update();
		//timer.enforceFrameRate();
		render();
	}
	window.destroyWindow();
}
void LoopManager::setup(){
	gameRunning = window.initWindow();
	LoopTimer::getInstance().start();
	LoopTimer::getInstance().setFPS(50);
	
	for(int i = 0; i < 2;i++){
		GameObject* square = new GameObject("square2",i*40,i*40,20,20,0,0);
		objectList.push_back(square);
	}
}
void LoopManager::render(){
	fprintf(stderr,"**********render********** \n");
	if(gameRunning){
		window.render(objectList);
	}
}

void LoopManager::update() {
	fprintf(stderr,"**********normal update********** \n");
    LoopTimer& timer = LoopTimer::getInstance();
	
    float delta= timer.getDeltaTime();

    for (int i = 0; i < objectList.size(); i++) {
        objectList[i]->setX(objectList[i]->getX() + 50 * delta); 
        objectList[i]->setY(objectList[i]->getY() + 50 * delta);
    }
}