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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| public class GunCraft { private boolean done = false; Texture texture; Camera cam; private float yspeed; float legAngle; float playerX = 0; float playerZ = 0; float playerSpeed = 0.01f; float enemyX = 0; float enemyZ = 0; private TrueTypeFont font2;
public static void main(String args[]) { GunCraft gc = new GunCraft(); gc.run(); }
public void run() { try { init(); while (!done) { mainloop(); render(); Display.update(); } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } }
private void mainloop() { if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { done = true; } if(Display.isCloseRequested()) { done = true; } }
private void render() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); font2.drawString(100, 100, "NICE LOOKING FONTS!", Color.green);
loadIdentity(); drawRect(texture, 1f, 1f, 1f);
}
public void loadIdentity(){ GL11.glLoadIdentity(); GL11.glMatrixMode(GL11.GL_MODELVIEW); float camAngle = (float) Math.atan2(playerX, playerZ); float radius = 5; GLU.gluLookAt(playerX + (float)(Math.sin(camAngle) * radius), 0, playerZ + (float)(Math.cos(camAngle) * radius), 0, 0, 0, 0, 1, 0); }
public void drawRect(Texture texture, float width, float height, float depth){ texture.bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glNormal3f( 0.0f, 0.0f, 1.0f); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width, -height, depth); GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width, -height, depth); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width, height, depth); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width, height, depth); GL11.glNormal3f( 0.0f, 0.0f,-1.0f); GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height, -depth); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width, height, -depth); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width, height, -depth); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height, -depth); GL11.glNormal3f( 0.0f, 1.0f, 0.0f); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width, height, -depth); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width, height, depth); GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width, height, depth); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width, height, -depth); GL11.glNormal3f( 0.0f,-1.0f, 0.0f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width, -height, -depth); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width, -height, -depth); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height, depth); GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height, depth); GL11.glNormal3f( 1.0f, 0.0f, 0.0f); GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width, -height, -depth); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width, height, -depth); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width, height, depth); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height, depth); GL11.glNormal3f(-1.0f, 0.0f, 0.0f); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width, -height, -depth); GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height, depth); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width, height, depth); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width, height, -depth); GL11.glEnd(); }
private void createWindow() throws Exception { Display.setFullscreen(false); Display.setDisplayMode(new DisplayMode(640, 480)); Display.setTitle("GunCraft - By William Starkovich"); Display.create(); }
private void init() throws Exception { createWindow(); try{ FileInputStream fis; fis = new FileInputStream(new File("font.ttf"));
Font awtFont2 = Font.createFont(Font.TRUETYPE_FONT, fis); awtFont2 = awtFont2.deriveFont(24f); font2 = new TrueTypeFont(awtFont2, false);
} catch (Exception e) { e.printStackTrace(); }
initGL(640,480); }
private void initGL(int width, int height) { GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glShadeModel(GL11.GL_SMOOTH); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL11.glClearDepth(1.0); GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glDepthFunc(GL11.GL_LEQUAL); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GLU.gluPerspective( 45.0f, (float) Display.getDisplayMode().getWidth() / (float) Display.getDisplayMode().getHeight(), 0.1f, 100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, width, height, 0, -1000, 1000); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glDisable(GL11.GL_CULL_FACE);
try { texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("tex.png")); } catch (IOException e) { e.printStackTrace(); } }
private void cleanup() { Display.destroy(); }
} |