blob: 5a3746a312a474380d2dd6a56839a56c7101c937 [file] [log] [blame]
Geoff Lang49be2ad2014-02-28 13:05:51 -05001//
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 Madill1cfaaf82014-08-21 10:04:04 -04008#include "EGLWindow.h"
Geoff Lang49be2ad2014-02-28 13:05:51 -05009
Jamie Madill96e46cd2014-04-07 15:51:15 -040010SampleApplication::SampleApplication(const std::string& name, size_t width, size_t height,
Geoff Langf0955f12014-06-20 16:07:07 -040011 EGLint glesMajorVersion, EGLint requestedRenderer)
Jamie Madill1cfaaf82014-08-21 10:04:04 -040012 : mName(name),
Corentin Wallezf3357ee2015-07-22 14:10:19 -040013 mWidth(width),
14 mHeight(height),
Jamie Madill586666c2014-08-21 10:04:05 -040015 mRunning(false)
Geoff Lang49be2ad2014-02-28 13:05:51 -050016{
Corentin Wallezf3357ee2015-07-22 14:10:19 -040017 mEGLWindow.reset(new EGLWindow(glesMajorVersion, EGLPlatformParameters(requestedRenderer)));
Jamie Madill586666c2014-08-21 10:04:05 -040018 mTimer.reset(CreateTimer());
19 mOSWindow.reset(CreateOSWindow());
Jamie Madill3757a5a2014-08-26 13:16:36 -040020
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 Lang49be2ad2014-02-28 13:05:51 -050030}
31
32SampleApplication::~SampleApplication()
33{
34}
35
36bool SampleApplication::initialize()
37{
38 return true;
39}
40
41void SampleApplication::destroy()
42{
43}
44
45void SampleApplication::step(float dt, double totalTime)
46{
47}
48
49void SampleApplication::draw()
50{
51}
52
Geoff Lange977efc2014-03-04 11:43:04 -050053void SampleApplication::swap()
54{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040055 mEGLWindow->swap();
Geoff Lange977efc2014-03-04 11:43:04 -050056}
57
Jamie Madill1cfaaf82014-08-21 10:04:04 -040058OSWindow *SampleApplication::getWindow() const
Geoff Lang49be2ad2014-02-28 13:05:51 -050059{
Jamie Madill586666c2014-08-21 10:04:05 -040060 return mOSWindow.get();
Geoff Lang49be2ad2014-02-28 13:05:51 -050061}
62
Geoff Lange977efc2014-03-04 11:43:04 -050063EGLConfig SampleApplication::getConfig() const
64{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040065 return mEGLWindow->getConfig();
Geoff Lange977efc2014-03-04 11:43:04 -050066}
67
Geoff Langf0955f12014-06-20 16:07:07 -040068EGLDisplay SampleApplication::getDisplay() const
69{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040070 return mEGLWindow->getDisplay();
Geoff Langf0955f12014-06-20 16:07:07 -040071}
72
Geoff Lange977efc2014-03-04 11:43:04 -050073EGLSurface SampleApplication::getSurface() const
74{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040075 return mEGLWindow->getSurface();
Geoff Lange977efc2014-03-04 11:43:04 -050076}
77
78EGLContext SampleApplication::getContext() const
79{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040080 return mEGLWindow->getContext();
Geoff Lange977efc2014-03-04 11:43:04 -050081}
82
Geoff Lang49be2ad2014-02-28 13:05:51 -050083int SampleApplication::run()
84{
Corentin Wallezf3357ee2015-07-22 14:10:19 -040085 if (!mOSWindow->initialize(mName, mWidth, mHeight))
Jamie Madill96e46cd2014-04-07 15:51:15 -040086 {
87 return -1;
88 }
89
Jamie Madill4119ed32014-10-01 10:41:40 -040090 mOSWindow->setVisible(true);
91
Jamie Madill586666c2014-08-21 10:04:05 -040092 if (!mEGLWindow->initializeGL(mOSWindow.get()))
Geoff Lang49be2ad2014-02-28 13:05:51 -050093 {
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 Lange977efc2014-03-04 11:43:04 -0500133 swap();
Geoff Lang49be2ad2014-02-28 13:05:51 -0500134
Jamie Madill586666c2014-08-21 10:04:05 -0400135 mOSWindow->messageLoop();
Geoff Lang49be2ad2014-02-28 13:05:51 -0500136
137 prevTime = elapsedTime;
138 }
139
140 destroy();
Jamie Madill1cfaaf82014-08-21 10:04:04 -0400141 mEGLWindow->destroyGL();
Jamie Madill586666c2014-08-21 10:04:05 -0400142 mOSWindow->destroy();
Geoff Lang49be2ad2014-02-28 13:05:51 -0500143
144 return result;
145}
146
147void SampleApplication::exit()
148{
149 mRunning = false;
150}
151
152bool SampleApplication::popEvent(Event *event)
153{
Jamie Madill586666c2014-08-21 10:04:05 -0400154 return mOSWindow->popEvent(event);
Geoff Lang49be2ad2014-02-28 13:05:51 -0500155}