Android - How to properly loop in an activity and pause it and summarize it

1

I have a project of a CHIP-8 emulator that I'm trying to port to android, the whole emulator is written in C ++ I just created a C ++ library in android studio and added it to my project, the problem is, I need to manipulate he would see activity, whenever a user for example press the back button the emulation loop should be paused, and whenever a button on the screen was pressed the loop should pause, check which "key" was pressed, perform the appropriate action in the emulator and finally summarize the loop, the problem is, the loop that I created gets executed when it returns to the previous activity or crash if I by him in the background to access another application, the loop model that I think is similar to SDL_PollEvent that allows you to handle input events even with the running loop.

package com.samuelives.chipeight;

import android.os.Bundle;
import android.widget.Toast;

import com.samuelives.chipeight.emulator.RenderSurface;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class Emulator extends AppCompatActivity {

    static {
        System.loadLibrary("CHIP-8");
    }

    private RenderSurface svEmulator = null;
    private boolean isRunning = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_emulator);

        svEmulator = (RenderSurface)findViewById(R.id.svEmulator);

        initEmulator();

        if(!loadROM(getIntent().getStringExtra("gamePath"))){
            Toast.makeText(this, "Could not load game", Toast.LENGTH_LONG).show();
            stopEmulator();
            finish();
        }

        cpuThread.start();

    }

    @Override
    protected void onResume() {
        super.onResume();
        svEmulator.onResume();
        isRunning = true;
        if(!cpuThread.isAlive()){
            cpuThread.notify();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        svEmulator.onPause();
        isRunning = false;
        if(cpuThread.isAlive()) {
            try {
                cpuThread.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //stopEmulator();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRunning = false;
        cpuThread.interrupt();
        stopEmulator();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        isRunning = false;
        cpuThread.interrupt();
        stopEmulator();
    }

    private Thread cpuThread = new Thread(new Runnable() {

        @Override
        public void run() {

            while(isRunning){
                execute();
                if(hasDraw()){
                    svEmulator.requestRender();
                }
            }

        }

    });

    public native void initEmulator();
    public native boolean loadROM(String path);
    public native void execute();
    public native void stopEmulator();
    public native boolean hasDraw();

}
    
asked by anonymous 19.09.2018 / 20:42

0 answers