source: trunk/tutorial/04_texture.cpp @ 2183

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

File size: 3.2 KB
Line 
1//
2// Lol Engine - Graphing 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://www.wtfpl.net/ 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
21static int const TEXTURE_WIDTH = 128;
22
23extern char const *lolfx_04_texture;
24
25class TextureDemo : public WorldEntity
26{
27public:
28    TextureDemo() :
29        m_frames(0),
30        m_ready(false)
31    {
32        m_vertices << vec2(-1.0,  1.0);
33        m_vertices << vec2(-1.0, -1.0);
34        m_vertices << vec2( 1.0, -1.0);
35        m_vertices << vec2(-1.0,  1.0);
36        m_vertices << vec2( 1.0, -1.0);
37        m_vertices << vec2( 1.0,  1.0);
38
39        m_heightmap = new uint8_t[4 * TEXTURE_WIDTH * 1];
40    }
41
42    virtual ~TextureDemo()
43    {
44        delete m_heightmap;
45    }
46
47    virtual void TickGame(float seconds)
48    {
49        WorldEntity::TickGame(seconds);
50
51        /* Generate a new heightmap at the beginning */
52        if (m_frames == 0)
53            memset(m_heightmap, 255, 4 * TEXTURE_WIDTH);
54
55        /* Scroll left */
56        for (int i = 0; i < TEXTURE_WIDTH - 1; i++)
57            m_heightmap[4 * i] = m_heightmap[4 * i + 4];
58
59        int height = m_heightmap[4 * (TEXTURE_WIDTH - 1)];
60        height = height / 2 + 255 / 4 + rand() % 97 - 48;
61        height = std::max(15, std::min(height, 240));
62        m_heightmap[4 * (TEXTURE_WIDTH - 1)] = height;
63
64        /* Update frame counter */
65        ++m_frames;
66    }
67
68    virtual void TickDraw(float seconds)
69    {
70        WorldEntity::TickDraw(seconds);
71
72        /* Initialise GPU data */
73        if (!m_ready)
74        {
75            m_texture = new Texture(ivec2(TEXTURE_WIDTH, 1), PixelFormat::A8R8G8B8);
76
77            m_shader = Shader::Create(lolfx_04_texture);
78            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
79
80            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
81
82            m_vbo = new VertexBuffer(m_vertices.Bytes());
83            void *vertices = m_vbo->Lock(0, 0);
84            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
85            m_vbo->Unlock();
86
87            m_ready = true;
88
89            /* FIXME: this object never cleans up */
90        }
91
92        /* Send new heightmap to GPU */
93        m_texture->SetData(m_heightmap);
94
95        m_shader->Bind();
96        m_shader->SetUniform(m_texture_uni, m_texture->GetTexture(), 0);
97        m_vdecl->SetStream(m_vbo, m_coord);
98        m_vdecl->Bind();
99        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
100        m_vdecl->Unbind();
101    }
102
103private:
104    Array<vec2> m_vertices;
105    Texture *m_texture;
106    Shader *m_shader;
107    ShaderAttrib m_coord;
108    ShaderUniform m_texture_uni;
109    VertexDeclaration *m_vdecl;
110    VertexBuffer *m_vbo;
111    uint8_t *m_heightmap;
112    int m_frames;
113    bool m_ready;
114};
115
116int main(int argc, char **argv)
117{
118    Application app("Tutorial 4: Texture", ivec2(640, 480), 60.0f);
119
120    new TextureDemo();
121
122    app.Run();
123    return EXIT_SUCCESS;
124}
125
Note: See TracBrowser for help on using the repository browser.