Guest User

1 second lag code

a guest
Nov 14th, 2012
49
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package standard;
  2.  
  3. import java.io.IOException;
  4. import org.newdawn.slick.opengl.Texture;
  5. import java.io.FileInputStream;
  6. import java.io.File;
  7. import java.util.Scanner;
  8. import java.util.HashMap;
  9. import org.lwjgl.Sys;
  10. import org.newdawn.slick.opengl.TextureLoader;
  11. import org.lwjgl.opengl.Display;
  12. import org.lwjgl.opengl.DisplayMode;
  13. import static org.lwjgl.opengl.GL11.*;
  14.  
  15. public class LWJGLTestStub {
  16.  
  17.     TextureLoader textureLoader;
  18.     int screenWidth = 900;
  19.     int screenHeight = 800;
  20.     private String WINDOW_TITLE;
  21.     private boolean fullscreen = true;
  22.     static boolean gameRunning = true;
  23.     static Sprite testSprite;
  24.     private static long timerTicksPerSecond = Sys.getTimerResolution();
  25.     private long lastLoopTime = getTime();
  26.     private long lastFpsTime;
  27.     private int fps;
  28.     int targetFramerate = 60;
  29.     int[][] frameInfo;
  30.     public static int tileEnd = 4;
  31.     public static int BUSH = 5;
  32.     public static int MOUSE = 6;
  33.     public static int GROUNDHOG = 7;
  34.     public static int GOLFER = 8;
  35.     public static int DESTINATION_ICON = 9;
  36.     public static int INVISIBLE = 10;
  37.     public static int ERROR = 11;
  38.     public static int WALL = 12;
  39.     HashMap<Integer, Sprite> allImages = new HashMap<Integer, Sprite>();
  40.  
  41.     public LWJGLTestStub() {
  42.         initialize();
  43.     }
  44.  
  45.     public static void main(String[] args) throws InterruptedException {
  46.         LWJGLTestStub rl = new LWJGLTestStub();
  47.         testSprite = rl.getSprite("images/bush.jpg");
  48.         System.out.println("LWJGLTestStub testsprite " + testSprite.getWidth());
  49.         testSprite.setDimensions(15, 15);
  50.         //while(true)
  51.         //{
  52.         while (LWJGLTestStub.gameRunning) {
  53.             //System.out.println("hrstjsrj");
  54.             long starttime = LWJGLTestStub.getTime();
  55.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  56.             glMatrixMode(GL_MODELVIEW);
  57.             glLoadIdentity();
  58.             //Display.sync(60);
  59.             long midtime1 = LWJGLTestStub.getTime();
  60.             rl.renderAndInput();
  61.             long midtime2 = LWJGLTestStub.getTime();
  62.             Display.update();
  63.             long endtime = LWJGLTestStub.getTime();
  64.  
  65.             if (endtime - starttime > 3 * 1000 / rl.targetFramerate) {
  66.                 System.out.println("LWJGLTestStub checking things: 1:" + (midtime1 - starttime) + " render:"
  67.                         + (midtime2 - midtime1) + " updatedisplay:" + (endtime - midtime2) + " objects being rendered " + objectsRendered);
  68.             }
  69.             //Thread.sleep(200);
  70.         }
  71.         Display.destroy();
  72.         //}
  73.     }
  74.  
  75.     /**
  76.      * Intialise the common elements for the game
  77.      */
  78.     public void initialize() {
  79.         // initialize the window beforehand
  80.         try {
  81.             frameInfo = new int[330][5];
  82.             Scanner sc = new Scanner(new File("testframes.txt"));
  83.             int count = 0;
  84.             while (sc.hasNextLine()) {
  85.                 count++;
  86.                 int count2 = 0;
  87.                 for (String s : sc.nextLine().split(" ")) {
  88.                     frameInfo[count][count2] = Integer.parseInt(s);
  89.                     count2++;
  90.                 }
  91.             }
  92.  
  93.             setDisplayMode();
  94.             Display.setTitle(WINDOW_TITLE);
  95.             Display.setFullscreen(fullscreen);
  96.             Display.create();
  97.             Display.setVSyncEnabled(true);
  98.             glEnable(GL_TEXTURE_2D);
  99.             glDisable(GL_DEPTH_TEST);
  100.             glMatrixMode(GL_PROJECTION);
  101.             glLoadIdentity();
  102.             glOrtho(0, screenWidth, screenHeight, 0, -1, 1);
  103.             glMatrixMode(GL_MODELVIEW);
  104.             glLoadIdentity();
  105.             glViewport(0, 0, screenWidth, screenHeight);
  106.             textureLoader = new TextureLoader();
  107.  
  108.             allImages = new HashMap<Integer, Sprite>();
  109.             HashMap<Integer, Sprite> tileImages = new HashMap<Integer, Sprite>();
  110.             tileImages.put(0, new Sprite(textureLoader, "images/G000M800.BMP"));
  111.             tileImages.put(1, new Sprite(textureLoader, "images/G000M801.BMP"));
  112.             tileImages.put(2, new Sprite(textureLoader, "images/G000M802.BMP"));
  113.             tileImages.put(3, new Sprite(textureLoader, "images/G000M803.BMP"));
  114.             tileImages.put(4, new Sprite(textureLoader, "images/water.jpg"));
  115.             allImages.putAll(tileImages);
  116.             allImages.put(BUSH, new Sprite(textureLoader, "images/bush.jpg"));
  117.             allImages.put(MOUSE, new Sprite(textureLoader, "images/mouse.png"));
  118.             allImages.put(GROUNDHOG, new Sprite(textureLoader, "images/groundhog.jpg"));
  119.             allImages.put(GOLFER, new Sprite(textureLoader, "images/golfer.jpg"));
  120.             allImages.put(INVISIBLE, new Sprite(textureLoader, "images/groundhog.jpg"));//invis, only have this line for testing
  121.             //allImages.put(SimpleGame.ERROR,new Sprite(textureLoader,"images/erroricon.png"));
  122.             allImages.put(SimpleGame.WALL, new Sprite(textureLoader, "images/wall.jpg"));
  123.         } catch (Exception e) {
  124.             e.printStackTrace();
  125.             throw new RuntimeException();
  126.         }
  127.  
  128.  
  129.     }
  130.  
  131.     private boolean setDisplayMode() {
  132.         try {
  133.             DisplayMode[] dm = Display.getAvailableDisplayModes();
  134.             Display.setDisplayMode(new DisplayMode(screenWidth, screenHeight));//dm[0]);//
  135.             return true;
  136.         } catch (Exception e) {
  137.             e.printStackTrace();
  138.             System.out.println("Unable to enter fullscreen, continuing in windowed mode");
  139.         }
  140.         return false;
  141.     }
  142.  
  143.     public static long getTime() {
  144.         return (Sys.getTime() * 1000) / timerTicksPerSecond;
  145.     }
  146.     static int objectsRendered = 0;
  147.  
  148.     public void renderAndInput() {
  149.         long delta = getTime() - lastLoopTime;
  150.         lastLoopTime = getTime();
  151.         lastFpsTime += delta;
  152.         fps++;
  153.         // update our FPS counter if a second has passed
  154.         if (lastFpsTime >= 1000) {
  155.             Display.setTitle(WINDOW_TITLE + " (FPS: " + fps + ") ");//+sg.theWorld.localGrid.processedLast);
  156.             lastFpsTime = 0;
  157.             fps = 0;
  158.         }
  159.         Display.sync(targetFramerate);
  160.         int size = 40;
  161.         testSprite.setDimensions(size, size);
  162.         objectsRendered = 0;
  163.         for (int i = 0; i < frameInfo.length; i++) {
  164.             Sprite s = allImages.get(frameInfo[i][0]);
  165.             s.setDimensions(frameInfo[i][3], frameInfo[i][4]);
  166.             s.draw(frameInfo[i][1], frameInfo[i][2]);
  167.         }
  168.         /*
  169.         for(int i=0;i<18;i++)
  170.         {
  171.         for(int j=0;j<18;j++)
  172.         {
  173.         testSprite.draw(i*size, j*size);
  174.         objectsRendered++;
  175.         }
  176.         }
  177.          *
  178.          */
  179.  
  180.     }
  181.  
  182.     public Sprite getSprite(String ref) {
  183.         return new Sprite(textureLoader, ref);
  184.     }
  185. }
  186.  
  187. class Sprite {
  188.  
  189.     /** The texture that stores the image for this sprite */
  190.     private Texture texture;
  191.     /** The width in pixels of this sprite */
  192.     private int width;
  193.     /** The height in pixels of this sprite */
  194.     private int height;
  195.  
  196.     /**
  197.      * Create a new sprite from a specified image.
  198.      *
  199.      * @param loader the texture loader to use
  200.      * @param ref A reference to the image on which this sprite should be based
  201.      */
  202.     public Sprite(TextureLoader loader, String ref) {
  203.         try {
  204.             texture = loader.getTexture(ref.substring(ref.lastIndexOf('.')), new FileInputStream(new File(ref)));//getTexture(ref);
  205.             width = texture.getImageWidth();
  206.             height = texture.getImageHeight();
  207.         } catch (IOException ioe) {
  208.             ioe.printStackTrace();
  209.             System.exit(-1);
  210.         }
  211.     }
  212.  
  213.     /**
  214.      * Get the width of this sprite in pixels
  215.      *
  216.      * @return The width of this sprite in pixels
  217.      */
  218.     public int getWidth() {
  219.         return texture.getImageWidth();
  220.     }
  221.  
  222.     /**
  223.      * Get the height of this sprite in pixels
  224.      *
  225.      * @return The height of this sprite in pixels
  226.      */
  227.     public int getHeight() {
  228.         return texture.getImageHeight();
  229.     }
  230.  
  231.     public void setDimensions(int w, int h) {
  232.         width = w;
  233.         height = h;
  234.     }
  235.  
  236.     /**
  237.      * Draw the sprite at the specified location
  238.      *
  239.      * @param x The x location at which to draw this sprite
  240.      * @param y The y location at which to draw this sprite
  241.      */
  242.     public void draw(int x, int y) {
  243.         // store the current model matrix
  244.         glPushMatrix();
  245.  
  246.         // bind to the appropriate texture for this sprite
  247.         texture.bind();
  248.  
  249.         // translate to the right location and prepare to draw
  250.         glTranslatef(x, y, 0);
  251.  
  252.         // draw a quad textured to match the sprite
  253.         glBegin(GL_QUADS);
  254.         {
  255.             glTexCoord2f(0, 0);
  256.             glVertex2f(0, 0);
  257.  
  258.             glTexCoord2f(0, texture.getHeight());
  259.             glVertex2f(0, height);
  260.  
  261.             glTexCoord2f(texture.getWidth(), texture.getHeight());
  262.             glVertex2f(width, height);
  263.  
  264.             glTexCoord2f(texture.getWidth(), 0);
  265.             glVertex2f(width, 0);
  266.         }
  267.         glEnd();
  268.  
  269.         // restore the model view matrix to prevent contamination
  270.         glPopMatrix();
  271.     }
  272. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×