LWJGL rendering problem?

Hello!
Can anybody help me? Ive got this problem. I have created a game, where you have to build shops. I have this code:


if (Mouse.isButtonDown(0) {
            stall(mouseX, mouseY);
        }

And this code to set the value to the var, in the main game loop:


mouseX = Mouse.getX();
mouseY = Mouse.getY();

When I start the game, and I click the left mouse button, the shop appeares, but after I take my finger of the mouse
button, it disappeares! Please help!

my shop code:


public static void stall(int x, int y) {
        glLoadIdentity();
        store.bind();
        glTranslatef(x, y, 0);
        glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
            glTexCoord2f(0, store.getHeight());
            glVertex2f(0, 50);
            glTexCoord2f(store.getWidth(), store.getHeight());
            glVertex2f(50, 50);
            glTexCoord2f(store.getWidth(), 0);
            glVertex2f(50, 0);
        glEnd();

I would recommend storing the last state of the left-button in a persistent variable somewhere.


boolean lastLeftButtonState;

Then each frame while you are processing the input events, check to see if a state change (0 -> 1 or 1 -> 0) occurs. This corresponds to a ‘pressed’ or ‘released’ event.


boolean isLeftButtonDown = Mouse.isButtonDown(0);
if (isLeftButtonDown != lastLeftButtonState) {
  // a transition has occurred; determine what it is and trigger an event.
  if (isLeftButtonDown) {
    // pressed event; add your shop to the game state
  } else {
    // released event
  }
  lastLeftButtonState = isLeftButtonDown;
}

Then add handling to detect a ‘pressed’ event and add your shop.

Inside your render loop, check your game state for the shop and if it’s present, display it.

Sorry, Id did not work :frowning:

You need to actually store the existence of your shop in a variable somewhere. Then read that variable during the render pass.

Right now you’re triggering the display of your shop based on the mouse button state. The input event handling and render pass is occurring 60 times per second.

What your code is currently doing is that it checks if the mouse button is pressed down and draws a stall if it is. Once you let it go it will disappear, because that’s exactly what you’ve told it to do.

What you want to do is add your stall to a list, and draw all stalls in the list each frame. This will allow you add and remove stalls whenever you want to.

I don’t know, but maybe there is isMousePressed(int mb) method, that you should use ?

I’m going to go one step further and assume you don’t fully understand Java and OOP. I recommend you learn both fully before jumping into a complex API like OpenGL.

I have seen stuff you previously coded.

that stall(mouseX, mouseY) only renders the stall.

If you stop pressing the left mouse button, it stops to get inside the if statment, and with that doesn’t render your stall :slight_smile:

(A little hard without to know what stall actually does. I’ve seen it in another post of you :slight_smile: )