Skip to content

Commit 0c3a850

Browse files
committed
update sdl3 to java (todo use fixed version!) + added buttins
1 parent 8ab585c commit 0c3a850

File tree

3 files changed

+98
-28
lines changed

3 files changed

+98
-28
lines changed

android-project/app/src/main/java/org/libsdl/app/SDLActivity.java

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.os.Build;
2424
import android.os.Bundle;
2525
import android.os.Handler;
26+
import android.os.LocaleList;
2627
import android.os.Message;
2728
import android.os.ParcelFileDescriptor;
2829
import android.util.DisplayMetrics;
@@ -60,7 +61,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
6061
private static final String TAG = "SDL";
6162
private static final int SDL_MAJOR_VERSION = 3;
6263
private static final int SDL_MINOR_VERSION = 2;
63-
private static final int SDL_MICRO_VERSION = 14;
64+
private static final int SDL_MICRO_VERSION = 24;
6465
/*
6566
// Display InputType.SOURCE/CLASS of events and devices
6667
//
@@ -2116,6 +2117,44 @@ static class SDLFileDialogState {
21162117
int requestCode;
21172118
boolean multipleChoice;
21182119
}
2120+
2121+
/**
2122+
* This method is called by SDL using JNI.
2123+
*/
2124+
public static String getPreferredLocales() {
2125+
String result = "";
2126+
if (Build.VERSION.SDK_INT >= 24 /* Android 7 (N) */) {
2127+
LocaleList locales = LocaleList.getAdjustedDefault();
2128+
for (int i = 0; i < locales.size(); i++) {
2129+
if (i != 0) result += ",";
2130+
result += formatLocale(locales.get(i));
2131+
}
2132+
} else if (mCurrentLocale != null) {
2133+
result = formatLocale(mCurrentLocale);
2134+
}
2135+
return result;
2136+
}
2137+
2138+
public static String formatLocale(Locale locale) {
2139+
String result = "";
2140+
String lang = "";
2141+
if (locale.getLanguage() == "in") {
2142+
// Indonesian is "id" according to ISO 639.2, but on Android is "in" because of Java backwards compatibility
2143+
lang = "id";
2144+
} else if (locale.getLanguage() == "") {
2145+
// Make sure language is never empty
2146+
lang = "und";
2147+
} else {
2148+
lang = locale.getLanguage();
2149+
}
2150+
2151+
if (locale.getCountry() == "") {
2152+
result = lang;
2153+
} else {
2154+
result = lang + "_" + locale.getCountry();
2155+
}
2156+
return result;
2157+
}
21192158
}
21202159

21212160
/**
@@ -2157,7 +2196,11 @@ class SDLClipboardHandler implements
21572196
}
21582197

21592198
public boolean clipboardHasText() {
2160-
return mClipMgr.hasPrimaryClip();
2199+
if (Build.VERSION.SDK_INT >= 28 /* Android 9 (P) */) {
2200+
return mClipMgr.hasPrimaryClip();
2201+
} else {
2202+
return mClipMgr.hasText();
2203+
}
21612204
}
21622205

21632206
public String clipboardGetText() {
@@ -2175,10 +2218,19 @@ public String clipboardGetText() {
21752218
}
21762219

21772220
public void clipboardSetText(String string) {
2178-
mClipMgr.removePrimaryClipChangedListener(this);
2179-
ClipData clip = ClipData.newPlainText(null, string);
2180-
mClipMgr.setPrimaryClip(clip);
2181-
mClipMgr.addPrimaryClipChangedListener(this);
2221+
mClipMgr.removePrimaryClipChangedListener(this);
2222+
if (string.isEmpty()) {
2223+
if (Build.VERSION.SDK_INT >= 28 /* Android 9 (P) */) {
2224+
mClipMgr.clearPrimaryClip();
2225+
} else {
2226+
ClipData clip = ClipData.newPlainText(null, "");
2227+
mClipMgr.setPrimaryClip(clip);
2228+
}
2229+
} else {
2230+
ClipData clip = ClipData.newPlainText(null, string);
2231+
mClipMgr.setPrimaryClip(clip);
2232+
}
2233+
mClipMgr.addPrimaryClipChangedListener(this);
21822234
}
21832235

21842236
@Override

cmake/cpm/sdl3-config.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cpmaddpackage(
66
GITHUB_REPOSITORY
77
libsdl-org/SDL
88
GIT_TAG
9-
a96677bdf6b4acb84af4ec294e5f60a4e8cbbe03
9+
a8589a84226a6202831a3d49ff4edda4acab9acd
1010
SYSTEM
1111
ON
1212
GIT_SHALLOW

src/sdl_main.cpp

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
11
#include <SDL3/SDL.h>
22
#include <SDL3/SDL_main.h>
3+
#include <SDL3/SDL_messagebox.h>
34

45
#include <cstdlib>
56

6-
// avoid SDL_main unresolved externals
77
int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
88
{
9-
if (!SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO))
10-
{
11-
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
12-
"SDL_Init failed: %s",
13-
SDL_GetError());
9+
if (!SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO)) {
10+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed: %s", SDL_GetError());
1411
return EXIT_FAILURE;
1512
}
1613

17-
struct SDLGuard final
18-
{
19-
~SDLGuard() { SDL_Quit(); }
20-
} sdl_guard;
21-
22-
if (SDL_ShowSimpleMessageBox(
23-
SDL_MESSAGEBOX_INFORMATION,
24-
"Hello World",
25-
"!! Your SDL project successfully runs on Android !!",
26-
nullptr) != 0) // Prefer nullptr over NULL
27-
{
28-
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
29-
"Message box failed: %s",
30-
SDL_GetError());
14+
struct sdl_guard final { ~sdl_guard() { SDL_Quit(); } } sdl_guard;
15+
16+
const SDL_MessageBoxButtonData buttons[] = {
17+
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "Info" },
18+
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Exit" },
19+
};
20+
21+
const SDL_MessageBoxData box = {
22+
SDL_MESSAGEBOX_INFORMATION, // flags
23+
nullptr, // parent window
24+
"Hello World", // title
25+
"!! Your SDL project successfully runs on Android !!", // message
26+
(int)(sizeof(buttons) / sizeof(buttons[0])), // num buttons
27+
buttons, // buttons
28+
nullptr // color scheme
29+
};
30+
31+
int button_id = -1;
32+
if (!SDL_ShowMessageBox(&box, &button_id)) {
33+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ShowMessageBox failed: %s", SDL_GetError());
3134
return EXIT_FAILURE;
3235
}
3336

37+
if (button_id == 1) {
38+
// Minimal “about me” info; tweak as needed
39+
if (!SDL_ShowSimpleMessageBox(
40+
SDL_MESSAGEBOX_INFORMATION,
41+
"About",
42+
"Minimalist system architect.\n"
43+
"- C/C++/Python\n"
44+
"- alt linux, bash\n"
45+
"- snake_case, dir-name, less code fewer bugs\n",
46+
nullptr)) {
47+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Info box failed: %s", SDL_GetError());
48+
return EXIT_FAILURE;
49+
}
50+
}
51+
3452
return EXIT_SUCCESS;
35-
}
53+
}

0 commit comments

Comments
 (0)