我希望窗口标题在窗口调整大小时自动更新。它应该采用一种格式,以便显示标题 窗口大小 - [宽度] x [高度] 但结果我得到一个标题显示_alloca已损坏。
int main(void)
{
GLFWwindow *window = NULL; //initialise window with pointer to GLFW handle
glfwSetErrorCallback(error_callback);
//initialise GLFW
if (!glfwInit())
{
//if failed to initialise GLFW
exit(EXIT_FAILURE);
}
//minimum OpenGL version 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//create the window here
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Window Size - " + WINDOW_WIDTH + "x" + WINDOW_HEIGHT, NULL, NULL);
//if window fail, terminate GLFW
if (window == NULL)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window); // set window context as the current context
glfwSwapInterval(1); // swap buffer interval
// initialise GLEW
if (glewInit() != GLEW_OK)
{
// if failed to initialise GLEW
cerr << "GLEW initialisation failed" << endl;
exit(EXIT_FAILURE);
}
//set key callback function
glfwSetKeyCallback(window, key_callback);
//render loop
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window); //swap buffers
glfwPollEvents(); //poll for events
}
//close window, terminate GLFW
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
答案 0 :(得分:2)
正如@NicoleBolas已经在评论中提到的,有问题的部分是
"Window Size - " + WINDOW_WIDTH + "x" + WINDOW_HEIGHT
这是合法的C ++,但没有按照您的预期执行。此处WINDOW_WIDTH
的值会添加到"Window Size"
常量的地址中。结果是一个新指针,它指向一个无效的内存地址。
如果要将宽度和高度附加为文本,则必须先将数字转换为文本,然后将结果字符串连接起来。一种简单的方法是使用stringstream
:
std::stringstream s;
s << "Window Size - " << WINDOW_WIDTH << "x" << WINDOW_HEIGHT;
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, s.str().c_str(), NULL, NULL);