source: trunk/src/input.cpp @ 233

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

Make SdlInput regularly inject the mouse position into the Input singleton.

  • Property svn:keywords set to Id
File size: 1.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    {
34        mouse.x = mouse.y = -1;
35    }
36
37    Int2 mouse;
38}
39inputdata;
40
41static InputData * const data = &inputdata;
42
43/*
44 * Public Input class
45 */
46
47Float2 Input::GetAxis(int axis)
48{
49    float invsqrt2 = sqrtf(0.5f);
50    Float2 f;
51
52    /* Simulate a joystick using the keyboard. This SDL call is free. */
53    Uint8 *keystate = SDL_GetKeyState(NULL);
54    int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
55    int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
56    f.x += left;
57    f.y += up;
58    if (left && up)
59    {
60        f.x *= invsqrt2;
61        f.y *= invsqrt2;
62    }
63
64    return f;
65}
66
67void Input::SetMousePos(Int2 coord)
68{
69    data->mouse = coord;
70}
71
72Int2 Input::GetMousePos()
73{
74    return data->mouse;
75}
76
Note: See TracBrowser for help on using the repository browser.