input - C++ using a member function from from one class in the main function? -
i have main , im setting input im using getters/setters use method inputhander main doing causing stack overflow there infinite loop going on.
the warning get:
warning c4717: 'key_callback' : recursive on control paths, function cause runtime stack overflow
i have no other idea how call method other use getter , setter
main.cpp
#include <glfw3.h> #include <stdio.h> #include <iostream> #include "inputhandler.h" using namespace std; /* begin void prototyping */ void error_callback(int error, const char* description); void key_callback(glfwwindow* window, int key, int scancode, int action, int mods); int main(void) { glfwwindow* window; /* initializes error call-backs */ glfwseterrorcallback(error_callback); /* initialize library */ if (!glfwinit()) return -1; /* create windowed mode window , opengl context */ window = glfwcreatewindow(640, 480, "hello world", null, null); // fullscreen glfwgetprimarymonitor() (first null) if (!window) { glfwterminate(); return -1; } /* makes opengl context current */ glfwmakecontextcurrent(window); /* make window's context current */ glfwmakecontextcurrent(window); /* receives input events */ glfwsetkeycallback(window, key_callback); /* loop until user closes window */ while (!glfwwindowshouldclose(window)) { /* render here */ float ratio; int width, height; glfwgetframebuffersize(window, &width, &height); ratio = width / (float) height; glviewport(0, 0, width, height); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glenable(gl_depth_test); gldepthfunc(gl_lequal); glmatrixmode(gl_projection); glloadidentity(); glortho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f); glmatrixmode(gl_modelview); glloadidentity(); glrotatef((float) glfwgettime() * 10.f, 0.f, 10.f, 0.f); /* swap front , buffers */ glfwswapbuffers(window); /* poll , process events */ glfwpollevents(); } glfwdestroywindow(window); glfwterminate(); return 0; } /* calls program if glfw function fail , logs */ void error_callback(int error, const char* description) { fputs(description, stderr); } /* gets inputhandler::key_callback returns key_callback use in main.cpp */ void key_callback(glfwwindow* window, int key, int scancode, int action, int mods) { return key_callback(window, key, scancode, action, mods); }
inputhandler.h
#pragma once #include <glfw3.h> #include <iostream> using namespace std; class inputhandler { public: inputhandler(void); ~inputhandler(void); static void key_callback(glfwwindow* window, int key, int scancode, int action, int mods); };
inputhandler.cpp
#include "inputhandler.h" glfwwindow* window; inputhandler::inputhandler(void) { } inputhandler::~inputhandler(void) { } /* gives keys events */ static void key_callback(glfwwindow* window, int key, int scancode, int action, int mods) { switch(key) { case glfw_key_escape: glfwsetwindowshouldclose(window, gl_true); break; case glfw_key_w: cout << "w working" << endl; break; case glfw_key_a: break; case glfw_key_s: break; case glfw_key_d: break; case glfw_key_1: break; case glfw_key_2: break; } }
as error message says, function:
void key_callback(glfwwindow* window, int key, int scancode, int action, int mods) { return key_callback(window, key, scancode, action, mods); }
calls itself, entering recursive death spiral. presumably, meant
return inputhandler::key_callback(window, key, scancode, action, mods);
more simply, remove non-member function , set real callback directly:
glfwsetkeycallback(window, &inputhandler::key_callback);
you'll need fix definition of static member:
/*no static here*/ void inputhandler::key_callback(glfwwindow* window, int key, int scancode, int action, int mods) {// ^^^^^^^^^^^^^^ // whatever }
Comments
Post a Comment