File: input.cpp

package info (click to toggle)
epiphany 0.7.0+0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 7,532 kB
  • sloc: cpp: 6,404; ansic: 141; makefile: 78; sh: 7
file content (144 lines) | stat: -rw-r--r-- 3,867 bytes parent folder | download | duplicates (6)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************************
                          input.cpp  -  description
                             -------------------
    begin                : Jun 13 2007
    copyright            : (C) 2007 by Giuseppe D'Aqui'
    email                : kumber@tiscalinet.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License, Version 2,      *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/
 
 
#include "dephine.h"
#include <SDL/SDL.h>
#include "input.h"



Sint32 Input::event_filter(const SDL_Event *event)
{

	switch( event->type )
    {
	case SDL_KEYDOWN:
       // std::cout<<"Keydown event\n";
        return 1;
        break;
  
	case SDL_KEYUP:
        //std::cout<<"Keyup event\n";
        return 1;
        break;


	case SDL_QUIT:
		m_quit=true;
        return 0;

	default:
        return 0;
	}
	return 1;
}

Sint32 sdl_event_filter(const SDL_Event *event)
{
    return Input::instance()->event_filter(event);
}


void Input::reset_states()
{
    m_quit= false;
    m_die = false;
    m_left = false;
    m_right = false;
    m_up = false;
    m_down = false;
    m_alt = false;
    m_pause = false;
    m_fire = false;
    m_enter = false;
}

void Input::init()
{
    m_fullscreen = false;
    m_pause = false;
    reset_states();
    SDL_SetEventFilter(sdl_event_filter);
    SDL_EnableKeyRepeat(0,0);
}


void Input::update()
{
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
	switch( event.type )
    {
	case SDL_KEYDOWN:
		if (event.key.keysym.sym==SDLK_LEFT) m_left=true;
		if (event.key.keysym.sym==SDLK_RIGHT) m_right=true;
		if (event.key.keysym.sym==SDLK_UP) m_up=true;
		if (event.key.keysym.sym==SDLK_DOWN) m_down=true;
		if (event.key.keysym.sym==SDLK_RCTRL) m_fire=true;
		if (event.key.keysym.sym==SDLK_LCTRL) m_fire=true;
		if (event.key.keysym.sym==SDLK_SPACE) m_fire=true;
		if (event.key.keysym.sym==SDLK_ESCAPE) m_die=true;
		if (event.key.keysym.sym==SDLK_LALT) m_alt=true;
		if (event.key.keysym.sym==SDLK_RALT) m_alt=true;
        if (event.key.keysym.sym==SDLK_RETURN) {m_enter=true;}
		if (event.key.keysym.sym==SDLK_p) m_pause=!m_pause;
		if (event.key.keysym.sym==SDLK_x && m_alt) m_quit=true;
		if ((event.key.keysym.sym==SDLK_q) && m_alt) m_quit=true;
		if	(event.key.keysym.sym==SDLK_ESCAPE) m_quit=true;
		if ((event.key.keysym.sym==SDLK_F4) && m_alt) m_quit=true;
        if ((event.key.keysym.sym==SDLK_RETURN) && m_alt) m_fullscreen=!m_fullscreen;        
		break;

	case SDL_KEYUP:
		if (event.key.keysym.sym==SDLK_LEFT) m_left=false;
		if (event.key.keysym.sym==SDLK_RIGHT) m_right=false;
		if (event.key.keysym.sym==SDLK_UP) m_up=false;
		if (event.key.keysym.sym==SDLK_DOWN) m_down=false;
		if (event.key.keysym.sym==SDLK_RCTRL) m_fire=false;
		if (event.key.keysym.sym==SDLK_LCTRL) m_fire=false;
		if (event.key.keysym.sym==SDLK_SPACE) m_fire=false;
		if (event.key.keysym.sym==SDLK_RETURN) m_enter=false;
		if (event.key.keysym.sym==SDLK_ESCAPE) m_die=false;
		if	(event.key.keysym.sym==SDLK_ESCAPE) m_quit=false;
		if (event.key.keysym.sym==SDLK_LALT) m_alt=false;
		if (event.key.keysym.sym==SDLK_RALT) m_alt=false;
		break;


	case SDL_QUIT:
		m_quit=true;

	}
    }
}


// begin Singleton stuff

Input* Input::_instance = 0;

Input* Input::instance()
{
	if (_instance == 0)
	{
		_instance = new Input;
	}
	return _instance;
}

//end Singleton stuff