Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package standard;
- import java.io.IOException;
- import org.newdawn.slick.opengl.Texture;
- import java.io.FileInputStream;
- import java.io.File;
- import java.util.Scanner;
- import java.util.HashMap;
- import org.lwjgl.Sys;
- import org.newdawn.slick.opengl.TextureLoader;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import static org.lwjgl.opengl.GL11.*;
- public class LWJGLTestStub {
- TextureLoader textureLoader;
- int screenWidth = 900;
- int screenHeight = 800;
- private String WINDOW_TITLE;
- private boolean fullscreen = true;
- static boolean gameRunning = true;
- static Sprite testSprite;
- private static long timerTicksPerSecond = Sys.getTimerResolution();
- private long lastLoopTime = getTime();
- private long lastFpsTime;
- private int fps;
- int targetFramerate = 60;
- int[][] frameInfo;
- public static int tileEnd = 4;
- public static int BUSH = 5;
- public static int MOUSE = 6;
- public static int GROUNDHOG = 7;
- public static int GOLFER = 8;
- public static int DESTINATION_ICON = 9;
- public static int INVISIBLE = 10;
- public static int ERROR = 11;
- public static int WALL = 12;
- HashMap<Integer, Sprite> allImages = new HashMap<Integer, Sprite>();
- public LWJGLTestStub() {
- initialize();
- }
- public static void main(String[] args) throws InterruptedException {
- LWJGLTestStub rl = new LWJGLTestStub();
- testSprite = rl.getSprite("images/bush.jpg");
- System.out.println("LWJGLTestStub testsprite " + testSprite.getWidth());
- testSprite.setDimensions(15, 15);
- //while(true)
- //{
- while (LWJGLTestStub.gameRunning) {
- //System.out.println("hrstjsrj");
- long starttime = LWJGLTestStub.getTime();
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- //Display.sync(60);
- long midtime1 = LWJGLTestStub.getTime();
- rl.renderAndInput();
- long midtime2 = LWJGLTestStub.getTime();
- Display.update();
- long endtime = LWJGLTestStub.getTime();
- if (endtime - starttime > 3 * 1000 / rl.targetFramerate) {
- System.out.println("LWJGLTestStub checking things: 1:" + (midtime1 - starttime) + " render:"
- + (midtime2 - midtime1) + " updatedisplay:" + (endtime - midtime2) + " objects being rendered " + objectsRendered);
- }
- //Thread.sleep(200);
- }
- Display.destroy();
- //}
- }
- /**
- * Intialise the common elements for the game
- */
- public void initialize() {
- // initialize the window beforehand
- try {
- frameInfo = new int[330][5];
- Scanner sc = new Scanner(new File("testframes.txt"));
- int count = 0;
- while (sc.hasNextLine()) {
- count++;
- int count2 = 0;
- for (String s : sc.nextLine().split(" ")) {
- frameInfo[count][count2] = Integer.parseInt(s);
- count2++;
- }
- }
- setDisplayMode();
- Display.setTitle(WINDOW_TITLE);
- Display.setFullscreen(fullscreen);
- Display.create();
- Display.setVSyncEnabled(true);
- glEnable(GL_TEXTURE_2D);
- glDisable(GL_DEPTH_TEST);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, screenWidth, screenHeight, 0, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glViewport(0, 0, screenWidth, screenHeight);
- textureLoader = new TextureLoader();
- allImages = new HashMap<Integer, Sprite>();
- HashMap<Integer, Sprite> tileImages = new HashMap<Integer, Sprite>();
- tileImages.put(0, new Sprite(textureLoader, "images/G000M800.BMP"));
- tileImages.put(1, new Sprite(textureLoader, "images/G000M801.BMP"));
- tileImages.put(2, new Sprite(textureLoader, "images/G000M802.BMP"));
- tileImages.put(3, new Sprite(textureLoader, "images/G000M803.BMP"));
- tileImages.put(4, new Sprite(textureLoader, "images/water.jpg"));
- allImages.putAll(tileImages);
- allImages.put(BUSH, new Sprite(textureLoader, "images/bush.jpg"));
- allImages.put(MOUSE, new Sprite(textureLoader, "images/mouse.png"));
- allImages.put(GROUNDHOG, new Sprite(textureLoader, "images/groundhog.jpg"));
- allImages.put(GOLFER, new Sprite(textureLoader, "images/golfer.jpg"));
- allImages.put(INVISIBLE, new Sprite(textureLoader, "images/groundhog.jpg"));//invis, only have this line for testing
- //allImages.put(SimpleGame.ERROR,new Sprite(textureLoader,"images/erroricon.png"));
- allImages.put(SimpleGame.WALL, new Sprite(textureLoader, "images/wall.jpg"));
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException();
- }
- }
- private boolean setDisplayMode() {
- try {
- DisplayMode[] dm = Display.getAvailableDisplayModes();
- Display.setDisplayMode(new DisplayMode(screenWidth, screenHeight));//dm[0]);//
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("Unable to enter fullscreen, continuing in windowed mode");
- }
- return false;
- }
- public static long getTime() {
- return (Sys.getTime() * 1000) / timerTicksPerSecond;
- }
- static int objectsRendered = 0;
- public void renderAndInput() {
- long delta = getTime() - lastLoopTime;
- lastLoopTime = getTime();
- lastFpsTime += delta;
- fps++;
- // update our FPS counter if a second has passed
- if (lastFpsTime >= 1000) {
- Display.setTitle(WINDOW_TITLE + " (FPS: " + fps + ") ");//+sg.theWorld.localGrid.processedLast);
- lastFpsTime = 0;
- fps = 0;
- }
- Display.sync(targetFramerate);
- int size = 40;
- testSprite.setDimensions(size, size);
- objectsRendered = 0;
- for (int i = 0; i < frameInfo.length; i++) {
- Sprite s = allImages.get(frameInfo[i][0]);
- s.setDimensions(frameInfo[i][3], frameInfo[i][4]);
- s.draw(frameInfo[i][1], frameInfo[i][2]);
- }
- /*
- for(int i=0;i<18;i++)
- {
- for(int j=0;j<18;j++)
- {
- testSprite.draw(i*size, j*size);
- objectsRendered++;
- }
- }
- *
- */
- }
- public Sprite getSprite(String ref) {
- return new Sprite(textureLoader, ref);
- }
- }
- class Sprite {
- /** The texture that stores the image for this sprite */
- private Texture texture;
- /** The width in pixels of this sprite */
- private int width;
- /** The height in pixels of this sprite */
- private int height;
- /**
- * Create a new sprite from a specified image.
- *
- * @param loader the texture loader to use
- * @param ref A reference to the image on which this sprite should be based
- */
- public Sprite(TextureLoader loader, String ref) {
- try {
- texture = loader.getTexture(ref.substring(ref.lastIndexOf('.')), new FileInputStream(new File(ref)));//getTexture(ref);
- width = texture.getImageWidth();
- height = texture.getImageHeight();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- System.exit(-1);
- }
- }
- /**
- * Get the width of this sprite in pixels
- *
- * @return The width of this sprite in pixels
- */
- public int getWidth() {
- return texture.getImageWidth();
- }
- /**
- * Get the height of this sprite in pixels
- *
- * @return The height of this sprite in pixels
- */
- public int getHeight() {
- return texture.getImageHeight();
- }
- public void setDimensions(int w, int h) {
- width = w;
- height = h;
- }
- /**
- * Draw the sprite at the specified location
- *
- * @param x The x location at which to draw this sprite
- * @param y The y location at which to draw this sprite
- */
- public void draw(int x, int y) {
- // store the current model matrix
- glPushMatrix();
- // bind to the appropriate texture for this sprite
- texture.bind();
- // translate to the right location and prepare to draw
- glTranslatef(x, y, 0);
- // draw a quad textured to match the sprite
- glBegin(GL_QUADS);
- {
- glTexCoord2f(0, 0);
- glVertex2f(0, 0);
- glTexCoord2f(0, texture.getHeight());
- glVertex2f(0, height);
- glTexCoord2f(texture.getWidth(), texture.getHeight());
- glVertex2f(width, height);
- glTexCoord2f(texture.getWidth(), 0);
- glVertex2f(width, 0);
- }
- glEnd();
- // restore the model view matrix to prevent contamination
- glPopMatrix();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement