1
package com.brackeen.javagamebook.test;
4
import javax.swing.ImageIcon;
5
import javax.swing.JButton;
6
import javax.swing.JFrame;
8
import com.brackeen.javagamebook.graphics.NullRepaintManager;
9
import com.brackeen.javagamebook.graphics.ScreenManager;
12
Simple abstract class used for testing. Subclasses should
13
implement the draw() method.
15
public abstract class GameCore {
17
protected static final int FONT_SIZE = 24;
19
private static final DisplayMode POSSIBLE_MODES[] = {
20
new DisplayMode(800, 600, 16, 0),
21
new DisplayMode(800, 600, 32, 0),
22
new DisplayMode(800, 600, 24, 0),
23
new DisplayMode(640, 480, 16, 0),
24
new DisplayMode(640, 480, 32, 0),
25
new DisplayMode(640, 480, 24, 0),
26
new DisplayMode(1024, 768, 16, 0),
27
new DisplayMode(1024, 768, 32, 0),
28
new DisplayMode(1024, 768, 24, 0),
31
private boolean isRunning;
32
protected ScreenManager screen;
36
Signals the game loop that it's time to quit
44
Calls init() and gameLoop()
52
screen.restoreScreen();
59
Exits the VM from a daemon thread. The daemon thread waits
60
2 seconds then calls System.exit(0). Since the VM should
61
exit when only daemon threads are running, this makes sure
62
System.exit(0) is only called if neccesary. It's neccesary
63
if the Java Sound system is running.
65
public void lazilyExit() {
66
Thread thread = new Thread() {
68
// first, wait for the VM exit on its own.
72
catch (InterruptedException ex) { }
73
// system is still running, so force an exit
77
thread.setDaemon(true);
83
Sets full screen mode and initiates and objects.
86
screen = new ScreenManager();
87
DisplayMode displayMode =
88
screen.findFirstCompatibleMode(POSSIBLE_MODES);
89
screen.setFullScreen(displayMode);
90
NullRepaintManager.install();
91
Window window = screen.getFullScreenWindow();
92
window.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
93
window.setBackground(Color.blue);
94
window.setForeground(Color.white);
100
public Image loadImage(String fileName) {
101
return new ImageIcon(fileName).getImage();
106
Runs through the game loop until stop() is called.
108
public void gameLoop() {
109
long startTime = System.currentTimeMillis();
110
long currTime = startTime;
114
System.currentTimeMillis() - currTime;
115
currTime += elapsedTime;
122
Graphics2D g = screen.getGraphics();
124
JFrame frame = screen.getJFrame();
126
//this.setVisible(true);
130
// don't take a nap! run as fast as possible
134
catch (InterruptedException ex) { }*/
140
Updates the state of the game/animation based on the
141
amount of elapsed time that has passed.
143
public void update(long elapsedTime) {
149
Draws to the screen. Subclasses must override this
152
public abstract void draw(Graphics2D g);