File size: 10,002 Bytes
dec7e7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */

/*
 * Copyright 2015-2020 Mark Callow.
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @internal
 * @file
 * @~English
 *
 * @brief Implementation of app for running a set of OpenGL load tests.
 */

#include <exception>
#include <sstream>
#include <ktx.h>

#include "GLLoadTests.h"
#include "ltexceptions.h"

GLLoadTests::GLLoadTests(const sampleInvocation samples[],
                         const uint32_t numSamples,
                         const char* const name,
                         const SDL_GLProfile profile,
                         const int majorVersion,
                         const int minorVersion)
                : GLAppSDL(name, 640, 480, profile, majorVersion, minorVersion),
                  siSamples(samples), sampleIndex(numSamples)
{
    pCurSample = nullptr;
}

GLLoadTests::~GLLoadTests()
{
    delete pCurSample;
}

bool
GLLoadTests::initialize(Args& args)
{
    bool explicitlyLoadOpenGL = false;
    for (uint32_t i = 1; i < args.size(); i++) {
        if (args[i].compare("--load-gl") == 0) {
            explicitlyLoadOpenGL = true;
            args.erase(args.begin() + i);
            break;
        }
    }

    if (!GLAppSDL::initialize(args))
        return false;

    // --load-gl allows testing of explicitly loading the OpenGL pointers.
    // Default is for ktxTexture_GLUpload to implicitly load them.
    if (explicitlyLoadOpenGL)
        ktxLoadOpenGL(reinterpret_cast<PFNGLGETPROCADDRESS>(SDL_GL_GetProcAddress));

    for (auto it = args.begin() + 1; it != args.end(); it++) {
        infiles.push_back(*it);
    }
    if (infiles.size() > 0) {
        sampleIndex.setNumSamples((uint32_t)infiles.size());
    }
    // Launch the first sample.
    invokeSample(Direction::eForward);
    return true;
}


void
GLLoadTests::finalize()
{
    delete pCurSample;
    GLAppSDL::finalize();
}


bool
GLLoadTests::doEvent(SDL_Event* event)
{
    bool result = false;
    switch (event->type) {
      case SDL_EVENT_KEY_UP:
        switch (event->key.key) {
          case 'q':
            quit = true;
            break;
          case 'n':
            ++sampleIndex;
            invokeSample(Direction::eForward);
            break;
          case 'p':
            --sampleIndex;
            invokeSample(Direction::eBack);
            break;
          default:
            result = true;
        }
        break;
      case SDL_EVENT_MOUSE_BUTTON_DOWN:
        // Forward to sample in case this is the start of motion.
        result = true;
        switch (event->button.button) {
          case SDL_BUTTON_LEFT:
            buttonDown.x = event->button.x;
            buttonDown.y = event->button.y;
            buttonDown.timestamp = event->button.timestamp;
            break;
          default:
            break;
        }
        break;
      case SDL_EVENT_MOUSE_BUTTON_UP:
        // Forward to sample so it doesn't get stuck in button down state.
        result = true;
        switch (event->button.button) {
          case SDL_BUTTON_LEFT:
            if (SDL_fabs(event->button.x - buttonDown.x) < 5
                && SDL_fabs(event->button.y - buttonDown.y) < 5
                && (event->button.timestamp - buttonDown.timestamp) < 100) {
                // Advance to the next sample.
                ++sampleIndex;
                invokeSample(Direction::eForward);
            }
            break;
          default:
            break;
        }
        break;
      // On macOS drop events come also when Launch Pad sends a file open event.
      case SDL_EVENT_DROP_BEGIN:
        // Opens of multiple selected files from Finder/LaunchPad come as
        // a BEGIN, COMPLETE sequence per file. Only clear infiles after a
        // suitable pause between COMPLETE and BEGIN.
        if (event->drop.timestamp - dropCompleteTime > 500) {
            infiles.clear();
        }
        break;
      case SDL_EVENT_DROP_FILE:
        infiles.push_back(event->drop.data);
        break;
      case SDL_EVENT_DROP_COMPLETE:
        if (!infiles.empty()) {
            // Guard against the drop being text.
            dropCompleteTime = event->drop.timestamp;
            sampleIndex.setNumSamples((uint32_t)infiles.size());
            invokeSample(Direction::eForward);
        }
        break;
      case SDL_EVENT_USER:
        if (event->user.code == SwipeDetector::swipeGesture) {
            // This is horrible.
            uint64_t udirection = reinterpret_cast<uint64_t>(event->user.data1);
            SwipeDetector::Direction direction =
              static_cast<SwipeDetector::Direction>(udirection);
            switch (direction) {
              case SwipeDetector::Direction::left:
                ++sampleIndex;
                invokeSample(Direction::eForward);
                break;
              case SwipeDetector::Direction::right:
                --sampleIndex;
                invokeSample(Direction::eBack);
                break;
              default:
                result = true;
            }
        } else {
            result = true;
        }
        break;
      default:
        result = swipeDetector.doEvent(event);
    }
    
    if (result) {
        // Further processing required.
        if (pCurSample != nullptr)
            result = pCurSample->doEvent(event);  // Give sample a chance.
        if (result)
            return GLAppSDL::doEvent(event);  // Pass to base class.
    }
    return result;
}


void
GLLoadTests::windowResized()
{
    if (pCurSample != nullptr)
        pCurSample->resize(w_width, w_height);
}


void
GLLoadTests::drawFrame(uint32_t msTicks)
{
    if (pCurSample != nullptr)
        pCurSample->run(msTicks);

    GLAppSDL::drawFrame(msTicks);
}

#if 0
void
GLLoadTests::getOverlayText(GLTextOverlay * textOverlay)
{
    if (enableTextOverlay && pCurSample != nullptr) {
        pCurSample->getOverlayText(textOverlay);
    }
}
#endif

void
GLLoadTests::invokeSample(Direction dir)
{
    const sampleInvocation* sampleInv = &siSamples[sampleIndex];

    delete pCurSample;
    // Certain events can be triggered during new sample initialization
    // while pCurSample is not valid, e.g. FOCUS_LOST. Protect against
    // problems from this by indicating there is no current sample.
    pCurSample = nullptr;

    uint32_t unsupportedTypeExceptions = 0;
    std::string fileTitle;
    for (;;) {
        try {
            if (infiles.size() > 0) {
                fileTitle = "Viewing file ";
                fileTitle += infiles[sampleIndex];
                pCurSample = showFile(infiles[sampleIndex]);
            } else {
                sampleInv = &siSamples[sampleIndex];
                pCurSample = sampleInv->createSample(w_width, w_height,
                                                     sampleInv->args,
                                                     sBasePath);
            }
            break;
        } catch (unsupported_ctype& e) {
            (void)e; // To quiet unused variable warnings from some compilers.
            unsupportedTypeExceptions++;
            if (unsupportedTypeExceptions == sampleIndex.getNumSamples()) {
                SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
                    infiles.size() > 0 ? fileTitle.c_str() : sampleInv->title,
                    "None of the specified samples or files use texture types "
                    "supported on this platform.",
                    NULL);
                exit(0);
            } else {
                dir == Direction::eForward ? ++sampleIndex : --sampleIndex;
            }
        } catch (std::exception& e) {
            const SDL_MessageBoxButtonData buttons[] = {
                /* .flags, .buttonid, .text */
                { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "Continue" },
                { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 1, "Abort" },
            };
#if 0
            const SDL_MessageBoxColorScheme colorScheme = {
                { /* .colors (.r, .g, .b) */
                    /* [SDL_MESSAGEBOX_COLOR_BACKGROUND] */
                    { 255,   0,   0 },
                    /* [SDL_MESSAGEBOX_COLOR_TEXT] */
                    {   0, 255,   0 },
                    /* [SDL_MESSAGEBOX_COLOR_BUTTON_BORDER] */
                    { 255, 255,   0 },
                    /* [SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND] */
                    {   0,   0, 255 },
                    /* [SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] */
                    { 255,   0, 255 }
                }
            };
#endif
            const SDL_MessageBoxData messageboxdata = {
                SDL_MESSAGEBOX_ERROR,                               // .flags
                NULL,                                               // .window
                infiles.size() > 0 ?
                 fileTitle.c_str() : sampleInv->title,              // .title
                e.what(),                                           // .message
                SDL_arraysize(buttons),                             // .numbuttons
                buttons,                                            // .buttons
                NULL //&colorScheme                                 // .colorScheme
            };
            int buttonid;
            if (!SDL_ShowMessageBox(&messageboxdata, &buttonid)) {
                SDL_Log("error displaying error message box");
                exit(1);
            }
            if (buttonid == 0) {
                // Continue
                dir == Direction::eForward ? ++sampleIndex : --sampleIndex;
            } else {
                // We've been told to quit or no button was pressed.
                exit(1);
            }
        }
    }
    setAppTitle(infiles.size() > 0 ? fileTitle.c_str()  : sampleInv->title);
    pCurSample->resize(w_width, w_height);
}

void
GLLoadTests::onFPSUpdate()
{
    // Using onFPSUpdate avoids rewriting the title every frame.
    //setWindowTitle(pCurSample->title);
    GLAppSDL::onFPSUpdate();
}