Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | #include "SampleApplication.h" |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 8 | #include "EGLWindow.h" |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 9 | |
Jamie Madill | 96e46cd | 2014-04-07 15:51:15 -0400 | [diff] [blame] | 10 | SampleApplication::SampleApplication(const std::string& name, size_t width, size_t height, |
Geoff Lang | f0955f1 | 2014-06-20 16:07:07 -0400 | [diff] [blame] | 11 | EGLint glesMajorVersion, EGLint requestedRenderer) |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 12 | : mName(name), |
Corentin Wallez | f3357ee | 2015-07-22 14:10:19 -0400 | [diff] [blame^] | 13 | mWidth(width), |
| 14 | mHeight(height), |
Jamie Madill | 586666c | 2014-08-21 10:04:05 -0400 | [diff] [blame] | 15 | mRunning(false) |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 16 | { |
Corentin Wallez | f3357ee | 2015-07-22 14:10:19 -0400 | [diff] [blame^] | 17 | mEGLWindow.reset(new EGLWindow(glesMajorVersion, EGLPlatformParameters(requestedRenderer))); |
Jamie Madill | 586666c | 2014-08-21 10:04:05 -0400 | [diff] [blame] | 18 | mTimer.reset(CreateTimer()); |
| 19 | mOSWindow.reset(CreateOSWindow()); |
Jamie Madill | 3757a5a | 2014-08-26 13:16:36 -0400 | [diff] [blame] | 20 | |
| 21 | mEGLWindow->setConfigRedBits(8); |
| 22 | mEGLWindow->setConfigGreenBits(8); |
| 23 | mEGLWindow->setConfigBlueBits(8); |
| 24 | mEGLWindow->setConfigAlphaBits(8); |
| 25 | mEGLWindow->setConfigDepthBits(24); |
| 26 | mEGLWindow->setConfigStencilBits(8); |
| 27 | |
| 28 | // Disable vsync |
| 29 | mEGLWindow->setSwapInterval(0); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | SampleApplication::~SampleApplication() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | bool SampleApplication::initialize() |
| 37 | { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | void SampleApplication::destroy() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void SampleApplication::step(float dt, double totalTime) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void SampleApplication::draw() |
| 50 | { |
| 51 | } |
| 52 | |
Geoff Lang | e977efc | 2014-03-04 11:43:04 -0500 | [diff] [blame] | 53 | void SampleApplication::swap() |
| 54 | { |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 55 | mEGLWindow->swap(); |
Geoff Lang | e977efc | 2014-03-04 11:43:04 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 58 | OSWindow *SampleApplication::getWindow() const |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 59 | { |
Jamie Madill | 586666c | 2014-08-21 10:04:05 -0400 | [diff] [blame] | 60 | return mOSWindow.get(); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Geoff Lang | e977efc | 2014-03-04 11:43:04 -0500 | [diff] [blame] | 63 | EGLConfig SampleApplication::getConfig() const |
| 64 | { |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 65 | return mEGLWindow->getConfig(); |
Geoff Lang | e977efc | 2014-03-04 11:43:04 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Geoff Lang | f0955f1 | 2014-06-20 16:07:07 -0400 | [diff] [blame] | 68 | EGLDisplay SampleApplication::getDisplay() const |
| 69 | { |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 70 | return mEGLWindow->getDisplay(); |
Geoff Lang | f0955f1 | 2014-06-20 16:07:07 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Geoff Lang | e977efc | 2014-03-04 11:43:04 -0500 | [diff] [blame] | 73 | EGLSurface SampleApplication::getSurface() const |
| 74 | { |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 75 | return mEGLWindow->getSurface(); |
Geoff Lang | e977efc | 2014-03-04 11:43:04 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | EGLContext SampleApplication::getContext() const |
| 79 | { |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 80 | return mEGLWindow->getContext(); |
Geoff Lang | e977efc | 2014-03-04 11:43:04 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 83 | int SampleApplication::run() |
| 84 | { |
Corentin Wallez | f3357ee | 2015-07-22 14:10:19 -0400 | [diff] [blame^] | 85 | if (!mOSWindow->initialize(mName, mWidth, mHeight)) |
Jamie Madill | 96e46cd | 2014-04-07 15:51:15 -0400 | [diff] [blame] | 86 | { |
| 87 | return -1; |
| 88 | } |
| 89 | |
Jamie Madill | 4119ed3 | 2014-10-01 10:41:40 -0400 | [diff] [blame] | 90 | mOSWindow->setVisible(true); |
| 91 | |
Jamie Madill | 586666c | 2014-08-21 10:04:05 -0400 | [diff] [blame] | 92 | if (!mEGLWindow->initializeGL(mOSWindow.get())) |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 93 | { |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | mRunning = true; |
| 98 | int result = 0; |
| 99 | |
| 100 | if (!initialize()) |
| 101 | { |
| 102 | mRunning = false; |
| 103 | result = -1; |
| 104 | } |
| 105 | |
| 106 | mTimer->start(); |
| 107 | double prevTime = 0.0; |
| 108 | |
| 109 | while (mRunning) |
| 110 | { |
| 111 | double elapsedTime = mTimer->getElapsedTime(); |
| 112 | double deltaTime = elapsedTime - prevTime; |
| 113 | |
| 114 | step(static_cast<float>(deltaTime), elapsedTime); |
| 115 | |
| 116 | // Clear events that the application did not process from this frame |
| 117 | Event event; |
| 118 | while (popEvent(&event)) |
| 119 | { |
| 120 | // If the application did not catch a close event, close now |
| 121 | if (event.Type == Event::EVENT_CLOSED) |
| 122 | { |
| 123 | exit(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (!mRunning) |
| 128 | { |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | draw(); |
Geoff Lang | e977efc | 2014-03-04 11:43:04 -0500 | [diff] [blame] | 133 | swap(); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 134 | |
Jamie Madill | 586666c | 2014-08-21 10:04:05 -0400 | [diff] [blame] | 135 | mOSWindow->messageLoop(); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 136 | |
| 137 | prevTime = elapsedTime; |
| 138 | } |
| 139 | |
| 140 | destroy(); |
Jamie Madill | 1cfaaf8 | 2014-08-21 10:04:04 -0400 | [diff] [blame] | 141 | mEGLWindow->destroyGL(); |
Jamie Madill | 586666c | 2014-08-21 10:04:05 -0400 | [diff] [blame] | 142 | mOSWindow->destroy(); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 143 | |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | void SampleApplication::exit() |
| 148 | { |
| 149 | mRunning = false; |
| 150 | } |
| 151 | |
| 152 | bool SampleApplication::popEvent(Event *event) |
| 153 | { |
Jamie Madill | 586666c | 2014-08-21 10:04:05 -0400 | [diff] [blame] | 154 | return mOSWindow->popEvent(event); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 155 | } |