-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventHandler.cpp
91 lines (84 loc) · 2.27 KB
/
EventHandler.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "EventHandler.h"
#include "Gameshell.h"
#include <iostream>
EventHandler::EventHandler()
{
direction.x = direction.y = 0;
direction.w = 0;
quit = false;
use_item = false;
right_inv_arrow = false;
left_inv_arrow = false;
}
EventHandler::~EventHandler()
{
}
void EventHandler::reset()
{
direction.x = direction.y = direction.w = 0;
use_item = false;
left_inv_arrow = right_inv_arrow = false;
}
void EventHandler::handleEvents()
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
else
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
direction.x = 0;
direction.y = -1;
direction.w = 0;
break;
case SDLK_DOWN:
direction.x = 0;
direction.y = 1;
direction.w = 2;
break;
case SDLK_RIGHT:
direction.x = 1;
direction.y = 0;
direction.w = 1;
break;
case SDLK_LEFT:
direction.x = -1;
direction.y = 0;
direction.w = 3;
break;
case SDLK_q:
quit = true;
break;
default:
direction.x = 0;
direction.y = 0;
break;
}
}
else
if(event.type == SDL_MOUSEBUTTONDOWN)
{
int x = event.motion.x;
int y = event.motion.y;
std::cout << "X = " << x << " " << "Y = " << y << "\n";
if(x >= 14 * TILE_WIDTH && x < 15 * TILE_WIDTH && y >= 0 && y < TILE_HEIGHT)
use_item = true;
else
if(x >= 13 * TILE_WIDTH && x < 14 * TILE_WIDTH && y >= 0 && y < TILE_HEIGHT)
left_inv_arrow = true;
else
if(x >= 15 * TILE_WIDTH && x < 16 * TILE_WIDTH && y >= 0 && y < TILE_HEIGHT)
right_inv_arrow = true;
}
}
}
bool EventHandler::exitGame()
{
return quit;
}