I'm learning how to make a Java game with ThinMatrix's LWJGL 2 tutorials that I'm adapting to LWJGL 3. Anyway, I can't get the game to detect my monitors. This was working fine just a few days ago on the exact same system with the exact same hardware.
I have tried installing the drivers for them, uninstalling the drivers, and uninstalling the monitors completely from Device Manager. Nothing works. If you need any more information just let me know!
Here is my source code:
Main.java:
package engineTest;
import static org.lwjgl.glfw.GLFW.*;
import static renderEngine.displayManager.*;
public class Main {
public static void main(String[] args) {
window = glfwCreateWindow(WIDTH, HEIGHT, "Farm Game", glfwGetPrimaryMonitor(), 0);
while(!glfwWindowShouldClose(window)) {
updateDisplay();
}
glfwDestroyWindow(window);
}
}
displayManager.java
package renderEngine;
import static org.lwjgl.glfw.GLFW.*;
import org.lwjgl.opengl.GL11;
public class displayManager {
public static int WIDTH = 1280;
public static int HEIGHT = 720;
private static int FPS_CAP = 120;
public static long window;
public static void createDisplay() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GL11.glViewport(0, 0, WIDTH, HEIGHT);
}
public static void updateDisplay() {
glfwPollEvents();
glfwSwapBuffers(FPS_CAP);
}
public static void closeDisplay() {
glfwDestroyWindow(window);
}
}
Here is the error output.
Exception in thread "main" java.lang.NullPointerException
at org.lwjgl.system.Checks.check(Checks.java:99)
at org.lwjgl.glfw.GLFW.glfwWindowShouldClose(GLFW.java:1874)
at engineTest.Main.main(Main.java:13)
Edit
I have been trying to debug the NullPointerException, and I know what is causing it. The problem is I don't know how to fix it. Sorry, I'm pretty new to Java.
You have to call glfwInit() before you can use any other glfw method. Since this is missing, glfwCreateWindow always returns a nullptr.
Related
So I'm a moderately novice programmer, but after toiling over this error for a while, I can't find a solution. I'm making a puzzle game, and for some reason, it just refuses to run now. I always get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.lwjgl.system.Library
at org.lwjgl.system.MemoryAccess.<clinit>(MemoryAccess.java:22)
at org.lwjgl.system.Pointer.<clinit>(Pointer.java:24)
at org.lwjgl.glfw.GLFW.<clinit>(GLFW.java:594)
at Graphics.LaunchWindow.run(LaunchWindow.java:32)
at Graphics.LaunchWindow.main(LaunchWindow.java:96)
Eclipse tells me that none of my code has errors within it, and reinstalling LWJGL doesn't work (I tried both the stable 3.0.0b build 64 and the nightly 3.0.0 build 22). I have seen other similar questions taking about making sure the lwjgl.jar is in the class path, but I've made sure multiple times. Also, if it matters, I have lwjgl_util.jar and slick_util.jar in the class path as well, and even though they are outdated compared to lwjgl 3, removing them from the class path makes no difference.
package Graphics;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import Controls.KeyParser;
import Controls.KeyboardInput;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
public class LaunchWindow {
private GLFWErrorCallback errorCallback;
private GLFWKeyCallback keyCallback;
public int width = 1024;
public int height = 600;
public String title = "Duplicity";
public long fullscreen = NULL;
public long window;
public void run() {
try {
init();
loop();
glfwDestroyWindow(window);
keyCallback.release();
} finally {
glfwTerminate();
errorCallback.release();
}
}
private void init() {
glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
KeyboardInput.initiate();
if ( glfwInit() != GLFW_TRUE )
throw new IllegalStateException("Unable to initialize GLFW");
if(fullscreen == NULL){
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
}
window = glfwCreateWindow(width, height, title, fullscreen, NULL);
if (window == NULL)
throw new RuntimeException("Failed to create the GLFW window");
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, GLFW_TRUE);
}
});
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
keyCallback = GLFWKeyCallback.create(KeyboardInput::glfw_key_callback);
keyCallback.set(window);
/* mouseButtonCallback = GLFWMouseButtonCallback.create(Mouse::glfw_mouse_button_callback);
mouseButtonCallback.set(window);
cursorPosCallback = GLFWCursorPosCallback.create(Mouse::glfw_cursor_pos_callback);
cursorPosCallback.set(window); */
GL.createCapabilities();
}
public void loop() {
GL.createCapabilities();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
while ( glfwWindowShouldClose(window) == GLFW_FALSE ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
new KeyParser().checkKeyState();
}
}
public static void main(String[] args) {
new LaunchWindow().run();
}
}
Incorrect system library path can be one reason for this error, but this can also be caused by system library incompatibility (on Linux). Code included in LWJGL 3 requires GLIBC version 2.14 or higher. If your system (e.g. Linux) is older (like Debian Wheezy, for example), your GLIBC version will be older than the required one.
If that's the case you'll need to install a newer GLIBC version, or upgrade your system (e.g. to Debian Jessie).
HTH,
M
I downloaded LWJGL 3 today, and found out that it was almost completly rewritten. I looked up a tutorial on how to create a window, but I still have problems creating a window.
The code runs without problems: no errors in the console, but the window isn't displayed!
I hope you can help me, I searched a lot for LWJGL 3 tutorials, but they seem pretty old, so I decided to ask my question here.
Here's my code:
//EDIT: Changed my code so everything runs in one thread but it still doesn' t work. Even System.out.println() doesn't work. No console output is displayed!
//IMPORTANT: I just realized that this may be a bug in GLFW (I'm working on Linux) !
package net.newworld.test;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWVidMode;
public class Main {
private static long window;
private static int width = 1280;
private static int height = 800;
private static void init() {
glfwInit();
System.out.println("Initializing LWJGL...");
System.out.println("LWJGL Version: "+ Version.getVersion());
glfwWindowHint(GLFW_VISIBLE, GL_FALSE); //Set window visible after creation
window = glfwCreateWindow(width, height, "New World", 0, 0);
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); //Get primary monitor
glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2); //Set window position
glfwMakeContextCurrent(window); //Make OpenGL contect current
glfwShowWindow(window); //Show window
}
public static void main(String[] args) {
init();
}
}
The problem might be that you forgot to add the window proc loop (which is blocking)
public static void main(String[] args) {
init();
while (!glfwWindowShouldClose(window)) {
glfwWaitEvents();
}
}
I am trying to start my game from a Main Menu class that implements NiftyGUI. I am using OpenGL (LWJGL) and Java. The problem is that I am never able to get the main menu to disappear and then start the game class, which is World.java.
The following is what I would usually do when I have no main menu.
package com.dev.voxy;
import com.dev.voxy.utilities.GLScreen;
import com.dev.voxy.world.World;
public class Main extends GLScreen {
public static int WIDTH = 1280;
public static int HEIGHT = 720;
private World world;
public static void main(String[] args) {
Main main = new Main();
main.GLScreen(WIDTH, HEIGHT, false, 60, "Voxy");
}
#Override
public void init() {
world = new World();
}
#Override
public void update() {
world.update();
}
#Override
public void dispose() {
world.dispose();
}
That is my main class and extends GLScreen
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
public abstract class GLScreen {
protected static int SCREEN_WIDTH;
protected static int SCREEN_HEIGHT;
protected boolean FULL_SCREEN;
protected static int FPS = 60;
protected static String SCREEN_TITLE;
private static long lastFPS;
private static int lfps;
public static int fps;
public void GLScreen(int width, int height, boolean fullscreen, int sync, String title) {
this.SCREEN_HEIGHT = height;
this.SCREEN_WIDTH = width;
this.FULL_SCREEN = fullscreen;
this.FPS = sync;
this.SCREEN_TITLE = title;
InitScreen();
}
public void InitScreen() {
createWindow();
InitGL();
init();
Run();
}
void createWindow() {
try {
Display.setFullscreen(FULL_SCREEN);
Display.setTitle(SCREEN_TITLE);
DisplayMode displayMode = new DisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT);
Display.setDisplayMode(displayMode);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
void InitGL(){
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(67.0f, SCREEN_WIDTH / SCREEN_HEIGHT, 0.001f, 1000f); //TODO watch coding universe video
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
lastFPS = getTime();
}
public abstract void init();
public abstract void update();
public static int getFPS() {
int cfps;
if (getTime() - lastFPS > 1000 && lfps != fps) {
lfps = fps;
fps = 0; //reset the FPS counter
lastFPS += 1000; //add one second
cfps = fps;
} else {
cfps = lfps;
}
fps++;
return cfps;
}
public static long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
void Run(){
while(!Display.isCloseRequested()){
try{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0f, 0f);
glLoadIdentity();
update();
Display.update();
Display.sync(FPS);
} catch (Exception e){
e.printStackTrace();
}
}
dispose();
destroy();
}
public abstract void dispose();
public void destroy() {
Display.destroy();
System.exit(0);
}
}
And finally my World Class is below.
import com.dev.voxy.Main;
import org.lwjgl.opengl.Display;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
public class World extends WorldManager {
private Chunk chunk;
public World() {
super(Main.WIDTH, Main.HEIGHT);
init();
}
#Override
public void init() {
chunk = new Chunk(0, 0, 0);
setRenderStats(true);
}
public void update() {
input();
render();
}
public void render() {
render3D();
Translate();
chunk.render();
glLoadIdentity();
if (isRenderStats()) {
render2D();
glColor3f(1, 1, 1);
renderStats();
}
}
#Override
public void dispose() {
chunk.dispose();
Display.destroy();
System.exit(0);
}
}
The problem is that when I try and incorporate my Menu into this it never works. First I tried GameStates so that whenever the game state is GAME the nifty should no longer render but this causes the Window to close because nifty no longer keeps it open, so then I tried to have the loop be outside the MainMenu Class but that also did not work because I kept running into static/non-static errors which I could not figure out. Then my last idea was why not try and put the gameloop inside the MainMenu Class but try to stop the nifty render while I try to start the game, but that left elements of the nifty on the screen and the rest was blank and the World class did not render. The following are my current Main, MainMenu, GLScreen and MyScreenController and MainMenu.xml files The MyscreenController controls what happens when the respective buttons are pressed and the MainMenu.xml is the nifty xml file that lays out everything.
All relevant files and classes may be found at the following link, they are the current ones where I am trying to get the main menu working. The classes shown above show what I did to get ONLY the game part working in the first place, without a menu.
LINK
To sum up, I can get my game working when there is not main menu and I can get my main menu working when there is no game, I have tried as many things I can think of but after 3 days I'm simply lost and would like any suggestion/recommendation or idea. I think I just can't figure out how to manage game states and the game loop when there is a main menu and a game, I was thinking maybe if there was a way to stop nifty completely then clear the screen then set OpenGL to 3D I can get my game rendered, this is what I was trying to do near the end but could not figure it out.
Thank you.
I am trying to make a game where the character is a 75x75 object, and he moves around the screen. However, when I run my code, I get the error:
java.lang.IllegalArgumentException: Invalid display mode
at sun.awt.Win32GraphicsDevice.setDisplayMode(Unknown Source)
at sylvyrfysh.Screen.setFullScreen(Screen.java:17)
at sylvyrfysh.ImageDrawer.run(ImageDrawer.java:26)
at sylvyrfysh.ImageDrawer.main(ImageDrawer.java:17)
at sylvyrfysh.Main.main(Main.java:7)
I am not sure what is causing this, as I made another project with the same DisplayMode arguments, and it worked fine.
package sylvyrfysh;
import game.infos.Information;
import java.awt.*;
import javax.swing.*;
public class ImageDrawer extends JFrame{
/**
*
*/
private static final long serialVersionUID = -4278324581016693552L;
public static void main() throws InterruptedException{
DisplayMode dm=new DisplayMode(Information.sX,Information.sY,16,DisplayMode.REFRESH_RATE_UNKNOWN);
ImageDrawer i=new ImageDrawer();
i.run(dm);
}
private void loader(){
bg=new ImageIcon("src/sylvyrfysh/maze_icon.png").getImage();
chara=new ImageIcon("src/sylvyrfysh/char.png").getImage();
}
private void run(DisplayMode dm){
System.out.println("HI");
loader();
s=new Screen();
s.setFullScreen(dm,this);//error here
repaint();
while(EHandler.run){
if(rp){
repaint();
rp=false;
}
}
}
public void paint(Graphics g){
g.drawImage(bg,0,0,null);
g.drawImage(bg,360,0,null);
g.drawImage(bg,720,0,null);
g.drawImage(bg,0,480,null);
g.drawImage(bg,360,480,null);
g.drawImage(bg,720,480,null);
g.drawImage(chara,imgX,imgX,null);
}
private Image bg,chara;
Screen s;
public static int imgX=0;
public static int imgY=525;
public static Boolean rp=false;
}
Any help would be appreciated.
The ability to change graphics device's
display mode is platform- and configuration-dependent and may not always be available. GraphicsDevice.isDisplayChangeSupported() should be used to check prior to change the display mode on graphics device.
Some other important suggestion are made here nicely.
I am trying to make my own engine but I need some help.
I am currently doing the level system. The level class extends the render class and the level class overides the Render classes render method. Finally the render class is called from the main class but I dont call the level class.
EDIT:
I have removed the static but now cannot call the render method. I know I am very nooby I kind of teach my self.
package SimpleEngine.Render;
Render Class (This is called)
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import SimpleEngine.Primitives.*;
import static org.lwjgl.opengl.GL11.glClear;
public class Render {
public void Render() {
}
}
Level Class (This is not called) I want this Render method to override the Render classes render method but it doesn't work.
package SimpleEngine.Level;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;
import SimpleEngine.Render.*;
import SimpleEngine.Primitives.*;
public class Level extends Render {
public void Render() {
glClear(GL_COLOR_BUFFER_BIT);
Primitives.DrawSquare(200, 200, 50, 50, 1, 0, 0);
}
}
My main method (calls render but cannot anymore)
package SimpleEngine;
import org.lwjgl.LWJGLException;
import SimpleEngine.Level.*;
import SimpleEngine.Logic.*;
import SimpleEngine.Input.*;
import SimpleEngine.Render.*;
import SimpleEngine.Entites.*;
import SimpleEngine.Timer.*;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class simpleEngine {
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
private static boolean isRunning = true;
public static void main(String[] args) {
setUpDisplay();
setUpOpenGL();
Entity.setUpEntities();
Timer.setUpTimer();
while (isRunning) {
Render.Render();
Logic.logic(Timer.getDelta());
Input.input();
Display.update();
Display.sync(60);
if (Display.isCloseRequested()) {
isRunning = false;
}
}
Display.destroy();
System.exit(0);
}
private static void setUpDisplay() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle("SimpleEngine");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
}
private static void setUpOpenGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
}
You can't override static methods.
As per Oracle tutorail
If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass.
AND
You will get a compile-time error if you attempt to change an instance method in the superclass to a class method in the subclass, and vice versa.
Remove static from Render class render() method and change Level class Render method to render() to introduce overriding behavior
EDIT:
while (isRunning) {
new Level().Render();
Note: Java naming convention suggests that method name should start with small letter and name should be camelcase.
You can't override a static method.
Also, be careful because Java is a case sensitive language, so Render() is not the same as render(), and could be a completely different method.
I would recommend doing something more generic such as having an interface which defines common methods for any state your game could be in which actually looks like the direction you're heading in. This would just combine your Logic and Render classes. For example:
public Interface IGameState {
public void update(int delta);
public void render();
}
And then you can have a class that implements that interface such as:
public class InGameState implements IGameState {
public InGameState() { }
// This is similar to your Logic class
public void update(int delta) {
// Perform any updates necessary such as handling keyboard input
}
public void render() {
// What you included in your example
glClear(GL_COLOR_BUFFER_BIT);
Primitives.DrawSquare(200, 200, 50, 50, 1, 0, 0);
}
}
And then your main method could look something like this:
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
private static boolean isRunning = true;
private IGameState currentGameState;
public static void main(String[] args) {
setUpDisplay();
setUpOpenGL();
Entity.setUpEntities();
Timer.setUpTimer();
// Create a new game state
currentGameState = new InGameState();
while (isRunning) {
// Change to this type of thing
// The delta would be the time since last frame so you can stay frame rate
// independent
currentGameState.update(delta);
currentGameState.render();
Display.update();
Display.sync(60);
if (Display.isCloseRequested()) {
isRunning = false;
}
}
Display.destroy();
System.exit(0);
}