source: trunk/src/input.cpp @ 236

Last change on this file since 236 was 236, checked in by sam, 11 years ago

Mouse button support in Lol Engine.

  • Property svn:keywords set to Id
File size: 1.6 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <SDL.h>
16
17#include <cstdio>
18#include <cstdlib>
19#include <cmath>
20
21#include "core.h"
22
23/*
24 * Input implementation class
25 */
26
27static class InputData
28{
29    friend class Input;
30
31public:
32    InputData()
33      : mouse(-1, -1),
34        buttons(0, 0, 0)
35    { }
36
37    Int2 mouse;
38    Int3 buttons;
39}
40inputdata;
41
42static InputData * const data = &inputdata;
43
44/*
45 * Public Input class
46 */
47
48Float2 Input::GetAxis(int axis)
49{
50    float invsqrt2 = sqrtf(0.5f);
51    Float2 f;
52
53    /* Simulate a joystick using the keyboard. This SDL call is free. */
54    Uint8 *keystate = SDL_GetKeyState(NULL);
55    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
56    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
57    f.x += left;
58    f.y += up;
59    if (left && up)
60    {
61        f.x *= invsqrt2;
62        f.y *= invsqrt2;
63    }
64
65    return f;
66}
67
68void Input::SetMousePos(Int2 coord)
69{
70    data->mouse = coord;
71}
72
73Int2 Input::GetMousePos()
74{
75    return data->mouse;
76}
77
78void Input::SetMouseButton(int index)
79{
80    data->buttons[index] = 1;
81}
82
83void Input::UnsetMouseButton(int index)
84{
85    data->buttons[index] = 0;
86}
87
88Int3 Input::GetMouseButtons()
89{
90    return data->buttons;
91}
92
Note: See TracBrowser for help on using the repository browser.