source: trunk/tutorial/01_triangle.cpp @ 2183

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

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1//
2// Lol Engine - Triangle 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://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
21#if defined _WIN32
22#   include <direct.h>
23#endif
24
25extern char const *lolfx_01_triangle;
26
27class Triangle : public WorldEntity
28{
29public:
30    Triangle()
31    {
32        m_vertices << vec2( 0.0,  0.8);
33        m_vertices << vec2(-0.8, -0.8);
34        m_vertices << vec2( 0.8, -0.8);
35        m_ready = false;
36    }
37
38    virtual void TickDraw(float seconds)
39    {
40        WorldEntity::TickDraw(seconds);
41
42        if (!m_ready)
43        {
44            m_shader = Shader::Create(lolfx_01_triangle);
45            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
46
47            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
48
49            m_vbo = new VertexBuffer(m_vertices.Bytes());
50            void *vertices = m_vbo->Lock(0, 0);
51            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
52            m_vbo->Unlock();
53
54            m_ready = true;
55
56            /* FIXME: this object never cleans up */
57        }
58
59        m_shader->Bind();
60        m_vdecl->SetStream(m_vbo, m_coord);
61        m_vdecl->Bind();
62        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 3);
63        m_vdecl->Unbind();
64    }
65
66private:
67    Array<vec2> m_vertices;
68    Shader *m_shader;
69    ShaderAttrib m_coord;
70    VertexDeclaration *m_vdecl;
71    VertexBuffer *m_vbo;
72    bool m_ready;
73};
74
75int main(int argc, char **argv)
76{
77    Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
78
79#if defined _MSC_VER && !defined _XBOX
80    _chdir("..");
81#elif defined _WIN32 && !defined _XBOX
82    _chdir("../..");
83#endif
84
85    new DebugFps(5, 5);
86    new Triangle();
87
88    app.Run();
89    return EXIT_SUCCESS;
90}
91
Note: See TracBrowser for help on using the repository browser.