anarchokawaii
anarchokawaii

Reputation: 116

My GLFW window is displaying whatever is behind it

This is the code I am using

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main(){
        if(!glfwInit()){
                fprintf(stderr,"failed glfw initialization");
                return -1;
        }

        GLFWwindow* window = glfwCreateWindow(800,800,"i hope this works",NULL,NULL);
        if (!window){
                fprintf(stderr,"window creation failed");
                return -1;
        }
        glfwMakeContextCurrent(window);
        glewExperimental = 1;
        if(glewInit() != GLEW_OK){
                fprintf(stderr,"glew failed to initialize");
                return -1;
        }
        while(!glfwWindowShouldClose(window)){
        }
}

Whenever I run the program, the window shows up, but it has the image of whatever was behind it when the window was opened.

image description

Sometimes it changes when I resize or move the window.

Upvotes: 1

Views: 150

Answers (1)

Archmede
Archmede

Reputation: 1852

You need to clear your buffers. If you're using two buffers then write:

// Render
// Clear the colorbuffer
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

at the beginning of you Game Loop.

Also write:

// Swap the screen buffers
glfwSwapBuffers(window);

at the end of your Game Loop.

So all together:

while(!glfwWindowShouldClose(window)){
    // Render
    // Clear the colorbuffer
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* ... Code ... */

    // Swap the screen buffers
    glfwSwapBuffers(window);
}

Upvotes: 1

Related Questions