简体   繁体   English

C ++新手:我需要帮助来创建一个简单的计时器函数

[英]C++ noob: I need help creating a simple timer function

I'm a C++ beginner. 我是C ++初学者。 I'm searching for a way to may a simple timer function for a very simple console game (class assignment). 我正在寻找一种可以为非常简单的控制台游戏(类分配)提供简单计时器功能的方法。 Hopefully a preexisting function from a common library? 希望来自公共库的预先存在的功能? Please keep it simple since I am new. 由于我是新来的,请保持简单。 Thanks. 谢谢。 Sorry if I'm not wording this correctly. 抱歉,如果我的措词不正确。 Brand new to this forum as well! 也是这个论坛的新手!

http://en.cppreference.com/w/cpp/chrono/c/time http://en.cppreference.com/w/cpp/chrono/c/time

http://www.cplusplus.com/reference/ctime/ http://www.cplusplus.com/reference/ctime/

It's not that different from any other loop or iteration, simply use the given values/constant as your condition. 它与任何其他循环或迭代没有什么不同,只需使用给定的值/常量作为条件即可。

you can use the time function from #include <time.h> . 您可以使用#include <time.h>time功能。 Use it to get a start and stop time, then subtract to get the elapsed time. 使用它来获取开始和停止时间,然后减去以获取经过的时间。 Info here . 信息在这里

Assuming you are using plain C++ in Win32 there is a function SetTimer() from the Win32 API that goes like this 假设您在Win32中使用纯C ++,则Win32 API中有一个函数SetTimer()如下所示

UINT myTimer = SetTimer ( hwnd, ID_60SECONDS, 60 * 1000, NULL );

You will then need to add a case statement to your WndProc to handle the elapsed timer 然后,您需要在WndProc添加一个case语句来处理经过的计时器

case WM_TIMER:
switch (wParam)

{

    case ID_60SECONDS:

    // We processed it, so return 0

    return 0;

}

break;

Unfortunately, this involves dealing with Win32 which is no that easy but once you got the hang it is very obvious. 不幸的是,这涉及到处理Win32的过程并非易事,但是一旦遇到问题,这是非常明显的。

I took the code from this tutorial. 我从教程中获取了代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM