Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android support #575

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

Add Android support #575

wants to merge 10 commits into from

Conversation

cyberarm
Copy link
Collaborator

@cyberarm cyberarm commented Feb 7, 2021

Draft PR for tracking changes needed to compile for Android

See: https://github.com/cyberarm/gosu-android for Android project
Current goal is to get a very basic gosu app running

@cyberarm
Copy link
Collaborator Author

cyberarm commented Feb 8, 2021

@jlnr Do you know why the app crashes when FileAndroid::read returns?
The expected log output of "Read asset." from FileAndroid is printed but the log in IO of "Loaded file: filename" doesn't.

Call tree:

Gosu::Image("gosu.png");

gosu/src/BitmapIO.cpp

Lines 55 to 62 in 0a58b39

Gosu::Bitmap Gosu::load_image_file(const std::string& filename)
{
Buffer buffer;
__android_log_print(android_LogPriority::ANDROID_LOG_INFO, "Gosu", "Loading image: %s\n", filename.c_str());
load_file(buffer, filename);
__android_log_print(android_LogPriority::ANDROID_LOG_INFO, "Gosu", "Loaded image: %s\n", filename.c_str());
return load_image_file(buffer.front_reader());
}

gosu/src/IO.cpp

Lines 52 to 59 in 0a58b39

void Gosu::load_file(Buffer& buffer, const string& filename)
{
File file(filename);
buffer.resize(file.size());
__android_log_print(android_LogPriority::ANDROID_LOG_INFO, "Gosu", "Reading file: %s\n", filename.c_str());
file.read(0, buffer.size(), buffer.data());
__android_log_print(android_LogPriority::ANDROID_LOG_INFO, "Gosu", "Loaded file: %s\n", filename.c_str());
}

gosu/src/FileAndroid.cpp

Lines 72 to 80 in 0a58b39

void Gosu::File::read(size_t offset, size_t length, void* dest_buffer) const
{
__android_log_print(android_LogPriority::ANDROID_LOG_INFO, "Gosu", "Reading asset with length: %d/%d...\n", length, size());
AAsset_seek(pimpl->asset, offset, SEEK_SET);
AAsset_read(pimpl->asset, &dest_buffer, length);
__android_log_print(android_LogPriority::ANDROID_LOG_INFO, "Gosu", "Read asset.\n");
}

2021-02-08 14:45:17.065 8048-8103/org.libsdl.app I/Gosu: Loading image: gosu.png
...
(List of files in assets/ dir)
2021-02-08 14:45:17.066 8048-8103/org.libsdl.app I/Gosu: Asset filename: gosu.png
...
2021-02-08 14:45:17.066 8048-8103/org.libsdl.app I/Gosu: Opening asset: gosu.png
2021-02-08 14:45:17.066 8048-8103/org.libsdl.app I/Gosu: Reading file: gosu.png
2021-02-08 14:45:17.066 8048-8103/org.libsdl.app I/Gosu: Reading asset with length: 1617/1617...
2021-02-08 14:45:17.067 8048-8103/org.libsdl.app I/Gosu: Read asset.
2021-02-08 14:45:17.067 8048-8103/org.libsdl.app A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x30518635e7854 in tid 8103 (SDLThread), pid 8048 (SDLActivity)

@jlnr
Copy link
Member

jlnr commented Feb 10, 2021

@cyberarm You are passing &dest_buffer into AAsset_read, which will read into the memory that starts with the dest_buffer parameter on the stack. The extra & is the problem.

@cyberarm
Copy link
Collaborator Author

Pointer, reference, value... 🤦🏽‍♂️ Thanks, @jlnr, it compiles now. 😀
photo5557551483772315565

@cyberarm
Copy link
Collaborator Author

@jlnr How do we want to implement Gosu's touch callbacks?
SDL provides us with 3 touch events SDL_FINGERMOTION, SDL_FINGERDOWN, and SDL_FINGERUP SDL TouchFingerEvent

SDL doesn't provide a touch_cancelled event.

Do we want to keep the struct based callback?

struct Touch {
  int id; // device_id + finger_id?
  int device_index, finger_index;
  double x, y;
};

void touch_began(Touch touch);
void touch_moved(Touch touch);
void touch_ended(Touch touch);

or use a more verbose callback?

void touch_start(int device_index, int finger_index, double x, double y);
void touch_move(int device_index, int finger_index, double x, double y);
void touch_end(int device_index, int finger_index, double x, double y);

@jlnr
Copy link
Member

jlnr commented May 8, 2021

Hmm, good question(s). Some thoughts after a busy couple of weeks:

  • I like the struct because it's easier to add stuff like touch pressure later on.
  • I also like the concept of touch_cancelled. I think for the most part it can be replaced with the lose_focus callback – basically, if something happens in the foreground while the finger is down, we should not count the lost (cancelled) touch as a button tap, and so on.
    Let's leave it for now, even if only iOS calls it (which is already the case for the lose_focus callback...).
  • Yes, I think we should try to make it a single id. Maybe hashing both ids together is safer than relying on addition or other simple operations, but we can always tweak that later.

@cyberarm
Copy link
Collaborator Author

@jlnr what is the best way for Gosu::Input to access Gosu::Window's instance of Gosu::Graphics for width() and height() to convert SDL's normalized (0..1) screen touch coordinates to window coordinates?

@jlnr
Copy link
Member

jlnr commented May 22, 2021

@jlnr It is actually supposed to work the other way around: Window should set the mouse factors of its Input object to scale correctly. Technically touches are not a mouse, but can we reuse the same mechanism? (Sorry, still no time to dig into the code a lot...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants