source: trunk/src/input.cpp @ 295

Last change on this file since 295 was 295, checked in by sam, 10 years ago

Fix a few warnings here and there, and try to use the vector classes.

  • Property svn:keywords set to Id
File size: 1.5 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        f = f * invsqrt2;
61
62    return f;
63}
64
65void Input::SetMousePos(int2 coord)
66{
67    data->mouse = coord;
68}
69
70int2 Input::GetMousePos()
71{
72    return data->mouse;
73}
74
75void Input::SetMouseButton(int index)
76{
77    data->buttons[index] = 1;
78}
79
80void Input::UnsetMouseButton(int index)
81{
82    data->buttons[index] = 0;
83}
84
85int3 Input::GetMouseButtons()
86{
87    return data->buttons;
88}
89
Note: See TracBrowser for help on using the repository browser.