Repository navigation
Expand file tree
/
Copy pathButtonClickerNativeActivity.cpp
More file actions
746 lines (661 loc) · 25.4 KB
/
Copy pathButtonClickerNativeActivity.cpp
File metadata and controls
746 lines (661 loc) · 25.4 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Copyright (c) 2014 Google. All rights reserved.
// 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.
//
/*
* This file demonstrates,
* - How to use RTMP features in C++ code with gpg native client, including
* - Sign in to gpg service
* - How to handle RTMP callbacks
* - How to initiate RTMP matchs via several ways (QuickMatch, Invitation etc)
* - How to send reliable/unreliable packets to peers
* - Setup management UI and game UI
*/
/*
* Include files
*/
#include "ButtonClickerNativeActivity.h"
/*
* Initialize GooglePlayGameServices via gpg::GameServices::Builder
* In the build, it's setting several callbacks such as auth status changes,
* receiving invitations etc.
*/
void Engine::InitGooglePlayGameServices() {
if (service_ != nullptr) {
return;
}
gpg::AndroidInitialization::android_main(app_);
// Game Services have not been initialized, create a new Game Services.
gpg::AndroidPlatformConfiguration platform_configuration;
platform_configuration.SetActivity(app_->activity->clazz);
gpg::GameServices::Builder builder;
service_ =
builder.SetOnAuthActionStarted([this](gpg::AuthOperation op) {
// This callback is invoked when auth
// action started
// While auth action is going on, disable
// auth and related UI
OnAuthActionStarted(op);
})
.SetOnAuthActionFinished([this](gpg::AuthOperation op,
gpg::AuthStatus status) {
// This callback is invoked when auth action finished
// Check status code and update UI to signed-in state
OnAuthActionFinished(op, status);
})
.SetOnMultiplayerInvitationEvent([this](
gpg::MultiplayerEvent event, std::string match_id,
gpg::MultiplayerInvitation invitation) {
// Invoked when invitation has been received
// It can be received from the Play Game app, a notification, or
// live while the app is running.
// (e.g. Though PlayGam app, while the app is running)
LOGI("MultiplayerInvitationEvent callback");
if (event ==
gpg::MultiplayerEvent::UPDATED_FROM_APP_LAUNCH) {
// In this case, an invitation has been accepted already
// in notification or in Play game app
gpg::RealTimeMultiplayerManager::RealTimeRoomResponse result =
service_->RealTimeMultiplayer().AcceptInvitationBlocking(
invitation, this);
if (gpg::IsSuccess(result.status)) {
room_ = result.room;
service_->RealTimeMultiplayer().ShowWaitingRoomUI(
room_, MIN_PLAYERS,
[this](gpg::RealTimeMultiplayerManager::
WaitingRoomUIResponse const &waitResult) {
EnableUI(true);
if (gpg::IsSuccess(waitResult.status)) {
PlayGame();
}
});
} else {
LeaveGame();
}
} else {
// Otherwise, show default inbox and let players to accept an
// invitation
ShowRoomInbox();
}
})
.Create(platform_configuration);
}
/*
* Callback: Authentication action started
*
* gpg::AuthOperation op : SIGN_IN = 1, SIGN_OUT = 2
*
*/
void Engine::OnAuthActionStarted(gpg::AuthOperation op) {
if(!initialized_resources_) {
return;
}
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([this, op]() {
EnableUI(false);
if (op == gpg::AuthOperation::SIGN_IN) {
status_text_->SetAttribute("Text", "Signing In...");
} else {
status_text_->SetAttribute("Text", "Signing Out...");
}
});
}
/*
* Callback: Authentication action finishes
*
* gpg::AuthOperation op : SIGN_IN = 1, SIGN_OUT = 2
*
*/
void Engine::OnAuthActionFinished(gpg::AuthOperation op,
gpg::AuthStatus status) {
if (gpg::IsSuccess(status)) {
service_->Players().FetchSelf([this](
gpg::PlayerManager::FetchSelfResponse const &response) {
if (gpg::IsSuccess(response.status)) {
self_id_ = response.data.Id();
}
});
}
if(!initialized_resources_) {
return;
}
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([this, status]() {
EnableUI(true);
button_sign_in_->SetAttribute(
"Text", gpg::IsSuccess(status) ? "Sign Out" : "Sign In");
status_text_->SetAttribute(
"Text", gpg::IsSuccess(status) ? "Signed In" : "Signed Out");
});
}
/*
* Show room inbox
*/
void Engine::ShowRoomInbox() {
service_->RealTimeMultiplayer().ShowRoomInboxUI([this](
gpg::RealTimeMultiplayerManager::RoomInboxUIResponse const &response) {
if (gpg::IsSuccess(response.status)) {
gpg::RealTimeMultiplayerManager::RealTimeRoomResponse result =
service_->RealTimeMultiplayer().AcceptInvitationBlocking(
response.invitation, this);
if (gpg::IsSuccess(result.status)) {
room_ = result.room;
service_->RealTimeMultiplayer().ShowWaitingRoomUI(
room_, MIN_PLAYERS,
[this](
gpg::RealTimeMultiplayerManager::WaitingRoomUIResponse const &
waitResult) {
EnableUI(true);
if (gpg::IsSuccess(waitResult.status)) {
PlayGame();
}
});
} else
EnableUI(true); // Go back to original state for retry
} else {
LOGI("Invalid response status");
EnableUI(true); // Go back to original state for retry
}
});
EnableUI(false);
}
/*
* Quick match
* - Create a match with minimal setting and play the game
*/
void Engine::QuickMatch() {
gpg::RealTimeRoomConfig config =
gpg::RealTimeRoomConfig::Builder()
.SetMinimumAutomatchingPlayers(MIN_PLAYERS)
.SetMaximumAutomatchingPlayers(MAX_PLAYERS)
.Create();
service_->RealTimeMultiplayer().CreateRealTimeRoom(
config, this,
[this](gpg::RealTimeMultiplayerManager::RealTimeRoomResponse const &
response) {
LOGI("created a room %d", response.status);
if (gpg::IsSuccess(response.status)) {
room_ = response.room;
service_->RealTimeMultiplayer().ShowWaitingRoomUI(
room_, MIN_PLAYERS,
[this](
gpg::RealTimeMultiplayerManager::WaitingRoomUIResponse const &
waitResult) {
EnableUI(true);
if (gpg::IsSuccess(waitResult.status)) {
PlayGame();
}
});
} else
EnableUI(true); // Go back to original state for retry
});
EnableUI(false);
}
/*
* Invite friends
* - Show Player Select UI via ShowPlayerSelectUI,
* - When the UI is finished, create match and show game UI
*/
void Engine::InviteFriend() {
service_->RealTimeMultiplayer().ShowPlayerSelectUI(
MIN_PLAYERS, MAX_PLAYERS, true,
[this](gpg::RealTimeMultiplayerManager::PlayerSelectUIResponse const &
response) {
LOGI("inviting friends %d", response.status);
if (gpg::IsSuccess(response.status)) {
// Create room
gpg::RealTimeRoomConfig config =
gpg::RealTimeRoomConfig::Builder()
.PopulateFromPlayerSelectUIResponse(response)
.Create();
auto roomResponse =
service_->RealTimeMultiplayer().CreateRealTimeRoomBlocking(config,
this);
if (gpg::IsSuccess(roomResponse.status)) {
room_ = roomResponse.room;
service_->RealTimeMultiplayer().ShowWaitingRoomUI(
room_, MIN_PLAYERS,
[this](gpg::RealTimeMultiplayerManager::
WaitingRoomUIResponse const &waitResult) {
EnableUI(true);
if (gpg::IsSuccess(waitResult.status)) {
PlayGame();
}
});
} else
EnableUI(true); // Go back to original state for retry
} else
EnableUI(true); // Go back to original state for retry
});
EnableUI(false);
}
/*
* Broadcast my score to peers
*/
void Engine::BroadcastScore(bool bFinal) {
std::vector<uint8_t> v;
if (!bFinal) {
v.push_back('U');
v.push_back(static_cast<uint8_t>(score_counter_));
service_->RealTimeMultiplayer().SendUnreliableMessageToOthers(room_, v);
} else {
v.push_back('F');
v.push_back(static_cast<uint8_t>(score_counter_));
const std::vector<gpg::MultiplayerParticipant> participants =
room_.Participants();
for (gpg::MultiplayerParticipant participant : participants) {
service_->RealTimeMultiplayer().SendReliableMessage(
room_, participant, v, [](gpg::MultiplayerStatus const &) {});
}
}
}
/*
* Got message from peers
* room : The room which from_participant is in.
* from_participant : The participant who sent the data.
* data : The data which was received.
* is_reliable : Whether the data was sent using the unreliable or
* reliable mechanism.
* In this app, packet format is defined as:
* 1 byte: indicate score type 'F': final score 'U' updating score
* 1 byte: score
*/
void Engine::OnDataReceived(gpg::RealTimeRoom const &room,
gpg::MultiplayerParticipant const &from_participant,
std::vector<uint8_t> data, bool is_reliable) {
if (data[0] == 'F' && is_reliable) {
// Got final score
players_score_[from_participant.Id()].score = data[1];
players_score_[from_participant.Id()].finished = true;
LOGI("Got final data from name:%s ID:%s",
from_participant.DisplayName().c_str(), from_participant.Id().c_str());
} else if (data[0] == 'U' && !is_reliable) {
// Got current score
uint8_t score = players_score_[from_participant.Id()].score;
players_score_[from_participant.Id()].score = std::max(score, data[1]);
LOGI("Got data from name:%s ID:%s",
from_participant.DisplayName().c_str(), from_participant.Id().c_str());
}
UpdateScore();
}
/*
* Room status change callback
*/
void Engine::OnRoomStatusChanged(gpg::RealTimeRoom const &room) {
room_ = room;
}
/*
* Invoked when participant status changed
*/
void Engine::OnParticipantStatusChanged(
gpg::RealTimeRoom const &room,
gpg::MultiplayerParticipant const &participant) {
// Update participant status
LOGI("Participant %s status changed: %d", participant.Id().c_str(),
participant.Status());
if (participant.Status() != gpg::ParticipantStatus::JOINED) {
{
std::lock_guard<std::mutex> lock(mutex_);
if (players_score_.find(participant.Id()) != players_score_.end()) {
players_score_[participant.Id()].finished = true;
}
}
UpdateScore();
}
}
/*
* Play games UI that is in your turn
*/
void Engine::PlayGame() {
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([this]() {
LOGI("Playing match");
if (dialog_) delete dialog_;
// Start game
InitializeGame();
//
// Using jui_helper, a support library, to create and bind gameplay buttons.
//
dialog_ = new jui_helper::JUIDialog(app_->activity);
// Setting up labels
time_text_ = new jui_helper::JUITextView("0:00");
time_text_->AddRule(jui_helper::LAYOUT_PARAMETER_ALIGN_PARENT_TOP,
jui_helper::LAYOUT_PARAMETER_TRUE);
time_text_->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_IN_PARENT,
jui_helper::LAYOUT_PARAMETER_TRUE);
time_text_->SetAttribute("TextSize", jui_helper::ATTRIBUTE_UNIT_SP, 18.f);
time_text_->SetAttribute("Padding", 10, 10, 10, 10);
my_score_text_ = new jui_helper::JUITextView("000");
my_score_text_->AddRule(jui_helper::LAYOUT_PARAMETER_BELOW, time_text_);
my_score_text_->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_IN_PARENT,
jui_helper::LAYOUT_PARAMETER_TRUE);
my_score_text_->SetAttribute("TextSize", jui_helper::ATTRIBUTE_UNIT_SP,
24.f);
my_score_text_->SetAttribute("Padding", 10, 10, 10, 10);
// Take Turn Button
button_play_ = new jui_helper::JUIButton("Click me!");
button_play_->SetCallback([this](jui_helper::JUIView *view,
const int32_t message) {
switch (message) {
case jui_helper::JUICALLBACK_BUTTON_UP: {
if (!playing_) return;
score_counter_++;
UpdateScore();
UpdateTime();
// Broadcast my score to others via unreliable protocol
BroadcastScore(false);
}
}
});
button_play_->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_IN_PARENT,
jui_helper::LAYOUT_PARAMETER_TRUE);
button_play_->AddRule(jui_helper::LAYOUT_PARAMETER_BELOW, my_score_text_);
const int32_t labelWidth = 600;
const int32_t labelHeight = 300;
scores_text_ = new jui_helper::JUITextView("0:00");
scores_text_->AddRule(jui_helper::LAYOUT_PARAMETER_BELOW, button_play_);
scores_text_->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_IN_PARENT,
jui_helper::LAYOUT_PARAMETER_TRUE);
scores_text_->SetAttribute("TextSize", jui_helper::ATTRIBUTE_UNIT_SP, 18.f);
scores_text_->SetAttribute("MinimumWidth", labelWidth);
scores_text_->SetAttribute("MinimumHeight", labelHeight);
scores_text_->SetAttribute("Padding", 10, 10, 10, 10);
UpdateScore();
dialog_->AddView(my_score_text_);
dialog_->AddView(button_play_);
dialog_->AddView(time_text_);
dialog_->AddView(scores_text_);
dialog_->SetAttribute("Title", "Click the button");
dialog_->SetCallback(
jui_helper::JUICALLBACK_DIALOG_DISMISSED,
[this](jui_helper::JUIDialog *dialog, const int32_t message) {
LOGI("Dialog dismissed");
LeaveGame();
dialog_ = nullptr;
});
dialog_->Show();
LOGI("Showing dialog");
//
// Invoke time counter periodically
//
std::thread([this]() {
ndk_helper::JNIHelper &helper =
*ndk_helper::JNIHelper::GetInstance();
helper.AttachCurrentThread();
while (UpdateTime()) {
std::chrono::milliseconds d(100);
std::this_thread::sleep_for(d);
}
// Broadcast my score to others via reliable protocol
BroadcastScore(true);
UpdateScore();
helper.DetachCurrentThread();
}).detach();
});
}
/*
* Update game UI when some player's score is updated
*/
void Engine::UpdateScore() {
// Lock mutex since this one can be called from multiple thread,
// gpg callback tread and UI callback thread
std::lock_guard<std::mutex> lock(mutex_);
size_t SIZE = 64;
char str[SIZE];
snprintf(str, SIZE, "%03d", score_counter_);
std::string str_myscore(str);
snprintf(str, SIZE, "My score: %03d %s\n", score_counter_,
playing_ ? "" : "*");
std::string allstr(str);
// Append other player
std::vector<gpg::MultiplayerParticipant> participants = room_.Participants();
for (gpg::MultiplayerParticipant participant : participants) {
LOGI("Participant name:%s ID:%s", participant.DisplayName().c_str(),
participant.Id().c_str());
if (participant.HasPlayer())
LOGI("self:%s PlayerID:%s", self_id_.c_str(),
participant.Player().Id().c_str());
if (participant.HasPlayer() &&
participant.Player().Id().compare(self_id_) == 0)
continue; // Skip local player
int32_t score = 0;
bool finished = false;
if (players_score_.find(participant.Id()) != players_score_.end()) {
score = players_score_[participant.Id()].score;
finished = players_score_[participant.Id()].finished;
}
LOGI("Status %d", participant.Status());
snprintf(str, SIZE, "%s: %03d %s\n", participant.DisplayName().c_str(),
score, finished ? "*" : "");
allstr += str;
}
// Update game UI, UI update needs to be performed in UI thread
ndk_helper::JNIHelper::GetInstance()
->RunOnUiThread([this, str_myscore, allstr]() {
my_score_text_->SetAttribute("Text", str_myscore.c_str());
scores_text_->SetAttribute("Text", allstr.c_str());
});
}
/*
* Update game timer and game UI
*/
bool Engine::UpdateTime() {
// UpdateTime() is invoked from other thread asynchronously
// So need to Lock mutex
std::lock_guard<std::mutex> lock(mutex_);
if (!playing_) return false;
double current_time = monitor_.GetCurrentTime();
current_time -= start_time_ + 1.0;
if (current_time >= GAME_DURATION) {
// finish game
playing_ = false;
current_time = GAME_DURATION;
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([this]() {
button_play_->SetAttribute("Enabled", false);
});
}
// Update game UI, UI update needs to be performed in UI thread
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([this, current_time]() {
size_t SIZE = 64;
char str[SIZE];
snprintf(str, SIZE, "0:%02.0f", GAME_DURATION - current_time);
time_text_->SetAttribute("Text", const_cast<const char *>(str));
});
return true;
}
/*
* Initialize game state
*/
void Engine::InitializeGame() {
// Lock mutex
std::lock_guard<std::mutex> lock(mutex_);
playing_ = true;
start_time_ = monitor_.GetCurrentTime();
score_counter_ = 0;
players_score_.clear();
}
/*
* Leave game
*/
void Engine::LeaveGame() {
// Lock mutex
std::lock_guard<std::mutex> lock(mutex_);
service_->RealTimeMultiplayer().LeaveRoom(
room_, [](gpg::ResponseStatus const &status) {});
LOGI("Game is over");
playing_ = false;
}
/*
* Initialize game management UI,
* invoking jui_helper functions to create java UIs
*/
void Engine::InitUI() {
// The window initialization
jui_helper::JUIWindow::Init(app_->activity, JUIHELPER_CLASS_NAME);
// Show toast with app label
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([]() {
if(NULL == jui_helper::JUIWindow::GetInstance()->GetContext()) {
return;
}
jui_helper::JUIToast toast(
ndk_helper::JNIHelper::GetInstance()->GetAppLabel());
toast.Show();
});
//
// Using jui_helper, a support library, to create and bind game management
// UIs.
//
//
// Buttons
//
// Sign in button.
button_sign_in_ = new jui_helper::JUIButton(
service_->IsAuthorized() ? "Sign Out" : "Sign In");
SetParameters(button_sign_in_, nullptr);
button_sign_in_->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_IN_PARENT,
jui_helper::LAYOUT_PARAMETER_TRUE);
button_sign_in_->SetCallback([this](jui_helper::JUIView *view,
const int32_t message) {
if (message == jui_helper::JUICALLBACK_BUTTON_UP) {
if (service_->IsAuthorized()) {
service_->SignOut();
} else {
service_->StartAuthorizationUI();
}
}
});
button_sign_in_->SetMargins(0, 0, 0, 50);
jui_helper::JUIWindow::GetInstance()->AddView(button_sign_in_);
button_quick_match_ = new jui_helper::JUIButton("Quick match");
SetParameters(button_quick_match_, button_sign_in_);
button_quick_match_->SetCallback([this](jui_helper::JUIView *view,
const int32_t message) {
if (message == jui_helper::JUICALLBACK_BUTTON_UP) {
QuickMatch();
}
});
jui_helper::JUIWindow::GetInstance()->AddView(button_quick_match_);
button_invite_ = new jui_helper::JUIButton("Invite Friends!");
SetParameters(button_invite_, button_quick_match_);
button_invite_->SetCallback([this](jui_helper::JUIView *view,
const int32_t message) {
if (message == jui_helper::JUICALLBACK_BUTTON_UP) {
InviteFriend();
}
});
jui_helper::JUIWindow::GetInstance()->AddView(button_invite_);
button_matches_ = new jui_helper::JUIButton("All my invitations");
SetParameters(button_matches_, button_invite_);
button_matches_->SetCallback([this](jui_helper::JUIView *view,
const int32_t message) {
if (message == jui_helper::JUICALLBACK_BUTTON_UP) {
ShowRoomInbox();
}
});
jui_helper::JUIWindow::GetInstance()->AddView(button_matches_);
status_text_ = new jui_helper::JUITextView(
service_->IsAuthorized() ? "Signed In." : "Signed Out.");
status_text_->AddRule(jui_helper::LAYOUT_PARAMETER_ALIGN_PARENT_BOTTOM,
jui_helper::LAYOUT_PARAMETER_TRUE);
status_text_->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_IN_PARENT,
jui_helper::LAYOUT_PARAMETER_TRUE);
jui_helper::JUIWindow::GetInstance()->AddView(status_text_);
textViewFPS_ = new jui_helper::JUITextView("0.0FPS");
textViewFPS_->SetAttribute("Gravity", jui_helper::ATTRIBUTE_GRAVITY_LEFT);
textViewFPS_->SetAttribute("TextColor", 0xffffffff);
textViewFPS_->SetAttribute("TextSize", jui_helper::ATTRIBUTE_UNIT_SP, 18.f);
textViewFPS_->SetAttribute("Padding", 10, 10, 10, 10);
jui_helper::JUIWindow::GetInstance()->AddView(textViewFPS_);
// Init play game services
InitGooglePlayGameServices();
return;
}
/*
* Set common parameters to button
*/
void Engine::SetParameters(jui_helper::JUIButton *button,
jui_helper::JUIView *below) {
const int32_t buttonWidth = 600;
button->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_IN_PARENT,
jui_helper::LAYOUT_PARAMETER_TRUE);
button->SetAttribute("MinimumWidth", buttonWidth);
if (below != nullptr)
button->AddRule(jui_helper::LAYOUT_PARAMETER_BELOW, below);
}
/*
* Enable/Disable management UI
*/
void Engine::EnableUI(bool enable) {
ndk_helper::JNIHelper::GetInstance()->RunOnUiThread([this, enable]() {
button_sign_in_->SetAttribute("Enabled", enable);
bool b = enable;
if (service_->IsAuthorized() == false) b = false;
button_quick_match_->SetAttribute("Enabled", b);
button_invite_->SetAttribute("Enabled", b);
button_matches_->SetAttribute("Enabled", b);
});
}
/*
* JNI functions those manage activity lifecycle
*/
extern "C" {
/*
* These callbacks are necessary to work Google Play Game Services UIs properly
*
* For apps which target Android 2.3 or 3.x devices (API Version prior to 14),
* Play Game Services has no way to automatically receive Activity lifecycle
* callbacks. In these cases, Play Game Services relies on the owning Activity
* to notify it of lifecycle events. Any Activity which owns a GameServices
* object should call the AndroidSupport::* functions from within their own
* lifecycle callback functions. The arguments in these functions match those
* provided by Android, so no additional processing is necessary.
*/
JNIEXPORT void
Java_com_google_example_games_ButtonClicker_ButtonClickerNativeActivity_nativeOnActivityResult(
JNIEnv *env, jobject thiz, jobject activity, jint requestCode,
jint resultCode, jobject data) {
gpg::AndroidSupport::OnActivityResult(env, activity, requestCode, resultCode,
data);
}
JNIEXPORT void
Java_com_google_example_games_ButtonClicker_ButtonClickerNativeActivity_nativeOnActivityCreated(
JNIEnv *env, jobject thiz, jobject activity, jobject saved_instance_state) {
gpg::AndroidSupport::OnActivityCreated(env, activity, saved_instance_state);
}
JNIEXPORT void
Java_com_google_example_games_ButtonClicker_ButtonClickerNativeActivity_nativeOnActivityDestroyed(
JNIEnv *env, jobject thiz, jobject activity) {
gpg::AndroidSupport::OnActivityDestroyed(env, activity);
}
JNIEXPORT void
Java_com_google_example_games_ButtonClicker_ButtonClickerNativeActivity_nativeOnActivityPaused(
JNIEnv *env, jobject thiz, jobject activity) {
gpg::AndroidSupport::OnActivityPaused(env, activity);
}
JNIEXPORT void
Java_com_google_example_games_ButtonClicker_ButtonClickerNativeActivity_nativeOnActivityResumed(
JNIEnv *env, jobject thiz, jobject activity) {
gpg::AndroidSupport::OnActivityResumed(env, activity);
}
JNIEXPORT void
Java_com_google_example_games_ButtonClicker_ButtonClickerNativeActivity_nativeOnActivitySaveInstanceState(
JNIEnv *env, jobject thiz, jobject activity, jobject out_state) {
gpg::AndroidSupport::OnActivitySaveInstanceState(env, activity, out_state);
}
JNIEXPORT void
Java_com_google_example_games_ButtonClicker_ButtonClickerNativeActivity_nativeOnActivityStarted(
JNIEnv *env, jobject thiz, jobject activity) {
gpg::AndroidSupport::OnActivityStarted(env, activity);
}
JNIEXPORT void
Java_com_google_example_games_ButtonClicker_ButtonClickerNativeActivity_nativeOnActivityStopped(
JNIEnv *env, jobject thiz, jobject activity) {
gpg::AndroidSupport::OnActivityStopped(env, activity);
}
}