• windows编程:画线,简单的碰撞检测,简单的帧率锁定


    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <windowsx.h>
    #include <mmsystem.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define WINDOW_CLASS_NAME "WINCLASS1"
    
    #define WINDOW_WIDTH  640
    #define WINDOW_HEIGHT 480
    
    #define KEYDOWN(vk_code)    ((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0)
    #define KEYUP(vk_code)        ((GetAsyncKeyState(vk_code) & 0x8000) ? 0:1)
    
    //全局变量
    HWND main_window_handle = NULL;
    HINSTANCE hInstance_App = NULL;
    
    char buffer[80];
    
    //窗口处理函数
    LRESULT CALLBACK WindowProc(HWND hwnd,
        UINT msg,
        WPARAM wParam,
        LPARAM lPram)
    {
        PAINTSTRUCT ps;
        HDC hdc;
        RECT rect;
        char buffer[80];
        switch (msg)
        {
        case WM_CREATE:
        {
                          return 0;
        }break;
        case WM_PAINT:
        {
                         hdc = GetDC(hwnd);
                         ReleaseDC(hwnd, hdc);
                         GetClientRect(hwnd, &rect);
                         ValidateRect(hwnd, &rect);
    
                         return 0;
        }break;
        case WM_CLOSE:
        {
                         if (IDYES != MessageBox(hwnd, "确实要退出应用程序?", "退出", MB_YESNO | MB_ICONEXCLAMATION))
                         {
                             return 0;
                         }
                         else
                         {
                             PostQuitMessage(0);
                         }
        }break;
        case WM_SIZE:
        {
    
        }break;
        case WM_DESTROY:
        {
                           PostQuitMessage(0);
                           return 0;
        }break;
        default:break;
        }
        return DefWindowProc(hwnd, msg, wParam, lPram);
    }
    void GameMain()
    {
        return;
    }
    int WINAPI WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR lpCmdLine,
        int nCmdShow)
    {
        //定义窗口类
        WNDCLASSEX winClass;
        HWND hWnd;
        MSG msg;
    
        HPEN pen = NULL;
        int color_change_count = 100;
    
        //填充窗口类的各成员
        winClass.cbSize = sizeof(WNDCLASSEX);
        winClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
        winClass.lpfnWndProc = WindowProc;    //窗口消息处理函数
        winClass.cbClsExtra = 0;
        winClass.cbWndExtra = 0;
        winClass.hInstance = hInstance;
        winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        winClass.lpszMenuName = NULL;
        winClass.lpszClassName = WINDOW_CLASS_NAME;        //窗口类名
        winClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
        //保存实例句柄到全局变量
        hInstance_App = hInstance;
    
        //注册窗口类
        if (!RegisterClassEx(&winClass))
        {
            return 0;
        }
    
        //创建窗口类的一个成员
        if (!(hWnd = CreateWindowEx(NULL,
            WINDOW_CLASS_NAME,
            "时间锁定的屏幕保护程序",
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            200, 200,
            WINDOW_WIDTH, WINDOW_HEIGHT,
            NULL,
            NULL,
            hInstance,
            NULL)))
        {
            return 0;
        }
    
        //保存窗体句柄到全局变量中
        main_window_handle = hWnd;
    
        //得到设备上下文
        HDC hdc = GetDC(hWnd);
    
        //设置随机数生成器的种子
        srand(GetTickCount());
    
        //线段终点坐标
        int x1 = rand() % WINDOW_WIDTH;
        int y1 = rand() % WINDOW_HEIGHT;
        int x2 = rand() % WINDOW_WIDTH;
        int y2 = rand() % WINDOW_HEIGHT;
    
        //线段终点的速度
        int x1v = -4 + rand() % 8;
        int y1v = -4 + rand() % 8;
        int x2v = -4 + rand() % 8;
        int y2v = -4 + rand() % 8;
    
    
    
        //消息循环
        while (TRUE)
        {
            DWORD start_time = GetTickCount();
    
            if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
            {
                if (msg.message == WM_QUIT)
                {
                    break;
                }
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            //100次变换一次颜色
            if (++color_change_count >= 100)
            {
                color_change_count = 0;
                if (pen)
                {
                    DeleteObject(pen);
                }
                pen = CreatePen(PS_SOLID, 1, RGB(rand() % 256, rand() % 256, rand() % 256));
                SelectObject(hdc, pen);
            }
            //移动线段终点
            x1 += x1v;
            y1 += y1v;
    
            x2 += x2v;
            y2 += y2v;
    
            //碰撞检测,看是否碰到窗体的边界
            if (x1<0 || x1>WINDOW_WIDTH)
            {
                x1v = -x1v;
                x1 += x1v;
            }
            if (y1<0 || y1>WINDOW_HEIGHT)
            {
                y1v = -y1v;
                y1 += y1v;
            }
    
            if (x2<0 || x2>WINDOW_WIDTH)
            {
                x2v = -x2v;
                x2 += x2v;
            }
            if (y2<0 || y2>WINDOW_HEIGHT)
            {
                y2v = -y2v;
                y2 += y2v;
            }
    
            MoveToEx(hdc, x1, y1,NULL);
            LineTo(hdc, x2, y2);
    
            //锁定帧率为30fps,1/30秒,约等于33毫秒。
            while (GetTickCount() - start_time < 33);
            //如果用户按了ESC,发送WM_CLOSE消息。退出程序。
            if (KEYDOWN(VK_ESCAPE))
                SendMessage(hWnd, WM_CLOSE, 0, 0);
        }
        ReleaseDC(hWnd, hdc);
        return msg.wParam;
    }
  • 相关阅读:
    Winform编程总结2—常用的XML读写
    SQL点滴4—筛选数据列的类型,字段大小,是否可为空,是否是主键,约束等等信息
    SSIS常用的包—发送Email任务
    SSIS常用的包—FTP任务
    Centos 下同时安装 Mysql4 和 Mysql5,二进制方式
    iptables 学习笔记
    em是否到了退出的时候
    CentOs 5.2设置DVD为本地yum源
    WAMP环境配置
    Silverlight学习笔记:布局之stackpanel
  • 原文地址:https://www.cnblogs.com/chenjiahong/p/4204656.html
Copyright © 2020-2023  润新知