2017-08-22 4 views
-2

ich zur Zeit mache einen kleinen C++ Motor opengl mit und GLFW und ich erhalte einen seltsamen Fehler beim Versuch, die glfwWindowShouldClose(window) Funktion hier zu nennen, sind mein Code:glfwWindowShouldClose Fehler

#include "Engine\Window.h" 

#include <iostream> 

using namespace std; 

int main() 
{ 
    GLFWwindow* window = 0; 
    Window::InitGLFW(); 
    Window::CreateWindow(window,640,480,"1"); 

    while (true) 
    { 
     if (glfwWindowShouldClose(window))//here is the error 
     { 
      Window::DestroyWindow(window); 
      Window::EndGLFW(); 
      return 0; 
     } 
    } 
    system("pause"); 
    return 0; 
} 

Window.h Datei:

#ifndef ENGINE_WINDOW 
#define ENGINE_WINDOW 

#include <iostream> 
#include <vector> 

#include "GL\glew.h" 
#include "GLFW\glfw3.h" 

using namespace std; 

class Window 
{ 
public: 


    static bool InitGLFW(); 
    static void EndGLFW(); 
    static bool CreateWindow(GLFWwindow* Window, int Width, int Height, char* Title); 

    static void DestroyWindow(GLFWwindow* Window); 

private: 
    static void error_callback(int error, const char* description); 
}; 



#endif 

Nun ist die Window.cpp Datei:

#include "Window.h" 


void Window::error_callback(int error, const char* description) 
{ 
    cout << "Error: %s\n" << description; 
} 

bool Window::CreateWindow(GLFWwindow* Window, int Width, int Height,char* Title) 
{ 
    Window = glfwCreateWindow(Width, Height, Title, NULL, NULL); 
    if (!Window) 
    { 
     cout << "Window or OpenGL context creation failed"; 
     return 1; 
    } 
    glfwMakeContextCurrent(Window); 
    return 0; 
} 

bool Window::InitGLFW() 
{ 
    glfwSetErrorCallback(error_callback); 

    if (!glfwInit()) 
    { 
     cout << "glfw Initialization failed"; 
     return 1; 
    } 
} 

void Window::DestroyWindow(GLFWwindow* Window) 
{ 
    glfwDestroyWindow(Window); 
} 

void Window::EndGLFW() 
{ 
    glfwTerminate(); 
} 

so wie Sie sehen können die das Programm gibt mir einen Fehler, wenn ihre Lauf nicht, wenn ich kompilieren und der Fehler ist:

Unhandled exception to 0x56B34B9F (glfw3.dll) in Engine.exe: 0xC0000005: Access violation when reading location 0x00000014. 

ich die Variable annehmen, dass glfwWindowShouldClose Blick auf nicht erstellt wird?

wenn Sie muss ich wissen, leite auf 10 Fenster und ich verwende Visual Studio 2015

Antwort

1
if (glfwWindowShouldClose(window))//here is the error 

Das ist, weil window ein NULL-Zeiger ist.

Sie initialisieren es hier:

GLFWwindow* window = 0; 

und ändern nie Wert es wieder.

Diese Funktion

bool Window::CreateWindow(GLFWwindow* Window, int Width, int Height,char* Title) 
{ 
    Window = glfwCreateWindow(Width, Height, Title, NULL, NULL); 
    [...] 
} 

gerade aktualisiert es lokale Kopie des Window Variable ist und leckt den Zeiger, wenn die Funktion verlassen wird.