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