-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.h
74 lines (50 loc) · 1.83 KB
/
Input.h
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
//
// Created by flo on 10/28/2021.
//
#ifndef NOMPROJET_INPUT_H
#define NOMPROJET_INPUT_H
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstring>
using namespace sf;
using namespace std;
class Input{
private:
bool curPressed[Keyboard::KeyCount];
bool justReleased[Keyboard::KeyCount];
Keyboard::Key lastReleased;
const Keyboard::Key DEFAULT_NULL = Keyboard::Key::BackSlash;
int mouseWheelDelta = 0;
public:
Input() { memset(curPressed,false, sizeof curPressed); }
void update(Event &event){
// Keys
if((event.type == Event::KeyPressed || event.type == Event::KeyReleased)){
curPressed[event.key.code] = (event.type == Event::KeyPressed); // se è premuto
curPressed[event.key.code] &= (event.type != Event::KeyReleased); // se è rilasciato
}
lastReleased = (event.type == Event::KeyReleased) ? event.key.code : DEFAULT_NULL;
//Mouse
if(event.type == sf::Event::MouseWheelScrolled) mouseWheelDelta = (int)event.mouseWheelScroll.delta;
else mouseWheelDelta=0;
}
bool isKey(Keyboard::Key k){
return curPressed[k];
}
bool isKeyJustReleased(Keyboard::Key k){
return lastReleased ? lastReleased == k : false;
}
int mouseWheelScrool(){
return mouseWheelDelta;
}
bool isLeft(){ return isKey(Keyboard::Left) || isKey(Keyboard::A); }
bool isRight(){return isKey(Keyboard::Right) || isKey(Keyboard::D);}
bool isUp(){return isKey(Keyboard::Up) || isKey(Keyboard::W);}
bool isDown(){return isKey(Keyboard::Down) || isKey(Keyboard::S);}
bool isSpace(){return isKey(Keyboard::Space);}
bool isEscape(){return isKey(Keyboard::Escape);}
bool isClose(Event e){return (e.type == Event::Closed);}
};
#endif //NOMPROJET_INPUT_H