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"
8#include "EGLWindow.h"
9
10SampleApplication::SampleApplication(const std::string& name, size_t width, size_t height,
11                                     EGLint glesMajorVersion, EGLint requestedRenderer)
12    : mName(name),
13      mRunning(false)
14{
15    mEGLWindow.reset(new EGLWindow(width, height, glesMajorVersion, requestedRenderer));
16    mTimer.reset(CreateTimer());
17    mOSWindow.reset(CreateOSWindow());
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);
28}
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
51void SampleApplication::swap()
52{
53    mEGLWindow->swap();
54}
55
56OSWindow *SampleApplication::getWindow() const
57{
58    return mOSWindow.get();
59}
60
61EGLConfig SampleApplication::getConfig() const
62{
63    return mEGLWindow->getConfig();
64}
65
66EGLDisplay SampleApplication::getDisplay() const
67{
68    return mEGLWindow->getDisplay();
69}
70
71EGLSurface SampleApplication::getSurface() const
72{
73    return mEGLWindow->getSurface();
74}
75
76EGLContext SampleApplication::getContext() const
77{
78    return mEGLWindow->getContext();
79}
80
81int SampleApplication::run()
82{
83    if (!mOSWindow->initialize(mName, mEGLWindow->getWidth(), mEGLWindow->getHeight()))
84    {
85        return -1;
86    }
87
88    if (!mEGLWindow->initializeGL(mOSWindow.get()))
89    {
90        return -1;
91    }
92
93    mRunning = true;
94    int result = 0;
95
96    if (!initialize())
97    {
98        mRunning = false;
99        result = -1;
100    }
101
102    mTimer->start();
103    double prevTime = 0.0;
104
105    while (mRunning)
106    {
107        double elapsedTime = mTimer->getElapsedTime();
108        double deltaTime = elapsedTime - prevTime;
109
110        step(static_cast<float>(deltaTime), elapsedTime);
111
112        // Clear events that the application did not process from this frame
113        Event event;
114        while (popEvent(&event))
115        {
116            // If the application did not catch a close event, close now
117            if (event.Type == Event::EVENT_CLOSED)
118            {
119                exit();
120            }
121        }
122
123        if (!mRunning)
124        {
125            break;
126        }
127
128        draw();
129        swap();
130
131        mOSWindow->messageLoop();
132
133        prevTime = elapsedTime;
134    }
135
136    destroy();
137    mEGLWindow->destroyGL();
138    mOSWindow->destroy();
139
140    return result;
141}
142
143void SampleApplication::exit()
144{
145    mRunning = false;
146}
147
148bool SampleApplication::popEvent(Event *event)
149{
150    return mOSWindow->popEvent(event);
151}
152