EDIT:
i edit the title, just to make it look more like a problem than a discussion
Hello,
i create this topic to show you how i decompose my project classes and see if am doing it the “right” way or not .
but then it seems that i couldn’t fix the flickering issue, even when i run the examples from lwjgl wiki it doesn’t run smoothly it always have that kinda of lag
and there is my codes :
/**
* This class is the main class
*
*/
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class Game {
// game width and height
private final int WIDTH = 800, HEIGHT = 600;
// variables of different classes
Board board = new Board(); // the game board
Loop loop = new Loop(); // the loop class
MyOpenGL ogl = new MyOpenGL(); // openGl initialization
public void start() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
//initialize open Gl
ogl.initGL();
//initialize the game board
board.init();
while (!Display.isCloseRequested()) {
//calling Board main update method
board.myBoardLoop();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
/**
* This class will handle the game logic
*
*/
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
public class Board {
// varibable of other classes
private Drawing draw = new Drawing(); // drawing class
private Loop loop = new Loop(); // the loop class
private double heroX, heroY, heroW, heroH, heroSpeed;
private Player hero;
int delta;
public void init() {
// initialize the loop
loop.init();
initHero();
hero = new Player(heroX, heroY, heroW, heroH);
}
// the main game logic "caller"
public void myBoardLoop() {
delta = loop.getDelta();
painting();
update(delta);
}
// the update method
private void update(int delta) {
heroUpdate();
loop.updateFPS();
}
private void painting() {
// clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// using pre-build draw method
draw.coloring("blue");
draw.drawRect(hero.getX(), hero.getY(), hero.getWidth(),
hero.getHeight());
}
// initializing the player
private void initHero() {
heroX = 400;
heroY = 300;
heroW = 30;
heroH = 30;
heroSpeed = 0.5;
}
// updateing the player
private void heroUpdate() {
control(delta);
hero.setX(heroX);
hero.setY(heroY);
hero.setWidth(heroW);
hero.setHeight(heroH);
}
// controlling the player
private void control(int delta) {
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
heroX -= heroSpeed * delta;
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
heroX += heroSpeed * delta;
}
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
heroY += heroSpeed * delta;
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
heroY -= heroSpeed * delta;
}
}
}
/**
* This class will handle the game Frame rate
* it's taken from the lwjgl wiki, "TimingExample"
*/
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
public class Loop {
public static long lastFrame;
public static int fps;
public static long lastFPS;
public void init(){
getDelta();
lastFPS = getTime();
}
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("FPS: " + fps);
fps = 0;
lastFPS += 1000;
}
fps++;
}
}
/**
* This class will handle basic OpenGl painting
*
*/
import java.util.Random;
import org.lwjgl.opengl.GL11;
public class Drawing {
// fill a rectangle
public void fillRect(double x, double y, double w, double h) {
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(x, y); // up left corner
GL11.glVertex2d(x + w, y);// up right corner
GL11.glVertex2d(x + w, y + h);// down right corner
GL11.glVertex2d(x, y + h);// down left corner
GL11.glEnd();
}
// draw a rectangle
public void drawRect(double x, double y, double w, double h) {
GL11.glBegin(GL11.GL_LINES);
GL11.glVertex2d(x, y);// x,y
GL11.glVertex2d(x + w, y);// w,y
GL11.glVertex2d(x + w, y);// w,y
GL11.glVertex2d(x + w, y + h);// w,h
GL11.glVertex2d(x + w, y + h);// w,h
GL11.glVertex2d(x, y + h);// x,h
GL11.glVertex2d(x, y + h);// x,h
GL11.glVertex2d(x, y);// x,y
GL11.glEnd();
}
// draw a line
public void drawLine(double x1, double y1, double x2, double y2) {
GL11.glBegin(GL11.GL_LINES);
GL11.glVertex2d(x1, y1);
GL11.glVertex2d(x2, y2);
GL11.glEnd();
}
// set a color
public void coloring(String color) {
if (color == "white") {
GL11.glColor3f(1.0f, 1.0f, 1.0f);
} else if (color == "black") {
GL11.glColor3f(0.0f, 0.0f, 0.0f);
} else if (color == "red") {
GL11.glColor3f(1.0f, 0.0f, 0.0f);
} else if (color == "green") {
GL11.glColor3f(0.0f, 1.0f, 0.0f);
} else if (color == "blue") {
GL11.glColor3f(0.0f, 0.0f, 1.0f);
} else if (color == "random") {
Random randomColor = new Random();
float r = randomColor.nextFloat();
float g = randomColor.nextFloat();
float b = randomColor.nextFloat();
GL11.glColor3f(r, g, b);
}
}
}
/**
* This class will handle basic OpenGl initialisation
*
*/
import org.lwjgl.opengl.GL11;
public class MyOpenGL {
// initializing openGl
public void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
}
/**
* this class will hanle the player properties
*/
public class Player {
private double x, y, width, height;
public Player(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// setting values
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
// getting values
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public double getWidth() {
return this.width;
}
public double getHeight() {
return this.height;
}
}