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

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

tutorial: fix unbalanced parenthesis breaking compilation.

  • Property svn:keywords set to Id
File size: 5.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 "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 in_Position;"
56                "void main(void) {"
57                "    gl_Position = vec4(in_Position, 0.0, 1.0);"
58                "}",
59
60                "#version 120\n"
61                "void main(void) {"
62                "    gl_FragColor = vec4(0.7, 0.5, 0.2, 1.0);"
63                "}"
64#else
65                "void main(float2 in_Position : POSITION,"
66                "          out float4 out_Position : POSITION) {"
67                "    out_Position = float4(in_Position, 0.0, 1.0);"
68                "}",
69
70                "void main(out float4 out_FragColor : COLOR) {"
71                "    out_FragColor = float4(0.7, 0.5, 0.2, 1.0);"
72                "}"
73#endif
74            );
75#if !defined _XBOX
76            m_attrib = m_shader->GetAttribLocation("in_Position");
77#endif
78            m_ready = true;
79
80#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX
81            /* Method 1: store vertex buffer on the GPU memory */
82            glGenBuffers(1, &m_vbo);
83            glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
84            glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices,
85                         GL_STATIC_DRAW);
86#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX
87            /* Method 2: upload vertex information at each frame */
88#elif defined _XBOX
89            extern D3DDevice *g_d3ddevice;
90            D3DVERTEXELEMENT9 const elements[2] =
91            {
92                { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
93                D3DDECL_END()
94            };
95            g_d3ddevice->CreateVertexDeclaration(elements, &m_vdecl);
96            if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(m_vertices), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_vbo, NULL)))
97                exit(0);
98
99            vec2 *vertices;
100            if (FAILED(m_vbo->Lock(0, 0, (void **)&vertices, 0)))
101                exit(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->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
114        g_d3ddevice->SetVertexDeclaration(m_vdecl);
115        g_d3ddevice->SetStreamSource(0, m_vbo, 0, sizeof(*m_vertices));
116#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
117        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
118        glEnableVertexAttribArray(m_attrib);
119        glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
120#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
121        /* Never used for now */
122        glEnableVertexAttribArray(m_attrib);
123        glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, m_vertices);
124#else
125        glEnableClientState(GL_VERTEX_ARRAY);
126        glVertexPointer(3, GL_FLOAT, 0, m_vertices);
127#endif
128
129#if defined _XBOX
130        g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
131#else
132        glDrawArrays(GL_TRIANGLES, 0, 3);
133#endif
134
135#if defined _XBOX
136        /* FIXME: do we need to unset anything here? */
137#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
138        glDisableVertexAttribArray(m_attrib);
139        glBindBuffer(GL_ARRAY_BUFFER, 0);
140#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
141        /* Never used for now */
142        glDisableVertexAttribArray(m_attrib);
143#else
144        glDisableClientState(GL_VERTEX_ARRAY);
145#endif
146    }
147
148private:
149    vec2 m_vertices[3];
150    Shader *m_shader;
151#if defined _XBOX
152    D3DVertexDeclaration *m_vdecl;
153    D3DVertexBuffer *m_vbo;
154#else
155    int m_attrib;
156#   if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
157    GLuint m_vbo;
158#   endif
159#endif
160    bool m_ready;
161};
162
163int main(int argc, char **argv)
164{
165    Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
166
167#if defined _MSC_VER && !defined _XBOX
168    _chdir("..");
169#elif defined _WIN32 && !defined _XBOX
170    _chdir("../..");
171#endif
172
173    new DebugFps(5, 5);
174    new Triangle();
175
176    app.Run();
177    return EXIT_SUCCESS;
178}
179
Note: See TracBrowser for help on using the repository browser.