Lesson 5: 实时消息循环

本文详细介绍了PeekMessage()和GetMessage()这两个Windows消息处理函数的不同之处,并通过直观的图表进行了对比。对于游戏编程而言,PeekMessage()提供了更为灵活的控制方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:
http://www.directxtutorial.com/Lesson.aspx?lessonid=11-1-5

In this lesson we will cover a single function, PeekMessage(), and how this function differs from its evil twin, GetMessage().
主要讲的就是PeekMessage()和GetMessage()两个获取消息函数的不同,而对于游戏编程,我们更需要的是前者。这一点已经在Lesson3中说了,不再做赘述。

简单上两个图片再次说明两个函数获取消息的差别吧:
GetMessage():
这里写图片描述

PeekMessage():
这里写图片描述

如何将PeekMessage()函数补充到我们之前的消息循环中呢?

// enter the main loop:

// this struct holds Windows event messages
MSG msg = {0};

// Enter the infinite message loop
while(TRUE)
{
    // Check to see if any messages are waiting in the queue
    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);

        // check to see if it's time to quit
        if(msg.message == WM_QUIT)
            break;
    }
    else
    {
        // Run game code here
        // ...
        // ...
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值