1
\$\begingroup\$

After some months on libGDX, I chose to make a little java game library based on LWJGL. I have already tried to do it, but got stuck on this same problem: how render text on screen.

The first time, I used Sick, but it was deprecated... how do I should do?

This time I tried this code, of couse it doesn't work (I read it on StackOverflow):

public class Starter {

private String GAME_NAME;
public String VERSION;
private int WIDTH, HEIGHT;
private static long window;

public Starter(@NotNull JJGLInfo info){
    Objects.requireNonNull(info);
    GAME_NAME = info.getGAME_NAME();
    VERSION = info.getGAME_VERSION();
    WIDTH = info.getWidth();
    HEIGHT = info.getHeight();
}

public void run(){

   System.out.println("Hello player by JJGL\nJJGL Version: " + VERSION + "\nLWJGL Version: " + Version.getVersion());

   this.init();
   this.loop();

   this.terminate();
}

private void terminate() {
    glfwFreeCallbacks(this.window);
    glfwDestroyWindow(this.window);

    glfwTerminate();
    glfwSetErrorCallback(null).free();
}

ByteBuffer pixels = this.getByteBufferFromString();
private void loop() {

    GL.createCapabilities();
    glClearColor(1f, 1f, 0f, 0f);

    while (!glfwWindowShouldClose(this.window)){
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glfwSwapBuffers(this.window);

        ByteBuffer pixels = this.getByteBufferFromString();
        glEnable(GL_TEXTURE_2D);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 1000, 200, 5, GL_BLUE, GL_UNSIGNED_BYTE, pixels);
        glEnd();

        if(Input.isKeyPressed(GLFW_KEY_A))
            System.out.println(1);

        glfwPollEvents();
    }
}

private ByteBuffer getByteBufferFromString(){

    String text = "ABCD";
    int s = 256; //Take whatever size suits you.
    BufferedImage b = new BufferedImage(s, s, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g = b.createGraphics();
    g.drawString(text, 0, 0);

    int co = b.getColorModel().getNumComponents();

    byte[] data = new byte[co * s * s];
    b.getRaster().getDataElements(0, 0, s, s, data);

    ByteBuffer pixels = BufferUtils.createByteBuffer(data.length);
    pixels.put(data);
    pixels.rewind();
    return pixels;
}

/**
 * Initialize window
 */
private void init() {
    //Set Callback, when library get an error, it will be print
    GLFWErrorCallback.createPrint(System.err).set();

    //Let's initialize GLFW. Without this call, most of functions won't work
    if(!glfwInit()){
        throw new JJGLInitException("Could not initialize GLFW");
    }

    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);   //Make window hidden
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);  //Make window resizeable

    window = this.createWindow();

    glfwMakeContextCurrent(this.window);

    glfwShowWindow(this.window);
}

/**
 * Creating window. If you have passed -1 at one side,
 * it will be maximize (-1, 30 only width will be maximized)
 * @return
 */
private long createWindow() {
    //Getting info about screen
    GLFWVidMode glfwVidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());

    if(this.WIDTH <= -1)
        this.WIDTH = glfwVidMode.width();

    if(this.HEIGHT <= -1)
        this.HEIGHT = glfwVidMode.width();

    //Creating window
    return  glfwCreateWindow(this.WIDTH, this.HEIGHT, this.GAME_NAME, MemoryUtil.NULL, MemoryUtil.NULL);
}

public static long getWindow() { return window; }
}

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ "It doesn't work" is never enough information to diagnose a problem. Tell us the patient's symptoms: the specific way it fails to work. Do you get a compiler error? A runtime error? No output? The wrong output? A blue screen? Your computer catches fire? There are many varied ways for something to "not work" and they all tell us useful things about how to make it work, so never leave that out. \$\endgroup\$
    – DMGregory
    Commented May 7, 2021 at 13:54
  • \$\begingroup\$ Oh yeah. Sorry. I forgot them. I've edited my question adding the whole code and an image, too. I have no output: no string rendered \$\endgroup\$
    – DynoZ
    Commented May 7, 2021 at 14:51
  • \$\begingroup\$ I've figured out this git repo: git. Has someone already try this? \$\endgroup\$
    – DynoZ
    Commented May 7, 2021 at 14:52
  • \$\begingroup\$ Don't tell us in the comments. Edit your question text to describe that situation specifically. If that git repo solved your problem, share your solution as an answer. If you have a question about it, edit your post to ask that question. \$\endgroup\$
    – DMGregory
    Commented May 7, 2021 at 15:02

1 Answer 1

1
\$\begingroup\$

I see several issues and missing things in your approach. Foremost problem is that you are not issuing any actual OpenGL draw call, i.e. you are never telling OpenGL to render anything.

In your loop function you do:

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glfwSwapBuffers(this.window);

This clears the back buffer (to yellow in your case) and then you immediately present the contents of this back buffer to your window with the glfwSwapBuffers command.

What you would need to do is in between these lines issue actual drawing commands, such as glDrawArrays(...) etc. For such a draw call to work, you need to pass some geometry to OpenGL. This can be done in several ways, modern GPUs and modern OpenGL, versions 3.x and 4.x, prefer to use so-called vertex buffers which reside in memory on the GPU. These vertex buffers are associated with so-called vertex arrays which you can use to actually relate your vertex data to certain vertex shader inputs.

I would recommend you look into basic tutorials such as ones on LearnOpenGL. I know the examples there are not written in Java, but the basic facts about how to use OpenGL should be very much applicable anyway.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .