aboutsummaryrefslogtreecommitdiff
path: root/apps/sample_app/src/cpp/main.cpp
blob: e73dd885624e263c74863f2e62c7876787e70080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "agq.h"
#include "circle.h"
#include "common.h"
#include "renderer.h"
#include "vecmath.h"

#include <cassert>
#include <chrono>
#include <cinttypes>
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <initializer_list>
#include <memory>
#include <sys/time.h>
#include <vector>

#include <EGL/egl.h>
#include <EGL/eglext.h>

#include <android/sensor.h>
#include <android/log.h>
#include <android_native_app_glue.h>

using namespace std::chrono_literals;
using namespace android::gamecore;

namespace {

int animating = 0;

/**
 * Process the next input event.
 */
int32_t engine_handle_input(struct android_app*, AInputEvent* event) {
    if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
        animating = 1;
        return 1;
    }
    return 0;
}

/**
 * Process the next main command.
 */
void engine_handle_cmd(struct android_app* app, int32_t cmd) {
    //struct engine* engine = (struct engine*)app->userData;
    Renderer* renderer = reinterpret_cast<Renderer*>(app->userData);

    switch (cmd) {
        case APP_CMD_SAVE_STATE:
            // We are not saving the state.
            break;
        case APP_CMD_INIT_WINDOW:
            // The window is being shown, get it ready.
            if (app->window != NULL) {
                renderer->initDisplay(app->window);
                renderer->draw();
                animating = 1;
            }
            break;
        case APP_CMD_TERM_WINDOW:
            // The window is being hidden or closed, clean it up.
            //engine_term_display(engine);
            renderer->terminateDisplay();
            animating = 0;
            break;
        case APP_CMD_LOST_FOCUS:
            // Also stop animating.
            animating = 0;
            renderer->draw();
            break;
        default:
            break;
    }
}

} // end of anonymous namespace

/**
 * This is the main entry point of a native application that is using
 * android_native_app_glue.  It runs in its own thread, with its own
 * event loop for receiving input events and doing other things.
 */
void android_main(struct android_app* state) {
    std::srand(0);

    LOGI("Running with SDK %d", state->activity->sdkVersion);

    std::unique_ptr<Renderer> renderer(new Renderer(1));
    state->userData = renderer.get();
    state->onAppCmd = engine_handle_cmd;
    state->onInputEvent = engine_handle_input;

    // loop waiting for stuff to do.
    while (1) {
        // Read all pending events.
        int events;
        struct android_poll_source* source;

        // If not animating, we will block forever waiting for events.
        // If animating, we loop until all events are read, then continue
        // to draw the next frame of animation.
        while (ALooper_pollOnce(animating ? 0 : -1, NULL, &events, (void**)&source) >= 0) {

            // Process this event.
            if (source != NULL) {
                source->process(state, source);
            }

            // Check if we are exiting.
            if (state->destroyRequested != 0) {
                renderer->terminateDisplay();
                return;
            }
        }

        if (animating) {
            renderer->update();

            // Drawing is throttled to the screen update rate, so there
            // is no need to do timing here.
            renderer->draw();

            // Broadcast intent every 5 seconds.
            static auto last_timestamp = std::chrono::steady_clock::now();
            auto now = std::chrono::steady_clock::now();
            if (now - last_timestamp >= std::chrono::seconds(5)) {
                last_timestamp = now;
                android::GameQualification qualification;
                qualification.startLoop(state->activity);
            }
        }
    }
}