Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.lwjgl.opengl.GL11.*;
- import java.applet.Applet;
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.io.IOException;
- import org.lwjgl.input.Keyboard;
- import org.lwjgl.input.Mouse;
- import org.lwjgl.opengl.*;
- import org.lwjgl.*;
- import org.newdawn.slick.opengl.Texture;
- import org.newdawn.slick.opengl.TextureLoader;
- public class Main extends Applet{
- static Camera cam;
- static float x=0;
- static Texture wood, grass;
- static int fps=0, upd=0;
- static int startTime;
- static int passedSec = 0;
- static int width, height;
- static Thread gameThread;
- static boolean running =false;
- //applet
- static Canvas display_parent;
- public void init(){
- setLayout(new BorderLayout());
- try {
- display_parent = new Canvas() {
- /**
- *
- */
- private static final long serialVersionUID = -8951216526237771039L;
- public final void addNotify() {
- super.addNotify();
- startOGL();
- }
- public final void removeNotify() {
- stopOGL();
- super.removeNotify();
- }
- };
- //display_parent.setSize(getWidth(),getHeight());
- setSize(640, 480);
- display_parent.setSize(640, 480);
- width = getWidth();
- height = getHeight();
- add(display_parent);
- display_parent.setFocusable(true);
- display_parent.requestFocus();
- display_parent.setIgnoreRepaint(true);
- setVisible(true);
- } catch (Exception e) {
- System.err.println(e);
- throw new RuntimeException("Unable to create display");
- }
- }
- public void start(){
- }
- public void stop(){
- }
- public void destroy() {
- remove(display_parent);
- super.destroy();
- }
- //endapplet
- public static void stopOGL() {
- running =false;
- try{
- gameThread.join();
- }catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- public static void startOGL() {
- gameThread = new Thread() {
- public void run(){
- running=true;
- initDisplay();
- startTime = (int) System.currentTimeMillis();
- gameLoop();
- cleanUp(); //STOP game
- }
- };
- gameThread.start();
- }
- public static void initDisplay()
- {
- try {
- //Display.setDisplayMode(new DisplayMode(640, 480));
- Display.setParent(display_parent);
- Display.create();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static Texture loadTexture(String path)
- {
- try {
- return TextureLoader.getTexture("png", Main.class.getResourceAsStream("res/"+path + ".png"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- public static void gameLoop()
- {
- cam = new Camera(70, (float)Display.getWidth()/(float)Display.getHeight(), 0.3F, 1000);
- wood = loadTexture("wood");
- grass = loadTexture("grass");
- Mouse.setGrabbed(true);
- while(running)
- {
- update();
- }
- Display.destroy();
- }
- static int lastCheck = 0;
- static int updates = 0;
- static boolean paused = false, lastPauseKey = false;
- private static void update()
- {
- updates++;
- passedSec = ((int)System.currentTimeMillis() - startTime)/60;
- if(passedSec - lastCheck >= 1)
- {
- upd = updates;
- //System.out.println("upd: " + upd + " || timepassed " + passedSec + " || lastCheck " + lastCheck);
- lastCheck = passedSec;
- updates = 0;
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_W))
- {
- cam.move(0.005f, 1);
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_S))
- {
- cam.move(-0.003f, 1);
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_A))
- {
- cam.move(0.003f, 0);
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_D))
- {
- cam.move(-0.003f, 0);
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_LEFT))
- {
- cam.rotateY(-0.03F);
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
- {
- cam.rotateY(0.03F);
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_UP))
- {
- cam.rotateX(-0.03F);
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
- {
- cam.rotateX(0.03F);
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && !lastPauseKey)
- {
- paused = !paused;
- Mouse.setGrabbed(!paused);
- System.out.println(paused);
- }
- lastPauseKey = Keyboard.isKeyDown(Keyboard.KEY_ESCAPE);
- if(!paused && (Mouse.getDX() != 0 || Mouse.getDY() != 0))
- handleMouseMove(Mouse.getX(), Mouse.getY());
- draw();
- }
- //COPIE
- static float camXRot=0,camYRot=90,camZRot=0;
- static void handleMouseMove(int mouseX, int mouseY)
- {
- float vertMouseSensitivity = 13.0f;
- float horizMouseSensitivity = 15.0f;
- int midWindowX = width/2;
- int midWindowY = height/2;
- //cout << "Mouse cursor is at position (" << mouseX << ", " << mouseY << endl;
- int horizMovement = mouseX - midWindowX;
- int vertMovement = mouseY - midWindowY;
- camXRot -= vertMovement / vertMouseSensitivity;
- camYRot += horizMovement / horizMouseSensitivity;
- // Control looking up and down with the mouse forward/back movement
- // Limit loking up to vertically up
- if (camXRot < -90.0f)
- {
- camXRot = -90.0f;
- }
- // Limit looking down to vertically down
- if (camXRot > 90.0f)
- {
- camXRot = 90.0f;
- }
- // Looking left and right. Keep the angles in the range -180.0f (anticlockwise turn looking behind) to 180.0f (clockwise turn looking behind)
- if (camYRot < -180.0f)
- {
- camYRot += 360.0f;
- }
- if (camYRot > 180.0f)
- {
- camYRot -= 360.0f;
- }
- // Reset the mouse position to the centre of the window each frame
- if(!paused)
- Mouse.setCursorPosition(midWindowX, midWindowY);
- cam.setRot(camXRot, camYRot, camZRot);
- }
- private static void draw()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- cam.useView();
- glPushMatrix();
- glRotatef(x,1,1,1);
- wood.bind();
- glBegin(GL_QUADS);
- {
- //Front
- glColor3f(1,0,0);
- glTexCoord2f(0,0);glVertex3f(-1,-1,1);
- glTexCoord2f(0,1);glVertex3f(-1,1,1);
- glTexCoord2f(1,1);glVertex3f(1, 1, 1);
- glTexCoord2f(1,0);glVertex3f(1,-1,1);
- //back
- glColor3f(0,1,0);
- glTexCoord2f(0,0);glVertex3f(-1,-1,-1);
- glTexCoord2f(0,1);glVertex3f(-1,1,-1);
- glTexCoord2f(1,1);glVertex3f(1, 1, -1);
- glTexCoord2f(1,0);glVertex3f(1,-1,-1);
- //bottom
- glColor3f(0,0,1);
- glTexCoord2f(0,0);glVertex3f(-1,-1,-1);
- glTexCoord2f(0,1);glVertex3f(-1,-1,1);
- glTexCoord2f(1,1);glVertex3f(-1,1,1);
- glTexCoord2f(1,0);glVertex3f(-1,1,-1);
- //top
- glColor3f(0,1,1);
- glTexCoord2f(0,0);glVertex3f(1,-1,-1);
- glTexCoord2f(0,1);glVertex3f(1,-1,1);
- glTexCoord2f(1,1);glVertex3f(1,1,1);
- glTexCoord2f(1,0);glVertex3f(1,1,-1);
- //left
- glColor3f(1,1,0);
- glTexCoord2f(0,0);glVertex3f(-1,-1,-1);
- glTexCoord2f(0,1);glVertex3f(1,-1,-1);
- glTexCoord2f(1,1);glVertex3f(1,-1,1);
- glTexCoord2f(1,0);glVertex3f(-1,-1,1);
- //left
- glColor3f(1,0,1);
- glTexCoord2f(0,0);glVertex3f(-1,1,-1);
- glTexCoord2f(0,1);glVertex3f(1,1,-1);
- glTexCoord2f(1,1);glVertex3f(1,1,1);
- glTexCoord2f(1,0);glVertex3f(-1,1,1);
- }
- glEnd();
- glPopMatrix();
- int xT = 10;
- int yT = 10;
- for(int i=0;i<xT; i++)
- {
- for(int y=0;y<yT; y++)
- {
- glPushMatrix();
- glTranslatef(-5 * xT/2, 0, -5 * yT/2);
- glColor3f(1,1,1);
- grass.bind();
- glBegin(GL_QUADS);
- {
- glTexCoord2f(0,0);glVertex3f(5*i, -2, 5*y);
- glTexCoord2f(0,1);glVertex3f(5*i, -2, 5*y + 5);
- glTexCoord2f(1,1);glVertex3f(5*i + 5, -2, 5*y + 5);
- glTexCoord2f(1,0);glVertex3f(5*i + 5, -2, 5*y);
- }
- glEnd();
- glPopMatrix();
- }
- }
- x+=0.1f;
- Display.update();
- }
- public static void cleanUp(){
- Display.destroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement