source: trunk/test/tutorial/tut01.cpp @ 1192

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

test: fix uninitialised member variable.

  • Property svn:keywords set to Id
File size: 5.2 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 "lolgl.h"
17#include "loldebug.h"
18
19using namespace std;
20using namespace lol;
21
22#if USE_SDL && defined __APPLE__
23#   include <SDL_main.h>
24#endif
25
26#if defined __native_client__
27#   define main old_main
28#endif
29
30#if defined _WIN32
31#   undef main /* FIXME: still needed? */
32#   include <direct.h>
33#endif
34
35class Triangle : public WorldEntity
36{
37public:
38    Triangle()
39    {
40        m_vertices[0] = vec2( 0.0,  0.8);
41        m_vertices[1] = vec2(-0.8, -0.8);
42        m_vertices[2] = vec2( 0.8, -0.8);
43        m_ready = false;
44    }
45
46    virtual void TickDraw(float deltams)
47    {
48        WorldEntity::TickDraw(deltams);
49
50        if (!m_ready)
51        {
52            m_shader = Shader::Create(
53#if !defined __CELLOS_LV2__ && !defined _XBOX
54                "#version 120\n"
55                "attribute vec2 coord2d;"
56                "void main(void) {"
57                "    gl_Position = vec4(coord2d, 0.0, 1.0);"
58                "}",
59
60                "#version 120\n"
61                "void main(void) {"
62                "    gl_FragColor[0] = 0.0;"
63                "    gl_FragColor[1] = 0.0;"
64                "    gl_FragColor[2] = 1.0;"
65                "}"
66#else
67                "void main(float2 coord2d : POSITION,"
68                "          out float4 out_Position : POSITION) {"
69                "    out_Position = float4(coord2d, 0.0, 1.0);"
70                "}",
71
72                "void main(out float4 out_FragColor : COLOR) {"
73                "    out_FragColor[0] = 0.0;"
74                "    out_FragColor[0] = 0.0;"
75                "    out_FragColor[0] = 1.0;"
76                "}"
77#endif
78            );
79            m_attrib = m_shader->GetAttribLocation("coord2d");
80            m_ready = true;
81
82#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX
83            /* Method 1: store vertex buffer on the GPU memory */
84            glGenBuffers(1, &m_vbo);
85            glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
86            glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices,
87                         GL_STATIC_DRAW);
88#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX
89            /* Method 2: upload vertex information at each frame */
90#elif defined _XBOX
91            extern D3DDevice *g_d3ddevice;
92            D3DVERTEXELEMENT9 const elements[3] =
93            {
94                { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
95                D3DDECL_END()
96            };
97            g_d3ddevice->CreateVertexDeclaration(elements, &m_vdecl);
98            g_d3ddevice->CreateVertexBuffer(sizeof(m_vertices), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_vbo, NULL);
99
100            vec2 *vertices;
101            m_vbo->Lock(0, 0, (void **)&vertices, 0);
102            memcpy(vertices, m_vertices, sizeof(m_vertices));
103            m_vbo->Unlock();
104#else
105#endif
106
107            /* FIXME: this object never cleans up */
108        }
109
110        m_shader->Bind();
111#if defined _XBOX
112        extern D3DDevice *g_d3ddevice;
113        g_d3ddevice->SetVertexDeclaration(m_vdecl);
114        g_d3ddevice->SetStreamSource(0, m_vbo, 0, sizeof(*m_vertices));
115#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
116        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
117        glEnableVertexAttribArray(m_attrib);
118        glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
119#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
120        /* Never used for now */
121        glEnableVertexAttribArray(m_attrib);
122        glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, m_vertices);
123#else
124        glEnableClientState(GL_VERTEX_ARRAY);
125        glVertexPointer(3, GL_FLOAT, 0, m_vertices);
126#endif
127
128#if defined _XBOX
129        g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
130#else
131        glDrawArrays(GL_TRIANGLES, 0, 3);
132#endif
133
134#if defined _XBOX
135        /* FIXME: do we need to unset anything here? */
136#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
137        glDisableVertexAttribArray(m_attrib);
138        glBindBuffer(GL_ARRAY_BUFFER, 0);
139#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
140        /* Never used for now */
141        glDisableVertexAttribArray(m_attrib);
142#else
143        glDisableClientState(GL_VERTEX_ARRAY);
144#endif
145    }
146
147private:
148    vec2 m_vertices[3];
149    Shader *m_shader;
150#if defined _XBOX
151    D3DVertexDeclaration *m_vdecl;
152    D3DVertexBuffer *m_vbo;
153#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
154    GLuint m_vbo;
155#endif
156    int m_attrib;
157    bool m_ready;
158};
159
160int main(int argc, char **argv)
161{
162    Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
163
164#if defined _MSC_VER && !defined _XBOX
165    _chdir("..");
166#elif defined _WIN32 && !defined _XBOX
167    _chdir("../..");
168#endif
169
170    new DebugFps(5, 5);
171    new Triangle();
172
173    app.Run();
174    return EXIT_SUCCESS;
175}
176
Note: See TracBrowser for help on using the repository browser.