source: trunk/src/input.cpp @ 329

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

Minor updates to the mouse tracking architecture.

  • Property svn:keywords set to Id
File size: 2.3 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        nentities(0)
36    { }
37
38private:
39    int2 mouse;
40    int3 buttons;
41
42    static int const MAX_ENTITIES = 100;
43    WorldEntity *entities[MAX_ENTITIES];
44    int nentities;
45}
46inputdata;
47
48static InputData * const data = &inputdata;
49
50/*
51 * Public Input class
52 */
53
54float2 Input::GetAxis(int axis)
55{
56    float invsqrt2 = sqrtf(0.5f);
57    float2 f;
58
59    /* Simulate a joystick using the keyboard. This SDL call is free. */
60    Uint8 *keystate = SDL_GetKeyState(NULL);
61    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
62    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
63    f.x += left;
64    f.y += up;
65    if (left && up)
66        f = f * invsqrt2;
67
68    return f;
69}
70
71int2 Input::GetMousePos()
72{
73    return data->mouse;
74}
75
76int3 Input::GetMouseButtons()
77{
78    return data->buttons;
79}
80
81void Input::TrackMouse(WorldEntity *e)
82{
83    if (data->nentities >= InputData::MAX_ENTITIES)
84        return;
85    data->entities[data->nentities] = e;
86    data->nentities++;
87}
88
89void Input::UntrackMouse(WorldEntity *e)
90{
91    for (int n = 0; n < data->nentities; n++)
92    {
93        if (data->entities[n] != e)
94            continue;
95
96        data->entities[n] = data->entities[data->nentities - 1];
97        data->nentities--;
98        n--;
99    }
100}
101
102void Input::SetMousePos(int2 coord)
103{
104    data->mouse = coord;
105
106    /* FIXME: parse all subscribed entities and update them */
107}
108
109void Input::SetMouseButton(int index)
110{
111    data->buttons[index] = 1;
112
113    /* FIXME: parse all subscribed entities and update them */
114}
115
116void Input::UnsetMouseButton(int index)
117{
118    data->buttons[index] = 0;
119
120    /* FIXME: parse all subscribed entities and update them */
121}
122
Note: See TracBrowser for help on using the repository browser.