View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2005 Andres Navarro
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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   * @author Andres Navarro
33   * @author radoshi
34   */
35  public abstract class GameCanvas extends Canvas {
36      // keystate constants
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      // true if this GameCanvas doesn't generate
48      // key events for the game keys
49      private boolean suppressKeyEvents;
50      // the latched state of the keys
51      // reseted on call to getKeyStates
52      private int latchedKeyState;
53      // the current keys state
54      // this is copied to latchedKeyState
55      // on call to getKeyState
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      /** Creates a new instance of GameCanvas */
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          // never should the size of the Canvas become greater than this
99          // if the size reported by Canvas.getXXX() isn't the maximum for
100         // a given Canvas implementation other methods should be used to
101         // obtain the maximum possible size for such particular implementation
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         // i dont know if this is correct, the specification is a little
115         // vague here.
116         // For example this methods fail if the paint method is
117         // overriden with something else
118         repaint(x, y, width, height);
119     }
120 
121     public void flushGraphics() {
122     	
123         // NOTE see comment at the other flushGraphics method
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     // TODO
136     // should isDoubleBuffered from class Canvas return true in this class??
137 }
138 
139