Hey please could someone help me with this lwjgl code
Hey I'm trying to create a menu for my game so i have my three buttons (which are just rectangles at the moment) but i want to wrap text to them. however when i run my program a window just flashes up then immediately closes.I'm not entirely sure why it does this so I have posted the two relevant classes in hope you can help me :D thanks , josh
main class
Code:
package MainPackage;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.*;
;
public class Main {
public static enum State {
INTRO, MAIN_MENU, GAME;
}
public State state = State.INTRO;
public Main() {
}
public void start(){
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Maths");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
// Initialization code OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
/* MAIN STUFF */
while (!Display.isCloseRequested()) {
// Render
glClear(GL_COLOR_BUFFER_BIT);
Menu.draw();
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
Display.destroy();
System.exit(0);
}
int mousex = Mouse.getX();
int mousey = 480 - Mouse.getY();
if (Menu.isInBounds(mousex, mousey) && Mouse.isButtonDown(0)) {
Display.destroy();
System.exit(0);
}
if (Menu.isInBoundstwo(mousex, mousey) && Mouse.isButtonDown(0)) {
Display.destroy();
System.exit(0);
}
if (Menu.isInBoundsthree(mousex, mousey) && Mouse.isButtonDown(0)) {
Display.destroy();
System.exit(0);
}
// TESTING JUST PRINTS X & Y
if (Mouse.isButtonDown(0))
System.out.println("X : " + mousex + "|| Y :" + mousey);
Display.update();
Display.sync(60);
}
Display.destroy();
}
public void render(){
switch(state){
case INTRO:
break;
case GAME:
break;
case MAIN_MENU:
break;
}
}
public void checkInput(){
switch(state){
case INTRO:
break;
case GAME:
break;
case MAIN_MENU:
break;
}
}
/* end of constructor */
public static void main(String[] args) {
new Main().start();
}
}
menu class
Code:
package MainPackage;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2i;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.opengl.GL11.*;
public class Menu {
public static Texture play;
// set OpenGL settings
public Menu() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
play = loadTexture("play");
}
//Button coordinate variables
public static int x1;
public static int x2;
public static int y1;
public static int y2;
public static int x3;
public static int x4;
public static int y3;
public static int y4;
public static int x5;
public static int x6;
public static int y5;
public static int y6;
// Drawing Buttons
static void draw() {
play.bind();
/* PLAY BUTTON */
x1 = 170;
x2 = 470;
y1 = 200;
y2 = 250;
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2i(x1, y1); // Upper-left
glTexCoord2f(1,0);
glVertex2i(x2, y1); // Upper-right
glTexCoord2f(1,1);
glVertex2i(x2, y2); // Bottom-right
glTexCoord2f(0,1);
glVertex2i(x1, y2); // Bottom-left
glEnd();
/* Settings Button */
x3 = 170;
x4 = 470;
y3 = 300;
y4 = 350;
glBegin(GL_QUADS);
glVertex2i(x3, y3); // Upper-left
glVertex2i(x4, y3); // Upper-right
glVertex2i(x4, y4); // Bottom-right
glVertex2i(x3, y4); // Bottom-left
glEnd();
/* Exit Button */
x5 = 170;
x6 = 470;
y5 = 400;
y6 = 450;
glBegin(GL_QUADS);
glVertex2i(x5, y5); // Upper-left
glVertex2i(x6, y5); // Upper-right
glVertex2i(x6, y6); // Bottom-right
glVertex2i(x5, y6); // Bottom-left
glEnd();
}
public static Texture loadTexture(String key){
try {
return TextureLoader.getTexture("PNG", new FileInputStream(new File("C:/Users/joshua jackson/Documents/Program/Maths/res" + key + ".png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// checks if mouse is inside buttons returns true if it is
/* PLAY BUTTON */
public static boolean isInBounds(int mousex, int mousey) {
if (mousex > x1 && mousex < x2 && mousey > y1 && mousey < y2) {
return true;
} else {
return false;
}
}
/* Settings Button */
public static boolean isInBoundstwo(int mousex, int mousey) {
if (mousex > x3 && mousex < x4 && mousey > y3 && mousey < y4) {
return true;
} else {
return false;
}
}
/* Exit Button */
public static boolean isInBoundsthree(int mousex, int mousey) {
if (mousex > x5 && mousex < x6 && mousey > y5 && mousey < y6) {
return true;
} else {
return false;
}
}
}
Re: Hey please could someone help me with this lwjgl code
Duplicate thread locked -- one is enough. Please re-read the forum rules and abide by them.