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

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

win32: add a lot of DirectX stubs.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1//
2// Lol Engine - Triangle tutorial
3//
4// Copyright: (c) 2011 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    }
44
45    virtual void TickDraw(float deltams)
46    {
47        WorldEntity::TickDraw(deltams);
48
49        if (!m_ready)
50        {
51            m_shader = Shader::Create(
52                "#version 120\n"
53                "attribute vec2 coord2d;"
54                "void main(void) {"
55                "    gl_Position = vec4(coord2d, 0.0, 1.0);"
56                "}",
57
58                "#version 120\n"
59                "void main(void) {"
60                "    gl_FragColor[0] = 0.0;"
61                "    gl_FragColor[1] = 0.0;"
62                "    gl_FragColor[2] = 1.0;"
63                "}");
64            m_attrib = m_shader->GetAttribLocation("coord2d");
65            m_ready = true;
66
67#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
68            /* Method 1: store vertex buffer on the GPU memory */
69            glGenBuffers(1, &m_vbo);
70            glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
71            glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices,
72                         GL_STATIC_DRAW);
73#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
74            /* Method 2: upload vertex information at each frame */
75#else
76#endif
77
78            /* FIXME: this object never cleans up */
79        }
80
81        m_shader->Bind();
82#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
83        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
84        glEnableVertexAttribArray(m_attrib);
85        glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
86#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
87        /* Never used for now */
88        glEnableVertexAttribArray(m_attrib);
89        glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, m_vertices);
90#else
91        glEnableClientState(GL_VERTEX_ARRAY);
92        glVertexPointer(3, GL_FLOAT, 0, m_vertices);
93#endif
94
95        glDrawArrays(GL_TRIANGLES, 0, 3);
96
97#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
98        glDisableVertexAttribArray(m_attrib);
99        glBindBuffer(GL_ARRAY_BUFFER, 0);
100#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
101        /* Never used for now */
102        glDisableVertexAttribArray(m_attrib);
103#else
104        glDisableClientState(GL_VERTEX_ARRAY);
105#endif
106    }
107
108private:
109    vec2 m_vertices[3];
110    Shader *m_shader;
111#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
112    GLuint m_vbo;
113#endif
114    int m_attrib;
115    bool m_ready;
116};
117
118int main(int argc, char **argv)
119{
120    Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
121
122#if defined _MSC_VER
123    _chdir("..");
124#elif defined _WIN32
125    _chdir("../..");
126#endif
127
128    new DebugFps(5, 5);
129    new Triangle();
130
131    app.Run();
132
133    return EXIT_SUCCESS;
134}
135
Note: See TracBrowser for help on using the repository browser.