LWJGL Forum

Programming => General Java Game Development => Topic started by: sourceskyboxer on October 01, 2017, 19:29:05

Title: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: sourceskyboxer on October 01, 2017, 19:29:05
Hello everyone,

I already created 2 Java files.

Window.java looks like it made by "Levvy055"

I have replaced to SDLWindow.java

And I add more features like setWidth and setHeight.

package lwhl;

import static org.lwjgl.glfw.GLFW.*;

import org.lwjgl.glfw.*;

public class SDLWindow
{
private long window;

private int width, height;

private boolean fullscreen;

public SDLWindow()
{
this.setSize(640, 480);
this.setFullscreen(true);
}

public void CreateWindow(String title)
{
window = glfwCreateWindow(width, height, title, fullscreen ? glfwGetPrimaryMonitor() : 0, 0);

if(window == 0)
throw new IllegalStateException("Error: Failed to create window!");

if(!fullscreen)
{
GLFWVidMode vid = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window,
(vid.width()-width/2),
(vid.height()-height/2));

glfwShowWindow(window);
}

glfwMakeContextCurrent(window);
}

public boolean ShouldClose()
{
return glfwWindowShouldClose(window);
}

public void SwapBuffer()
{
glfwSwapBuffers(window);
}

public void setSize(int width, int height)
{
this.width = width;
this.height = height;
}

public int setWidth(int width)
{
return this.width = width;
}

public int setHeight(int height)
{
return this.height = height;
}

public void setFullscreen(boolean fullscreen)
{
this.fullscreen = fullscreen;
}

public int getWidth() { return width; }
public int getHeight() { return height; }
public boolean getFullscreen() { return fullscreen; }
}


And I create lwhl ( Lightweight's Half-Life )
package lwhl;

import org.lwjgl.opengl.*;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;

public class lwhl {

public lwhl(String[] args)
{
SDLWindow win = new SDLWindow();
win.setSize(640, 480);
win.setFullscreen(true);
win.CreateWindow("Half-Life");

for(int argIndex = 0; argIndex < args.length; argIndex++)
{
if(args[argIndex] == "-windowed")
{
win.setFullscreen(false);
}

if(args[argIndex+1] == "-w" || args[argIndex+1] == "-width")
{
win.setWidth(Integer.parseInt(args[argIndex]));
}

if(args[argIndex+1] == "-h" || args[argIndex+1] == "-height")
{
win.setHeight(Integer.parseInt(args[argIndex]));
}
}

GL.createCapabilities();
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

while (!win.ShouldClose())
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

win.SwapBuffer();

glfwPollEvents();
}

glfwTerminate();
}

public static void main(String[] args) {
new lwhl(args);
}
}

I don't understand why does String[] args not work if I add current window. What's it wrong?
public lwhl(String[] args) { ... } into new lwhl(args);

And output throws error:
Exception in thread "main" java.lang.IllegalStateException: There is no OpenGL context current in the current thread.
at org.lwjgl.opengl.GL.createCapabilities(GL.java:363)
at org.lwjgl.opengl.GL.createCapabilities(GL.java:312)
at lwhl.lwhl.<init>(lwhl.java:33)
at lwhl.lwhl.main(lwhl.java:49)

How do I fix? I already tried. No success! I have watched youtube by "Levvy055" and I tried. Why they're lucky if your development has gone errors. Why does it happen with me :(?

Please help me! Thanks!

Sorry my English is bad.
Title: Re: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: spasi on October 01, 2017, 19:40:46
Based on the code above, the CreateWindow method is never called.
Title: Re: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: sourceskyboxer on October 01, 2017, 19:46:07
Quote from: spasi on October 01, 2017, 19:40:46
Based on the code above, the CreateWindow method is never called.
Check I already edited but it throws always error:
Exception in thread "main" java.lang.IllegalStateException: Failed to create window!
at lwhl.SDLWindow.CreateWindow(SDLWindow.java:25)
at lwhl.lwhl.<init>(lwhl.java:15)
at lwhl.lwhl.main(lwhl.java:51)


I can't understand why does it happen?

Is it possible that I may not use constructor with String[] args? Or just like HelloWorld.java from Get Started run(String[] args), init() and loop(), right?
Title: Re: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: spasi on October 01, 2017, 20:20:43
You're also missing a call to glfwInit().

You should also add a GLFW error callback, so that you get notified when something goes wrong. Simplest example:

GLFWErrorCallback.createPrint().set();
Title: Re: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: sourceskyboxer on October 01, 2017, 20:50:50
Thanks works fine now....
// EDIT Removed contents because it is wrong about always same here.

Ah you're right I forget to add "GL.createCapabilities();" I thought that createCapabilities means old display cards. I am using newest highest display card Radeon RX 480 8 GB Video-Ram.

// EDIT:

I can not understand why do -w and -h not work if I use parameter like lwhl.jar -w 1024 -h 768 -windowed
result: if width and height of glfwWindow don't want resize - How do I fix?
(https://i.imgur.com/visaso5.jpg)PS Sorry my monitor is really big :/
I tried to replace into like -w / -width 1024 and -h / -height 768 than glfwwindow will change after passing parameters into showing window?

package lwhl;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.*;

public class lwhl {

private String JarWidth = "";
private String JarHeight = "";
private String JarWindowed = "";

public lwhl(String[] args)
{
final SDLWindow win = new SDLWindow();
win.CreateWindow("Half-Life");

for(int argIndex = 0; argIndex < args.length; argIndex++)
{
if(args[argIndex] == "-windowed")
{
if(!win.getFullscreen())
{
JarWindowed = "false";
win.setFullscreen(Boolean.parseBoolean(JarWindowed));
}
}
else
{
win.setFullscreen(true);
}

if(args[argIndex] == "-w" || args[argIndex] == "-width")
{
win.setWidth(Integer.parseInt(args[argIndex + Integer.parseInt(JarWidth)]));
}

if(args[argIndex] == "-h" || args[argIndex] == "-height")
{
win.setHeight(Integer.parseInt(args[argIndex + Integer.parseInt(JarHeight)]));
}
}

GL.createCapabilities();
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

while (!win.ShouldClose())
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

win.SwapBuffer();

glfwPollEvents();
}

glfwTerminate();
}

public static void main(String[] args) {
new lwhl(args);
}
}


// EDIT: Nothing working how do I pass parameters?

final SDLWindow win = new SDLWindow();
win.CreateWindow("Half-Life");

for(int i = 0; i < args.length; i++)
{
if(args[i] == "-windowed ")
{
if(!win.getFullscreen())
{
JarWindowed = "false";
//win.setFullscreen(Boolean.parseBoolean(JarWindowed));
}
}
else
{
win.setFullscreen(true);
}

if(args[i] == "-w" || args[i] == "-width")
{
win.setSize(Integer.parseInt(args[i+1]), win.getHeight());
}

if(args[i] == "-h" || args[i] == "-height")
{
win.setSize(win.getWidth(), Integer.parseInt(args[i+1]));
}
}


I am surprised because Java won't work with passing parameter?

-width and -height changing length from command line arguments

-windowed is switching as windowed mode of lwjgl game's window.
If I don't use -windowed than game window will make as fullscreen.

I always tried..... Nothing works yet.

I can not understand why C# and C++ work fine but Java not? Stop scamming me! Because I found google but I tried tried... nothing sucks arguments.... Thanks!
Title: Re: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: Cornix on October 02, 2017, 06:40:19
You dont seem to understand how java works. A line like this:
arg[0] == "someString"
will test whether arg[0] is referencing the same object as the constant "someString" which it will most probably not. What you want to check is whether the objects at arg[0] and the constant "someString" have the same content. To do this you can call the equals(Object) method that String provides:
arg[0].equals("someString")

This is very basic stuff. Before starting with something as big and complicated as LWJGL I would highly recommend doing some simple java tutorials first. LWJGL can be tricky even for experienced programmers.
Title: Re: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: sourceskyboxer on October 02, 2017, 13:28:36
Thanks for explanation!

I really understand you said.

But I can not get passing command line like shit libraries like
commons cli,
args4j,
jCommander,
jopt-Simple etc

Why do they not work for me? I have tried.

If I want pass "-w" or "-width" with integer of glfwWindow.

Is it possible? How do I create simple passing command line from Java without commons cli or args4j? Just made from Java.

I search Google. But it doesn't find. If I want find easy explanation. I already used Swing, AWT and SWT now. I don't have experience about argument with passing custom methods. That is why I cannot find because Google shows always very old unstable command line parsers

I want know how do I get -width <int> It means int from width of glfwWindow
package lwhl;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;

import org.kohsuke.args4j.*;

import org.lwjgl.opengl.*;

public class lwhl
{
public lwhl(String[] args)
{
Arguments args4j = new Arguments();
CmdLineParser parser = new CmdLineParser(args4j);

final SDLWindow win = new SDLWindow();
win.setFullscreen(true);
win.CreateWindow("Half-Life");

try
{
parser.parseArgument(args);

if(args4j.lwWindowed)
{
win.setFullscreen(false);
}

if(args4j.lwWidth == 640)
{
win.setSize(args4j.lwWidth, win.getHeight());
}

if(args4j.lwHeight == 480)
{
win.setSize(win.getWidth(), args4j.lwHeight);
}
}

catch(CmdLineException e)
{
System.err.println(e.getMessage());
            parser.printUsage(System.err);
}

GL.createCapabilities();
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

while (!win.ShouldClose())
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

win.SwapBuffer();

glfwPollEvents();
}

glfwTerminate();
}

public static void main(String[] args)
{
new lwhl(args);
}

class Arguments
{
@Option(name = "-windowed")
public boolean lwWindowed = false;

@Option(name = "-width", metaVar = "INT")
public int lwWidth = 0;

@Option(name = "-height", metaVar = "INT")
public int lwHeight = 0;
}
}

But I am using args4j But I really don't understand.

If game launch without command starts default game window with fullscreen than game windows shows fullscreen mode gamewindow.

If I use "-windowed" than game window will start in windowed mode. But it shows like crazy why does passing argument not work? I already tried and I have no success.

But I knew since C# with OpenTK or MonoGame and not problem but it cannot make fullscreen and windowed :( only width and height work fine. )

But why does Java not work for me? I always tried with library like commons cli, args4j or jopt-simple etc.. no success. Example works fine but for me not?

My Java is 1.8.0_131 Version. Is it bugged? Or I need update?
Title: Re: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: KaiHH on October 02, 2017, 14:26:09
Look. You very obviously lack even the most basic knowledge of sequence / program flow.
You are creating the window with CreateWindow() (and glfwCreateWindow) before you are evaluating and setting your command line arguments.
Also, using the debugger in Eclipse would not hurt. Try to get familiar with the debugger in Eclipse. You will be using it alot in the next months.
Title: Re: Creating Window with passing arguments like -width #, -height # -windowed?
Post by: sourceskyboxer on October 05, 2017, 22:39:39
Hello everyone,

I am here now:

I tried just clean HelloWorld.java from Get Started.

public class HelloWorld {

// The window handle
private long window;
private GLFWVidMode vid;
private int width;
private int height;

public void run(String[] args) {
System.out.println("Hello LWJGL " + Version.getVersion() + "!");
for(int i = 0; i < args.length; i++)
{
switch(args[i])
{
case "-w":
case "-width":
width = Integer.parseInt(args[i+1]);

System.out.println("argument is for w/width "+args[i+1]+"\n");
break;
case "-h":
case "-height":
height = Integer.parseInt(args[i+1]);
System.out.println("argument is for h/height "+args[i+1]+"\n");
break;
case "-windowed":
vid = glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
glfwSetWindowMonitor(window, glfwGetWindowMonitor(window), (vid.width()-width)/2, (vid.height()-height)/2, width, height, 60);
System.out.println("argument is for windowed = false \n");
break;
default:
break;
}
}

init();
loop();

// Free the window callbacks and destroy the window
glfwFreeCallbacks(window);
glfwDestroyWindow(window);

// Terminate GLFW and free the error callback
glfwTerminate();
glfwSetErrorCallback(null).free();
}

private void init() {
// Setup an error callback. The default implementation
// will print the error message in System.err.
GLFWErrorCallback.createPrint(System.err).set();

// Initialize GLFW. Most GLFW functions will not work before doing this.
if ( !glfwInit() )
throw new IllegalStateException("Unable to initialize GLFW");

// Configure GLFW
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // the window will be resizable
glfwWindowHint(GLFW_MAXIMIZED, GLFW_FALSE);

// Create the window
window = glfwCreateWindow(640, 480, "Hello World!", glfwGetPrimaryMonitor(), NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window");

// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
});

// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);

// Make the window visible
glfwShowWindow(window);
}

private void loop() {
// 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 GLCapabilities instance and makes the OpenGL
// bindings available for use.
GL.createCapabilities();

// Set the clear color
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while ( !glfwWindowShouldClose(window) ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer

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 HelloWorld().run(args);
}

}
Is it correct? But I understand like glfw forum I always tried glfwSetWindowMonitor()

But it crashed now:
Hello LWJGL 3.1.4 SNAPSHOT!
argument is for w/width 1200

argument is for h/height 720

Exception in thread "main" java.lang.NullPointerException
at org.lwjgl.system.Checks.check(Checks.java:96)
at org.lwjgl.glfw.GLFW.nglfwGetVideoMode(GLFW.java:1284)
at org.lwjgl.glfw.GLFW.glfwGetVideoMode(GLFW.java:1306)
at HelloWorld.run(HelloWorld.java:35)
at HelloWorld.main(HelloWorld.java:116)


If I use -windowed than glfwSetWindowMonitor will change to width and height. But it won't work. How do I fix?