1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.microedition.lcdui.game;
21
22 import javax.microedition.lcdui.Canvas;
23 import javax.microedition.lcdui.Graphics;
24 import javax.microedition.lcdui.Image;
25
26 import org.microemu.GameCanvasKeyAccess;
27 import org.microemu.MIDletBridge;
28
29
30
31
32
33
34
35 public abstract class GameCanvas extends Canvas {
36
37 public static final int UP_PRESSED = 1 << Canvas.UP;
38 public static final int DOWN_PRESSED = 1 << Canvas.DOWN;
39 public static final int LEFT_PRESSED = 1 << Canvas.LEFT;
40 public static final int RIGHT_PRESSED = 1 << Canvas.RIGHT;
41 public static final int FIRE_PRESSED = 1 << Canvas.FIRE;
42 public static final int GAME_A_PRESSED = 1 << Canvas.GAME_A;
43 public static final int GAME_B_PRESSED = 1 << Canvas.GAME_B;
44 public static final int GAME_C_PRESSED = 1 << Canvas.GAME_C;
45 public static final int GAME_D_PRESSED = 1 << Canvas.GAME_D;
46
47
48
49 private boolean suppressKeyEvents;
50
51
52 private int latchedKeyState;
53
54
55
56 private int actualKeyState;
57
58 Image offscreenBuffer;
59
60 private class KeyAccess implements GameCanvasKeyAccess {
61 public boolean suppressedKeyEvents(GameCanvas canvas) {
62 return canvas.suppressKeyEvents;
63 }
64 public void recordKeyPressed(GameCanvas canvas, int gameCode) {
65 int bit = 1 << gameCode;
66 synchronized(canvas) {
67 latchedKeyState |= bit;
68 actualKeyState |= bit;
69 }
70 }
71
72 public void recordKeyReleased(GameCanvas canvas, int gameCode) {
73 int bit = 1 << gameCode;
74 synchronized(canvas) {
75 actualKeyState &= ~bit;
76 }
77 }
78
79 public void setActualKeyState(GameCanvas canvas, int keyState) {
80 synchronized(canvas) {
81 actualKeyState = keyState;
82 }
83 }
84
85 public void initBuffer() {
86 offscreenBuffer = Image.createImage(getWidth(), getHeight());
87 }
88 }
89
90
91 protected GameCanvas(boolean suppressKeyEvents)
92 {
93 if (MIDletBridge.getMIDletAccess().getGameCanvasKeyAccess() == null) {
94 MIDletBridge.getMIDletAccess().setGameCanvasKeyAccess(new KeyAccess());
95 }
96
97 this.suppressKeyEvents = suppressKeyEvents;
98
99
100
101
102 this.offscreenBuffer = Image.createImage(this.getWidth(), this.getHeight());
103 }
104
105 protected Graphics getGraphics() {
106 return offscreenBuffer.getGraphics();
107 }
108
109 public void paint(Graphics g) {
110 g.drawImage(offscreenBuffer, 0, 0, Graphics.TOP | Graphics.LEFT);
111 }
112
113 public void flushGraphics(int x, int y, int width, int height) {
114
115
116
117
118 repaint(x, y, width, height);
119 }
120
121 public void flushGraphics() {
122
123
124 repaint();
125 }
126
127 public int getKeyStates() {
128 synchronized(this) {
129 int ret = latchedKeyState;
130 latchedKeyState = actualKeyState;
131 return ret;
132 }
133 }
134
135
136
137 }
138
139