~brzeti/patat/dev

« back to all changes in this revision

Viewing changes to src/com/brackeen/javagamebook/test/GameCore.java

  • Committer: Arthur Skonecki
  • Date: 2008-12-02 12:59:20 UTC
  • Revision ID: admin@adb.cba.pl-20081202125920-hmvdzcrxgq1tl514
initial project's files submission

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.brackeen.javagamebook.test;
 
2
 
 
3
import java.awt.*;
 
4
import javax.swing.ImageIcon;
 
5
import javax.swing.JButton;
 
6
import javax.swing.JFrame;
 
7
 
 
8
import com.brackeen.javagamebook.graphics.NullRepaintManager;
 
9
import com.brackeen.javagamebook.graphics.ScreenManager;
 
10
 
 
11
/**
 
12
    Simple abstract class used for testing. Subclasses should
 
13
    implement the draw() method.
 
14
*/
 
15
public abstract class GameCore {
 
16
 
 
17
    protected static final int FONT_SIZE = 24;
 
18
 
 
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),
 
29
    };
 
30
 
 
31
    private boolean isRunning;
 
32
    protected ScreenManager screen;
 
33
 
 
34
 
 
35
    /**
 
36
        Signals the game loop that it's time to quit
 
37
    */
 
38
    public void stop() {
 
39
        isRunning = false;
 
40
    }
 
41
 
 
42
 
 
43
    /**
 
44
        Calls init() and gameLoop()
 
45
    */
 
46
    public void run() {
 
47
        try {
 
48
            init();
 
49
            gameLoop();
 
50
        }
 
51
        finally {
 
52
            screen.restoreScreen();
 
53
            lazilyExit();
 
54
        }
 
55
    }
 
56
 
 
57
 
 
58
    /**
 
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.
 
64
    */
 
65
    public void lazilyExit() {
 
66
        Thread thread = new Thread() {
 
67
            public void run() {
 
68
                // first, wait for the VM exit on its own.
 
69
                try {
 
70
                    Thread.sleep(2000);
 
71
                }
 
72
                catch (InterruptedException ex) { }
 
73
                // system is still running, so force an exit
 
74
                System.exit(0);
 
75
            }
 
76
        };
 
77
        thread.setDaemon(true);
 
78
        thread.start();
 
79
    }
 
80
 
 
81
 
 
82
    /**
 
83
        Sets full screen mode and initiates and objects.
 
84
    */
 
85
    public void init() {
 
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);
 
95
 
 
96
        isRunning = true;
 
97
    }
 
98
 
 
99
 
 
100
    public Image loadImage(String fileName) {
 
101
        return new ImageIcon(fileName).getImage();
 
102
    }
 
103
 
 
104
 
 
105
    /**
 
106
        Runs through the game loop until stop() is called.
 
107
    */
 
108
    public void gameLoop() {
 
109
        long startTime = System.currentTimeMillis();
 
110
        long currTime = startTime;
 
111
 
 
112
        while (isRunning) {
 
113
            long elapsedTime =
 
114
                System.currentTimeMillis() - currTime;
 
115
            currTime += elapsedTime;
 
116
 
 
117
            // update
 
118
            update(elapsedTime);
 
119
 
 
120
            // draw the screen
 
121
            
 
122
            Graphics2D g = screen.getGraphics();
 
123
            draw(g);
 
124
            JFrame frame = screen.getJFrame();
 
125
            
 
126
            //this.setVisible(true);
 
127
            g.dispose();
 
128
            screen.update();
 
129
 
 
130
            // don't take a nap! run as fast as possible
 
131
            /*try {
 
132
                Thread.sleep(20);
 
133
            }
 
134
            catch (InterruptedException ex) { }*/
 
135
        }
 
136
    }
 
137
 
 
138
 
 
139
    /**
 
140
        Updates the state of the game/animation based on the
 
141
        amount of elapsed time that has passed.
 
142
    */
 
143
    public void update(long elapsedTime) {
 
144
        // do nothing
 
145
    }
 
146
 
 
147
 
 
148
    /**
 
149
        Draws to the screen. Subclasses must override this
 
150
        method.
 
151
    */
 
152
    public abstract void draw(Graphics2D g);
 
153
}