SHOW:
|
|
- or go back to the newest paste.
1 | - | package net.hackforums.jonalu.gl11testA; |
1 | + | |
2 | ||
3 | import java.awt.Font; | |
4 | import java.io.InputStream; | |
5 | import java.util.Random; | |
6 | ||
7 | import org.lwjgl.LWJGLException; | |
8 | import org.lwjgl.Sys; | |
9 | import org.lwjgl.opengl.Display; | |
10 | import org.lwjgl.opengl.DisplayMode; | |
11 | import org.lwjgl.opengl.GL11; | |
12 | ||
13 | import org.newdawn.slick.Color; | |
14 | import org.newdawn.slick.TrueTypeFont; | |
15 | import org.newdawn.slick.util.ResourceLoader; | |
16 | ||
17 | public class FontExample { | |
18 | ||
19 | /** The fonts to draw to the screen */ | |
20 | private TrueTypeFont font; | |
21 | private TrueTypeFont font2; | |
22 | ||
23 | /** Boolean flag on whether AntiAliasing is enabled or not */ | |
24 | private boolean antiAlias = true; | |
25 | private long lastTime; | |
26 | private float angle; | |
27 | ||
28 | /** | |
29 | * Start the test | |
30 | */ | |
31 | public void start() { | |
32 | initGL(800,600); | |
33 | init(); | |
34 | ||
35 | while (true) { | |
36 | GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); | |
37 | render(); | |
38 | ||
39 | Display.update(); | |
40 | Display.sync(100); | |
41 | ||
42 | if (Display.isCloseRequested()) { | |
43 | Display.destroy(); | |
44 | System.exit(0); | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | /** | |
50 | * Initialise the GL display | |
51 | * | |
52 | * @param width The width of the display | |
53 | * @param height The height of the display | |
54 | */ | |
55 | private void initGL(int width, int height) { | |
56 | try { | |
57 | Display.setDisplayMode(new DisplayMode(width,height)); | |
58 | Display.create(); | |
59 | Display.setVSyncEnabled(true); | |
60 | } catch (LWJGLException e) { | |
61 | e.printStackTrace(); | |
62 | System.exit(0); | |
63 | } | |
64 | ||
65 | GL11.glEnable(GL11.GL_TEXTURE_2D); | |
66 | GL11.glShadeModel(GL11.GL_SMOOTH); | |
67 | GL11.glDisable(GL11.GL_DEPTH_TEST); | |
68 | GL11.glDisable(GL11.GL_LIGHTING); | |
69 | ||
70 | GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
71 | GL11.glClearDepth(1); | |
72 | ||
73 | GL11.glEnable(GL11.GL_BLEND); | |
74 | GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | |
75 | ||
76 | GL11.glViewport(0,0,width,height); | |
77 | GL11.glMatrixMode(GL11.GL_MODELVIEW); | |
78 | ||
79 | GL11.glMatrixMode(GL11.GL_PROJECTION); | |
80 | GL11.glLoadIdentity(); | |
81 | GL11.glOrtho(0, width, height, 0, 1, -1); | |
82 | GL11.glMatrixMode(GL11.GL_MODELVIEW); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Initialise resources | |
87 | */ | |
88 | public void init() { | |
89 | // load a default java font | |
90 | Font awtFont = new Font("Times New Roman", Font.BOLD, 24); | |
91 | font = new TrueTypeFont(awtFont, true); | |
92 | ||
93 | // load font from file | |
94 | /*try { | |
95 | InputStream inputStream = ResourceLoader.getResourceAsStream("myfont.ttf"); | |
96 | ||
97 | Font awtFont2 = Font.createFont(Font.TRUETYPE_FONT, inputStream); | |
98 | awtFont2 = awtFont2.deriveFont(24f); // set font size | |
99 | font2 = new TrueTypeFont(awtFont2, antiAlias); | |
100 | ||
101 | } catch (Exception e) { | |
102 | e.printStackTrace(); | |
103 | }/**/ | |
104 | } | |
105 | ||
106 | /** | |
107 | * Game loop render | |
108 | */ | |
109 | public void render() { | |
110 | GL11.glPushMatrix(); | |
111 | Color.white.bind(); | |
112 | //GL11.glScalef(0.1F, 0.1F, 0.1F); | |
113 | ||
114 | font.drawString(100, 50, "THE LIGHTWEIGHT JAVA GAMES LIBRARY", Color.yellow); | |
115 | GL11.glPopMatrix(); | |
116 | //GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); | |
117 | ||
118 | // Wenn man eine bestimmte Hintergrundfarbe will macht man das. Format RGBA | |
119 | //GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
120 | GL11.glPushMatrix(); | |
121 | //GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); | |
122 | //GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
123 | // Standard Matrix laden | |
124 | //GL11.glLoadIdentity(); | |
125 | ||
126 | // Bewege 10 nach unten und 60 nach hinten sodass wir was sehen | |
127 | GL11.glTranslatef((float) 0, -10.0f, -60.0f); | |
128 | ||
129 | // Unser aktuelle Zeit in ms | |
130 | long currentTime = Sys.getTime() * 1000 / Sys.getTimerResolution(); | |
131 | ||
132 | // Winkel zwischen 0 und 360 wachsen lassen | |
133 | angle += ((currentTime - lastTime) / 10.0f) % 360; | |
134 | ||
135 | // Zeitpunkt des letzten Frames ist jetzt | |
136 | lastTime = currentTime; | |
137 | ||
138 | // Rotiere um die Y-Achse um angle Grad | |
139 | //GL11.glRotatef(angle, 0.0f, 1.0f, 0.0f); | |
140 | ||
141 | GL11.glRotatef(angle, 0, 1, 0); | |
142 | ||
143 | // Farbe auswählen womit wir alle zukünftigen Ecken malen | |
144 | // Farben sind entweder in RGB oder RGBA format A = Alpha = Durchsichtigkeit | |
145 | // Bei gl______f handelt es sich um float Werte zwischen 0 und 1.0 | |
146 | // Alles andere z.B. gl______ub sollte man in Java vermeiden | |
147 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
148 | ||
149 | // Beginne mit einem Viereck | |
150 | GL11.glBegin(GL11.GL_QUADS); | |
151 | ||
152 | // Erste Ecke festlegen | |
153 | GL11.glVertex3f(-5,-5,5); | |
154 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
155 | ||
156 | // Zweite Ecke festlegen | |
157 | GL11.glVertex3f(5,-5,5); | |
158 | ||
159 | // Zwischendurch farbe wechseln | |
160 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
161 | ||
162 | // Dritte Ecke festlegen | |
163 | GL11.glVertex3f(5,5,5); | |
164 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
165 | ||
166 | // Vierte Ecke festlegen | |
167 | GL11.glVertex3f(-5,5,5); | |
168 | ||
169 | // Nach vier Ecken sind wir mit einem Viereck fertig... lol | |
170 | GL11.glEnd(); | |
171 | ||
172 | // Zweite Seite | |
173 | GL11.glBegin(GL11.GL_QUADS); | |
174 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
175 | GL11.glVertex3f(-5,-5,-5); | |
176 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
177 | GL11.glVertex3f(-5,5,-5); | |
178 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
179 | GL11.glVertex3f(5,5,-5); | |
180 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
181 | GL11.glVertex3f(5,-5,-5); | |
182 | GL11.glEnd(); | |
183 | ||
184 | // Dritte Seite | |
185 | GL11.glBegin(GL11.GL_QUADS); | |
186 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
187 | GL11.glVertex3f(5,-5,5); | |
188 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
189 | GL11.glVertex3f(5,5,5); | |
190 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
191 | GL11.glVertex3f(5,5,-5); | |
192 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
193 | GL11.glVertex3f(5,-5,-5); | |
194 | GL11.glEnd(); | |
195 | ||
196 | // Vierte Seite | |
197 | GL11.glBegin(GL11.GL_QUADS); | |
198 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
199 | GL11.glVertex3f(-5,-5,-5); | |
200 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
201 | GL11.glVertex3f(-5,5,-5); | |
202 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
203 | GL11.glVertex3f(-5,5,5); | |
204 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
205 | GL11.glVertex3f(-5,-5,5); | |
206 | GL11.glEnd(); | |
207 | ||
208 | // Fünfte Seite | |
209 | GL11.glBegin(GL11.GL_QUADS); | |
210 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
211 | GL11.glVertex3f(-5,5,-5); | |
212 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
213 | GL11.glVertex3f(5,5,-5); | |
214 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
215 | GL11.glVertex3f(5,5,5); | |
216 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
217 | GL11.glVertex3f(-5,5,5); | |
218 | GL11.glEnd(); | |
219 | ||
220 | // Sechste Seite | |
221 | GL11.glBegin(GL11.GL_QUADS); | |
222 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
223 | GL11.glVertex3f(-5,-5,5); | |
224 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
225 | GL11.glVertex3f(5,-5,5); | |
226 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
227 | GL11.glVertex3f(5,-5,-5); | |
228 | GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); | |
229 | GL11.glVertex3f(-5,-5,-5); | |
230 | GL11.glEnd(); | |
231 | GL11.glPopMatrix(); | |
232 | // Unser neuer Frame den Benutzer präsentieren | |
233 | } | |
234 | ||
235 | /** | |
236 | * Main method | |
237 | */ | |
238 | public static void main(String[] argv) { | |
239 | FontExample fontExample = new FontExample(); | |
240 | fontExample.start(); | |
241 | } | |
242 | Random rand = new Random(); | |
243 | } |