File: input_devices.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (113 lines) | stat: -rw-r--r-- 3,054 bytes parent folder | download
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
/*
	The example shows how the physical buttons can be abstracted into logical
	buttons used by the game.
	
	For instance, the fire_button is the game's logical fire button. But
	physically it is serveral buttons on the keyboards and the joystick. 
*/

#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>

class InputDevApp : public CL_ClanApplication
{
public:
	virtual char *get_title() { return "Input device application"; }

	virtual int main(int, char **)
	{
		// Create a console window for text-output if not available
		CL_ConsoleWindow console("Console");
		console.redirect_stdio();

		try
		{
			CL_SetupCore::init();
			CL_SetupDisplay::init();

			// Set mode: 320x240 16 bpp
			CL_Display::set_videomode(320, 240, 16, false);
			
			// Make sure the display is black at startup:
			CL_Display::clear_display();
			
			// Abstract game input from physical input:
			CL_InputButton_Group *fire_button = new CL_InputButton_Group;
			CL_InputAxis_Group *hori_axis = new CL_InputAxis_Group;
			CL_InputAxis_Group *vert_axis = new CL_InputAxis_Group;

			// Add keyboard input:
			fire_button->add(
				CL_Input::keyboards[0]->get_button(CL_KEY_ESCAPE));

			fire_button->add(
				CL_Input::keyboards[0]->get_button(CL_KEY_SPACE));

			hori_axis->add(
				new CL_InputButtonToAxis_Analog(
					CL_Input::keyboards[0]->get_button(CL_KEY_LEFT),
					CL_Input::keyboards[0]->get_button(CL_KEY_RIGHT)
					));

			vert_axis->add(
				new CL_InputButtonToAxis_Analog(
					CL_Input::keyboards[0]->get_button(CL_KEY_UP),
					CL_Input::keyboards[0]->get_button(CL_KEY_DOWN)
					));

			// Add joystick input (if available):
			if (CL_Input::joysticks.size())
			{
				fire_button->add(CL_Input::joysticks[0]->get_button(0));
				fire_button->add(CL_Input::joysticks[0]->get_button(1));
				
				hori_axis->add(CL_Input::joysticks[0]->get_axis(0));
				vert_axis->add(CL_Input::joysticks[0]->get_axis(1));
			}

			// Loop until one of the fire buttons are down:
			while (fire_button->is_pressed() == false)
			{
				// Clear display with alpha background:
				CL_Display::clear_display(0.0f, 0.0f, 0.5f, 0.2f);

				// Calculate box position:			
				int x = CL_Display::get_width()/2 +
					(int) (hori_axis->get_pos()*CL_Display::get_width()/2);

				int y = CL_Display::get_height()/2 +
					(int) (vert_axis->get_pos()*CL_Display::get_height()/2);

				// Draw a box where the axes are:
				CL_Display::fill_rect(
					x-5, y-5, x+5, y+5,
					1.0f, 0.0f, 0.0f, 1.0f);

				// Flip front and backbuffer. This makes the changes visible:
				CL_Display::flip_display();
			
				// Update keyboard input and handle system events:
				CL_System::keep_alive();
			}

			delete fire_button;
			delete hori_axis;
			delete vert_axis;

			CL_SetupDisplay::deinit();
			CL_SetupCore::deinit();
		}
		catch (CL_Error err)
		{
			std::cout << "Error: " << err.message.c_str() << std::endl;

			// Display console close message and wait for a key
			console.display_close_message();

			return 1;
		}

		return 0;
	}
} app;