代码改变世界

Programming 2D Games 读书笔记(第二章)

  Clingingboy  阅读(468)  评论(0编辑  收藏  举报

 

本意还是想了解DirectX的,由于网上拿不到书的pdf文档,幸好有作者的源代码示例,想完整的看一下,基本的游戏需要的点.

下面直接以代码为例,仅用于帮助自身理解

http://www.programming2dgames.com/chapter2.htm

示例一:Hello World

创建了一个标准的Win32消息循环程序示例

示例二:Character Input

介绍了键盘输入消息WM_CHAR

        case WM_CHAR:               // a character was entered by the keyboard
            switch (wParam)         // the character is in wParam
            {
                case 0x08:          // backspace
                case 0x09:          // tab
                case 0x0A:          // linefeed
                case 0x0D:          // carriage return
                case 0x1B:          // escape
                    MessageBeep((UINT) -1);    // beep but do not display
                    return 0;
                default:            // displayable character
                    ch = (TCHAR) wParam;    // get the character
                    InvalidateRect(hwnd, NULL, TRUE);    // force WM_PAINT
                    return 0;
            }

示例三:Keys Down

介绍了键盘消息

        case WM_KEYDOWN:                                // key down
            vkKeys[wParam] = true;
            switch(wParam)
            {
                case VK_SHIFT:                          // shift key
                    nVirtKey = GetKeyState(VK_LSHIFT);  // get state of left shift
                    if (nVirtKey & SHIFTED)             // if left shift
                        vkKeys[VK_LSHIFT] = true;
                    nVirtKey = GetKeyState(VK_RSHIFT);  // get state of right shift
                    if (nVirtKey & SHIFTED)             // if right shift
                        vkKeys[VK_RSHIFT] = true;
                    break;
                case VK_CONTROL:                        // control key
                    nVirtKey = GetKeyState(VK_LCONTROL);
                    if (nVirtKey & SHIFTED)             // if left control
                        vkKeys[VK_LCONTROL] = true;
                    nVirtKey = GetKeyState(VK_RCONTROL);
                    if (nVirtKey & SHIFTED)             // if right control
                        vkKeys[VK_RCONTROL] = true;
                    break;
            }
            InvalidateRect(hwnd, NULL, TRUE);           // force WM_PAINT
            return 0;
            break;

示例四:Prevent Multiple

使用Mutex实现单实例

bool AnotherInstance()
{
    HANDLE ourMutex;

    // Attempt to create a mutex using our unique string
    ourMutex = CreateMutex(NULL, true, "Use_a_different_string_here_for_each_program_48161-XYZZY");

    if (GetLastError() == ERROR_ALREADY_EXISTS)
        return true;            // another instance was found
    
    return false;               // we are the only instance
}
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示