xref: /aosp_15_r20/external/oboe/samples/RhythmGame/src/main/cpp/native-lib.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <jni.h>
17 #include <memory>
18 
19 #include <android/asset_manager_jni.h>
20 
21 #include "utils/logging.h"
22 #include "Game.h"
23 
24 
25 extern "C" {
26 
27 std::unique_ptr<Game> game;
28 
29 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_rhythmgame_MainActivity_native_1onStart(JNIEnv * env,jobject instance,jobject jAssetManager)30 Java_com_google_oboe_samples_rhythmgame_MainActivity_native_1onStart(JNIEnv *env, jobject instance,
31                                                                      jobject jAssetManager) {
32 
33     AAssetManager *assetManager = AAssetManager_fromJava(env, jAssetManager);
34     if (assetManager == nullptr) {
35         LOGE("Could not obtain the AAssetManager");
36         return;
37     }
38 
39     game = std::make_unique<Game>(*assetManager);
40     game->start();
41 }
42 
43 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_rhythmgame_RendererWrapper_native_1onSurfaceCreated(JNIEnv * env,jobject instance)44 Java_com_google_oboe_samples_rhythmgame_RendererWrapper_native_1onSurfaceCreated(JNIEnv *env,
45                                                                                 jobject instance) {
46     game->onSurfaceCreated();
47 }
48 
49 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_rhythmgame_RendererWrapper_native_1onSurfaceChanged(JNIEnv * env,jclass type,jint width,jint height)50 Java_com_google_oboe_samples_rhythmgame_RendererWrapper_native_1onSurfaceChanged(JNIEnv *env,
51                                                                                 jclass type,
52                                                                                 jint width,
53                                                                                 jint height) {
54     game->onSurfaceChanged(width, height);
55 }
56 
57 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_rhythmgame_RendererWrapper_native_1onDrawFrame(JNIEnv * env,jclass type)58 Java_com_google_oboe_samples_rhythmgame_RendererWrapper_native_1onDrawFrame(JNIEnv *env,
59                                                                            jclass type) {
60     game->tick();
61 }
62 
63 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_rhythmgame_GameSurfaceView_native_1onTouchInput(JNIEnv * env,jclass type,jint event_type,jlong time_since_boot_ms,jint pixel_x,jint pixel_y)64 Java_com_google_oboe_samples_rhythmgame_GameSurfaceView_native_1onTouchInput(JNIEnv *env,
65                                                                             jclass type,
66                                                                             jint event_type,
67                                                                             jlong time_since_boot_ms,
68                                                                             jint pixel_x,
69                                                                             jint pixel_y) {
70     game->tap(time_since_boot_ms);
71 }
72 
73 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_rhythmgame_GameSurfaceView_native_1surfaceDestroyed__(JNIEnv * env,jclass type)74 Java_com_google_oboe_samples_rhythmgame_GameSurfaceView_native_1surfaceDestroyed__(JNIEnv *env,
75                                                                                   jclass type) {
76     game->onSurfaceDestroyed();
77 }
78 
79 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_rhythmgame_MainActivity_native_1onStop(JNIEnv * env,jobject instance)80 Java_com_google_oboe_samples_rhythmgame_MainActivity_native_1onStop(JNIEnv *env, jobject instance) {
81 
82     game->stop();
83 }
84 
85 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_rhythmgame_MainActivity_native_1setDefaultStreamValues(JNIEnv * env,jclass type,jint sampleRate,jint framesPerBurst)86 Java_com_google_oboe_samples_rhythmgame_MainActivity_native_1setDefaultStreamValues(JNIEnv *env,
87                                                                                   jclass type,
88                                                                                   jint sampleRate,
89                                                                                   jint framesPerBurst) {
90     oboe::DefaultStreamValues::SampleRate = (int32_t) sampleRate;
91     oboe::DefaultStreamValues::FramesPerBurst = (int32_t) framesPerBurst;
92 }
93 } // extern "C"
94