LWJGL Forum

Please login or register.

Login with username, password and session length

Author Topic: [CLOSED] Something goes terribly wrong.  (Read 2015 times)

technik3k

  • Newbie
  • *
  • Offline Offline
  • Posts: 42
[CLOSED] Something goes terribly wrong.
« on: August 18, 2015, 16:12:34 »

I was just try to draw an image onto the screen. When I ran the code it first every thing got lagy and the screen got mest up. Then my computer froze and I had to restart. I have lunix. This is my exact code.
Code: [Select]
private static final long serialVersionUID = 1L;
private static GLFWErrorCallback errorCallback;
private static long windowID;

public static int w;
public static int h;

public static int imageId;

public static void main(String[] args)
{
errorCallback = Callbacks.errorCallbackPrint(System.err);
glfwSetErrorCallback(errorCallback);
glfwInit();

LanchWindow(800, 600, "Game");

glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_MODELVIEW);

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

BufferedImage i;
int textureID = glGenTextures();
System.out.println(textureID);

try
{
i = ImageIO.read(new File("/home/game/Black Hole Breakers/Graphics/wink1.png"));
glGenTextures(textureID, convertImage(i));
}
catch (Exception e1)
{
e1.printStackTrace();
}

while(GLFW.glfwWindowShouldClose(windowID) == GL_FALSE)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, textureID);

glBegin(GL_QUADS);

glTexCoord2f(0,0);
glVertex2f(0,0);

glTexCoord2f(1,0);
glVertex2f(800,0);

glTexCoord2f(1,1);
glVertex2f(800,600);

glTexCoord2f(0,1);
glVertex2f(0,600);

glEnd();

glfwSwapBuffers(windowID);

try { Thread.sleep(50); } catch (InterruptedException e) {Thread.currentThread().interrupt();}
}
}

private static void LanchWindow(int width, int height, String Title)
{
windowID = glfwCreateWindow(width, height, Title, MemoryUtil.NULL, MemoryUtil.NULL);

if(windowID == MemoryUtil.NULL) throw new IllegalStateException("Window Failed");

glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

glfwSetWindowPos(windowID, (GLFWvidmode.width(vidmode) - width) / 2, (GLFWvidmode.height(vidmode) - height) / 2);

glfwMakeContextCurrent(windowID);

glfwShowWindow(windowID);

GLContext.createFromCurrent();

glMatrixMode(GL_PROJECTION);

glOrtho(0, width, 0 , height, -1, 1);
}

private static final int BYTES_PER_PIXEL = 4;

public static ByteBuffer convertImage(BufferedImage image)
{     
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL);

for(int y = 0; y < image.getHeight(); y++)
{
            for(int x = 0; x < image.getWidth(); x++)
            {
                int pixel = pixels[y * image.getWidth() + x];
                buffer.put((byte) ((pixel >> 16) & 0xFF));
                buffer.put((byte) ((pixel >> 8) & 0xFF));
                buffer.put((byte) (pixel & 0xFF));
                buffer.put((byte) ((pixel >> 24) & 0xFF));
            }
        }

        buffer.flip();

return buffer;
}
Logged

spasi

  • Administrator
  • Nerdus Imperius
  • *****
  • Online Online
  • Posts: 1888
Re: Something goes terribly wrong.
« Reply #1 on: August 18, 2015, 17:26:47 »

You forgot glfwPollEvents() in the render loop.
Logged

technik3k

  • Newbie
  • *
  • Offline Offline
  • Posts: 42
Re: Something goes terribly wrong.
« Reply #2 on: August 19, 2015, 03:54:51 »

Thanks a lot, it works smoothly. :)
Logged

technik3k

  • Newbie
  • *
  • Offline Offline
  • Posts: 42
Re: [CLOSED] Something goes terribly wrong.
« Reply #3 on: August 24, 2015, 21:12:05 »

Sorry about that my computer crashed again and I figured out that the problem was my laptop overheting.
Logged

Kai

  • Guest
Re: [CLOSED] Something goes terribly wrong.
« Reply #4 on: August 24, 2015, 21:19:07 »

... and I figured out that the problem was my laptop overheting.
Tip: You might want to disassemble your laptop a bit such that you see its integrated fan and then remove the accumulated dust/grit on the grill and fan and blow the rest through the fan outlet.
Every three to four month that works wonders for the effectiveness/air throughput of my fan. :)
Logged