| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Screen |
|
| 1.9285714285714286;1.929 |
| 1 | /* |
|
| 2 | * ScreenPanel.java |
|
| 3 | * |
|
| 4 | * This file is part of JavaGear. |
|
| 5 | * |
|
| 6 | * JavaGear is free software; you can redistribute it and/or modify |
|
| 7 | * it under the terms of the GNU General Public License as published by |
|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|
| 9 | * (at your option) any later version. |
|
| 10 | * |
|
| 11 | * JavaGear is distributed in the hope that it will be useful, |
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 | * GNU General Public License for more details. |
|
| 15 | * |
|
| 16 | * You should have received a copy of the GNU General Public License |
|
| 17 | * along with JavaGear; if not, write to the Free Software |
|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
| 19 | */ |
|
| 20 | ||
| 21 | package uk.co.javagear; |
|
| 22 | ||
| 23 | import java.awt.Color; |
|
| 24 | import java.awt.Component; |
|
| 25 | import java.awt.Dimension; |
|
| 26 | import java.awt.Font; |
|
| 27 | import java.awt.Graphics; |
|
| 28 | import org.apache.log4j.Logger; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * Component to draw the emulated display. |
|
| 32 | * |
|
| 33 | * @author Copyright (C) 2002-2003 Chris White |
|
| 34 | * @version 18th January 2003 |
|
| 35 | * @see "JavaGear Final Project Report" |
|
| 36 | */ |
|
| 37 | public final class Screen extends Component { |
|
| 38 | ||
| 39 | /** |
|
| 40 | * Pointer to Graphics Context. |
|
| 41 | */ |
|
| 42 | private Graphics gfx; |
|
| 43 | ||
| 44 | /** |
|
| 45 | * Display FPS Counter. |
|
| 46 | */ |
|
| 47 | private boolean showFps; |
|
| 48 | ||
| 49 | /** |
|
| 50 | * FPS String. |
|
| 51 | */ |
|
| 52 | private String fps; |
|
| 53 | ||
| 54 | /** |
|
| 55 | * Font to use. |
|
| 56 | */ |
|
| 57 | private Font f; |
|
| 58 | ||
| 59 | /** |
|
| 60 | * Pointer to Fast Image Consumer. |
|
| 61 | */ |
|
| 62 | private FastImage fastImage; |
|
| 63 | ||
| 64 | /** |
|
| 65 | * Pixel has been updated? |
|
| 66 | */ |
|
| 67 | private boolean[] pixelUpdate; |
|
| 68 | ||
| 69 | /** |
|
| 70 | * Pointer to VDP. |
|
| 71 | */ |
|
| 72 | private Vdp vdp; |
|
| 73 | ||
| 74 | /** |
|
| 75 | * Pointer to General Parameters. |
|
| 76 | */ |
|
| 77 | private Setup setup; |
|
| 78 | ||
| 79 | private boolean forceUpdate; |
|
| 80 | ||
| 81 | /** |
|
| 82 | * ScreenPanel Constructor. |
|
| 83 | * |
|
| 84 | * @param set Pointer to General Parameters |
|
| 85 | * @param v Pointer to VDP |
|
| 86 | */ |
|
| 87 | 0 | public Screen(Setup set, Vdp v) { |
| 88 | 0 | this.setup = set; |
| 89 | 0 | this.vdp = v; |
| 90 | ||
| 91 | 0 | fastImage = new FastImage(setup.SMS_WIDTH, 1); |
| 92 | 0 | pixelUpdate = new boolean[setup.SMS_HEIGHT]; |
| 93 | ||
| 94 | // Do not display FPS Counter by default |
|
| 95 | 0 | fps = ""; |
| 96 | 0 | showFps = false; |
| 97 | ||
| 98 | // Font for FPS Counter |
|
| 99 | 0 | f = new Font("SansSerif", Font.BOLD, 12); |
| 100 | 0 | } |
| 101 | ||
| 102 | /** |
|
| 103 | * Refresh the screen. |
|
| 104 | */ |
|
| 105 | public void refresh() { |
|
| 106 | // Create Graphics Context for Component |
|
| 107 | 0 | if (gfx == null) { |
| 108 | 0 | gfx = getGraphics(); |
| 109 | } |
|
| 110 | ||
| 111 | 0 | switch (setup.getSystem()) { |
| 112 | case SMS: // Draw and Scale Image |
|
| 113 | // Flag Changes Since Last Screen Redraw |
|
| 114 | 0 | vdp.flagChanges(pixelUpdate); |
| 115 | // Draw each line and scale individually |
|
| 116 | // This yielded a speed increase over drawing one single 256x192 image |
|
| 117 | 0 | for (int y = setup.SMS_HEIGHT; y-- != 0;) { |
| 118 | // Check line has been updated |
|
| 119 | 0 | if (pixelUpdate[y] || forceUpdate) { |
| 120 | 0 | fastImage.update(vdp.getDisplay(), (y << 8)); |
| 121 | 0 | fastImage.paint(gfx, 0, y * setup.getScale() |
| 122 | , setup.getHEndScaled(), setup.getScale()); |
|
| 123 | } |
|
| 124 | } |
|
| 125 | 0 | break; |
| 126 | case GG: // Draw and Scale Centered Image |
|
| 127 | 0 | vdp.flagChanges(pixelUpdate); |
| 128 | 0 | for (int y = setup.GG_Y_OFFSET; y < setup.getVEnd(); y++) { |
| 129 | 0 | if (pixelUpdate[y] || forceUpdate) { |
| 130 | 0 | fastImage.update(vdp.getDisplay(), (y << 8)); |
| 131 | 0 | fastImage.paint(gfx, setup.getHStartScaled(), y * setup.getScale() |
| 132 | , setup.getHEndScaledGG(), y * setup.getScale() + setup.getScale() |
|
| 133 | , setup.GG_X_OFFSET, 0 |
|
| 134 | , setup.getHEnd(), 1); |
|
| 135 | } |
|
| 136 | } |
|
| 137 | 0 | break; |
| 138 | default: |
|
| 139 | 0 | Logger.getLogger(this.getClass()).fatal("Invalid system: " + setup.getSystem()); |
| 140 | } |
|
| 141 | ||
| 142 | 0 | if (forceUpdate) { |
| 143 | 0 | forceUpdate = false; |
| 144 | } |
|
| 145 | ||
| 146 | // Paint FPS Counter |
|
| 147 | 0 | if (showFps) { |
| 148 | // PAINT RECTANGLE |
|
| 149 | 0 | gfx.setColor(Color.blue); |
| 150 | 0 | gfx.fillRect(0, 0, 86, 20); |
| 151 | ||
| 152 | // PAINT FPS TEXT |
|
| 153 | 0 | gfx.setFont(f); |
| 154 | 0 | gfx.setColor(Color.white); |
| 155 | //gfx.drawString(fps, 1, 15); |
|
| 156 | 0 | gfx.drawString(getFPS(), 1, 15); |
| 157 | ||
| 158 | } |
|
| 159 | 0 | } |
| 160 | ||
| 161 | /** |
|
| 162 | * Paint FPS counter. |
|
| 163 | * |
|
| 164 | * @param b <code>true</code> to paint, <code>false</code> to not paint. |
|
| 165 | */ |
|
| 166 | public void paintFPS(boolean b) { |
|
| 167 | 0 | showFps = b; |
| 168 | 0 | } |
| 169 | ||
| 170 | ||
| 171 | /** |
|
| 172 | * Get frames per second. |
|
| 173 | * |
|
| 174 | * @return average frames rendered per second, with corresponding percentage. |
|
| 175 | */ |
|
| 176 | private String getFPS() { |
|
| 177 | 0 | return ((int) Throttle.getAverageFPS() |
| 178 | + " FPS (" + Throttle.getPercentage() + "%)"); |
|
| 179 | } |
|
| 180 | ||
| 181 | ||
| 182 | /** |
|
| 183 | * Repaint Screen When Window Obscured. |
|
| 184 | * |
|
| 185 | * @param g The graphics context to use for painting. |
|
| 186 | */ |
|
| 187 | public void paint(Graphics g) { |
|
| 188 | 0 | switch (setup.getSystem()) { |
| 189 | case SMS: |
|
| 190 | 0 | for (int y = setup.SMS_HEIGHT; y-- != 0;) { |
| 191 | 0 | fastImage.update(vdp.getDisplay(), (y << 8)); |
| 192 | 0 | fastImage.paint(g, 0, y * setup.getScale() |
| 193 | , setup.getHEndScaled(), setup.getScale()); |
|
| 194 | } |
|
| 195 | 0 | break; |
| 196 | case GG: |
|
| 197 | 0 | for (int y = setup.GG_Y_OFFSET; y < setup.getVEnd(); y++) { |
| 198 | 0 | fastImage.update(vdp.getDisplay(), (y << 8)); |
| 199 | 0 | fastImage.paint(g, setup.getHStartScaled() |
| 200 | , y * setup.getScale() |
|
| 201 | , setup.getHEndScaledGG() |
|
| 202 | , y * setup.getScale() + setup.getScale() |
|
| 203 | , setup.GG_X_OFFSET, 0, setup.getHEnd(), 1); |
|
| 204 | } |
|
| 205 | 0 | break; |
| 206 | default: |
|
| 207 | 0 | Logger.getLogger(this.getClass()).fatal("Invalid system: " + setup.getSystem()); |
| 208 | } |
|
| 209 | 0 | } |
| 210 | ||
| 211 | /** |
|
| 212 | * Force screen to update regardless of whether pixel changes have been made. |
|
| 213 | */ |
|
| 214 | public void forceUpdate() { |
|
| 215 | 0 | forceUpdate = true; |
| 216 | 0 | } |
| 217 | ||
| 218 | ||
| 219 | /** |
|
| 220 | * Overridden for optimisation. Does nothing. |
|
| 221 | * |
|
| 222 | * @param g The graphics context to use for painting. |
|
| 223 | */ |
|
| 224 | public void paintAll(Graphics g) { |
|
| 225 | 0 | } |
| 226 | ||
| 227 | ||
| 228 | /** |
|
| 229 | * Overridden for optimization. Does nothing. |
|
| 230 | * |
|
| 231 | * @param g The graphics context to use for painting. |
|
| 232 | */ |
|
| 233 | public void update(Graphics g) { |
|
| 234 | 0 | } |
| 235 | ||
| 236 | ||
| 237 | /** |
|
| 238 | * Overridden for optimization. Does nothing. |
|
| 239 | */ |
|
| 240 | public void repaint() { |
|
| 241 | 0 | } |
| 242 | ||
| 243 | /** |
|
| 244 | * Overridden for optimization. Does Nothing. |
|
| 245 | * |
|
| 246 | * @param x the <i>x</i> coordinate. |
|
| 247 | * @param y the <i>y</i> coordinate. |
|
| 248 | * @param width the width. |
|
| 249 | * @param height the height. |
|
| 250 | */ |
|
| 251 | public void repaint(int x, int y, int width, int height) { |
|
| 252 | 0 | } |
| 253 | ||
| 254 | ||
| 255 | /** |
|
| 256 | * Overridden for optimization. Does Nothing. |
|
| 257 | * |
|
| 258 | * @param tm maximum time in milliseconds before update |
|
| 259 | * @param x the <i>x</i> coordinate. |
|
| 260 | * @param y the <i>y</i> coordinate. |
|
| 261 | * @param width the width. |
|
| 262 | * @param height the height. |
|
| 263 | */ |
|
| 264 | public void repaint(long tm, int x, int y, int width, int height) { |
|
| 265 | 0 | } |
| 266 | ||
| 267 | ||
| 268 | /** |
|
| 269 | * Dispose of graphics context. Necessary to resize the screen. |
|
| 270 | */ |
|
| 271 | public void disposeGraphics() { |
|
| 272 | 0 | gfx = null; |
| 273 | 0 | forceUpdate = true; |
| 274 | 0 | } |
| 275 | ||
| 276 | ||
| 277 | /** |
|
| 278 | * Gets the preferred size of this component. |
|
| 279 | * |
|
| 280 | * @return A dimension object indicating this component's preferred size. |
|
| 281 | */ |
|
| 282 | public Dimension getPreferredSize() { |
|
| 283 | 0 | return getSize(); |
| 284 | } |
|
| 285 | ||
| 286 | ||
| 287 | /** |
|
| 288 | * Gets the mininimum size of this component. |
|
| 289 | * |
|
| 290 | * @return A dimension object indicating this component's minimum size. |
|
| 291 | */ |
|
| 292 | public Dimension getMinimumSize() { |
|
| 293 | 0 | return getPreferredSize(); |
| 294 | } |
|
| 295 | ||
| 296 | } |