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

Convert some C to C++. #1005

Merged
merged 2 commits into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions native-activity/app/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
/**
* Our saved state data.
*/
struct saved_state {
struct SavedState {
float angle;
int32_t x;
int32_t y;
Expand All @@ -76,8 +76,8 @@ struct saved_state {
/**
* Shared state for our app.
*/
struct engine {
struct android_app* app;
struct Engine {
android_app* app;

ASensorManager* sensorManager;
const ASensor* accelerometerSensor;
Expand All @@ -89,7 +89,7 @@ struct engine {
EGLContext context;
int32_t width;
int32_t height;
struct saved_state state;
SavedState state;

void CreateSensorListener(ALooper_callbackFunc callback) {
CHECK_NOT_NULL(app);
Expand All @@ -109,7 +109,7 @@ struct engine {
/**
* Initialize an EGL context for the current display.
*/
static int engine_init_display(struct engine* engine) {
static int engine_init_display(Engine* engine) {
// initialize OpenGL ES and EGL

/*
Expand Down Expand Up @@ -209,7 +209,7 @@ static int engine_init_display(struct engine* engine) {
/**
* Just the current frame in the display.
*/
static void engine_draw_frame(struct engine* engine) {
static void engine_draw_frame(Engine* engine) {
if (engine->display == nullptr) {
// No display.
return;
Expand All @@ -226,7 +226,7 @@ static void engine_draw_frame(struct engine* engine) {
/**
* Tear down the EGL context currently associated with the display.
*/
static void engine_term_display(struct engine* engine) {
static void engine_term_display(Engine* engine) {
if (engine->display != EGL_NO_DISPLAY) {
eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);
Expand All @@ -247,9 +247,9 @@ static void engine_term_display(struct engine* engine) {
/**
* Process the next input event.
*/
static int32_t engine_handle_input(struct android_app* app,
static int32_t engine_handle_input(android_app* app,
AInputEvent* event) {
auto* engine = (struct engine*)app->userData;
auto* engine = (Engine*)app->userData;
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
engine->animating = 1;
engine->state.x = AMotionEvent_getX(event, 0);
Expand All @@ -262,14 +262,14 @@ static int32_t engine_handle_input(struct android_app* app,
/**
* Process the next main command.
*/
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
auto* engine = (struct engine*)app->userData;
static void engine_handle_cmd(android_app* app, int32_t cmd) {
auto* engine = (Engine*)app->userData;
switch (cmd) {
case APP_CMD_SAVE_STATE:
// The system has asked us to save our current state. Do so.
engine->app->savedState = malloc(sizeof(struct saved_state));
*((struct saved_state*)engine->app->savedState) = engine->state;
engine->app->savedStateSize = sizeof(struct saved_state);
engine->app->savedState = malloc(sizeof(SavedState));
*((SavedState*)engine->app->savedState) = engine->state;
engine->app->savedStateSize = sizeof(SavedState);
break;
case APP_CMD_INIT_WINDOW:
// The window is being shown, get it ready.
Expand Down Expand Up @@ -311,7 +311,7 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {

int OnSensorEvent(int /* fd */, int /* events */, void* data) {
CHECK_NOT_NULL(data);
engine* engine = reinterpret_cast<struct engine*>(data);
Engine* engine = reinterpret_cast<Engine*>(data);

CHECK_NOT_NULL(engine->accelerometerSensor);
ASensorEvent event;
Expand All @@ -332,8 +332,8 @@ int OnSensorEvent(int /* fd */, int /* events */, void* data) {
* 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) {
struct engine engine {};
void android_main(android_app* state) {
Engine engine {};

memset(&engine, 0, sizeof(engine));
state->userData = &engine;
Expand All @@ -346,15 +346,15 @@ void android_main(struct android_app* state) {

if (state->savedState != nullptr) {
// We are starting with a previous saved state; restore from it.
engine.state = *(struct saved_state*)state->savedState;
engine.state = *(SavedState*)state->savedState;
}

// loop waiting for stuff to do.

while (true) {
// Read all pending events.
int events;
struct android_poll_source* source;
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
Expand Down
Loading