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 FPS Counter #9

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.panda3ds.pandroid.AlberDriver;
Expand All @@ -21,94 +23,125 @@
import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.view.PandaGlSurfaceView;
import com.panda3ds.pandroid.view.PandaLayoutController;
import javax.microedition.khronos.opengles.GL10;

import java.util.Locale;

public class GameActivity extends BaseActivity {
private final DrawerFragment drawerFragment = new DrawerFragment();
private final AlberInputListener inputListener = new AlberInputListener(this::onBackPressed);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
if (!intent.hasExtra(Constants.ACTIVITY_PARAMETER_PATH)) {
setContentView(new FrameLayout(this));
Toast.makeText(this, "Invalid rom path!", Toast.LENGTH_LONG).show();
finish();
return;
}

PandaGlSurfaceView pandaSurface = new PandaGlSurfaceView(this, intent.getStringExtra(Constants.ACTIVITY_PARAMETER_PATH));
setContentView(R.layout.game_activity);

((FrameLayout) findViewById(R.id.panda_gl_frame))
.addView(pandaSurface, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

PandaLayoutController controllerLayout = findViewById(R.id.controller_layout);
controllerLayout.initialize();

((CheckBox) findViewById(R.id.hide_screen_controller)).setOnCheckedChangeListener((buttonView, checked) -> {
findViewById(R.id.overlay_controller).setVisibility(checked ? View.VISIBLE : View.GONE);
findViewById(R.id.overlay_controller).invalidate();
findViewById(R.id.overlay_controller).requestLayout();
GlobalConfig.set(GlobalConfig.KEY_SCREEN_GAMEPAD_VISIBLE, checked);
});
((CheckBox) findViewById(R.id.hide_screen_controller)).setChecked(GlobalConfig.get(GlobalConfig.KEY_SCREEN_GAMEPAD_VISIBLE));

getSupportFragmentManager().beginTransaction().replace(R.id.drawer_fragment, drawerFragment).commitNow();
}

@Override
protected void onResume() {
super.onResume();
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
InputHandler.reset();
InputHandler.setMotionDeadZone(InputMap.getDeadZone());
InputHandler.setEventListener(inputListener);
}

@Override
protected void onPause() {
super.onPause();

InputHandler.reset();
drawerFragment.open();
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if ((!drawerFragment.isOpened()) && InputHandler.processKeyEvent(event)) {
return true;
}

return super.dispatchKeyEvent(event);
}

@Override
public void onBackPressed() {
if (drawerFragment.isOpened()) {
drawerFragment.close();
} else {
drawerFragment.open();
}
}

@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
if ((!drawerFragment.isOpened()) && InputHandler.processMotionEvent(ev)) {
return true;
}

return super.dispatchGenericMotionEvent(ev);
}

@Override
protected void onDestroy() {
if (AlberDriver.HasRomLoaded()) {
AlberDriver.Finalize();
}

super.onDestroy();
}
private final DrawerFragment drawerFragment = new DrawerFragment();
private final AlberInputListener inputListener = new AlberInputListener(this::onBackPressed);
private int frameCount = 0;
private long lastTimeMillis = SystemClock.elapsedRealtime();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
if (!intent.hasExtra(Constants.ACTIVITY_PARAMETER_PATH)) {
setContentView(new FrameLayout(this));
Toast.makeText(this, "Invalid rom path!", Toast.LENGTH_LONG).show();
finish();
return;
}

PandaGlSurfaceView pandaSurface = new PandaGlSurfaceView(this, intent.getStringExtra(Constants.ACTIVITY_PARAMETER_PATH));
setContentView(R.layout.game_activity);

((FrameLayout) findViewById(R.id.panda_gl_frame))
.addView(pandaSurface, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

PandaLayoutController controllerLayout = findViewById(R.id.controller_layout);
controllerLayout.initialize();

((CheckBox) findViewById(R.id.hide_screen_controller)).setOnCheckedChangeListener((buttonView, checked) -> {
findViewById(R.id.overlay_controller).setVisibility(checked ? View.VISIBLE : View.GONE);
findViewById(R.id.overlay_controller).invalidate();
findViewById(R.id.overlay_controller).requestLayout();
GlobalConfig.set(GlobalConfig.KEY_SCREEN_GAMEPAD_VISIBLE, checked);
});
((CheckBox) findViewById(R.id.hide_screen_controller)).setChecked(GlobalConfig.get(GlobalConfig.KEY_SCREEN_GAMEPAD_VISIBLE));

getSupportFragmentManager().beginTransaction().replace(R.id.drawer_fragment, drawerFragment).commitNow();
}

@Override
protected void onResume() {
super.onResume();
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
InputHandler.reset();
InputHandler.setMotionDeadZone(InputMap.getDeadZone());
InputHandler.setEventListener(inputListener);
}

@Override
protected void onPause() {
super.onPause();

InputHandler.reset();
drawerFragment.open();
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if ((!drawerFragment.isOpened()) && InputHandler.processKeyEvent(event)) {
return true;
}

return super.dispatchKeyEvent(event);
}

@Override
public void onBackPressed() {
if (drawerFragment.isOpened()) {
drawerFragment.close();
} else {
drawerFragment.open();
}
}

@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
if ((!drawerFragment.isOpened()) && InputHandler.processMotionEvent(ev)) {
return true;
}

return super.dispatchGenericMotionEvent(ev);
}

@Override
protected void onDestroy() {
if (AlberDriver.HasRomLoaded()) {
AlberDriver.Finalize();
}

super.onDestroy();
}

// Add this method for onDrawFrame
public void onDrawFrame(GL10 gl) {

// Increment frameCount for FPS calculation
frameCount++;

// Calculate FPS every second
long currentTimeMillis = SystemClock.elapsedRealtime();
long elapsedTimeMillis = currentTimeMillis - lastTimeMillis;
if (elapsedTimeMillis >= 1000) { // Check if one second has passed
double fps = (frameCount * 1000.0) / elapsedTimeMillis;

// Find the TextView and update it with the FPS value
runOnUiThread(() -> {
TextView fpsTextView = findViewById(R.id.fpsTextView);
fpsTextView.setText(String.format(Locale.getDefault(), "FPS: %.2f", fps));
});


// Reset counters for the next second
frameCount = 0;
lastTimeMillis = currentTimeMillis;
}
}
}

11 changes: 10 additions & 1 deletion src/pandroid/app/src/main/res/layout/game_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
android:layout_height="match_parent"
android:padding="25dp">

<TextView
android:id="@+id/fpsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:layout_marginTop="5dp"
android:textColor="#FFFFFF"
tools:text="60 FPS" />

<CheckBox
android:id="@+id/hide_screen_controller"
android:layout_width="24dp"
Expand All @@ -54,4 +63,4 @@

<include layout="@layout/drawer_game_container"/>

</FrameLayout>
</FrameLayout>
Loading