[CLOSED] Something goes terribly wrong.

Started by technik3k, August 18, 2015, 16:12:34

Previous topic - Next topic

technik3k

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.
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;
	}

spasi

You forgot glfwPollEvents() in the render loop.

technik3k


technik3k

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

Kai

Quote from: technik3k on August 24, 2015, 21:12:05
... 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. :)