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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* 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 "gate/applications.h"

#include "gate/gatemain.h"
#include "gate/results.h"

#include "gate/applications.hpp"
#include "gate/results.hpp"
#include "gate/exceptions.hpp"
#include "gate/wrappers.hpp"
#include "gate/graphics/gl_apis.hpp"


#if defined(GATE_COMPILER_MSVC) && defined(GATE_LEAK_DETECTION) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && (GATE_ARCH != GATE_ARCH_ARM32) && (GATE_ARCH != GATE_ARCH_ARM64)
#   include <vld.h>
#endif

static gate_bool_t run_app_tests = false;

namespace gate
{
namespace apps
{

    class GATE_API_LOCAL GateGraphicsOpenGlTest : public App, protected graph::GlSurfaceEventSink
    {
    public:
        GateGraphicsOpenGlTest();
        virtual ~GateGraphicsOpenGlTest() noexcept;

    protected:
        virtual void onRender(graph::GlSurface& surface) override;
        virtual void onExit(graph::GlSurface& surface) override;
        virtual void onResize(graph::GlSurface& surface, uint32_t width, uint32_t height) override;

        virtual void onKeyDown(graph::GlSurface& surface, Keyboard::code_t keyCode, Keyboard::states_t keyStates) override;
        virtual void onKeyUp(graph::GlSurface& surface, Keyboard::code_t keyCode, Keyboard::states_t keyStates) override;
        virtual void onPointerDown(graph::GlSurface& surface, uint32_t pointerType, real32_t x, real32_t y) override;
        virtual void onPointerMove(graph::GlSurface& surface, uint32_t pointerType, real32_t x, real32_t y) override;
        virtual void onPointerUp(graph::GlSurface& surface, uint32_t pointerType, real32_t x, real32_t y) override;
        virtual void onScroll(graph::GlSurface& surface, int32_t xDirection, int32_t yDirection) override;

    private:
        virtual void run() override;
        virtual void onSignal(int appsignal) override;
        virtual void onInit() override;

        graph::GlSurface   surface;

        bool_t exitFlag;
        bool_t resetFlag;
        real32_t rotationAngle;
        size_t loopCounter;
    };


    GateGraphicsOpenGlTest::GateGraphicsOpenGlTest()
    :   exitFlag(false),
        resetFlag(true),
        rotationAngle(0.0f),
        loopCounter(0)
    {
    }
    GateGraphicsOpenGlTest::~GateGraphicsOpenGlTest() noexcept
    {
    }

    void GateGraphicsOpenGlTest::onSignal(int appsignal)
    {

    }
    void GateGraphicsOpenGlTest::onInit()
    {
        this->resetFlag = true;
        this->exitFlag = false;
        this->surface.openScreen();
    }
    void GateGraphicsOpenGlTest::run()
    {
        this->surface.runEventLoop(*this);
        this->surface.close();
    }
    void GateGraphicsOpenGlTest::onResize(graph::GlSurface& surface, uint32_t width, uint32_t height)
    {
        surface.resize(width, height);
        this->resetFlag = true;
    }
    void GateGraphicsOpenGlTest::onRender(graph::GlSurface& surface)
    {
        graph::GlApi& gl = surface.getGlApi();
        ++this->loopCounter;
        if (run_app_tests)
        {
            if (this->loopCounter > 20)
            {
                this->exitFlag = true;
            }
        }
        if (this->exitFlag)
        {
#if !defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
            gl.disableClientState(graph::GlApi::ClientState_VertexArray);
            gl.disableClientState(graph::GlApi::ClientState_ColorArray);
#endif
            gl.disableCapability(graph::GlApi::Capability_DepthTest);
            this->surface.exitEventLoop();
            return;
        }

        if (this->resetFlag)
        {
            // render setup
            uint32_t width = 0, height = 0;
            surface.getSize(width, height);
            gl.setViewport(0, 0, width, height);

#if !defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
            gl.enableCapability(graph::GlApi::Capability_DepthTest);
            gl.setCullFace(graph::GlApi::CullFace_Front);
            gl.setShadeModel(graph::GlApi::ShadeModel_Flat);

            real32_t aspect = static_cast<real32_t>(width) / static_cast<real32_t>(height);
            gl.setMatrixMode(graph::GlApi::MatrixMode_Projection);
            gl.loadIdentity();
            gl.setPerspective(60.f, aspect, 0.1f, 100.0f);

            gl.setMatrixMode(graph::GlApi::MatrixMode_ModelView);
            gl.loadIdentity();
#endif

            this->resetFlag = false;
        }

        // data upload
        static const float cubeVertices[] = {<--- Variable 'cubeVertices' is assigned a value that is never used.
            // Front face (Z+)
            -1,-1, 1,   1,-1, 1,   1, 1, 1,   -1, 1, 1,
            // Back face (Z-)
            -1,-1,-1,  -1, 1,-1,   1, 1,-1,    1,-1,-1,
            // Left face
            -1,-1,-1,  -1,-1, 1,  -1, 1, 1,   -1, 1,-1,
            // Right face
            1,-1,-1,   1, 1,-1,   1, 1, 1,    1,-1, 1,
            // Top face
            -1, 1,-1,  -1, 1, 1,   1, 1, 1,    1, 1,-1,
            // Bottom face
            -1,-1,-1,   1,-1,-1,   1,-1, 1,   -1,-1, 1
        };
        static const float cubeColors[] = {<--- Variable 'cubeColors' is assigned a value that is never used.
            // Front
            1,0,0,  1,0,0,  1,0,0,  1,0,0,
            // Back
            0,1,0,  0,1,0,  0,1,0,  0,1,0,
            // Left
            0,0,1,  0,0,1,  0,0,1,  0,0,1,
            // Right
            1,1,0,  1,1,0,  1,1,0,  1,1,0,
            // Top
            1,0,1,  1,0,1,  1,0,1,  1,0,1,
            // Bottom
            0,1,1,  0,1,1,  0,1,1,  0,1,1
        };

#if !defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
        gl.enableClientState(graph::GlApi::ClientState_VertexArray);
        gl.enableClientState(graph::GlApi::ClientState_ColorArray);
        gl.setVertexPointer(3, 0, cubeVertices);
        gl.setColorPointer(3, 0, cubeColors);

        // render output
        gl.setClearColor(0.2f, 0.2f, 0.2f, 1.0f);
        gl.setClearDepth(1.0f);
        gl.clear(graph::GlApi::ClearBit_ColorBuffer | graph::GlApi::ClearBit_DepthBuffer);

        gl.loadIdentity();
        gl.translate(0.0f, 0.0f, -5.0f);

        gl.rotate(this->rotationAngle, 1.0f, 1.0f, 0.0f);

        // Draw 6 faces, each face has 4 vertices → GL_QUADS
        gl.drawArrays(graph::GlApi::DrawMode_Quads, 0, 24);

        gl.flush();
        gl.finish();
#endif
        surface.swapBuffers();

        this->rotationAngle += 1.0f;
        while (this->rotationAngle >= 360.0f)
        {
            this->rotationAngle -= 360.0f;
        }
    }
    void GateGraphicsOpenGlTest::onExit(graph::GlSurface& surface)
    {
        this->exitFlag = true;
    }
    void GateGraphicsOpenGlTest::onKeyDown(graph::GlSurface& surface, Keyboard::code_t keyCode, Keyboard::states_t keyStates)
    {
    }
    void GateGraphicsOpenGlTest::onKeyUp(graph::GlSurface& surface, Keyboard::code_t keyCode, Keyboard::states_t keyStates)
    {
    }
    void GateGraphicsOpenGlTest::onPointerDown(graph::GlSurface& surface, uint32_t pointerType, real32_t x, real32_t y)
    {
    }
    void GateGraphicsOpenGlTest::onPointerMove(graph::GlSurface& surface, uint32_t pointerType, real32_t x, real32_t y)
    {
    }
    void GateGraphicsOpenGlTest::onPointerUp(graph::GlSurface& surface, uint32_t pointerType, real32_t x, real32_t y)
    {
    }
    void GateGraphicsOpenGlTest::onScroll(graph::GlSurface& surface, int32_t xDirection, int32_t yDirection)
    {
    }

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

int gate_main(char const* program, char const* const* arguments, gate_size_t argcount, gate_uintptr_t apphandle)
{
    gate::apps::GateGraphicsOpenGlTest app;
    if (argcount > 0)
    {
        if (0 == gate_str_comp_ic(arguments[0], "--runtests"))
        {
            run_app_tests = true;
        }
    }
    int ret = gate::App::runApp(app, program, arguments, argcount, apphandle);
    return ret;
}