Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/CanvasGameContainer.java
1456 views
1
package org.newdawn.slick;
2
3
import java.awt.Canvas;
4
5
import javax.swing.SwingUtilities;
6
7
import org.lwjgl.LWJGLException;
8
import org.lwjgl.opengl.Display;
9
import org.newdawn.slick.util.Log;
10
11
/**
12
* A game container that displays the game on an AWT Canvas.
13
*
14
* @author kevin
15
*/
16
public class CanvasGameContainer extends Canvas {
17
/** The actual container implementation */
18
protected Container container;
19
/** The game being held in this container */
20
protected Game game;
21
22
/**
23
* Create a new panel
24
*
25
* @param game The game being held
26
* @throws SlickException Indicates a failure during creation of the container
27
*/
28
public CanvasGameContainer(Game game) throws SlickException {
29
this(game, false);
30
}
31
32
/**
33
* Create a new panel
34
*
35
* @param game The game being held
36
* @param shared True if shared GL context should be enabled. This allows multiple panels
37
* to share textures and other GL resources.
38
* @throws SlickException Indicates a failure during creation of the container
39
*/
40
public CanvasGameContainer(Game game, boolean shared) throws SlickException {
41
super();
42
43
this.game = game;
44
setIgnoreRepaint(true);
45
requestFocus();
46
setSize(500,500);
47
48
container = new Container(game, shared);
49
container.setForceExit(false);
50
}
51
52
/**
53
* Start the game container rendering
54
*
55
* @throws SlickException Indicates a failure during game execution
56
*/
57
public void start() throws SlickException {
58
SwingUtilities.invokeLater(new Runnable() {
59
public void run() {
60
try {
61
Input.disableControllers();
62
63
try {
64
Display.setParent(CanvasGameContainer.this);
65
} catch (LWJGLException e) {
66
throw new SlickException("Failed to setParent of canvas", e);
67
}
68
69
container.setup();
70
scheduleUpdate();
71
} catch (SlickException e) {
72
e.printStackTrace();
73
System.exit(0);
74
}
75
}
76
});
77
}
78
79
/**
80
* Schedule an update on the EDT
81
*/
82
private void scheduleUpdate() {
83
if (!isVisible()) {
84
return;
85
}
86
87
SwingUtilities.invokeLater(new Runnable() {
88
public void run() {
89
try {
90
container.gameLoop();
91
} catch (SlickException e) {
92
e.printStackTrace();
93
}
94
container.checkDimensions();
95
scheduleUpdate();
96
}
97
});
98
}
99
/**
100
* Dispose the container and any resources it holds
101
*/
102
public void dispose() {
103
}
104
105
/**
106
* Get the GameContainer providing this canvas
107
*
108
* @return The game container providing this canvas
109
*/
110
public GameContainer getContainer() {
111
return container;
112
}
113
114
/**
115
* A game container to provide the canvas context
116
*
117
* @author kevin
118
*/
119
private class Container extends AppGameContainer {
120
/**
121
* Create a new container wrapped round the game
122
*
123
* @param game
124
* The game to be held in this container
125
* @param shared True if shared GL context should be enabled. This allows multiple panels
126
* to share textures and other GL resources.
127
* @throws SlickException Indicates a failure to initialise
128
*/
129
public Container(Game game, boolean shared) throws SlickException {
130
super(game, CanvasGameContainer.this.getWidth(), CanvasGameContainer.this.getHeight(), false);
131
132
width = CanvasGameContainer.this.getWidth();
133
height = CanvasGameContainer.this.getHeight();
134
135
if (shared) {
136
enableSharedContext();
137
}
138
}
139
140
/**
141
* Updated the FPS counter
142
*/
143
protected void updateFPS() {
144
super.updateFPS();
145
}
146
147
/**
148
* @see org.newdawn.slick.GameContainer#running()
149
*/
150
protected boolean running() {
151
return super.running() && CanvasGameContainer.this.isDisplayable();
152
}
153
154
/**
155
* @see org.newdawn.slick.GameContainer#getHeight()
156
*/
157
public int getHeight() {
158
return CanvasGameContainer.this.getHeight();
159
}
160
161
/**
162
* @see org.newdawn.slick.GameContainer#getWidth()
163
*/
164
public int getWidth() {
165
return CanvasGameContainer.this.getWidth();
166
}
167
168
/**
169
* Check the dimensions of the canvas match the display
170
*/
171
public void checkDimensions() {
172
if ((width != CanvasGameContainer.this.getWidth()) ||
173
(height != CanvasGameContainer.this.getHeight())) {
174
175
try {
176
setDisplayMode(CanvasGameContainer.this.getWidth(),
177
CanvasGameContainer.this.getHeight(), false);
178
} catch (SlickException e) {
179
Log.error(e);
180
}
181
}
182
}
183
}
184
}
185
186