2017-12-27 4 views
1

私が使用してLinux上++グラムで私の最初のGL簡単なプログラムをコンパイルしようとしている:機能がスコープで宣言されていなかった(GLFW、OpenGLの、Slackwareの)

-lxcb-dri2 -lxcb-lm -ldl -lXrender -ldrm -lXdamage -lX11-xcb -lxcb-glx -dri3 -l-L/usr/local/lib -lrt -lXrandr -lXinerama -lXi -lXcursor -lGL -lm xcb-present -lglfw -lxcb-sync -lxshmfence -lXxf86vm -lXfixes -lXext -lX11 -lpthread -lxcb -lXau -lXdmcp -lGLEW

と、私はこのエラーを持っている:

graphics/window.cpp: In member function 'bool je::graphics::Window::init()': 
graphics/window.cpp:55:32: error: 'key_callback' was not declared in this scope glfwSetKeyCallback(m_Window, key_callback); 

私のコードは3つのファイルにあります。

main.cppに:

#include "graphics/window.h" 

int main() 
{ 
    using namespace je; 
    using namespace graphics; 

    Window window("just engine", 800, 600); 
    glClearColor(0.2f, 0.3f, 0.8f, 1.0f); 

    GLuint vao; 
    glGenVertexArrays(1, &vao); 
    glBindVertexArray(vao); 

    while (!window.closed()) 
    { 
     window.clear(); 
     if (window.isKeyPressed(GLFW_KEY_A)) 
     { 
      std::cout << "PRESSED!" << std::endl; 
     } 

     glDrawArrays(GL_ARRAY_BUFFER, 0, 6); 
     window.update(); 
    } 
    return 0; 
} 

window.h:

#pragma once 
#include <iostream> 
#include <GL/glew.h> 
#include <GLFW/glfw3.h> 

namespace je{ namespace graphics{ 

#define MAX_KEYS 1024 
#define MAX_BUTTONS  32 

    class Window 
    { 
    private: 

     const char *m_Title; 
     int m_Width, m_Height; 
     GLFWwindow *m_Window; 
     bool m_Closed; 
       static bool m_Keys[MAX_KEYS]; 
       static bool m_MouseButtons[MAX_BUTTONS]; 
       static double mx, my; 
    public: 
     Window(const char *name, int width, int height); 
     ~Window(); 
       void clear() const; 
     void update(); 
       bool closed() const; 

     inline int getWidth() const {return m_Width; } 
     int getHeight() const {return m_Height; } 

     static bool isKeyPressed(unsigned int keycode); 
     //friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); 
    private: 
     bool init(); 
     friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); 
    }; 
}} 

window.cpp:

#include "window.h" 
#include <GLFW/glfw3.h> 

namespace je{ namespace graphics { 


    bool Window::m_Keys[MAX_KEYS]; 
    bool Window::m_MouseButtons[MAX_BUTTONS]; 
    double Window::mx; 
    double Window::my; 
    void window_resize(GLFWwindow *window, int width, int height); 
    Window::Window(const char *title, int width, int height) 
    { 
     m_Title = title; 
     m_Width = width; 
     m_Height = height; 
     if (!init()) 
      glfwTerminate(); 


     for (int i = 0; i < MAX_KEYS; i++) 
     { 
      m_Keys[i] = false; 
     } 
     for (int i = 0; i < MAX_BUTTONS; i++) 
     { 
      m_MouseButtons[i] = false; 
     } 
    } 

    Window::~Window() 
    { 
     glfwTerminate(); 
    } 

    bool Window::init() 
    { 

      if(!glfwInit()) 
      { 
       std::cout << "Failed to initialize GLFW" << std::endl; 
      return false; 
     } 
     m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL); 
     if (!m_Window) 
     { 
      glfwTerminate(); 
      std::cout << "Creating window failed." << std::endl; 
      return false; 
     } 

     glfwMakeContextCurrent(m_Window); 
     glfwSetWindowUserPointer(m_Window, this); 
     glfwSetWindowSizeCallback(m_Window, window_resize); 
     glfwSetKeyCallback(m_Window, key_callback); 

     if (glewInit() != GLEW_OK) 
     { 
      std::cout << "Could not initialize GLEW!" << std::endl; 
      return false; 
     } 

       std::cout << "OpenGL " << glGetString(GL_VERSION) << std::endl; 

     return true; 
    } 

     bool Window::isKeyPressed(unsigned int keycode) 
    { 
     if (keycode >= MAX_KEYS) 
      return false; 
     return m_Keys[keycode]; 
    } 


    void Window::clear() const 
    { 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    } 

    void Window::update() 
    { 
     glfwPollEvents(); 
     glfwGetFramebufferSize(m_Window, &m_Width, &m_Height); 
     glfwSwapBuffers(m_Window); 
    } 

     bool Window::closed() const 
     { 
       return glfwWindowShouldClose(m_Window) == 1; 
     } 

    void window_resize(GLFWwindow *window, int width, int height) 
    { 
     glViewport(0, 0, width, height); 
    } 

     void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 
    { 
     Window* win = (Window*) glfwGetWindowUserPointer(window); 
     win->m_Keys[key] = action != GLFW_RELEASE; 
    } 
} } 

私は本当に初心者だし、私はそれに迷ってしまいました。 ..私は方法を見つけるのを助けることができますか?ありがとうございました。

+0

key_callback(...)はメンバ関数であり、引数として使用することはできません。ラッパーを作成するか、スタティックとして定義するか、単にクラス外で定義する必要があります。 – leyanpan

+0

一貫してコードをインデントしてください。インデント時にタブとスペースを混用しないでください。読みやすさのために、関数間に空白行を2または3(一貫している)に配置します。 – user3629249

+1

C言語ではなくC言語でプログラミングしているようです.C言語にはクラス、コンストラクタ、デストラクタはありません。タグを調整することをお勧めします。 –

答えて

2

C実装のライブラリ関数glfwSetKeyCallback()のパラメータとして、C++のメンバ関数void key_callback()を使用するには、最初にクラス宣言の外側にあるstaticと宣言する必要があります。だからあなたのwindow.hのためにこれを試してみてください。

class Window; 
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); 

class Window { 
    private: 
     // fields here 

    public: 
     // other member functions here 

    private: 
     friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); 
}; 

そしてwindow.cppに:

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { 
    Window* win = (Window*) glfwGetWindowUserPointer(window); 
    win->m_Keys[key] = action != GLFW_RELEASE; 
} 

私はこのGLFWライブラリで書かれていないので、私の答えはあなたの目的に十分ではないかもしれません。次の質問をご覧ください。Pointing to a function that is a class memberIs it possible to declare a friend function as static?

+1

ありがとうございました! window.hだけの編集は私のために働いた。 – included

関連する問題