blob: 268df643341bfb9c68f1af22801083bf61db9ffb [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),
Jamie Madill586666c2014-08-21 10:04:05 -040013 mRunning(false)
Geoff Lang49be2ad2014-02-28 13:05:51 -050014{
Geoff Lang0d3683c2014-10-23 11:08:16 -040015 mEGLWindow.reset(new EGLWindow(width, height, glesMajorVersion, EGLPlatformParameters(requestedRenderer)));
Jamie Madill586666c2014-08-21 10:04:05 -040016 mTimer.reset(CreateTimer());
17 mOSWindow.reset(CreateOSWindow());
Jamie Madill3757a5a2014-08-26 13:16:36 -040018
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 Lang49be2ad2014-02-28 13:05:51 -050028}
29
30SampleApplication::~SampleApplication()
31{
32}
33
34bool SampleApplication::initialize()
35{
36 return true;
37}
38
39void SampleApplication::destroy()
40{
41}
42
43void SampleApplication::step(float dt, double totalTime)
44{
45}
46
47void SampleApplication::draw()
48{
49}
50
Geoff Lange977efc2014-03-04 11:43:04 -050051void SampleApplication::swap()
52{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040053 mEGLWindow->swap();
Geoff Lange977efc2014-03-04 11:43:04 -050054}
55
Jamie Madill1cfaaf82014-08-21 10:04:04 -040056OSWindow *SampleApplication::getWindow() const
Geoff Lang49be2ad2014-02-28 13:05:51 -050057{
Jamie Madill586666c2014-08-21 10:04:05 -040058 return mOSWindow.get();
Geoff Lang49be2ad2014-02-28 13:05:51 -050059}
60
Geoff Lange977efc2014-03-04 11:43:04 -050061EGLConfig SampleApplication::getConfig() const
62{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040063 return mEGLWindow->getConfig();
Geoff Lange977efc2014-03-04 11:43:04 -050064}
65
Geoff Langf0955f12014-06-20 16:07:07 -040066EGLDisplay SampleApplication::getDisplay() const
67{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040068 return mEGLWindow->getDisplay();
Geoff Langf0955f12014-06-20 16:07:07 -040069}
70
Geoff Lange977efc2014-03-04 11:43:04 -050071EGLSurface SampleApplication::getSurface() const
72{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040073 return mEGLWindow->getSurface();
Geoff Lange977efc2014-03-04 11:43:04 -050074}
75
76EGLContext SampleApplication::getContext() const
77{
Jamie Madill1cfaaf82014-08-21 10:04:04 -040078 return mEGLWindow->getContext();
Geoff Lange977efc2014-03-04 11:43:04 -050079}
80
Geoff Lang49be2ad2014-02-28 13:05:51 -050081int SampleApplication::run()
82{
Jamie Madill586666c2014-08-21 10:04:05 -040083 if (!mOSWindow->initialize(mName, mEGLWindow->getWidth(), mEGLWindow->getHeight()))
Jamie Madill96e46cd2014-04-07 15:51:15 -040084 {
85 return -1;
86 }
87
Jamie Madill4119ed32014-10-01 10:41:40 -040088 mOSWindow->setVisible(true);
89
Jamie Madill586666c2014-08-21 10:04:05 -040090 if (!mEGLWindow->initializeGL(mOSWindow.get()))
Geoff Lang49be2ad2014-02-28 13:05:51 -050091 {
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 Lange977efc2014-03-04 11:43:04 -0500131 swap();
Geoff Lang49be2ad2014-02-28 13:05:51 -0500132
Jamie Madill586666c2014-08-21 10:04:05 -0400133 mOSWindow->messageLoop();
Geoff Lang49be2ad2014-02-28 13:05:51 -0500134
135 prevTime = elapsedTime;
136 }
137
138 destroy();
Jamie Madill1cfaaf82014-08-21 10:04:04 -0400139 mEGLWindow->destroyGL();
Jamie Madill586666c2014-08-21 10:04:05 -0400140 mOSWindow->destroy();
Geoff Lang49be2ad2014-02-28 13:05:51 -0500141
142 return result;
143}
144
145void SampleApplication::exit()
146{
147 mRunning = false;
148}
149
150bool SampleApplication::popEvent(Event *event)
151{
Jamie Madill586666c2014-08-21 10:04:05 -0400152 return mOSWindow->popEvent(event);
Geoff Lang49be2ad2014-02-28 13:05:51 -0500153}