1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| package fr.megastudio.game.disp;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import fr.megastudio.game.Texture;
import fr.megastudio.game.Util;
import fr.megastudio.game.gui.Button;
public class Window {
private static int width;
private static int height;
private static boolean running = false;
private static boolean tick = false;
private static boolean render = false;
private static final String NAME = "Game";
private static DisplayMode dm;
public Window(int width, int height){
Window.width = width;
Window.height = height;
dm = new DisplayMode(1280, 720);
try {
Display.setDisplayMode(dm);
Display.setResizable(true);
Display.setTitle(NAME);
Display.create();
} catch (LWJGLException e) { e.printStackTrace(); }
}
private static void view2D(int width, int height){
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
public static void start(){
if(running)
return;
running = true;
loop();
}
public static void loop(){
double elapsed = 0;
double nanoSecond = 1000000000.0 / 60.0;
long before = System.nanoTime();
long timer = System.currentTimeMillis();
int ticks = 0;
int frames = 0;
while(running){
Display.update();
if(Display.isCloseRequested()) stop();
tick = false;
render = false;
long now = System.nanoTime();
elapsed = now - before;
if(elapsed > nanoSecond){
before += nanoSecond;
tick = true;
ticks++;
}else{
render = true;
frames++;
}
if(render) render();
if(tick) update();
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
Display.setTitle(NAME + " | FPS: " + frames + " TPS: " + ticks);
ticks = 0;
frames = 0;
}
}
exit();
}
public static void update(){
}
public static void render(){
view2D(Display.getWidth(), Display.getHeight());
width = Display.getWidth();
height = Display.getHeight();
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.8f, 0.9f, 1.0f, 1.0f);
Renderer.renderQuad(0, 0, width, height, new float[]{0, 100, 50});
Util.drawText("TEST", 16, 16);
}
public static void stop(){
running = false;
}
public static void exit(){
Display.destroy();
System.exit(0);
}
public static int getWidth(){
return width;
}
public static int getHeight(){
return height;
}
} |
Partager