Advertisement
Guest User

Untitled

a guest
Oct 21st, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package com.starshock;
  2.  
  3. import java.io.File;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;
  6. import java.nio.IntBuffer;
  7.  
  8. import org.lwjgl.LWJGLException;
  9. import org.lwjgl.LWJGLUtil;
  10. import org.lwjgl.Sys;
  11. import org.lwjgl.opengl.Display;
  12. import org.lwjgl.opengl.DisplayMode;
  13. import static org.lwjgl.opengl.GL11.*;
  14. import static org.lwjgl.opengl.GL30.*;
  15. import static org.lwjgl.util.glu.GLU.*;
  16.  
  17. public class Main {
  18.  
  19.     static {
  20.         System.setProperty("org.lwjgl.librarypath",
  21.                 new File(new File(System.getProperty("user.dir"), "native"),
  22.                         LWJGLUtil.getPlatformName()).getAbsolutePath());
  23.     }
  24.  
  25.     public static final int S_WIDTH = 320, S_HEIGHT = 200, S_SCALE = 2;
  26.  
  27.     public long lastFrame, lastFPS;
  28.     public int fps;
  29.     public int defaultFBO = 0, drawFBO, drawFBOTexture;
  30.  
  31.     public void start() {
  32.         try {
  33.             Display.setDisplayMode(new DisplayMode(S_WIDTH * S_SCALE, S_HEIGHT
  34.                     * S_SCALE));
  35.             Display.setTitle("Starhack - FPS: " + fps);
  36.             Display.create();
  37.         } catch (LWJGLException e) {
  38.             e.printStackTrace();
  39.             System.exit(0);
  40.         }
  41.  
  42.         initFBO();
  43.  
  44.         getDelta();
  45.         lastFPS = getTime();
  46.  
  47.         while (!Display.isCloseRequested()) {
  48.             int delta = getDelta();
  49.  
  50.             update(delta);
  51.             renderGL();
  52.  
  53.             Display.update();
  54.             Display.sync(60);
  55.         }
  56.  
  57.         Display.destroy();
  58.     }
  59.  
  60.     public void update(int delta) {
  61.         updateFPS();
  62.     }
  63.  
  64.     public int getDelta() {
  65.         long time = getTime();
  66.         int delta = (int) (time - lastFrame);
  67.         lastFrame = time;
  68.  
  69.         return delta;
  70.     }
  71.  
  72.     public long getTime() {
  73.         return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  74.     }
  75.  
  76.     public void updateFPS() {
  77.         if (getTime() - lastFPS > 1000) {
  78.             Display.setTitle("Starhack - FPS: " + fps);
  79.             fps = 0;
  80.             lastFPS += 1000;
  81.         }
  82.         fps++;
  83.     }
  84.  
  85.     public void initFBO() {
  86.         IntBuffer buffer = ByteBuffer.allocateDirect(4)
  87.                 .order(ByteOrder.nativeOrder()).asIntBuffer();
  88.         glGenFramebuffers(buffer);
  89.         drawFBO = buffer.get();
  90.         buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder())
  91.                 .asIntBuffer();
  92.         glGenTextures(buffer);
  93.         drawFBOTexture = buffer.get();
  94.         glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
  95.         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  96.                 GL_TEXTURE_2D, drawFBOTexture, 0);
  97.     }
  98.  
  99.     public void renderGL() {
  100.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  101.         glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
  102.         ready3D();
  103.         render3D();
  104.         ready2D();
  105.         render2D();
  106.         renderScaledFBO();
  107.     }
  108.  
  109.     public void ready3D() {
  110.         glViewport(0, 0, S_WIDTH, S_HEIGHT);
  111.         glMatrixMode(GL_PROJECTION);
  112.  
  113.         glLoadIdentity();
  114.         gluPerspective(45f, (float) S_WIDTH / S_HEIGHT, 0.0f, 5000.0f);
  115.  
  116.         glMatrixMode(GL_MODELVIEW);
  117.         glLoadIdentity();
  118.  
  119.         glDepthFunc(GL_LEQUAL);
  120.         glEnable(GL_DEPTH_TEST);
  121.     }
  122.  
  123.     public void render3D() {
  124.         glTranslatef(0, 0, -6f);
  125.         glBegin(GL_QUADS);
  126.         glVertex3f(0, 0, 0);
  127.         glVertex3f(2, 0, 0);
  128.         glVertex3f(2, 2, 0);
  129.         glVertex3f(0, 2, 0);
  130.         glEnd();
  131.     }
  132.  
  133.     public void ready2D() {
  134.         glMatrixMode(GL_PROJECTION);
  135.         glLoadIdentity();
  136.  
  137.         gluOrtho2D(0.0f, S_WIDTH, S_HEIGHT, 0.0f);
  138.  
  139.         glMatrixMode(GL_MODELVIEW);
  140.         glLoadIdentity();
  141.         glTranslatef(0.375f, 0.375f, 0.0f);
  142.  
  143.         glDisable(GL_DEPTH_TEST);
  144.     }
  145.  
  146.     public void render2D() {
  147.     }
  148.  
  149.     public void renderScaledFBO() {
  150.         glViewport(0, 0, S_WIDTH * S_SCALE, S_HEIGHT * S_SCALE);
  151.         glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
  152.         glBlitFramebuffer(0, 0, S_WIDTH, S_HEIGHT, 0, 0, S_WIDTH * S_SCALE,
  153.                 S_HEIGHT * S_SCALE, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  154.     }
  155.  
  156.     public static void main(String[] argv) {
  157.         Main main = new Main();
  158.         main.start();
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement