comparison game/Engine.java @ 25:bbac3e4a4b9b

Cleanups, etc.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 01 Feb 2011 18:05:19 +0200
parents 1be98362e5e9
children 3d4cc47df31a
comparison
equal deleted inserted replaced
24:1be98362e5e9 25:bbac3e4a4b9b
7 import java.awt.*; 7 import java.awt.*;
8 import java.awt.geom.*; 8 import java.awt.geom.*;
9 import java.awt.event.*; 9 import java.awt.event.*;
10 import java.awt.image.*; 10 import java.awt.image.*;
11 import java.awt.event.*; 11 import java.awt.event.*;
12 import java.awt.font.*;
12 import javax.imageio.*; 13 import javax.imageio.*;
13 import javax.swing.*; 14 import javax.swing.*;
14 import java.util.*; 15 import java.util.*;
15 import java.io.*; 16 import java.io.*;
16 import game.*; 17 import game.*;
192 193
193 194
194 public class Engine extends JPanel 195 public class Engine extends JPanel
195 implements Runnable, KeyListener, MouseListener 196 implements Runnable, KeyListener, MouseListener
196 { 197 {
198 long startTime;
199 float gameClock, gameFrames;
197 Thread animThread; 200 Thread animThread;
198 boolean animEnable = false; 201 boolean animEnable = false;
202
203 Font fontMain, font1, font2;
199 GameBoard lauta = null; 204 GameBoard lauta = null;
200 BufferedImage lautaBG = null, lautaBGScaled = null; 205 BufferedImage lautaBG = null, lautaBGScaled = null;
201 Dimension oldDim; 206 Dimension lautaDim;
202 float clock, frames;
203 207
204 SoundElement[] sounds; 208 SoundElement[] sounds;
209
205 public SoundElement snd(Sound snd) 210 public SoundElement snd(Sound snd)
206 { 211 {
207 return sounds[snd.ordinal()]; 212 return sounds[snd.ordinal()];
208 } 213 }
209 214
210 public Engine() 215 public Engine()
211 { 216 {
212 BufferedImage img; 217 // Initialize globals
213 clock = 0;
214
215 System.out.print("Engine() constructor\n"); 218 System.out.print("Engine() constructor\n");
216 219
220 gameClock = 0;
221 gameFrames = 0;
222 startTime = new Date().getTime();
223
224
225 // Load resources
217 try 226 try
218 { 227 {
219 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); 228 ResourceLoader res = new ResourceLoader("graphics/board.jpg");
220 lautaBG = ImageIO.read(res.getStream()); 229 lautaBG = ImageIO.read(res.getStream());
230
231 try {
232 res = new ResourceLoader("graphics/font.ttf");
233 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream());
234 font1 = fontMain.deriveFont(24f);
235 font2 = fontMain.deriveFont(32f);
236 }
237 catch (FontFormatException e)
238 {
239 System.out.print("Could not initialize fonts.\n");
240 }
221 241
222 sounds = new SoundElement[16]; 242 sounds = new SoundElement[16];
223 for (Sound s : Sound.values()) 243 for (Sound s : Sound.values())
224 { 244 {
225 System.out.print(s +" = "+ s.ordinal() +"\n"); 245 System.out.print(s +" = "+ s.ordinal() +"\n");
249 } 269 }
250 270
251 snd(Sound.MUSIC_GAME1).loop(-1); 271 snd(Sound.MUSIC_GAME1).loop(-1);
252 } 272 }
253 273
274
275 public void paintComponent(Graphics g)
276 {
277 Graphics2D g2 = (Graphics2D) g;
278
279 // Use antialiasing when rendering the game elements
280 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
281 RenderingHints.VALUE_ANTIALIAS_ON);
282
283 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
284 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
285
286 // Rescale background if component size has changed
287 Dimension dim = getSize();
288 if (lautaDim == null || !dim.equals(lautaDim))
289 {
290 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
291 Graphics2D gimg = lautaBGScaled.createGraphics();
292 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
293 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
294
295 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
296 lautaDim = dim;
297
298 System.out.print("scale changed\n");
299 }
300
301 // Background, pieces
302 g2.drawImage(lautaBGScaled, 0, 0, null);
303 lauta.paint(g2, 100, 150, 60);
304
305 // Scores
306 g2.setFont(font1);
307 g2.setPaint(Color.white);
308
309
310 // Other elements
311 long currTime = new Date().getTime();
312 g2.drawString("fps = "+ ((currTime - startTime) / gameFrames), dim.width - 120, 20);
313 }
314
254 public void startThreads() 315 public void startThreads()
255 { 316 {
256 System.out.print("startThreads()\n"); 317 System.out.print("startThreads()\n");
257 if (animThread == null) 318 if (animThread == null)
258 { 319 {
290 if (!hasFocus()) 351 if (!hasFocus())
291 { 352 {
292 System.out.print("requesting focus\n"); 353 System.out.print("requesting focus\n");
293 requestFocus(); 354 requestFocus();
294 } 355 }
295 }
296
297 public void paintComponent(Graphics g)
298 {
299 Graphics2D g2 = (Graphics2D) g;
300
301 // Use antialiasing when rendering the game elements
302 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
303 RenderingHints.VALUE_ANTIALIAS_ON);
304
305 // Rescale background if component size has changed
306 Dimension dim = getSize();
307 if (oldDim == null || !dim.equals(oldDim))
308 {
309 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
310 Graphics2D gimg = lautaBGScaled.createGraphics();
311 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
312 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
313
314 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
315 oldDim = dim;
316 System.out.print("scale changed\n");
317 }
318
319 // Background, pieces
320 g2.drawImage(lautaBGScaled, 0, 0, null);
321 lauta.paint(g2, 100, 150, 60);
322
323 // Other elements
324 } 356 }
325 357
326 public void keyTyped(KeyEvent e) 358 public void keyTyped(KeyEvent e)
327 { 359 {
328 } 360 }
355 387
356 public void run() 388 public void run()
357 { 389 {
358 while (animEnable) 390 while (animEnable)
359 { 391 {
360 clock++; 392 gameClock++;
361 393
362 lauta.animate(clock); 394 lauta.animate(gameClock);
363 395
364 if (clock % 3 == 1) 396 if (gameClock % 3 == 1)
365 { 397 {
366 repaint(); 398 repaint();
367 frames++; 399 gameFrames++;
368
369 if (frames % 10 == 1)
370 System.out.print("fps = "+ ((clock * 10) / frames) +"\n");
371 } 400 }
372 401
373 try { 402 try {
374 Thread.sleep(10); 403 Thread.sleep(10);
375 } 404 }