int main() 不断运行

发布于 2024-10-26 13:55:19 字数 352 浏览 6 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

狼性发作 2024-11-02 13:55:20

在 C++ 中,每个函数都是由它的调用和返回来定义的。例如:


void foo()
{
   cout << "hello world!";
   return;
}

int main()
{
    foo();
    return 0;
}

当 foo() 被调用时,它会一直运行到return语句为止。如果我们希望 foo 运行一段不确定的时间,我们可以,例如:

void foo()
{
   bool isExiting = false;
   char input;

   while( isExiting != true )
   {
       cout << "Exit? ";
       cin >> input;
       if ( input == 'y' )
       {
           isExiting = true;
       }

       return;
   }
}

int main()
{
    foo();
    return 0;
}

这是一个丑陋的例子 - 使用 cin 到 char 之类的 - 但它传达了这个想法。 while 循环将永远运行,并且它的内部结构(好吧,无论如何,这是逻辑)可以替换为您的程序需要执行的任何操作。

有道理吗?

In C++, each function is defined by it's call and it's return. For example:


void foo()
{
   cout << "hello world!";
   return;
}

int main()
{
    foo();
    return 0;
}

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:

void foo()
{
   bool isExiting = false;
   char input;

   while( isExiting != true )
   {
       cout << "Exit? ";
       cin >> input;
       if ( input == 'y' )
       {
           isExiting = true;
       }

       return;
   }
}

int main()
{
    foo();
    return 0;
}

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?

半山落雨半山空 2024-11-02 13:55:20

就图形库而言,有很多选择;您可以使用 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.

生生不灭 2024-11-02 13:55:20

我建议您查看 CinderOpenFrameworks 作为从Processing.org 的一个巧妙的过渡 - 特别是如果您计划开发多媒体应用程序(如果您正在使用Processing,则很可能)

它们都提供了一个非常相似的层处理的部分,并且会在一定程度上简化您的旅程。

如果您愿意的话,您还可以在 SDL 之上实现自己的基本框架。

作为对您的问题的更一般的回答, main() 函数基本上与Processing.org 中的 setup() 函数相同 - 主要区别在于它必须调用(用户提供的)draw() 函数或相等的。

因此,基本的等价物是:

bool quit = FALSE;

void setup() {
    // initialise the screen and so forth
}

void draw() {
    // perform some drawing and update tasks
}

int main(int argc, char *argv[]) {
    setup();
    while (!quit) {
        draw();
    }
    shutdown();
    return 0;
}

NB:上面的代码可能会编译,但除了循环之外它什么也不做,并且可能会使您的计算机陷入困境,因为它没有连接到任何图形库并且没有获得用户输入来修改 quit 布尔值。

最后,我将引用 Cinder 常见问题解答中的一段内容:

我在处理方面经验丰富,但我
我想我已经准备好尝试新事物了。
Cinder 适合我吗?

很有可能。
首先,请确保您确实需要
继续Cinder。你已经
尝试使用外部
像 Eclipse 这样的 IDE?你用的是原生的吗
OpenGL 调用而不是 PGraphics?
用 Toxi's 做实验怎么样?
优秀的图书馆?你会学到一些
那些最终会成为现实的事情
过渡到 Cinder 更容易,并且
尽管我们热衷于 C++,但这很容易
低估处理程度
可以带你。说了这么多,别让
我们也劝你别这么做——如果
您对学习 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:

bool quit = FALSE;

void setup() {
    // initialise the screen and so forth
}

void draw() {
    // perform some drawing and update tasks
}

int main(int argc, char *argv[]) {
    setup();
    while (!quit) {
        draw();
    }
    shutdown();
    return 0;
}

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:

I’m experienced with Processing, but I
think I’m ready to try something new.
Is Cinder right for me?

Very possibly.
First though, be sure you really need
to move on to Cinder. Have you already
experimented with using an external
IDE like Eclipse? Are you using native
OpenGL calls instead of PGraphics?
What about experimenting with Toxi’s
excellent libraries? You’ll learn some
things that will make an eventual
transition to Cinder much easier, and
as much as we’re into C++, it’s easy
to underestimate how far Processing
can take you. All that said, don’t let
us talk you out of this either — if
you’re excited about learning Cinder,
we’re excited to have you, and we bet
you’ll find it’s easier to get started
than you might imagine.

花落人断肠 2024-11-02 13:55:19

main() 通常应该进行设置,然后启动工具包提供的主消息处理循环。消息处理循环将持续运行,直到用户请求您的应用程序退出(或者您要求工具包关闭您的应用程序)。

每当您的窗口需要绘制时,您的工具包都会调用您的绘制函数。当用户输入(例如按键或鼠标单击)时,它将调用其他函数。

例如,如果您使用的是 GLUT 工具包(适用于 OpenGL,这是一种在 Mac、Windows、Linux 和许多移动设备上支持的非常流行的绘图 API),您的 main 函数可能如下所示 (完整教程):

void main(int argc, char **argv) 
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
  glutInitWindowPosition(100,100);
  glutInitWindowSize(500,500);
  glutCreateWindow("My First openGL Program");
  glutDisplayFunc(render);
  glutMainLoop();
}

对于 Cocoa,OSX 本机 API,它可能看起来像这样 (更多信息和链接):

#import <Cocoa/Cocoa.h>

int main(int argc, const char** argv)
{
    return NSApplicationMain(argc,  argv);
}

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):

void main(int argc, char **argv) 
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
  glutInitWindowPosition(100,100);
  glutInitWindowSize(500,500);
  glutCreateWindow("My First openGL Program");
  glutDisplayFunc(render);
  glutMainLoop();
}

For Cocoa, the OSX native API, it might look like this (more information and links here):

#import <Cocoa/Cocoa.h>

int main(int argc, const char** argv)
{
    return NSApplicationMain(argc,  argv);
}
若能看破又如何 2024-11-02 13:55:19

我可以建议您不要在 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.

锦欢 2024-11-02 13:55:19

您不应该尝试让 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;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文