• R/O
  • SSH

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Commit MetaInfo

Revision7bd359456a3e7e14e1acc2e508b43851ed3b896b (tree)
Zeit2015-07-02 07:20:47
AutorMirrgieRiana
CommiterMirrgieRiana

Log Message

add mirrg.applet.antimony

Ändern Zusammenfassung

Diff

diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/.classpath
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/.classpath Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,6 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<classpath>
3+ <classpathentry kind="src" path="src"/>
4+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+ <classpathentry kind="output" path="bin"/>
6+</classpath>
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/.project
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/.project Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,17 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<projectDescription>
3+ <name>mirrg.applet.antimony</name>
4+ <comment></comment>
5+ <projects>
6+ </projects>
7+ <buildSpec>
8+ <buildCommand>
9+ <name>org.eclipse.jdt.core.javabuilder</name>
10+ <arguments>
11+ </arguments>
12+ </buildCommand>
13+ </buildSpec>
14+ <natures>
15+ <nature>org.eclipse.jdt.core.javanature</nature>
16+ </natures>
17+</projectDescription>
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/IMirrgieApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/IMirrgieApplet.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,60 @@
1+package mirrg.applet.antimony;
2+
3+import java.awt.Graphics2D;
4+import java.awt.image.BufferedImage;
5+
6+import mirrg.applet.antimony.event.AppletListener;
7+import mirrg.applet.antimony.image.TripleBufferListener;
8+import mirrg.applet.antimony.thread.EventBuffer;
9+import mirrg.applet.antimony.thread.GameThreadListener;
10+import mirrg.geom.PointInt;
11+import mirrg.geom.RectangleInt;
12+
13+public interface IMirrgieApplet
14+{
15+
16+ RectangleInt getRegion();
17+
18+
19+ void addAppletListener(AppletListener l);
20+
21+
22+ double getLoadFactor();
23+
24+ double getFPS();
25+
26+ double getSuitableFPS();
27+
28+
29+ void addTripleBufferListener(TripleBufferListener l);
30+
31+ BufferedImage getBackBuffer();
32+
33+ Graphics2D getBackGraphics();
34+
35+ BufferedImage getSafetyBackBuffer();
36+
37+ Graphics2D getSafetyBackGraphics();
38+
39+ int getBackBufferWidth();
40+
41+ int getBackBufferHeight();
42+
43+
44+ PointInt getMousePosition();
45+
46+
47+ void addGameThreadListener(GameThreadListener l);
48+
49+ EventBuffer getEventBuffer();
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/MirrgieApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/MirrgieApplet.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,16 @@
1+package mirrg.applet.antimony;
2+
3+import mirrg.applet.antimony.fps.ManagedFPSApplet;
4+import mirrg.geom.RectangleInt;
5+
6+public class MirrgieApplet extends ManagedFPSApplet
7+implements IMirrgieApplet
8+{
9+
10+ @Override
11+ public RectangleInt getRegion()
12+ {
13+ return new RectangleInt(0, 0, getWidth(), getHeight());
14+ }
15+
16+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/event/AppletListener.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/event/AppletListener.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,15 @@
1+package mirrg.applet.antimony.event;
2+
3+
4+public interface AppletListener
5+{
6+
7+ void appletInitialized();
8+
9+ void appletStarted();
10+
11+ void appletStopped();
12+
13+ void appletDestroyed();
14+
15+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/event/EventReadyApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/event/EventReadyApplet.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,151 @@
1+package mirrg.applet.antimony.event;
2+
3+import java.awt.event.ComponentEvent;
4+import java.awt.event.ComponentListener;
5+import java.awt.event.KeyEvent;
6+import java.awt.event.KeyListener;
7+import java.awt.event.MouseEvent;
8+import java.awt.event.MouseListener;
9+import java.awt.event.MouseMotionListener;
10+import java.awt.event.MouseWheelEvent;
11+import java.awt.event.MouseWheelListener;
12+import java.util.LinkedList;
13+
14+import mirrg.applet.antimony.init.Initializer;
15+import mirrg.applet.antimony.init.InitializingApplet;
16+
17+public class EventReadyApplet
18+extends InitializingApplet
19+implements MouseListener, KeyListener,
20+MouseMotionListener, MouseWheelListener, ComponentListener,
21+AppletListener
22+{
23+
24+ private LinkedList<AppletListener> appletListeners;
25+
26+ public void addAppletListener(AppletListener l)
27+ {
28+ appletListeners.add(l);
29+ }
30+
31+
32+ @Initializer
33+ public final void initEventReadyApplet()
34+ {
35+ appletListeners = new LinkedList<AppletListener>();
36+ addAppletListener(this);
37+
38+
39+ addMouseListener(this);
40+ addMouseMotionListener(this);
41+ addMouseWheelListener(this);
42+ addKeyListener(this);
43+ addComponentListener(this);
44+
45+
46+ for (AppletListener l : appletListeners) {
47+ l.appletInitialized();
48+ }
49+ }
50+
51+
52+ @Override
53+ public final void start()
54+ {
55+ for (AppletListener l : appletListeners) {
56+ l.appletStarted();
57+ }
58+ }
59+
60+ @Override
61+ public final void stop()
62+ {
63+ for (AppletListener l : appletListeners) {
64+ l.appletStopped();
65+ }
66+ }
67+
68+ @Override
69+ public final void destroy()
70+ {
71+ for (AppletListener l : appletListeners) {
72+ l.appletDestroyed();
73+ }
74+ }
75+
76+
77+ // -------------------------------- AppletListener
78+
79+ @Override
80+ public void appletInitialized() {}
81+
82+ @Override
83+ public void appletStarted() {}
84+
85+ @Override
86+ public void appletStopped() {}
87+
88+ @Override
89+ public void appletDestroyed() {}
90+
91+
92+ // -------------------------------- ComponentListener
93+
94+ @Override
95+ public void componentResized(ComponentEvent e) {}
96+
97+ @Override
98+ public void componentMoved(ComponentEvent e) {}
99+
100+ @Override
101+ public void componentShown(ComponentEvent e) {}
102+
103+ @Override
104+ public void componentHidden(ComponentEvent e) {}
105+
106+
107+ // --------------------------------- MouseWheelListener
108+
109+ @Override
110+ public void mouseWheelMoved(MouseWheelEvent e) {}
111+
112+
113+ // --------------------------------- MouseMotionListener
114+
115+ @Override
116+ public void mouseDragged(MouseEvent e) {}
117+
118+ @Override
119+ public void mouseMoved(MouseEvent e) {}
120+
121+
122+ // ----------------------------------- KeyListener
123+
124+ @Override
125+ public void keyTyped(KeyEvent e) {}
126+
127+ @Override
128+ public void keyPressed(KeyEvent e) {}
129+
130+ @Override
131+ public void keyReleased(KeyEvent e) {}
132+
133+
134+ // ---------------------------------- MouseListener
135+
136+ @Override
137+ public void mouseClicked(MouseEvent e) {}
138+
139+ @Override
140+ public void mousePressed(MouseEvent e) {}
141+
142+ @Override
143+ public void mouseReleased(MouseEvent e) {}
144+
145+ @Override
146+ public void mouseEntered(MouseEvent e) {}
147+
148+ @Override
149+ public void mouseExited(MouseEvent e) {}
150+
151+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/fps/ManagedFPSApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/fps/ManagedFPSApplet.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,126 @@
1+package mirrg.applet.antimony.fps;
2+
3+import mirrg.applet.antimony.input.ManagedInputApplet;
4+
5+public class ManagedFPSApplet
6+extends ManagedInputApplet
7+{
8+
9+ /** 直前に計測した現在時刻です。 */
10+ private long previousTimeMs = 0;
11+
12+ /** スムージングされた負荷率です。 */
13+ private double smoothedLoadFactor = 0;
14+
15+ /** スムージングされたFPSです。 */
16+ private double smoothedFps = 0;
17+
18+ /** スムージングの定数です。 */
19+ private final static double K = 0.10;
20+
21+
22+ /**
23+ * sleepForNextTickの初回呼び出し時に呼出されるメソッドです。
24+ */
25+ private final void sleepForNextTickFirst()
26+ {
27+ smoothedFps = getSuitableFPS();
28+ smoothedLoadFactor = 1.0;
29+ previousTimeMs = System.currentTimeMillis();
30+ }
31+
32+ /**
33+ * 現在時刻と直前の時刻からFPSを計算し、適切にスリープします。
34+ */
35+ protected final void sleepForNextTick() throws InterruptedException
36+ {
37+ // 初回呼出しである場合、別のルーチンを実行
38+ if (previousTimeMs == 0) {
39+ sleepForNextTickFirst();
40+ return;
41+ }
42+
43+
44+ // 処理 Sleep
45+ // PTM ms
46+ // `-----------'
47+ // PLTM |
48+ // `-----------|----------'
49+ // dms
50+ // |
51+
52+ // 現在の時刻
53+ long ms = System.currentTimeMillis();
54+
55+ // 直前のSleep終了から現在までの長さ
56+ long previousLoopTimeMs = ms - previousTimeMs;
57+
58+ // 適正1ループ時間
59+ long dms = (long)(1000 / getSuitableFPS());
60+
61+ {
62+
63+ // ######################### Wait ########################
64+ // Sleepするべき時間ぶんだけSleep
65+ if (dms - previousLoopTimeMs - 2 >= 0) {
66+ Thread.sleep(dms - previousLoopTimeMs - 2);
67+ }
68+
69+ // 微調整
70+ while (System.currentTimeMillis() < previousTimeMs + dms);
71+ // #######################################################
72+
73+ }
74+
75+
76+ // 処理 Sleep
77+ // PTM ms ms2
78+ // `-----------' |
79+ // PLTM |
80+ // `----------------------'
81+ // dms |
82+ // |
83+
84+ // 現在の時刻
85+ long ms2 = System.currentTimeMillis();
86+
87+ // 直前の計測からのFPS
88+ double fps = (ms2 - previousTimeMs == 0) ? 1000 : (1000.0 / (ms2 - previousTimeMs));
89+
90+ // FPSを滑らかに反映
91+ smoothedFps = (1 - K) * smoothedFps + K * fps;
92+
93+ // 負荷率
94+ double loadFactor = (double)previousLoopTimeMs / dms;
95+
96+ // 負荷率を反映
97+ smoothedLoadFactor = (1 - K) * smoothedLoadFactor + K * loadFactor;
98+
99+ previousTimeMs = ms;
100+ }
101+
102+ public final double getLoadFactor()
103+ {
104+ if (smoothedLoadFactor == 0) return 1.0;
105+
106+ return smoothedLoadFactor;
107+ }
108+
109+ public final double getFPS()
110+ {
111+ if (smoothedFps == 0) return getSuitableFPS();
112+
113+ return smoothedFps;
114+ }
115+
116+ /**
117+ * 適正なFPS値を取得するオーバーライド可能なメソッドです。
118+ *
119+ * onLoopの呼び出し周期は、なるべくこのFPS値に従うように調整されます。
120+ */
121+ public double getSuitableFPS()
122+ {
123+ return 50;
124+ }
125+
126+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/image/TripleBufferListener.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/image/TripleBufferListener.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,8 @@
1+package mirrg.applet.antimony.image;
2+
3+public interface TripleBufferListener
4+{
5+
6+ void tripleBufferRebuffered();
7+
8+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/image/TripleBufferedApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/image/TripleBufferedApplet.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,150 @@
1+package mirrg.applet.antimony.image;
2+
3+import java.awt.Color;
4+import java.awt.Graphics;
5+import java.awt.Graphics2D;
6+import java.awt.event.ComponentEvent;
7+import java.awt.event.ComponentListener;
8+import java.awt.image.BufferedImage;
9+import java.util.LinkedList;
10+
11+import mirrg.applet.antimony.init.Initializer;
12+import mirrg.applet.antimony.thread.GameThreadApplet;
13+import mirrg.applet.antimony.thread.GameThreadListener;
14+
15+public class TripleBufferedApplet
16+extends GameThreadApplet
17+implements TripleBufferListener
18+{
19+
20+ private BufferedImage backBuffer, safetyBackBuffer;
21+
22+ private Graphics2D backGraphics, safetyBackGraphics;
23+
24+ private LinkedList<TripleBufferListener> tripleBufferListeners;
25+
26+
27+ public void addTripleBufferListener(TripleBufferListener l)
28+ {
29+ tripleBufferListeners.add(l);
30+ }
31+
32+
33+ @SuppressWarnings("all")
34+ public BufferedImage getBackBuffer()
35+ {
36+ return backBuffer;
37+ }
38+
39+ public Graphics2D getBackGraphics()
40+ {
41+ return backGraphics;
42+ }
43+
44+ @SuppressWarnings("all")
45+ public BufferedImage getSafetyBackBuffer()
46+ {
47+ return safetyBackBuffer;
48+ }
49+
50+ public Graphics2D getSafetyBackGraphics()
51+ {
52+ return safetyBackGraphics;
53+ }
54+
55+
56+ public int getBackBufferWidth()
57+ {
58+ return backBuffer.getWidth();
59+ }
60+
61+ public int getBackBufferHeight()
62+ {
63+ return backBuffer.getHeight();
64+ }
65+
66+
67+ private void rebuffer()
68+ {
69+
70+ if (getWidth() <= 0) return;
71+ if (getHeight() <= 0) return;
72+
73+
74+ backBuffer = new BufferedImage(
75+ getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
76+ backGraphics = backBuffer.createGraphics();
77+
78+ safetyBackBuffer = new BufferedImage(
79+ getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
80+ safetyBackGraphics = safetyBackBuffer.createGraphics();
81+
82+
83+ backGraphics.setBackground(Color.white);
84+ backGraphics.setColor(Color.black);
85+
86+ backGraphics.clearRect(0, 0, backBuffer.getWidth(), backBuffer.getHeight());
87+
88+
89+ for (TripleBufferListener l : tripleBufferListeners) {
90+ l.tripleBufferRebuffered();
91+ }
92+ }
93+
94+ @Initializer
95+ public void initTripleBufferedApplet()
96+ {
97+ tripleBufferListeners = new LinkedList<TripleBufferListener>();
98+ addTripleBufferListener(this);
99+
100+ Listener l = new Listener();
101+
102+ addGameThreadListener(l);
103+ addComponentListener(l);
104+
105+ rebuffer();
106+ }
107+
108+ @Override
109+ public void update(Graphics g)
110+ {
111+ paint(g);
112+ }
113+
114+ @Override
115+ public void paint(Graphics g)
116+ {
117+ g.drawImage(safetyBackBuffer, 0, 0, this);
118+ }
119+
120+ private class Listener
121+ implements GameThreadListener, ComponentListener
122+ {
123+
124+ @Override
125+ public void gameThreadDoEvents()
126+ {
127+ safetyBackGraphics.drawImage(backBuffer, 0, 0, TripleBufferedApplet.this);
128+ }
129+
130+ @Override
131+ public void componentHidden(ComponentEvent e) {}
132+
133+ @Override
134+ public void componentMoved(ComponentEvent e) {}
135+
136+ @Override
137+ public void componentResized(ComponentEvent e)
138+ {
139+ rebuffer();
140+ }
141+
142+ @Override
143+ public void componentShown(ComponentEvent e) {}
144+
145+ }
146+
147+ @Override
148+ public void tripleBufferRebuffered() {}
149+
150+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/init/Initializer.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/init/Initializer.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,13 @@
1+package mirrg.applet.antimony.init;
2+
3+import java.lang.annotation.ElementType;
4+import java.lang.annotation.Retention;
5+import java.lang.annotation.RetentionPolicy;
6+import java.lang.annotation.Target;
7+
8+@Target(ElementType.METHOD)
9+@Retention(RetentionPolicy.RUNTIME)
10+public @interface Initializer
11+{
12+
13+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/init/InitializingApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/init/InitializingApplet.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,84 @@
1+package mirrg.applet.antimony.init;
2+
3+import java.applet.Applet;
4+import java.lang.annotation.Annotation;
5+import java.lang.reflect.InvocationTargetException;
6+import java.lang.reflect.Method;
7+import java.util.ArrayList;
8+
9+public class InitializingApplet extends Applet
10+{
11+
12+ @Override
13+ @SuppressWarnings("unchecked")
14+ public final void init()
15+ {
16+
17+ Class<Applet> classApplet;
18+ Class<Initializer> classInitializer;
19+
20+ try {
21+ classApplet = (Class<Applet>)Class.forName("mirrg.applet.init.InitializingApplet");
22+ classInitializer = (Class<Initializer>)Class.forName("mirrg.applet.init.Initializer");
23+ } catch (ClassNotFoundException e) {
24+ e.printStackTrace();
25+ return;
26+ }
27+
28+ try {
29+ invokeAllAnnotatedMethodsFromStopperToSubclass(
30+ this, classApplet, classInitializer);
31+ } catch (IllegalAccessException e) {
32+ throw new RuntimeException(e);
33+ } catch (IllegalArgumentException e) {
34+ throw e;
35+ } catch (InvocationTargetException e) {
36+ throw new RuntimeException(e);
37+ }
38+
39+ }
40+
41+ public static void invokeAllAnnotatedMethodsFromStopperToSubclass(
42+ Object object,
43+ Class<?> stopper,
44+ Class<? extends Annotation> annotation)
45+ throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
46+ {
47+
48+ // 親クラスをstopperまで列挙
49+ ArrayList<Class<?>> superclasses = new ArrayList<>();
50+ {
51+ Class<?> c = object.getClass();
52+
53+ superclasses.add(0, c);
54+
55+ while (c != null) {
56+
57+ c = (Class<?>) c.getGenericSuperclass();
58+ superclasses.add(0, c);
59+
60+ if (c.equals(stopper)) {
61+ break;
62+ }
63+ }
64+ }
65+
66+ // 親クラスの
67+ for (Class<?> c : superclasses) {
68+
69+ // すべてのメソッドのうち
70+ for (Method m : c.getDeclaredMethods()) {
71+
72+ // Initializerアノテーションがついていれば
73+ if (m.isAnnotationPresent(annotation)) {
74+
75+ // 実行を試みる
76+ m.invoke(object);
77+
78+ }
79+ }
80+ }
81+
82+ }
83+
84+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/input/ManagedInputApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/input/ManagedInputApplet.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,180 @@
1+package mirrg.applet.antimony.input;
2+
3+import java.awt.AWTEvent;
4+import java.awt.event.ComponentEvent;
5+import java.awt.event.KeyEvent;
6+import java.awt.event.MouseEvent;
7+import java.awt.event.MouseWheelEvent;
8+
9+import mirrg.applet.antimony.image.TripleBufferedApplet;
10+import mirrg.applet.antimony.init.Initializer;
11+import mirrg.applet.antimony.thread.FiveEventsListener;
12+import mirrg.geom.PointInt;
13+
14+public class ManagedInputApplet
15+extends TripleBufferedApplet
16+{
17+
18+ /** マウスのボタン数です。 */
19+ public static final int MOUSEBUTTON_COUNT = 8;
20+
21+ /** キーボードのキー数です。 */
22+ public static final int KEYBOARD_COUNT = 1024;
23+
24+ /** 現在のマウスカーソルのX座標です。 */
25+ public int mx;
26+
27+ /** 直前のマウスカーソルのX座標です。 */
28+ public int mmx;
29+
30+ /** 現在のマウスカーソルのY座標です。 */
31+ public int my;
32+
33+ /** 直前のマウスカーソルのY座標です。 */
34+ public int mmy;
35+
36+ /** マウスボタンの状態です。 */
37+ public int[] mb;
38+
39+ /** キーの状態です。 */
40+ public int[] kb;
41+
42+ /** 現在のホイールの位置です。 */
43+ public int mw;
44+
45+ /** 直前のホイールの位置です。 */
46+ public int mmw;
47+
48+
49+ public PointInt getMousePosition()
50+ {
51+ return new PointInt(mx, my);
52+ }
53+
54+ @Initializer
55+ public void initManagedInputApplet()
56+ {
57+ Listener l = new Listener();
58+
59+ getEventBuffer().addFiveEventsListener(l);
60+
61+ mb = new int[MOUSEBUTTON_COUNT];
62+ kb = new int[KEYBOARD_COUNT];
63+ }
64+
65+ public void doInputEvents()
66+ {
67+ mmx = mx;
68+ mmy = my;
69+ mmw = mw;
70+
71+ for (int i = 0; i < mb.length; i++) {
72+ if (mb[i] > 0) mb[i]++;
73+ else if (mb[i] < 0) mb[i]--;
74+ }
75+
76+ for (int i = 0; i < kb.length; i++) {
77+ if (kb[i] > 0) kb[i]++;
78+ else if (kb[i] < 0) kb[i]--;
79+ }
80+ }
81+
82+ private class Listener
83+ implements FiveEventsListener
84+ {
85+
86+ @Override
87+ public void mouseClicked(MouseEvent e) {
88+
89+ }
90+
91+ @Override
92+ public void mouseEntered(MouseEvent e) {
93+
94+ }
95+
96+ @Override
97+ public void mouseExited(MouseEvent e) {
98+
99+ }
100+
101+ @Override
102+ public void mousePressed(MouseEvent e)
103+ {
104+ mx = e.getX();
105+ my = e.getY();
106+ if (mb[e.getButton()] <= 0) mb[e.getButton()] = 1;
107+ }
108+
109+ @Override
110+ public void mouseReleased(MouseEvent e)
111+ {
112+ mx = e.getX();
113+ my = e.getY();
114+ if (mb[e.getButton()] >= 0) mb[e.getButton()] = -1;
115+ }
116+
117+ @Override
118+ public void keyPressed(KeyEvent e)
119+ {
120+ if (kb[e.getKeyCode()] <= 0) kb[e.getKeyCode()] = 1;
121+ }
122+
123+ @Override
124+ public void keyReleased(KeyEvent e)
125+ {
126+ if (kb[e.getKeyCode()] >= 0) kb[e.getKeyCode()] = -1;
127+ }
128+
129+ @Override
130+ public void keyTyped(KeyEvent e) {
131+
132+ }
133+
134+ @Override
135+ public void mouseDragged(MouseEvent e)
136+ {
137+ mouseMoved(e);
138+ }
139+
140+ @Override
141+ public void mouseMoved(MouseEvent e)
142+ {
143+ mx = e.getX();
144+ my = e.getY();
145+ }
146+
147+ @Override
148+ public void mouseWheelMoved(MouseWheelEvent e)
149+ {
150+ mw += e.getWheelRotation();
151+ }
152+
153+ @Override
154+ public void componentHidden(ComponentEvent e) {
155+
156+ }
157+
158+ @Override
159+ public void componentMoved(ComponentEvent e) {
160+
161+ }
162+
163+ @Override
164+ public void componentResized(ComponentEvent e) {
165+
166+ }
167+
168+ @Override
169+ public void componentShown(ComponentEvent e) {
170+
171+ }
172+
173+ @Override
174+ public boolean anyEventCaught(AWTEvent e) {
175+ return false;
176+ }
177+
178+ }
179+
180+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/thread/EventBuffer.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/thread/EventBuffer.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,192 @@
1+package mirrg.applet.antimony.thread;
2+
3+import java.awt.AWTEvent;
4+import java.awt.event.ComponentEvent;
5+import java.awt.event.KeyEvent;
6+import java.awt.event.MouseEvent;
7+import java.awt.event.MouseWheelEvent;
8+import java.util.ConcurrentModificationException;
9+import java.util.Iterator;
10+import java.util.LinkedList;
11+
12+import mirrg.geom.util.EventFilter;
13+
14+
15+public class EventBuffer implements FiveEventsListener
16+{
17+
18+ private LinkedList<AWTEvent> events;
19+
20+ private LinkedList<FiveEventsListener> listeners;
21+
22+
23+ public EventBuffer()
24+ {
25+ events = new LinkedList<AWTEvent>();
26+ listeners = new LinkedList<FiveEventsListener>();
27+ }
28+
29+ /**
30+ * イベントをキューに追加します。
31+ */
32+ private synchronized void addEvent(AWTEvent e)
33+ {
34+ events.add(e);
35+ }
36+
37+ public void addFiveEventsListener(FiveEventsListener l)
38+ {
39+ listeners.addLast(l);
40+ }
41+
42+ /**
43+ * 現在のスレッドのままイベントを処理します。
44+ *
45+ * すべてのイベントを単一のイベントハンドラで受け取ります。
46+ */
47+ public void doEvents()
48+ {
49+ LinkedList<AWTEvent> events = this.events;
50+
51+ this.events = new LinkedList<AWTEvent>();
52+
53+
54+ for (FiveEventsListener l : listeners) {
55+
56+ try {
57+
58+ Iterator<AWTEvent> i = events.iterator();
59+
60+ while (i.hasNext()) {
61+
62+ try {
63+ AWTEvent event = i.next();
64+ EventFilter.eventFilter(event, l);
65+ } catch (NullPointerException e) {
66+ e.printStackTrace();
67+ }
68+
69+ }
70+
71+ } catch (ConcurrentModificationException e) {
72+ e.printStackTrace();
73+ }
74+
75+ }
76+
77+ }
78+
79+
80+ // ================== EVENT HANDLERS ===============-
81+
82+ // -------------------------------- ComponentListener
83+
84+ @Override
85+ public void componentResized(ComponentEvent e)
86+ {
87+ addEvent(e);
88+ }
89+
90+ @Override
91+ public void componentMoved(ComponentEvent e)
92+ {
93+ addEvent(e);
94+ }
95+
96+ @Override
97+ public void componentShown(ComponentEvent e)
98+ {
99+ addEvent(e);
100+ }
101+
102+ @Override
103+ public void componentHidden(ComponentEvent e)
104+ {
105+ addEvent(e);
106+ }
107+
108+
109+ // --------------------------------- MouseWheelListener
110+
111+ @Override
112+ public void mouseWheelMoved(MouseWheelEvent e)
113+ {
114+ addEvent(e);
115+ }
116+
117+
118+ // --------------------------------- MouseMotionListener
119+
120+ @Override
121+ public void mouseDragged(MouseEvent e)
122+ {
123+ addEvent(e);
124+ }
125+
126+ @Override
127+ public void mouseMoved(MouseEvent e)
128+ {
129+ addEvent(e);
130+ }
131+
132+
133+ // ----------------------------------- KeyListener
134+
135+ @Override
136+ public void keyTyped(KeyEvent e)
137+ {
138+ addEvent(e);
139+ }
140+
141+ @Override
142+ public void keyPressed(KeyEvent e)
143+ {
144+ addEvent(e);
145+ }
146+
147+ @Override
148+ public void keyReleased(KeyEvent e)
149+ {
150+ addEvent(e);
151+ }
152+
153+
154+ // ---------------------------------- MouseListener
155+
156+ @Override
157+ public void mouseClicked(MouseEvent e)
158+ {
159+ addEvent(e);
160+ }
161+
162+ @Override
163+ public void mousePressed(MouseEvent e)
164+ {
165+ addEvent(e);
166+ }
167+
168+ @Override
169+ public void mouseReleased(MouseEvent e)
170+ {
171+ addEvent(e);
172+ }
173+
174+ @Override
175+ public void mouseEntered(MouseEvent e)
176+ {
177+ addEvent(e);
178+ }
179+
180+ @Override
181+ public void mouseExited(MouseEvent e)
182+ {
183+ addEvent(e);
184+ }
185+
186+ @Override
187+ public boolean anyEventCaught(AWTEvent e)
188+ {
189+ return false;
190+ }
191+
192+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/thread/FiveEventsListener.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/thread/FiveEventsListener.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,25 @@
1+package mirrg.applet.antimony.thread;
2+
3+import java.awt.AWTEvent;
4+import java.awt.event.ComponentListener;
5+import java.awt.event.KeyListener;
6+import java.awt.event.MouseListener;
7+import java.awt.event.MouseMotionListener;
8+import java.awt.event.MouseWheelListener;
9+
10+public interface FiveEventsListener
11+extends MouseListener, KeyListener,
12+ MouseMotionListener, MouseWheelListener, ComponentListener
13+{
14+
15+ /**
16+ * どんなイベントの発生時においても呼び出されるイベントハンドラです。
17+ * 数種類のリスナーのハンドラよりも先に呼び出され、
18+ * このメソッドがtrueを返した場合は本来発生するべきイベントが再び発生しません。
19+ *
20+ * @return
21+ * イベントをキャンセルした場合にtrue
22+ */
23+ boolean anyEventCaught(AWTEvent e);
24+
25+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/thread/GameThreadApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/thread/GameThreadApplet.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,130 @@
1+package mirrg.applet.antimony.thread;
2+
3+import java.awt.AWTEvent;
4+import java.util.LinkedList;
5+
6+import mirrg.applet.antimony.event.AppletListener;
7+import mirrg.applet.antimony.event.EventReadyApplet;
8+import mirrg.applet.antimony.init.Initializer;
9+
10+public class GameThreadApplet
11+extends EventReadyApplet
12+implements Runnable, FiveEventsListener, GameThreadListener
13+{
14+
15+ private EventBuffer eventBuffer;
16+
17+ private Thread mainThread;
18+
19+ private LinkedList<GameThreadListener> gameThreadListeners;
20+
21+
22+ public void addGameThreadListener(GameThreadListener l)
23+ {
24+ gameThreadListeners.add(l);
25+ }
26+
27+ public EventBuffer getEventBuffer()
28+ {
29+ return eventBuffer;
30+ }
31+
32+ @Initializer
33+ public void initManagedThreadApplet()
34+ {
35+ gameThreadListeners = new LinkedList<GameThreadListener>();
36+ addGameThreadListener(this);
37+
38+ addAppletListener(new Listener());
39+
40+ removeMouseListener(this);
41+ removeMouseMotionListener(this);
42+ removeMouseWheelListener(this);
43+ removeKeyListener(this);
44+ removeComponentListener(this);
45+
46+
47+ eventBuffer = new EventBuffer();
48+
49+ addMouseListener(eventBuffer);
50+ addMouseMotionListener(eventBuffer);
51+ addMouseWheelListener(eventBuffer);
52+ addKeyListener(eventBuffer);
53+ addComponentListener(eventBuffer);
54+
55+ eventBuffer.addFiveEventsListener(this);
56+
57+ }
58+
59+ public void run()
60+ {
61+
62+ for (;;) {
63+
64+ doInputEvents();
65+
66+ eventBuffer.doEvents();
67+
68+ for (GameThreadListener l : gameThreadListeners) {
69+ l.gameThreadDoEvents();
70+ }
71+
72+ repaint();
73+
74+ try {
75+ sleepForNextTick();
76+ } catch (InterruptedException e) {
77+ System.err.println("thread interrupted");
78+ break;
79+ }
80+
81+
82+ }
83+
84+ }
85+
86+ public void doInputEvents()
87+ {
88+
89+ }
90+
91+ protected void sleepForNextTick() throws InterruptedException
92+ {
93+ Thread.sleep(20);
94+ }
95+
96+ private class Listener implements AppletListener
97+ {
98+
99+ @Override
100+ public void appletInitialized() {}
101+
102+ @Override
103+ public void appletStarted()
104+ {
105+ mainThread = new Thread(GameThreadApplet.this);
106+
107+ mainThread.start();
108+ }
109+
110+ @Override
111+ public void appletStopped()
112+ {
113+ mainThread.interrupt();
114+ }
115+
116+ @Override
117+ public void appletDestroyed() {}
118+
119+ }
120+
121+ @Override
122+ public boolean anyEventCaught(AWTEvent e)
123+ {
124+ return false;
125+ }
126+
127+ @Override
128+ public void gameThreadDoEvents() {}
129+
130+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/applet/antimony/thread/GameThreadListener.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/applet/antimony/thread/GameThreadListener.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,8 @@
1+package mirrg.applet.antimony.thread;
2+
3+public interface GameThreadListener
4+{
5+
6+ void gameThreadDoEvents();
7+
8+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/geom/PointDouble.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/geom/PointDouble.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,77 @@
1+package mirrg.geom;
2+
3+import java.awt.geom.Point2D.Double;
4+
5+public class PointDouble extends Double
6+{
7+
8+ public PointDouble(double x, double y)
9+ {
10+ super(x, y);
11+ }
12+
13+ public PointDouble(PointInt p)
14+ {
15+ super(p.x, p.y);
16+ }
17+
18+ public void bAdd(PointDouble p)
19+ {
20+ x += p.x;
21+ y += p.y;
22+ }
23+
24+ public void bSubtraction(PointDouble p)
25+ {
26+ x -= p.x;
27+ y -= p.y;
28+ }
29+
30+ public PointDouble ubAdd(PointDouble p)
31+ {
32+ return new PointDouble(x + p.x, y + p.y);
33+ }
34+
35+ public PointDouble ubSubtraction(PointDouble p)
36+ {
37+ return new PointDouble(x - p.x, y - p.y);
38+ }
39+
40+ public void bMultiplication(double a)
41+ {
42+ x *= a;
43+ y *= a;
44+ }
45+
46+ public void bDivision(double a)
47+ {
48+ x /= a;
49+ y /= a;
50+ }
51+
52+ public PointDouble ubMultiplication(double a)
53+ {
54+ return new PointDouble(x * a, y * a);
55+ }
56+
57+ public PointDouble ubDivision(double a)
58+ {
59+ return new PointDouble(x / a, y / a);
60+ }
61+
62+ public PointDouble clone()
63+ {
64+ return new PointDouble(x, y);
65+ }
66+
67+ public double getMagnitude()
68+ {
69+ return Math.sqrt(getMagnitudeSq());
70+ }
71+
72+ public double getMagnitudeSq()
73+ {
74+ return Math.pow(x, 2) + Math.pow(y, 2);
75+ }
76+
77+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/geom/PointInt.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/geom/PointInt.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,18 @@
1+package mirrg.geom;
2+
3+import java.awt.Point;
4+
5+public class PointInt extends Point
6+{
7+
8+ public PointInt(int x, int y)
9+ {
10+ super(x, y);
11+ }
12+
13+ public PointInt(Point p)
14+ {
15+ super(p.x, p.y);
16+ }
17+
18+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/geom/RectangleInt.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/geom/RectangleInt.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,43 @@
1+package mirrg.geom;
2+
3+import java.awt.Rectangle;
4+
5+public class RectangleInt extends Rectangle
6+{
7+
8+ public RectangleInt(int x, int y, int width, int height)
9+ {
10+ super(x, y, width, height);
11+ }
12+
13+ public RectangleInt(Rectangle rect)
14+ {
15+ super(rect.x, rect.y, rect.width, rect.height);
16+ }
17+
18+ public RectangleInt ubAdd(int x, int y, int width, int height)
19+ {
20+ return new RectangleInt(
21+ this.x + x,
22+ this.y + y,
23+ this.width + width,
24+ this.height + height);
25+ }
26+
27+ public RectangleInt ubSet(
28+ Integer x, Integer y, Integer width, Integer height)
29+ {
30+ return new RectangleInt(
31+ x != null ? x : this.x,
32+ y != null ? y : this.y,
33+ width != null ? width : this.width,
34+ height != null ? height : this.height);
35+ }
36+
37+ public RectangleInt ubMargin(int margin)
38+ {
39+ return new RectangleInt(x - margin, y - margin,
40+ width + 2 * margin, height + 2 * margin);
41+ }
42+
43+}
diff -r 6d83a8adb110 -r 7bd359456a3e mirrg.applet.antimony/src/mirrg/geom/util/EventFilter.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.antimony/src/mirrg/geom/util/EventFilter.java Thu Jul 02 07:20:47 2015 +0900
@@ -0,0 +1,79 @@
1+package mirrg.geom.util;
2+
3+import java.awt.AWTEvent;
4+import java.awt.event.ComponentEvent;
5+import java.awt.event.KeyEvent;
6+import java.awt.event.MouseEvent;
7+import java.awt.event.MouseWheelEvent;
8+
9+import mirrg.applet.antimony.thread.FiveEventsListener;
10+
11+public class EventFilter
12+{
13+
14+ public static void eventFilter(AWTEvent e, FiveEventsListener l)
15+ {
16+
17+ if (!l.anyEventCaught(e)) {
18+
19+ switch (e.getID()) {
20+
21+ case ComponentEvent.COMPONENT_RESIZED:
22+ l.componentResized((ComponentEvent)e);
23+ break;
24+ case ComponentEvent.COMPONENT_MOVED:
25+ l.componentMoved((ComponentEvent)e);
26+ break;
27+ case ComponentEvent.COMPONENT_SHOWN:
28+ l.componentShown((ComponentEvent)e);
29+ break;
30+ case ComponentEvent.COMPONENT_HIDDEN:
31+ l.componentHidden((ComponentEvent)e);
32+ break;
33+
34+ case MouseEvent.MOUSE_WHEEL:
35+ l.mouseWheelMoved((MouseWheelEvent)e);
36+ break;
37+
38+ case MouseEvent.MOUSE_MOVED:
39+ l.mouseMoved((MouseEvent)e);
40+ break;
41+ case MouseEvent.MOUSE_DRAGGED:
42+ l.mouseDragged((MouseEvent)e);
43+ break;
44+
45+ case KeyEvent.KEY_PRESSED:
46+ l.keyPressed((KeyEvent)e);
47+ break;
48+
49+ case KeyEvent.KEY_RELEASED:
50+ l.keyReleased((KeyEvent)e);
51+ break;
52+
53+ case KeyEvent.KEY_TYPED:
54+ l.keyTyped((KeyEvent)e);
55+ break;
56+
57+ case MouseEvent.MOUSE_CLICKED:
58+ l.mouseClicked((MouseEvent)e);
59+ break;
60+ case MouseEvent.MOUSE_ENTERED:
61+ l.mouseEntered((MouseEvent)e);
62+ break;
63+ case MouseEvent.MOUSE_EXITED:
64+ l.mouseExited((MouseEvent)e);
65+ break;
66+ case MouseEvent.MOUSE_PRESSED:
67+ l.mousePressed((MouseEvent)e);
68+ break;
69+ case MouseEvent.MOUSE_RELEASED:
70+ l.mouseReleased((MouseEvent)e);
71+ break;
72+
73+ }
74+
75+ }
76+
77+ }
78+
79+}