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

Last change on this file since 1309 was 1269, checked in by sam, 8 years ago

core: use operator<< instead of operator+= to append stuff to arrays,
it's a bit less confusing.

  • Property svn:keywords set to Id
File size: 3.0 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
34class Triangle : public WorldEntity
35{
36public:
37    Triangle()
38    {
39        m_vertices << vec2( 0.0,  0.8);
40        m_vertices << vec2(-0.8, -0.8);
41        m_vertices << vec2( 0.8, -0.8);
42        m_ready = false;
43    }
44
45    virtual void TickDraw(float deltams)
46    {
47        WorldEntity::TickDraw(deltams);
48
49        if (!m_ready)
50        {
51            m_shader = Shader::Create(
52#if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
53                "#version 120\n"
54                "attribute vec2 in_Position;"
55                "void main(void) {"
56                "    gl_Position = vec4(in_Position, 0.0, 1.0);"
57                "}",
58
59                "#version 120\n"
60                "void main(void) {"
61                "    gl_FragColor = vec4(0.7, 0.2, 0.5, 1.0);"
62                "}"
63#else
64                "void main(float2 in_Position : POSITION,"
65                "          out float4 out_Position : POSITION) {"
66                "    out_Position = float4(in_Position, 0.0, 1.0);"
67                "}",
68
69                "void main(out float4 out_FragColor : COLOR) {"
70                "    out_FragColor = float4(0.7, 0.2, 0.5, 1.0);"
71                "}"
72#endif
73            );
74            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
75
76            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
77
78            m_vbo = new VertexBuffer(m_vertices.Bytes());
79            void *vertices = m_vbo->Lock(0, 0);
80            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
81            m_vbo->Unlock();
82
83            m_ready = true;
84
85            /* FIXME: this object never cleans up */
86        }
87
88        m_shader->Bind();
89        m_vdecl->SetStream(m_vbo, m_coord);
90        m_vdecl->Bind();
91        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
92        m_vdecl->Unbind();
93    }
94
95private:
96    Array<vec2> m_vertices;
97    Shader *m_shader;
98    ShaderAttrib m_coord;
99    VertexDeclaration *m_vdecl;
100    VertexBuffer *m_vbo;
101    bool m_ready;
102};
103
104int main(int argc, char **argv)
105{
106    Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
107
108#if defined _MSC_VER && !defined _XBOX
109    _chdir("..");
110#elif defined _WIN32 && !defined _XBOX
111    _chdir("../..");
112#endif
113
114    new DebugFps(5, 5);
115    new Triangle();
116
117    app.Run();
118    return EXIT_SUCCESS;
119}
120
Note: See TracBrowser for help on using the repository browser.