-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
71 lines (65 loc) · 1.7 KB
/
controller.cpp
File metadata and controls
71 lines (65 loc) · 1.7 KB
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
69
70
71
#include "controller.h"
#include "model.h"
#include <map>
using namespace std;
Controller::Controller() {
model = new Model(640, 480);
view = new View("Game", 640, 480);
}
Controller::~Controller() {
delete model;
delete view;
}
/**
References:
https://wiki.libsdl.org/SDL_PollEvent
https://wiki.libsdl.org/SDL_Event
*/
void Controller::loop() {
SDL_Event e;
e.type=NULL;
unsigned int lastTime = 0, currentTime;
std::map<SDL_Keycode, Direction> direction;
direction[SDLK_UP] = UP;
direction[SDLK_DOWN] = DOWN;
direction[SDLK_LEFT] = LEFT;
direction[SDLK_RIGHT] = RIGHT;
view->show(model);
SDL_Delay(4000);
while(!model->gameOver()) {
currentTime = SDL_GetTicks();
if (currentTime > lastTime + 100) {
for (int i = 0; i<8; i++){
model->move_pac();
model->move_ghost();
SDL_Delay(8);
view->show(model);
}
lastTime = currentTime;
}
// Do stuff here to animate as necessary
//view->show(model, pacdest, e);
if (SDL_PollEvent(&e) != 0) {
switch (e.type) {
case SDL_QUIT:
return;
case SDL_KEYDOWN:
switch(e.key.keysym.sym) {
case SDLK_DOWN:
case SDLK_UP:
case SDLK_LEFT:
case SDLK_RIGHT:
model->go(direction[e.key.keysym.sym]);
break;
default:
break;
}
case SDL_MOUSEBUTTONDOWN:
break;
}
}
}
// TODO: show something nice?
view->show(model);
SDL_Delay(3000);
}