source: trunk/src/sdlinput.cpp @ 781

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

sdl: fix touchscreen event handling by first applying mouse moves, then
button changes.

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