我正在使用 OpenGL 在 C++ 中制作塔防游戏。当尝试使用 Windows.H 中的“ sleep ”功能时,它会导致我的 OpenGL 窗口打开我输休眠眠的任何值的总时间,然后打开窗口。有什么建议么?
#include <GLFW/glfw3.h>
#include <iostream>
#include <Windows.h>
void enemy(int num) //Written By Ryan
{
switch (num)
{
case 1: glBegin(GL_TRIANGLES); //Position 3
glColor3ub(255, 0, 0);
glVertex2f(0.25, 0.9);
glVertex2f(0.1, 0.6);
glVertex2f(0.4, 0.6);
glEnd();
break;
case 2: glBegin(GL_TRIANGLES); //Position 6
glColor3ub(255, 0, 0);
glVertex2f(0.25, 0.4);
glVertex2f(0.1, 0.1);
glVertex2f(0.4, 0.1);
glEnd();
break;
case 3: glBegin(GL_TRIANGLES); //Position 7
glColor3ub(255, 0, 0);
glVertex2f(-0.25, 0.4);
glVertex2f(-0.4, 0.1);
glVertex2f(-0.1, 0.1);
glEnd();
break;
case 4: glBegin(GL_TRIANGLES); //Position 10
glColor3ub(255, 0, 0);
glVertex2f(-0.25, -0.1);
glVertex2f(-0.4, -0.4);
glVertex2f(-0.1, -0.4);
glEnd();
break;
case 5: glBegin(GL_TRIANGLES); //Position 14
glColor3ub(255, 0, 0);
glVertex2f(-0.25, -0.6);
glVertex2f(-0.4, -0.9);
glVertex2f(-0.1, -0.9);
glEnd();
break;
}
}
void map()
{
glColor3ub(0, 128, 0); // set color green
//Position 1
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f); //top left
glVertex3f(-1.0f, 0.5f, 0.0f);//bottom left
glVertex3f(-0.5f, 0.5f, 0.0f); //bottom right
glVertex3f(-0.5f, 1.0f, 0.0f); //top right
glEnd();
//Position 2
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f); //top left
glVertex3f(-0.5f, 0.5f, 0.0f);//bottom left
glVertex3f(0.0f, 0.5f, 0.0f); //bottom right
glVertex3f(0.0f, 1.0f, 0.0f); //top right
glEnd();
//Position 3
glColor3ub(139, 69, 19); // set color brown
glBegin(GL_QUADS);
glVertex3f(0.0f, 1.0f, 0.0f); //top left
glVertex3f(0.0f, 0.5f, 0.0f);//bottom left
glVertex3f(0.5f, 0.5f, 0.0f); //bottom right
glVertex3f(0.5f, 1.0f, 0.0f); //top right
glEnd();
//Position 4
glColor3ub(0, 128, 0); // set color green
glBegin(GL_QUADS);
glVertex3f(0.5f, 1.0f, 0.0f); //top left
glVertex3f(0.5f, 0.5f, 0.0f);//bottom left
glVertex3f(1.0f, 0.5f, 0.0f); //bottom right
glVertex3f(1.0f, 1.0f, 0.0f); //top right
glEnd();
//Position 5
glBegin(GL_QUADS);
glVertex3f(-1.0f, .5f, 0.0f); //top left
glVertex3f(-1.0f, 0.0f, 0.0f);//bottom left
glVertex3f(-0.5f, 0.0f, 0.0f); //bottom right
glVertex3f(-0.5f, 0.5f, 0.0f); //top right
glEnd();
//Position 6
glColor3ub(139, 69, 19); // set color brown
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0.5f, 0.0f); //top left
glVertex3f(-0.5f, 0.0f, 0.0f);//bottom left
glVertex3f(0.0f, 0.0f, 0.0f); //bottom right
glVertex3f(0.0f, 0.5f, 0.0f); //top right
glEnd();
//Position 7
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.5f, 0.0f); //top left
glVertex3f(0.0f, 0.0f, 0.0f);//bottom left
glVertex3f(0.5f, 0.0f, 0.0f); //bottom right
glVertex3f(0.5f, 0.5f, 0.0f); //top right
glEnd();
//Position 8
glColor3ub(0, 128, 0); // set color green
glBegin(GL_QUADS);
glVertex3f(0.5f, 0.5f, 0.0f); //top left
glVertex3f(0.5f, 0.0f, 0.0f);//bottom left
glVertex3f(1.0f, 0.0f, 0.0f); //bottom right
glVertex3f(1.0f, 0.5f, 0.0f); //top right
glEnd();
//Position 9
glBegin(GL_QUADS);
glVertex3f(-1.0f, 0.0f, 0.0f); //top left
glVertex3f(-1.0f, -0.5f, 0.0f);//bottom left
glVertex3f(-0.5f, -0.5f, 0.0f); //bottom right
glVertex3f(-0.5f, 0.0f, 0.0f); //top right
glEnd();
//Position 10
glColor3ub(139, 69, 19); // set color brown
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0.0f, 0.0f); //top left
glVertex3f(-0.5f, -0.5f, 0.0f);//bottom left
glVertex3f(0.0f, -0.5f, 0.0f); //bottom right
glVertex3f(0.0f, 0.0f, 0.0f); //top right
glEnd();
//Position 11
glColor3ub(0, 128, 0); // set color green
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f); //top left
glVertex3f(0.0f, -0.5f, 0.0f);//bottom left
glVertex3f(0.5f, -0.5f, 0.0f); //bottom right
glVertex3f(0.5f, 0.0f, 0.0f); //top right
glEnd();
//Position 12
glBegin(GL_QUADS);
glVertex3f(0.5f, 0.0f, 0.0f); //top left
glVertex3f(0.5f, -0.5f, 0.0f);//bottom left
glVertex3f(1.0f, -0.5f, 0.0f); //bottom right
glVertex3f(1.0f, 0.0f, 0.0f); //top right
glEnd();
//Position 13
glBegin(GL_QUADS);
glVertex3f(-1.0f, -0.5f, 0.0f); //top left
glVertex3f(-1.0f, -1.0f, 0.0f);//bottom left
glVertex3f(-0.5f, -1.0f, 0.0f); //bottom right
glVertex3f(-0.5f, -0.5f, 0.0f); //top right
glEnd();
//Position 14
glColor3ub(139, 69, 19); // set color brown
glBegin(GL_QUADS);
glVertex3f(-0.5f, -0.5f, 0.0f); //top left
glVertex3f(-0.5f, -1.0f, 0.0f);//bottom left
glVertex3f(-0.0f, -1.0f, 0.0f); //bottom right
glVertex3f(-0.0f, -0.5f, 0.0f); //top right
glEnd();
//Position 15
glColor3ub(0, 128, 0); // set color green
glBegin(GL_QUADS);
glVertex3f(0.0f, -0.5f, 0.0f); //top left
glVertex3f(0.0f, -1.0f, 0.0f);//bottom left
glVertex3f(0.5f, -1.0f, 0.0f); //bottom right
glVertex3f(0.5f, -0.5f, 0.0f); //top right
glEnd();
//Position 16
glBegin(GL_QUADS);
glVertex3f(0.5f, -0.5f, 0.0f); //top left
glVertex3f(0.5f, -1.0f, 0.0f);//bottom left
glVertex3f(1.0f, -1.0f, 0.0f); //bottom right
glVertex3f(1.0f, -0.5f, 0.0f); //top right
glEnd();
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(1000, 1000, "Aloha Tower Defence", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
std::cout << "Error!" << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
//'The' Loop
for (int x = 0; x <= 5; x++)
{
// glClear(GL_COLOR_BUFFER_BIT);
map();
enemy(x);
Sleep(100);
}
// glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
map();
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
所以上面会导致窗口静置 100 毫秒(* 5 因为循环值)然后它打开窗口并在 x 值 5 处显示 Enemy。我想看到敌人从 x 向下“移动”值 1-5
最佳答案
您必须使用应用程序循环。添加变量x
并在循环中增加变量:
int x = 0;
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
//'The' Loop
glClear(GL_COLOR_BUFFER_BIT);
map();
enemy(x);
if (x < 5)
x += 1;
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
Sleep(100);
}
而不是
Sleep
您可以使用 glfwGetTime
,它以秒为单位检索时间,因为 glfw 已初始化:int x = 0;
double startTime = glfwGetTime();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
double currentTime = glfwGetTime();
/* Render here */
//'The' Loop
glClear(GL_COLOR_BUFFER_BIT);
map();
enemy(x);
if (x < 5 && (currentTime - startTime) > x * 0.1)
x += 1;
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
关于c++ - 做一个循环 "slow down",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61432936/