move main loop into midlet class

This commit is contained in:
sillysagiri 2024-10-14 20:52:58 +07:00
parent 59c57aec1a
commit 3a7c10a53c
3 changed files with 36 additions and 27 deletions

View File

@ -2,16 +2,25 @@ package dothack;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import sillysagiri.Sagiri;
public class DotHack extends MIDlet implements Runnable {
public DotHackC game;
public Sagiri sagiri;
private Display _display;
public Thread thread;
public void startApp() {
Global.app = this;
sagiri = new Sagiri();
game = new DotHackC(this);
_display = Display.getDisplay(this);
_display.setCurrent(game);
thread = new Thread(this);
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
public void pauseApp() {}
@ -19,6 +28,17 @@ public class DotHack extends MIDlet implements Runnable {
public void destroyApp(boolean paramBoolean) {}
public void run() {
while (true) {
try
{
game.repaint();
game.serviceRepaints();
Thread.sleep(1000/sagiri.fps);
}
catch (Exception e)
{
// TODO: catch any unhandled exception here
}
}
}
}

View File

@ -150,30 +150,7 @@ public class DotHackC extends Canvas {
thread = new Thread(this);
this.thread.start();
}
public void run() {
while (true) {
if (_scene_current != null) _scene_current.Update();
repaint();
serviceRepaints();
try {
Thread.sleep(80L);
} catch (Exception exception) {}
}
}
public Scene Get_Scene() { return _scene_current; }
public void Set_Scene(Scene scene)
{
if (_scene_current != null)
{
_scene_current.Destroy();
_scene_current = null;
System.gc();
}
_scene_current = scene;
}
public void soundPlay(int id) {
try {
if (soundS) {

View File

@ -18,7 +18,7 @@ public class Sagiri
private Scene _scene_current;
private long _time_begin;
private long _time_delta;
public Sagiri()
{
_instance = this;
@ -35,5 +35,17 @@ public class Sagiri
_scene_current.Update(_time_delta = System.currentTimeMillis() - _time_begin, g);
}
public Scene Get_Scene() { return _scene_current; }
public void Set_Scene(Scene scene)
{
if (_scene_current != null)
{
_scene_current.Destroy();
_scene_current = null;
System.gc();
}
_scene_current = scene;
}
public final long GetFrameTime() { return _time_delta; }
}