Compare commits

...

6 Commits

Author SHA1 Message Date
48cfca9f63 implementing dt 2024-10-14 23:10:52 +07:00
df0183d2f5 remove general 2024-10-14 23:10:32 +07:00
f9f1a85eff move thread into app 2024-10-14 23:09:58 +07:00
4d3863ac70 add src path 2024-10-14 23:08:48 +07:00
3a7c10a53c move main loop into midlet class 2024-10-14 20:52:58 +07:00
59c57aec1a delta time thingy 2024-10-14 20:46:09 +07:00
8 changed files with 1209 additions and 1117 deletions

View File

@ -1,5 +1,8 @@
{
"java.project.referencedLibraries": [
"lib/**/*.jar",
],
"java.project.sourcePaths": [
"src"
]
}

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,21 @@ public class DotHack extends MIDlet implements Runnable {
public void destroyApp(boolean paramBoolean) {}
public void run() {
while (true) {
try
{
sagiri.time_begin = System.currentTimeMillis();
game.repaint();
game.serviceRepaints();
Thread.sleep(1000/sagiri.fps);
sagiri.time_delta = System.currentTimeMillis() - sagiri.time_begin;
}
catch (Exception e)
{
// TODO: catch any unhandled exception here
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,10 @@
package sillysagiri;
import java.util.Enumeration;
import java.util.Hashtable;
public final class Input {
public Hashtable<Integer, Integer> hash_keyState = new Hashtable<>();
public Hashtable hash_keyState = new Hashtable(20);
public int state_anyKey = 0;
public int state_touchState = Sagiri.TOUCH_NONE;
@ -18,11 +19,15 @@ public final class Input {
public final void Update()
{
for (Integer key : hash_keyState.keySet())
Enumeration i = hash_keyState.keys();
while(i.hasMoreElements())
{
int value = hash_keyState.get(key);
if (value == -1) hash_keyState.remove(key);
else hash_keyState.put(key, value+1);
Integer key = (Integer)i.nextElement();
Integer value = (Integer)hash_keyState.get(key);
if (value.intValue() == -1) hash_keyState.remove(key);
else hash_keyState.put(key, new Integer(value.intValue()+1));
}
if (state_anyKey != 0) state_anyKey++;
@ -31,12 +36,13 @@ public final class Input {
public final void HandleKeyDown(int keycode)
{
Integer value = hash_keyState.get(keycode);
Integer key = new Integer(keycode);
Integer value = (Integer)hash_keyState.get(key);
if (value != null)
hash_keyState.put(keycode, new Integer(Math.max(value, 1)));
hash_keyState.put(key, new Integer(Math.max(value.intValue(), 1)));
else
hash_keyState.put(keycode, 1);
hash_keyState.put(key, new Integer(1));
state_anyKey = Math.max(state_anyKey, 1);
state_lastKey = keycode;
@ -44,7 +50,9 @@ public final class Input {
public final void HandleKeyUp(int keycode)
{
hash_keyState.put(keycode, -1);
Integer key = new Integer(keycode);
hash_keyState.put(key, new Integer(-1));
state_anyKey = -1;
state_lastKey = keycode;
}
@ -58,7 +66,8 @@ public final class Input {
public final boolean IsKeyHeld(int keycode)
{
Integer value = hash_keyState.get(keycode);
Integer key = new Integer(keycode);
Integer value = (Integer)hash_keyState.get(key);
if (value != null)
return value.intValue() > 0;
@ -68,14 +77,15 @@ public final class Input {
public final boolean IsKeyHeld(int[] keycode)
{
for (int i : keycode)
for (int i=0; i<keycode.length; i++)
if (IsKeyHeld(keycode[i])) return true;
return false;
}
public final boolean IsKeyDown(int keycode)
{
Integer value = hash_keyState.get(keycode);
Integer key = new Integer(keycode);
Integer value = (Integer)hash_keyState.get(key);
if (value != null)
return value.intValue() == 0;
@ -85,14 +95,15 @@ public final class Input {
public final boolean IsKeyDown(int[] keycode)
{
for (int i : keycode)
for (int i=0; i<keycode.length; i++)
if (IsKeyDown(keycode[i])) return true;
return false;
}
public final boolean IsKeyReleased(int keycode)
{
Integer value = hash_keyState.get(keycode);
Integer key = new Integer(keycode);
Integer value = (Integer)hash_keyState.get(key);
if (value != null)
return value.intValue() == -1;
@ -102,7 +113,7 @@ public final class Input {
public final boolean IsKeyReleased(int[] keycode)
{
for (int i : keycode)
for (int i=0; i<keycode.length; i++)
if (IsKeyReleased(keycode[i])) return true;
return false;
}
@ -111,5 +122,5 @@ public final class Input {
public final boolean IsKeyDownAny() { return state_anyKey == 1; }
public final boolean IsKeyReleasedAny() { return state_anyKey == -1; }
public final int getKeyStatus(int keycode) { return hash_keyState.get(keycode); }
public final Integer getKeyStatus(int keycode) { return (Integer)hash_keyState.get(new Integer(keycode)); }
}

View File

@ -1,5 +1,7 @@
package sillysagiri;
import javax.microedition.lcdui.Graphics;
public class Sagiri
{
public final static int TOUCH_NONE = 0;
@ -13,24 +15,34 @@ public class Sagiri
public int fps = 20;
private Scene _scene_current;
private long _time_begin;
private long _time_delta;
private Scene _scene_current = null;
public long time_begin = 0;
public long time_delta = 0;
public Sagiri()
{
_instance = this;
input = new Input();
_scene_current = null;
}
public void Update()
public void Update(Graphics g)
{
_time_begin = System.currentTimeMillis();
input.Update();
if (_scene_current != null) _scene_current.Update();
if (_scene_current != null)
_scene_current.Update(time_delta, g);
}
public final long GetFrameTime() { return _time_delta; }
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; }
}

View File

@ -8,8 +8,7 @@ public abstract class Scene {
}
// override this
public abstract void Draw(Graphics g);
public abstract void Destroy();
public abstract void Update();
public abstract void Update(long dt, Graphics g);
}

View File

@ -26,4 +26,16 @@ public final class Utils {
{
return Global.app.game.getHeight();
}
public static int clamp(int value, int min, int max) {
return Math.max(min, Math.min(value, max));
}
public static float clamp(float value, float min, float max) {
return Math.max(min, Math.min(value, max));
}
public static double clamp(double value, double min, double max) {
return Math.max(min, Math.min(value, max));
}
}

View File

@ -10,53 +10,50 @@ import sillysagiri.Scene;
import sillysagiri.Utils;
public class Intro extends Scene {
private int counter = 0;
private long counter = 0;
private int state = 0;
private Image img_logo;
private Image img_logo = null;
private Image img_ez = null;
private Image img_theworld = null;
private Image img_vol1 = null;
private Image img_project = null;
public Intro() throws IOException
{
img_logo = Image.createImage("/t_1.png");
img_ez = Image.createImage("/pEZ.png");
}
public void Destroy()
{
img_logo = null;
img_ez = null;
img_theworld = null;
img_vol1 = null;
img_project = null;
}
public void Update()
public void Update(long dt, Graphics g)
{
// TODO: use time based counter instead
counter++;
if (counter > 100) counter = 0;
switch (state) {
case 0:
{
if (counter > 25)
{
// img_logo = Image.createImage("/pEZ.png");
counter = 0;
state = 1;
}
}
break;
counter += dt;
default: break;
}
}
public void Draw(Graphics g)
{
Utils.Clear_Screen(g, 73, 0, 0);
switch (state) {
case 0:
{
if (counter > 1800)
{
counter = 0;
state = 1;
break;
}
Utils.Clear_Screen(g, 73, 0, 0);
int halfW = img_logo.getWidth()/2;
int halfH = img_logo.getHeight()/2;
float r = Easing.CubicEaseOut((float)counter/25);
int pos = (int)(((Utils.GetScreenWidth()/2)+halfW) * r) - halfW;
float r = Easing.CubicEaseInOut((float)counter/1800);
int pos = (int)Math.ceil(((Utils.GetScreenWidth()/2)+halfW) * r) - halfW;
g.drawRegion(
img_logo,
@ -69,24 +66,53 @@ public class Intro extends Scene {
0, halfH, img_logo.getWidth(), img_logo.getHeight()-halfH, Sprite.TRANS_NONE,
Utils.GetScreenWidth() - pos, Utils.GetScreenHeight()/2,
Graphics.TOP | Graphics.HCENTER);
break;
}
break;
case 1:
{
g.drawImage(
img_logo,
Utils.GetScreenWidth()/2,
Utils.GetScreenHeight()/2,
Graphics.VCENTER | Graphics.HCENTER
);
if (counter > 500)
{
counter = 0;
state = 2;
break;
try {
img_logo = Image.createImage("/t_0.png");
img_theworld = Image.createImage("/t_2.png");
img_project = Image.createImage("/t_3.png");
img_vol1 = Image.createImage("/t_4.png");
}
catch(IOException e) {}
}
}
default: return;
break;
case 2:
{
Utils.Clear_Screen(g, 146, 36, 0);
g.drawImage(
img_logo,
Utils.GetScreenWidth()/2, Utils.GetScreenHeight()/2-35,
Graphics.HCENTER | Graphics.VCENTER);
g.drawImage(
img_theworld,
Utils.GetScreenWidth()/2, Utils.GetScreenHeight()/2,
Graphics.HCENTER | Graphics.TOP);
g.drawImage(img_project, 5, Utils.GetScreenHeight()-5, Graphics.LEFT | Graphics.BOTTOM);
g.drawImage(img_vol1, 5, 5, Graphics.LEFT | Graphics.TOP);
if (counter >= 1000) counter = 0;
if (counter < 600)
g.drawImage(
img_ez,
Utils.GetScreenWidth()/2, (int)(Utils.GetScreenHeight()*0.75),
Graphics.VCENTER | Graphics.HCENTER);
}
break;
default: break;
}
}
}