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