I'm trying to make a program in java with the libraries LWJGL and Slick-Util. I can successfully create a window and render a red rectangle but whenever I try to render an image it gives me a blank screen. There are four java files, here's the code I have:
MOBAelex.java:
package com.aelex.mobaelex;
import com.aelex.mobaelex.menu.MenuScreen;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
public class MOBAelex {
public static Texture playButton;
private static int ImagesLoaded = 0;
// We need to strongly reference callback instances.
private GLFWErrorCallback errorCallback;
private GLFWKeyCallback keyCallback;
private GLFWMouseButtonCallback mouse_button_callback;
private GLFWCursorPosCallback cursor_pos_callback;
// The window handle
private long window;
private double MouseX;
private double MouseY;
private static void LoadImages() {
try {
playButton = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("Assets/Pics/Play.png"));
ImagesLoaded = 1;
} catch (IOException e) {
e.printStackTrace();
ImagesLoaded = 0;
}
}
public void run() {
System.out.println("Starting MOBAelex");
init();
LoadImages();
loop();
System.out.println("Now exiting the program");
// Release window and window callbacks
glfwDestroyWindow(window);
keyCallback.release();
// Terminate GLFW and release the GLFWerrorfun
glfwTerminate();
errorCallback.release();
}
private void init() {
System.out.println("Running init");
// Setup an error callback. The default implementation
// will print the error message in System.err.
glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));
System.out.println("Set error callback");
// Initialize GLFW. Most GLFW functions will not work before doing this.
if ( glfwInit() != GL11.GL_TRUE )
throw new IllegalStateException("Unable to initialize GLFW");
// Configure our window
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable
System.out.println("comfigured new window");
int WIDTH = 600;
int HEIGHT = 600;
// Create the window
window = glfwCreateWindow(WIDTH, HEIGHT, "MOBAelex", NULL, NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window");
System.out.println("Created new window");
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
glfwSetWindowShouldClose(window, GL_TRUE); // We will detect this in our rendering loop
}
});
glfwSetCursorPosCallback(window, cursor_pos_callback = new GLFWCursorPosCallback() {
public void invoke(long window, double xpos, double ypos) {
MouseX = xpos;
MouseY = ypos;
}
});
glfwSetMouseButtonCallback(window, mouse_button_callback = new GLFWMouseButtonCallback() {
public void invoke(long window, int button, int action, int mods) {
if ( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE ) {
if (MouseX > MenuScreen.Play.X && MouseX < MenuScreen.Play.X + 200 && MouseY > MenuScreen.Play.Y && MouseY < MenuScreen.Play.Y + 100) {
MenuScreen.Play.ButtonOn();
}
}
}
});
System.out.println("Setup window close key callback");
// Get the resolution of the primary monitor
ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
System.out.println("Retrieved primary monitor's resolution: " + GLFWvidmode.WIDTH + ":" + GLFWvidmode.HEIGHT);
System.out.println("Please contact someone if this resolution is wrong");
// Center our window
glfwSetWindowPos(
window,
(GLFWvidmode.width(vidmode) - WIDTH) / 2,
(GLFWvidmode.height(vidmode) - HEIGHT) / 2
);
System.out.println("Positioned window");
// Make the OpenGL context current
glfwMakeContextCurrent(window);
System.out.println("Made the OpenGL context current");
// Enable v-sync
glfwSwapInterval(60);
System.out.println("Enabled V-Sync");
// Make the window visible
glfwShowWindow(window);
System.out.println("Window now visible");
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the ContextCapabilities instance and makes the OpenGL
// bindings available for use.
GLContext.createFromCurrent();
System.out.println("Created ContextCapabilties instance");
System.out.println("OpenGL bindings are now available for use within the code");
// Set the clear color
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
private void loop() {
if (ImagesLoaded == 1) {
System.out.println("Textures loaded successfully");
}
else if (ImagesLoaded == 0) {
System.out.println("Textures not loaded properly");
System.out.println("Now quitting");
glfwDestroyWindow(window);
keyCallback.release();
glfwTerminate();
errorCallback.release();
}
// Run the rendering loop until the user has attempted to <a style="background: none repeat scroll 0% 0% transparent ! important; border: medium none ! important; display: inline-block ! important; text-indent: 0px ! important; float: none ! important; font-weight: bold ! important; height: auto ! important; margin: 0px ! important; min-height: 0px ! important; min-width: 0px ! important; padding: 0px ! important; text-transform: uppercase ! important; text-decoration: underline ! important; vertical-align: baseline ! important; width: auto ! important;" title="Click to Continue > by Provider" in_rurl="http://ift.tt/1xPC7za" id="_GPLITA_5" href="#">close<img style="background: none repeat scroll 0% 0% transparent ! important; border: medium none ! important; display: inline-block ! important; text-indent: 0px ! important; float: none ! important; font-weight: bold ! important; height: 10px ! important; margin: 0px 0px 0px 3px ! important; min-height: 0px ! important; min-width: 0px ! important; padding: 0px ! important; text-transform: uppercase ! important; text-decoration: underline ! important; vertical-align: super ! important; width: 10px ! important;" src="http://ift.tt/1cH5FQF"></a>
// the window or has pressed the ESCAPE key.
while ( glfwWindowShouldClose(window) == GL_FALSE ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
MenuScreen.Menu();
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
}
public static void main(String[] args) {
new MOBAelex().run();
}
}
MenuScreen.java:
package com.aelex.mobaelex.menu;
import com.aelex.mobaelex.resources.Buttons;
import com.aelex.mobaelex.MOBAelex;
public class MenuScreen {
public static Buttons Play = new Buttons();
private static boolean ButtonRender;
public static void Menu() {
Play.X = 0.0f;
Play.Y = 0.0f;
Play.Button = MOBAelex.playButton;
Play.ButtonReaction = "ConnectServer";
Play.RenderButton();
ButtonRender = Play.ButtonRendered;
if (ButtonRender == false) {
System.out.println("Unable to render playButton!");
}
}
}
Buttons.java
package com.aelex.mobaelex.resources;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import com.aelex.mobaelex.MOBAelex;
import com.aelex.mobaelex.reactions.ButtonReactions;
public class Buttons {
public float X = 0;
public float Y = 0;
private float X2 = 0;
private float Y2 = 0;
public Texture Button;
public String ButtonReaction;
public boolean ButtonRendered = false;
public void ButtonOn() {
if (ButtonReaction.equals("ConnectServer")) {
System.out.println("Executing button code: " + ButtonReaction);
ButtonReactions.ConnectServer();
}
}
public void RenderButton() {
X2 = Button.getTextureWidth()/10000;
Y2 = Button.getTextureHeight()/10000;
Color.white.bind();
Button.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(X,Y);
GL11.glVertex2f(X+0.200f,Y);
GL11.glVertex2f(X+0.200f,Y+0.100f);
GL11.glVertex2f(X,Y+0.100f);
GL11.glEnd();
ButtonRendered = true;
}
}
ButtonReactions.java
package com.aelex.mobaelex.reactions;
public class ButtonReactions {
public static void ConnectServer() {
System.out.println("Successfully connected to Server");
}
}
The Play.png that this program is trying to use is a simple picture I made with Paint. It's 200x100 pixels and is all black.
Also I've tried changing Color.white.bind() in Buttons.java to Color.red.bind() and it caused a red rectangle to be rendered in the center of the window, but I want it to be at the top left corner? I'm not sure how to do this so if you could answer that question as well that would be great.
Aucun commentaire:
Enregistrer un commentaire