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

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

tutorial: port noise demo to PS3 and Xbox360.

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