1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.lang.Runnable;
import java.lang.Thread;
import java.awt.Toolkit;
public class Subconscious
extends JPanel
implements Runnable, MouseListener, KeyListener, ComponentListener {
public static final String TITLE = "Subconscious";
public static final long RENDER_PERIOD_MS = 10;
/* graphics */
private JFrame window;
private Scene currentScene;
private Graphics dbg = null; // double buffered graphics
private Image dbImage = null; // double buffered image
private Thread animator = null;
/* game */
// private volatile boolean gamePaused = false;
private volatile boolean gameOver = false;
private volatile boolean running = false;
public Subconscious() {
createWindow();
// setup component
addMouseListener(this);
addKeyListener(this);
addComponentListener(this);
setFocusable(true);
requestFocus();
// TODO remove demo
currentScene = new WorldScene(new Dimension(200, 200), 50);
}
private void createWindow() {
window = new JFrame(TITLE);
window.setSize(new Dimension(800, 600));
window.setMinimumSize(getSize());
window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(this);
}
/* game logic */
private void gameLoop() {
}
private void gameRender() {
if (dbImage == null) {
dbImage = createImage(getWidth(), getHeight());
if (dbImage == null) {
System.out.println("ERROR: Cannot create image");
return;
}
}
dbg = dbImage.getGraphics();
dbg.fillRect(0, 0, getWidth(), getHeight());
currentScene.render(dbg);
}
public void gameStart() {
if (animator == null || !running) {
animator = new Thread(this);
animator.start();
}
}
/* animator and game thread */
@Override
public void run() {
long beforeTime, afterTime, sleepTime;
window.setVisible(true);
beforeTime = System.currentTimeMillis();
running = true;
while (running) {
gameLoop();
gameRender();
repaint();
// could be better with NanoSeconds, but this is
// probably enough for this
afterTime = System.currentTimeMillis();
sleepTime = RENDER_PERIOD_MS - (beforeTime - afterTime);
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ex) {}
beforeTime = System.currentTimeMillis();
}
window.dispose();
System.exit(0);
}
/* graphics */
@Override
public void paintComponent(Graphics g) {
if (dbImage != null) {
g.drawImage(dbImage, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync(); // needed on linux
g.dispose();
}
/* listeners */
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
@Override public void mouseClicked(MouseEvent e) {
currentScene.mouseClicked(e.getX(), e.getY());
}
@Override public void keyPressed(KeyEvent e) {}
@Override public void keyTyped(KeyEvent e) {}
@Override public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_Q && e.isControlDown()) {
running = false;
}
}
@Override public void componentHidden(ComponentEvent e) {}
@Override public void componentShown(ComponentEvent e) {}
@Override public void componentMoved(ComponentEvent e) {}
@Override public void componentResized(ComponentEvent e) {
dbImage = null; // create new image with the correct size
}
/* main */
public static void main(String args[]) {
Subconscious g = new Subconscious();
g.gameStart();
}
}
|