source: trunk/tutorial/01_triangle.cpp @ 1645

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

core: switch the Win32/x64 binaries to the Windows subsystem so that they
do not open a console at startup.

  • Property svn:keywords set to Id
File size: 2.1 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
22#   include <SDL_main.h>
23#endif
24
25#if defined _WIN32
26#   include <direct.h>
27#endif
28
29extern char const *lolfx_01_triangle;
30
31class Triangle : public WorldEntity
32{
33public:
34    Triangle()
35    {
36        m_vertices << vec2( 0.0,  0.8);
37        m_vertices << vec2(-0.8, -0.8);
38        m_vertices << vec2( 0.8, -0.8);
39        m_ready = false;
40    }
41
42    virtual void TickDraw(float seconds)
43    {
44        WorldEntity::TickDraw(seconds);
45
46        if (!m_ready)
47        {
48            m_shader = Shader::Create(lolfx_01_triangle);
49            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
50
51            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
52
53            m_vbo = new VertexBuffer(m_vertices.Bytes());
54            void *vertices = m_vbo->Lock(0, 0);
55            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
56            m_vbo->Unlock();
57
58            m_ready = true;
59
60            /* FIXME: this object never cleans up */
61        }
62
63        m_shader->Bind();
64        m_vdecl->SetStream(m_vbo, m_coord);
65        m_vdecl->Bind();
66        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
67        m_vdecl->Unbind();
68    }
69
70private:
71    Array<vec2> m_vertices;
72    Shader *m_shader;
73    ShaderAttrib m_coord;
74    VertexDeclaration *m_vdecl;
75    VertexBuffer *m_vbo;
76    bool m_ready;
77};
78
79int main(int argc, char **argv)
80{
81    Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
82
83#if defined _MSC_VER && !defined _XBOX
84    _chdir("..");
85#elif defined _WIN32 && !defined _XBOX
86    _chdir("../..");
87#endif
88
89    new DebugFps(5, 5);
90    new Triangle();
91
92    app.Run();
93    return EXIT_SUCCESS;
94}
95
Note: See TracBrowser for help on using the repository browser.