source: trunk/test/tutorial/01_triangle.cpp @ 1385

Last change on this file since 1385 was 1385, checked in by sam, 9 years ago

gpu: allow to load a .lolfx file instead of all those shaders.

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