source: trunk/src/platform/sdl/sdlinput.cpp @ 1171

Last change on this file since 1171 was 1171, checked in by sam, 8 years ago

sdl: on Windows, perform the input tick in the main drawing thread.

  • Property svn:keywords set to Id
File size: 2.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#if defined USE_SDL
16#   include <SDL.h>
17#endif
18
19#include "core.h"
20#include "sdlinput.h"
21
22namespace lol
23{
24
25/*
26 * SDL Input implementation class
27 */
28
29class SdlInputData
30{
31    friend class SdlInput;
32
33private:
34    void Tick(float deltams);
35
36    static ivec2 GetMousePos();
37};
38
39/*
40 * Public SdlInput class
41 */
42
43SdlInput::SdlInput()
44  : data(new SdlInputData())
45{
46#if defined USE_SDL
47    SDL_Init(SDL_INIT_TIMER);
48#endif
49
50    m_gamegroup = GAMEGROUP_BEFORE;
51}
52
53void SdlInput::TickGame(float deltams)
54{
55    Entity::TickGame(deltams);
56
57#if !defined _WIN32
58    data->Tick(deltams);
59#endif
60}
61
62void SdlInput::TickDraw(float deltams)
63{
64    Entity::TickDraw(deltams);
65
66#if defined _WIN32
67    data->Tick(deltams);
68#endif
69}
70
71void SdlInputData::Tick(float deltams)
72{
73#if defined USE_SDL
74    /* Handle mouse input */
75    ivec2 mouse = SdlInputData::GetMousePos();;
76    Input::SetMousePos(mouse);
77
78    /* Handle keyboard and WM events */
79    SDL_Event event;
80    while (SDL_PollEvent(&event))
81    {
82        switch (event.type)
83        {
84        case SDL_QUIT:
85            Ticker::Shutdown();
86            break;
87#if 0
88        case SDL_KEYDOWN:
89            Input::KeyPressed(event.key.keysym.sym, deltams);
90            break;
91#endif
92        case SDL_MOUSEBUTTONDOWN:
93        case SDL_MOUSEBUTTONUP:
94        {
95            ivec2 newmouse = SdlInputData::GetMousePos();
96            if (newmouse != mouse)
97                Input::SetMousePos(mouse = newmouse);
98            if (event.type == SDL_MOUSEBUTTONDOWN)
99                Input::SetMouseButton(event.button.button - 1);
100            else
101                Input::UnsetMouseButton(event.button.button - 1);
102            break;
103        }
104        }
105    }
106
107    /* Send the whole keyboard state to the input system */
108#if 0
109    Uint8 *keystate = SDL_GetKeyState(NULL);
110    for (int i = 0; i < 256; i++)
111        if (keystate[i])
112            Input::KeyPressed(i, deltams);
113#endif
114#endif
115}
116
117SdlInput::~SdlInput()
118{
119    delete data;
120}
121
122ivec2 SdlInputData::GetMousePos()
123{
124    ivec2 ret(-1, -1);
125
126#if defined USE_SDL
127    if (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
128    {
129        SDL_GetMouseState(&ret.x, &ret.y);
130        ret.y = Video::GetSize().y - 1 - ret.y;
131    }
132#endif
133    return ret;
134}
135
136} /* namespace lol */
137
Note: See TracBrowser for help on using the repository browser.