source: trunk/src/input.cpp @ 664

Last change on this file since 664 was 664, checked in by sam, 9 years ago

Get rid of float3, float4, int3 etc. in favour of GLSL types.

  • Property svn:keywords set to Id
File size: 3.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),
34        buttons(0),
35        nentities(0),
36        lastfocus(0)
37    { }
38
39private:
40    vec2i mouse;
41    vec3i buttons;
42
43    static int const MAX_ENTITIES = 100;
44    WorldEntity *entities[MAX_ENTITIES];
45    int nentities;
46    WorldEntity *lastfocus;
47}
48inputdata;
49
50static InputData * const data = &inputdata;
51
52/*
53 * Public Input class
54 */
55
56vec2 Input::GetAxis(int axis)
57{
58    float invsqrt2 = sqrtf(0.5f);
59    vec2 f;
60
61    /* Simulate a joystick using the keyboard. This SDL call is free. */
62    Uint8 *keystate = SDL_GetKeyState(NULL);
63    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
64    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
65    f.x += left;
66    f.y += up;
67    if (left && up)
68        f = f * invsqrt2;
69
70    return f;
71}
72
73vec2i Input::GetMousePos()
74{
75    return data->mouse;
76}
77
78vec3i Input::GetMouseButtons()
79{
80    return data->buttons;
81}
82
83void Input::TrackMouse(WorldEntity *e)
84{
85    if (data->nentities >= InputData::MAX_ENTITIES)
86        return;
87    data->entities[data->nentities] = e;
88    data->nentities++;
89}
90
91void Input::UntrackMouse(WorldEntity *e)
92{
93    for (int n = 0; n < data->nentities; n++)
94    {
95        if (data->entities[n] != e)
96            continue;
97
98        data->entities[n] = data->entities[data->nentities - 1];
99        data->nentities--;
100        n--;
101    }
102}
103
104void Input::SetMousePos(vec2i coord)
105{
106    data->mouse = coord;
107
108    WorldEntity *best = NULL;
109
110    for (int n = 0; n < data->nentities; n++)
111    {
112        if (coord.x < data->entities[n]->bbox[0].x
113             || coord.x >= data->entities[n]->bbox[1].x
114             || coord.y < data->entities[n]->bbox[0].y
115             || coord.y >= data->entities[n]->bbox[1].y)
116            continue;
117
118        if (!best || best->bbox[1].z < data->entities[n]->bbox[1].z)
119            best = data->entities[n];
120    }
121
122    for (int n = 0; n < data->nentities; n++)
123    {
124        if (data->entities[n] == best)
125        {
126            data->entities[n]->mousepos = (vec2i)((vec3i)coord - best->bbox[0]);
127            if (best != data->lastfocus)
128                data->entities[n]->pressed = data->buttons;
129            else
130                data->entities[n]->clicked = 0;
131        }
132        else
133        {
134            data->entities[n]->mousepos = vec2i(-1);
135            /* FIXME */
136            data->entities[n]->released = 0;
137            data->entities[n]->pressed = 0;
138            data->entities[n]->clicked = 0;
139        }
140    }
141
142    data->lastfocus = best;
143}
144
145void Input::SetMouseButton(int index)
146{
147    data->buttons[index] = 1;
148
149    if (data->lastfocus)
150    {
151        if (!data->lastfocus->pressed[index])
152            data->lastfocus->clicked[index] = 1;
153        data->lastfocus->pressed[index] = 1;
154        data->lastfocus->released[index] = 0;
155    }
156}
157
158void Input::UnsetMouseButton(int index)
159{
160    data->buttons[index] = 0;
161
162    if (data->lastfocus)
163    {
164        if (data->lastfocus->pressed[index])
165            data->lastfocus->released[index] = 1;
166        data->lastfocus->pressed[index] = 0;
167        data->lastfocus->clicked[index] = 0;
168    }
169}
170
Note: See TracBrowser for help on using the repository browser.