int main() 不断运行
我是 C++ 的新手,所以如果这是一个愚蠢的问题,我深表歉意,但我似乎找不到答案。
我已经使用Processing有一段时间了,想开始使用C++,因为我听说它更快,而且我制作的程序太长/太密集,无法以合理的速度运行Processing。
在处理中,有一个运行一次的设置空白,然后是连续运行的绘制空白。这是我习惯的,我需要它来用 C++(国际象棋 AI)重新制作一个程序。
有没有办法让 int main 持续运行?如果没有,我可以让它调用一个将连续运行的函数吗?
还有一种方法可以在运行程序时弹出一个可以绘制几何图形的窗口? (理想情况下,我需要制作可以通过鼠标操作的部件)
顺便说一下,我正在使用 Xcode
I am brand new to c++ so I apologize if this is a stupid question but I can't seem to find the answer to it.
I have been using Processing for a while now and would like to start using c++ because I heard it is faster and a program I made is too long/dense for Processing to run at a reasonable speed.
In Processing there is a setup void which runs once and then the draw void which runs continuously after that. This is what I am used to and I need it to make remake a program in c++ (a chess AI).
Is there a way to get int main to run continuously? If not can I have it call a function that will run continuously?
Also is there a way to make a window pop up when you run the program which you can draw geometry to? (I will need to make pieces that can be manipulated by a mouse ideally)
I'm using Xcode by the way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 C++ 中,每个函数都是由它的调用和返回来定义的。例如:
当 foo() 被调用时,它会一直运行到return语句为止。如果我们希望 foo 运行一段不确定的时间,我们可以,例如:
这是一个丑陋的例子 - 使用 cin 到 char 之类的 - 但它传达了这个想法。 while 循环将永远运行,并且它的内部结构(好吧,无论如何,这是逻辑)可以替换为您的程序需要执行的任何操作。
有道理吗?
In C++, each function is defined by it's call and it's return. For example:
When foo() is called, it runs until the return statement. If we want foo to run for some indeterminate amount of time, we could, for example:
This is a kind of ugly example - using cin to a char and whatnot - but it gets the idea across. The while loop will run forever and the innards of it (well, it's logic, anyway) could be replaced with whatever your program needed to do.
Make sense?
就图形库而言,有很多选择;您可以使用 SDL、GLUT/OpenGL、DirectX,甚至是优秀的 Win32。然而,对于像 while 循环这样的基本事物相对陌生的人来说,我建议您暂时远离 C++,因为有许多特性可能会成为巨大的障碍。如果您确实需要每一点速度,我建议您使用对时间要求严格的算法制作一个 DLL,并将其与支持 DLL 的更简单的语言结合使用,并提供相对对开发人员友好的界面。尽管我确信有很多选择,但我立即想到的是 Game Maker。
祝你好运。
There are plenty of options as far as graphics libraries go; you can use SDL, GLUT/OpenGL, DirectX, even good ol' Win32. However, for someone who is relatively new to things as rudimentary as while loops, I suggest that you stay off the C++ for a while, as there are many peculiarities that might prove to be enormous roadblocks. If you really need every ounce of speed, I recommend that you make a DLL with your time-critical algorithms and use it in conjunction with a simpler language that supports DLL's, and provides a relatively developer-friendly interface. Game Maker comes immediately to mind, although I'm sure there are many options out there.
Best of luck.
我建议您查看 Cinder 或 OpenFrameworks 作为从Processing.org 的一个巧妙的过渡 - 特别是如果您计划开发多媒体应用程序(如果您正在使用Processing,则很可能)
它们都提供了一个非常相似的层处理的部分,并且会在一定程度上简化您的旅程。
如果您愿意的话,您还可以在 SDL 之上实现自己的基本框架。
作为对您的问题的更一般的回答, main() 函数基本上与Processing.org 中的 setup() 函数相同 - 主要区别在于它必须调用(用户提供的)draw() 函数或相等的。
因此,基本的等价物是:
NB:上面的代码可能会编译,但除了循环之外它什么也不做,并且可能会使您的计算机陷入困境,因为它没有连接到任何图形库并且没有获得用户输入来修改
quit
布尔值。最后,我将引用 Cinder 常见问题解答中的一段内容:
I'd recommend having a look at Cinder or OpenFrameworks as a neat transition from Processing.org - especially if you're planning on doing multimedia applications (which, if you were using Processing, is likely)
They both provide a very similar layer to that of Processing, and will ease your journey somewhat.
You could also implement your own basic framework on top of SDL if you feel up to it.
As a more general answer to your question, the main() function is basically the same as the setup() function in Processing.org - with the main distinction being that it has to call a (user-provided) draw() function or equivalent.
So a rudimentary equivalent would be:
NB: the above will probably compile, but it would do nothing except loop and potentially bog up your machine since it's not connected to any graphics library and is getting no user input to modify the
quit
boolean.finally, I'll quote a section from the Cinder faq:
main()
通常应该进行设置,然后启动工具包提供的主消息处理循环。消息处理循环将持续运行,直到用户请求您的应用程序退出(或者您要求工具包关闭您的应用程序)。每当您的窗口需要绘制时,您的工具包都会调用您的绘制函数。当用户输入(例如按键或鼠标单击)时,它将调用其他函数。
例如,如果您使用的是 GLUT 工具包(适用于 OpenGL,这是一种在 Mac、Windows、Linux 和许多移动设备上支持的非常流行的绘图 API),您的 main 函数可能如下所示 (完整教程):
对于 Cocoa,OSX 本机 API,它可能看起来像这样 (更多信息和链接):
main()
should typically do your setup and then start the main message-processing loop provided by your toolkit. The message processing loop will run continuously until the user requests your application to quit (or you ask the toolkit to shut down your app).Your toolkit will call your draw function whenever your window needs to be painted. It will call other functions when user input such as keypresses or mouse clicks happen.
For example, if you were using the GLUT toolkit (for OpenGL, a very popular drawing API supported on Mac, Windows, Linux, and many mobile devices), your main function might look like this (complete tutorial here):
For Cocoa, the OSX native API, it might look like this (more information and links here):
我可以建议您不要在 StackOverflow 上问这样非常基本的问题,而是花时间阅读其中一篇 数千个 散布在网络上的 C++ 入门教程。
阅读几个小时后,您会发现通过 Google 搜索可以更快地回答此类问题。
祝你学习顺利。
May I suggest that instead of asking very rudimentary questions like this on StackOverflow, you go and invest your time reading one of the thousands of introductory C++ tutorials that are scattered all over the web.
After a couple of hours of reading you'll find that questions like this are answered faster via a Google search.
Good luck with your learning.
您不应该尝试让 main() 连续运行。
您可以这样做:
int main() {
而(真){
//这里调用函数
}
返回1;
}
You should not try to get main() to run continuously.
You may instead do something like this:
int main() {
while (true) {
//call functions here
}
return 1;
}