1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at>                |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/
#include "triangles.hpp"

#include "gate/graphics/gl_apis.hpp"
#include "gate/mathematics.hpp"
#include "gate/graphics/pixmapimages.hpp"
#include "gate/debugging.h"

namespace gate
{
namespace apps
{

    TrianglesGame::TrianglesGame()
    :   ptrHost(NULL),
        resetRequired(false),
        shader_program(-1),
        vbo_pos(0),
        shader_loc_pos(-1)
    {
    }

    TrianglesGame::~TrianglesGame() noexcept
    {
    }

    void TrianglesGame::init(GameHost& host)
    {
        this->ptrHost = &host;
        this->resetRequired = true;
    }

#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
    static intptr_t create_shader_program(GlApi& gl)
    {
        static char const* const vertex_shader_src =
            "attribute vec2 aPos;"
            "void main() {"
            "  gl_Position = vec4(aPos, 0.0, 1.0);"
            "}";

        static char const * const fragment_shader_src =
            "precision mediump float;"
            "void main() {"
            "  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
            "}";

        gate_intptr_t success = 0;

        uintptr_t vertex_shader = gl.createShader(gate_gl2_api_shader_type_vertex);
        gl.shaderSource(vertex_shader, 1, &vertex_shader_src, NULL);
        gl.compileShader(vertex_shader);
        gl.getShaderIv(vertex_shader, gate_gl2_api_shader_param_compile_status, &success);
        if (!success)
        {
            char infoLog[1024];
            gate_size_t infoLoglength = sizeof(infoLog) - 1;
            gl.getShaderInfoLog(vertex_shader, infoLoglength, infoLoglength, infoLog);
            infoLog[infoLoglength] = 0;
            raiseException(results::Failed, infoLog, "glCompileShader(vertex)");
        }


        uintptr_t fragment_shader = gl.createShader(gate_gl2_api_shader_type_fragment);
        gl.shaderSource(fragment_shader, 1, &fragment_shader_src, NULL);
        gl.compileShader(fragment_shader);
        gl.getShaderIv(fragment_shader, gate_gl2_api_shader_param_compile_status, &success);
        if (!success)
        {
            char infoLog[1024];
            gate_size_t infoLoglength = sizeof(infoLog) - 1;
            gl.getShaderInfoLog(fragment_shader, infoLoglength, infoLoglength, infoLog);
            infoLog[infoLoglength] = 0;
            raiseException(results::Failed, infoLog, "glCompileShader(fragment)");
        }

        uintptr_t program = gl.createProgram();
        gl.attachShader(program, vertex_shader);
        gl.attachShader(program, fragment_shader);
        gl.linkProgram(program);
        gl.getProgramIv(program,gate_gl2_api_program_param_link_status, &success);
        if (!success)
        {
            char infoLog[1024];
            gate_size_t infoLoglength = sizeof(infoLog) - 1;
            gl.getProgramInfoLog(program, infoLoglength, &infoLoglength, infoLog);
            infoLog[infoLoglength] = 0;
            raiseException(results::Failed, infoLog, "glLinkProgram()");
        }

        return program;
    }
#endif /* GATE_GRAPHICS_OPENGL2_SUPPORT */

    void TrianglesGame::loadModule(GlSurface& surface)
    {
        GlApi& gl = surface.getGlApi();<--- Variable 'gl' is assigned a value that is never used.

    }
    void TrianglesGame::unloadModule(GlSurface& surface)
    {
        GlApi& gl = surface.getGlApi();<--- Variable 'gl' is assigned a value that is never used.
        /* TODO */
    }
    
    void TrianglesGame::resetSurface(GlSurface& surface)
    {
        GlApi& gl = surface.getGlApi();<--- Variable 'gl' is assigned a value that is never used.
        uint32_t width = 800;
        uint32_t height = 600;
        surface.getSize(width, height);
        GATE_DEBUG_TRACE_VALUE(width);
        GATE_DEBUG_TRACE_VALUE(height);
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
        gl.setViewport(0, 0, width, height);
        if(this->shader_program != ((gate::uintptr_t)-1))
        {
            gl.disableVertexAttribArray(this->shader_loc_pos);
            gl.deleteBuffers(1, &this->vbo_pos);
            gl.deleteProgram(this->shader_program);
        }
        this->shader_program = create_shader_program(gl);
        gl.useProgram(this->shader_program);

        static float vertex_array[] = {
             0.0f,  0.5f,
            -0.5f, -0.5f,
             0.5f, -0.5f
        };
        gl.genBuffers(1, &this->vbo_pos);
        gl.bindBuffer(gate_gl2_api_buffer_type_array, this->vbo_pos);
        gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(vertex_array), vertex_array, gate_gl2_api_usage_type_static_draw);
#endif
    }


    void TrianglesGame::resize(GlSurface& surface, uint32_t width, uint32_t height)
    {
        this->resetRequired = true;
    }

    void TrianglesGame::render(GlSurface& surface)
    {
        GlApi& gl = surface.getGlApi();
        if (this->resetRequired)
        {
            GATE_DEBUG_TRACE("Reset required");
            this->resetSurface(surface);
            this->resetRequired = false;
        }

#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
        gl.bindBuffer(gate_gl2_api_buffer_type_array, this->vbo_pos);
        intptr_t loc_pos = gl.getAttribLocation(this->shader_program, "aPos");
        gl.enableVertexAttribArray(loc_pos);
        gl.vertexAttribPointer(loc_pos, 2, gate_gl2_api_vertex_attrib_float, false, 0, NULL);

        gl.setClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        gl.clear(OpenGL::ClearBit_ColorBuffer);
        gl.drawArrays(GlApi::DrawMode_Triangles, 0, 3);
#endif

        {   // finish GL drawing and swap buffers
            gl.flush();
            //gl.finish();
            surface.swapBuffers();
        }
    }
    void TrianglesGame::processKeyDown(Keyboard::KeyEnum key, uint32_t state)
    {

    }
    void TrianglesGame::processKeyUp(Keyboard::KeyEnum key, uint32_t state)
    {

    }
    void TrianglesGame::processPointerDown(real32_t x, real32_t y, real32_t xMax, real32_t yMax)
    {

    }
    void TrianglesGame::processPointerMove(real32_t x, real32_t y, real32_t xMax, real32_t yMax)
    {

    }
    void TrianglesGame::processPointerUp(real32_t x, real32_t y, real32_t xMax, real32_t yMax)
    {

    }

} // end of namespace apps
} // end of namespace gate