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

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

build: major refactoring of the Win32 and Xbox project files; we now
link properly on the Xbox.

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