Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.starshock;
- import java.io.File;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.IntBuffer;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.LWJGLUtil;
- import org.lwjgl.Sys;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL30.*;
- import static org.lwjgl.util.glu.GLU.*;
- public class Main {
- static {
- System.setProperty("org.lwjgl.librarypath",
- new File(new File(System.getProperty("user.dir"), "native"),
- LWJGLUtil.getPlatformName()).getAbsolutePath());
- }
- public static final int S_WIDTH = 320, S_HEIGHT = 200, S_SCALE = 2;
- public long lastFrame, lastFPS;
- public int fps;
- public int defaultFBO = 0, drawFBO, drawFBOTexture;
- public void start() {
- try {
- Display.setDisplayMode(new DisplayMode(S_WIDTH * S_SCALE, S_HEIGHT
- * S_SCALE));
- Display.setTitle("Starhack - FPS: " + fps);
- Display.create();
- } catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(0);
- }
- initFBO();
- getDelta();
- lastFPS = getTime();
- while (!Display.isCloseRequested()) {
- int delta = getDelta();
- update(delta);
- renderGL();
- Display.update();
- Display.sync(60);
- }
- Display.destroy();
- }
- public void update(int delta) {
- updateFPS();
- }
- public int getDelta() {
- long time = getTime();
- int delta = (int) (time - lastFrame);
- lastFrame = time;
- return delta;
- }
- public long getTime() {
- return (Sys.getTime() * 1000) / Sys.getTimerResolution();
- }
- public void updateFPS() {
- if (getTime() - lastFPS > 1000) {
- Display.setTitle("Starhack - FPS: " + fps);
- fps = 0;
- lastFPS += 1000;
- }
- fps++;
- }
- public void initFBO() {
- IntBuffer buffer = ByteBuffer.allocateDirect(4)
- .order(ByteOrder.nativeOrder()).asIntBuffer();
- glGenFramebuffers(buffer);
- drawFBO = buffer.get();
- buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder())
- .asIntBuffer();
- glGenTextures(buffer);
- drawFBOTexture = buffer.get();
- glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D, drawFBOTexture, 0);
- }
- public void renderGL() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
- ready3D();
- render3D();
- ready2D();
- render2D();
- renderScaledFBO();
- }
- public void ready3D() {
- glViewport(0, 0, S_WIDTH, S_HEIGHT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45f, (float) S_WIDTH / S_HEIGHT, 0.0f, 5000.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_DEPTH_TEST);
- }
- public void render3D() {
- glTranslatef(0, 0, -6f);
- glBegin(GL_QUADS);
- glVertex3f(0, 0, 0);
- glVertex3f(2, 0, 0);
- glVertex3f(2, 2, 0);
- glVertex3f(0, 2, 0);
- glEnd();
- }
- public void ready2D() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0f, S_WIDTH, S_HEIGHT, 0.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.375f, 0.375f, 0.0f);
- glDisable(GL_DEPTH_TEST);
- }
- public void render2D() {
- }
- public void renderScaledFBO() {
- glViewport(0, 0, S_WIDTH * S_SCALE, S_HEIGHT * S_SCALE);
- glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
- glBlitFramebuffer(0, 0, S_WIDTH, S_HEIGHT, 0, 0, S_WIDTH * S_SCALE,
- S_HEIGHT * S_SCALE, GL_COLOR_BUFFER_BIT, GL_NEAREST);
- }
- public static void main(String[] argv) {
- Main main = new Main();
- main.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement