source: trunk/demos/tutorial/07_input.cpp @ 2925

Last change on this file since 2925 was 2925, checked in by benlitz, 6 years ago

input: added a name the controllers, and a static Get() function to easily retrieve a registered controller

  • Property svn:eol-style set to LF
File size: 7.5 KB
Line 
1//
2// Lol Engine - Input tutorial
3//
4// Copyright: (c) 2011-2013 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://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "loldebug.h"
17
18using namespace std;
19using namespace lol;
20
21LOLFX_RESOURCE_DECLARE(07_input);
22
23class InputTutorial : public WorldEntity
24{
25public:
26    InputTutorial()
27    {
28        m_controller = new Controller("Default", KEY_MAX, AXIS_MAX);
29
30        m_keyboard = InputDevice::Get("Keyboard");
31        if (m_keyboard)
32            m_controller->GetKey(KEY_MANUAL_ROTATION).Bind("Keyboard", "Space");
33
34        m_mouse = InputDevice::Get("Mouse");
35        if (m_mouse)
36        {
37            m_controller->GetKey(KEY_DRAG_MESH).Bind("Mouse", "ButtonLeft");
38            m_controller->GetAxis(AXIS_DRAG_PITCH).Bind("Mouse", "Y");
39            m_controller->GetAxis(AXIS_DRAG_YAW).Bind("Mouse", "X");
40        }
41
42        m_joystick = InputDevice::Get("Joystick1");
43        if (m_joystick)
44        {
45            m_controller->GetAxis(AXIS_PITCH).Bind("Joystick1", "Axis2");
46            m_controller->GetAxis(AXIS_YAW).Bind("Joystick1", "Axis1");
47        }
48
49        m_pitch_angle = 0;
50        m_yaw_angle = 0;
51        m_autorot = true;
52
53        /* Front vertices/colors */
54        m_mesh.Push(vec3(-1.0, -1.0,  1.0), vec3(1.0, 0.0, 1.0));
55        m_mesh.Push(vec3( 1.0, -1.0,  1.0), vec3(0.0, 1.0, 0.0));
56        m_mesh.Push(vec3( 1.0,  1.0,  1.0), vec3(1.0, 0.5, 0.0));
57        m_mesh.Push(vec3(-1.0,  1.0,  1.0), vec3(1.0, 1.0, 0.0));
58        /* Back */
59        m_mesh.Push(vec3(-1.0, -1.0, -1.0), vec3(1.0, 0.0, 0.0));
60        m_mesh.Push(vec3( 1.0, -1.0, -1.0), vec3(0.0, 0.5, 0.0));
61        m_mesh.Push(vec3( 1.0,  1.0, -1.0), vec3(0.0, 0.5, 1.0));
62        m_mesh.Push(vec3(-1.0,  1.0, -1.0), vec3(0.0, 0.0, 1.0));
63
64        m_faces_indices << 0 << 1 << 2 << 2 << 3 << 0;
65        m_faces_indices << 1 << 5 << 6 << 6 << 2 << 1;
66        m_faces_indices << 7 << 6 << 5 << 5 << 4 << 7;
67        m_faces_indices << 4 << 0 << 3 << 3 << 7 << 4;
68        m_faces_indices << 4 << 5 << 1 << 1 << 0 << 4;
69        m_faces_indices << 3 << 2 << 6 << 6 << 7 << 3;
70
71        m_lines_indices << 0 << 1 << 1 << 2 << 2 << 3 << 3 << 0;
72        m_lines_indices << 4 << 5 << 5 << 6 << 6 << 7 << 7 << 4;
73        m_lines_indices << 0 << 4 << 1 << 5 << 2 << 6 << 3 << 7;
74
75        m_text = new Text("", "data/font/ascii.png");
76        m_text->SetPos(ivec3(5, 5, 1));
77        Ticker::Ref(m_text);
78
79        m_ready = false;
80    }
81
82    ~InputTutorial()
83    {
84        Ticker::Unref(m_text);
85    }
86
87    virtual void TickGame(float seconds)
88    {
89        WorldEntity::TickGame(seconds);
90
91        /* Handle keyboard */
92        if (m_keyboard)
93        {
94            if (m_controller->GetKey(KEY_MANUAL_ROTATION).IsPressed())
95                m_autorot = !m_autorot;
96        }
97
98        /* Handle joystick */
99        if (m_joystick)
100        {
101            if (lol::abs(m_controller->GetAxis(AXIS_PITCH).GetValue()) > 0.2f)
102                m_pitch_angle -= m_controller->GetAxis(AXIS_PITCH).GetValue() * seconds * 100;
103            if (lol::abs(m_controller->GetAxis(AXIS_YAW).GetValue()) > 0.2f)
104                m_yaw_angle += m_controller->GetAxis(AXIS_YAW).GetValue() * seconds * 100;
105        }
106
107        /* Handle mouse */
108        if (m_mouse)
109        {
110            if (m_controller->GetKey(KEY_DRAG_MESH).IsDown())
111            {
112                InputDevice::CaptureMouse(true);
113                m_pitch_angle -= m_controller->GetAxis(AXIS_DRAG_PITCH).GetValue() * seconds * 100;
114                m_yaw_angle += m_controller->GetAxis(AXIS_DRAG_YAW).GetValue() * seconds * 100;
115            }
116            else
117            {
118                InputDevice::CaptureMouse(false);
119                if (m_autorot)
120                    m_yaw_angle += seconds * 20;
121            }
122
123            m_text->SetText(String::Printf(
124                "cursor: (%0.3f, %0.3f) - pixel (%d, %d)",
125                m_mouse->GetCursor(0).x, m_mouse->GetCursor(0).y,
126                m_mouse->GetCursorPixel(0).x, m_mouse->GetCursorPixel(0).y));
127        }
128        else
129        {
130            m_text->SetText("no mouse detected");
131        }
132
133        mat4 anim = mat4::fromeuler_yxz(m_yaw_angle, m_pitch_angle, 0.f);
134        mat4 model = mat4::translate(vec3(0, 0, -4.5));
135        mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
136        mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
137
138        m_matrix = proj * view * model * anim;
139    }
140
141    virtual void TickDraw(float seconds)
142    {
143        WorldEntity::TickDraw(seconds);
144
145        if (!m_ready)
146        {
147            m_shader = Shader::Create(LOLFX_RESOURCE_NAME(07_input));
148
149            m_mvp = m_shader->GetUniformLocation("in_Matrix");
150            m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
151            m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);
152
153            m_vdecl =
154              new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
155                                                            VertexUsage::Color));
156
157            m_vbo = new VertexBuffer(m_mesh.Bytes());
158            void *mesh = m_vbo->Lock(0, 0);
159            memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
160            m_vbo->Unlock();
161
162            m_lines_ibo = new IndexBuffer(m_lines_indices.Bytes());
163            void *indices = m_lines_ibo->Lock(0, 0);
164            memcpy(indices, &m_lines_indices[0], m_lines_indices.Bytes());
165            m_lines_ibo->Unlock();
166
167            m_faces_ibo = new IndexBuffer(m_faces_indices.Bytes());
168            indices = m_faces_ibo->Lock(0, 0);
169            memcpy(indices, &m_faces_indices[0], m_faces_indices.Bytes());
170            m_faces_ibo->Unlock();
171
172            /* FIXME: this object never cleans up */
173            m_ready = true;
174        }
175
176        g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
177
178        m_shader->Bind();
179        m_vdecl->SetStream(m_vbo, m_coord, m_color);
180        m_vdecl->Bind();
181
182        m_shader->SetUniform(m_mvp, m_matrix);
183        m_lines_ibo->Bind();
184        m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, 0, 0,
185                                     m_mesh.Count(), 0, m_lines_indices.Count());
186        m_lines_ibo->Unbind();
187
188        m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
189        m_faces_ibo->Bind();
190        m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0,
191                                     m_mesh.Count(), 0, m_faces_indices.Count());
192        m_faces_ibo->Unbind();
193
194        m_vdecl->Unbind();
195    }
196
197private:
198    enum
199    {
200        KEY_MANUAL_ROTATION,
201        KEY_DRAG_MESH,
202        KEY_MAX
203    };
204
205    enum
206    {
207        AXIS_DRAG_PITCH,
208        AXIS_DRAG_YAW,
209        AXIS_PITCH,
210        AXIS_YAW,
211        AXIS_MAX
212    };
213
214    InputDevice *m_keyboard, *m_mouse, *m_joystick;
215    Controller *m_controller;
216
217    bool m_autorot;
218    float m_pitch_angle;
219    float m_yaw_angle;
220    mat4 m_matrix;
221    Array<vec3,vec3> m_mesh;
222    Array<uint16_t> m_lines_indices, m_faces_indices;
223
224    Shader *m_shader;
225    ShaderAttrib m_coord, m_color;
226    ShaderUniform m_mvp;
227    VertexDeclaration *m_vdecl;
228    VertexBuffer *m_vbo;
229    IndexBuffer *m_lines_ibo, *m_faces_ibo;
230
231    Text *m_text;
232    bool m_ready;
233};
234
235int main(int argc, char **argv)
236{
237    System::Init(argc, argv);
238
239    Application app("Tutorial 7: Input", ivec2(640, 480), 60.0f);
240
241    new DebugFps(5, 5);
242    new InputTutorial();
243
244    app.Run();
245
246    return EXIT_SUCCESS;
247}
248
Note: See TracBrowser for help on using the repository browser.