source: trunk/tutorial/03_noise.cpp @ 2377

Last change on this file since 2377 was 2277, checked in by sam, 7 years ago

build: hide LolFx external declarations behind macros.

File size: 2.3 KB
Line 
1//
2// Lol Engine - Noise tutorial
3//
4// Copyright: (c) 2012-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(03_noise);
22
23class NoiseDemo : public WorldEntity
24{
25public:
26    NoiseDemo() :
27        m_time(0.0),
28        m_ready(false)
29    {
30        m_vertices << vec2(-1.0,  1.0);
31        m_vertices << vec2(-1.0, -1.0);
32        m_vertices << vec2( 1.0, -1.0);
33        m_vertices << vec2(-1.0,  1.0);
34        m_vertices << vec2( 1.0, -1.0);
35        m_vertices << vec2( 1.0,  1.0);
36        m_ready = false;
37    }
38
39    virtual void TickDraw(float seconds)
40    {
41        WorldEntity::TickDraw(seconds);
42
43        m_time += seconds;
44
45        if (!m_ready)
46        {
47            m_shader = Shader::Create(LOLFX_RESOURCE_NAME(03_noise));
48            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
49            m_time_uni = m_shader->GetUniformLocation("u_Time");
50
51            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
52
53            m_vbo = new VertexBuffer(m_vertices.Bytes());
54            void *vertices = m_vbo->Lock(0, 0);
55            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
56            m_vbo->Unlock();
57
58            m_ready = true;
59
60            /* FIXME: this object never cleans up */
61        }
62
63        m_shader->Bind();
64        m_shader->SetUniform(m_time_uni, m_time);
65        m_vdecl->SetStream(m_vbo, m_coord);
66        m_vdecl->Bind();
67        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
68        m_vdecl->Unbind();
69    }
70
71private:
72    Array<vec2> m_vertices;
73    Shader *m_shader;
74    ShaderAttrib m_coord;
75    ShaderUniform m_time_uni;
76    VertexDeclaration *m_vdecl;
77    VertexBuffer *m_vbo;
78    float m_time;
79    bool m_ready;
80};
81
82int main(int argc, char **argv)
83{
84    System::Init(argc, argv);
85
86    Application app("Tutorial 3: Noise", ivec2(1280, 720), 60.0f);
87
88    new NoiseDemo();
89
90    app.Run();
91    return EXIT_SUCCESS;
92}
93
Note: See TracBrowser for help on using the repository browser.