source: trunk/src/input.cpp @ 330

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

First implementation of the mouse tracker.

  • Property svn:keywords set to Id
File size: 2.9 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    WorldEntity *best = NULL;
107
108    for (int n = 0; n < data->nentities; n++)
109    {
110        if (coord.x < data->entities[n]->bbox[0].x
111             || coord.x >= data->entities[n]->bbox[1].x
112             || coord.y < data->entities[n]->bbox[0].y
113             || coord.y >= data->entities[n]->bbox[1].y)
114            continue;
115
116        if (!best || best->bbox[1].z < data->entities[n]->bbox[1].z)
117            best = data->entities[n];
118    }
119
120    for (int n = 0; n < data->nentities; n++)
121    {
122        if (data->entities[n] == best)
123            data->entities[n]->mousepos = (int2)((int3)coord - best->bbox[0]);
124        else
125            data->entities[n]->mousepos = int2(-1);
126    }
127}
128
129void Input::SetMouseButton(int index)
130{
131    data->buttons[index] = 1;
132
133    /* FIXME: parse all subscribed entities and update them */
134}
135
136void Input::UnsetMouseButton(int index)
137{
138    data->buttons[index] = 0;
139
140    /* FIXME: parse all subscribed entities and update them */
141}
142
Note: See TracBrowser for help on using the repository browser.