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

Last change on this file since 2270 was 2241, checked in by sam, 8 years ago

core: pass the project directory to the binary build and get rid of
that 2-year old "temporary Win32 hack".

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