Windows programming: Draw lines, simple collision detection, simple frame rate locking

Source: Internet
Author: User

#defineWin32_lean_and_mean#include<windows.h>#include<windowsx.h>#include<mmsystem.h>#include<stdio.h>#include<stdlib.h>#include<math.h>#defineWindow_class_name "WINCLASS1"#defineWindow_width 640#defineWindow_height 480#defineKEYDOWN (Vk_code) ((Getasynckeystate (vk_code) & 0x8000)? 1:0)#defineKEYUP (Vk_code) ((Getasynckeystate (vk_code) & 0x8000)? 0:1)//Global VariablesHWND Main_window_handle =NULL; HINSTANCE Hinstance_app=NULL;Charbuffer[ the];//window processing FunctionsLRESULT CALLBACK WindowProc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM lpram) {paintstruct ps;    HDC hdc;    Rect rect; Charbuffer[ the]; Switch(msg) { Casewm_create: {return 0; } Break;  Casewm_paint: {hdc=GetDC (HWND);                     ReleaseDC (hwnd, HDC); GetClientRect (hwnd,&rect); ValidateRect (hwnd,&rect); return 0; } Break;  Casewm_close: {if(Idyes! = MessageBox (hwnd,"Are you sure you want to exit the application? ","Exit", Mb_yesno |mb_iconexclamation)) {                         return 0; }                     Else{postquitmessage (0); }    } Break;  Casewm_size: {} Break;  CaseWm_destroy: {postquitmessage (0); return 0; } Break; default: Break; }    returnDefWindowProc (hwnd, MSG, WParam, Lpram);}voidGamemain () {return;}intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCmdLine,intncmdshow) {    //Defining window ClassesWndclassex Winclass;    HWND hwnd;    MSG msg; Hpen Pen=NULL; intColor_change_count = -; //populate the members of the window classWinclass.cbsize =sizeof(Wndclassex); Winclass.style= Cs_dblclks | CS_OWNDC | Cs_hredraw |Cs_vredraw; Winclass.lpfnwndproc= WindowProc;//window Message handler functionWinclass.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;//window class nameWINCLASS.HICONSM =LoadIcon (NULL, idi_application); //saving an instance handle to a global variableHinstance_app =hinstance; //Register window class    if(! RegisterClassEx (&Winclass)) {        return 0; }    //create a member of a window class    if(! (HWnd =CreateWindowEx (NULL, Window_class_name,"time-locked screen Saver", Ws_overlappedwindow|ws_visible, $, $, window_width, window_height, NULL, NULL, HINSTANCE, NULL))) {        return 0; }    //Save the form handle to a global variableMain_window_handle =hWnd; //Get device contextHDC HDC =GetDC (HWND); //set the seed of the random number generatorSrand (GetTickCount ()); //segment End coordinates    intx1 = rand ()%window_width; inty1 = rand ()%window_height; intx2 = rand ()%window_width; inty2 = rand ()%window_height; //the speed of the end of the segment    intX1V =-4+ rand ()%8; intY1V =-4+ rand ()%8; intX2V =-4+ rand ()%8; intY2V =-4+ rand ()%8; //message Loops     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 times The color change        if(++color_change_count >= -) {Color_change_count=0; if(pen) {DeleteObject (pen); } Pen= CreatePen (Ps_solid,1, RGB (rand ()% the, rand ()% the, rand ()% the));        SelectObject (hdc, pen); }        //Move end of segmentX1 + =x1v; Y1+=y1v; X2+=x2v; Y2+=y2v; //collision detection to see if the boundary of the form is encountered        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); //The lock frame rate is 30fps,1/30 seconds, approximately equal to 33 milliseconds.          while(GetTickCount ()-Start_time < -); //If the user presses ESC, the WM_CLOSE message is sent. Exits the program.         if(KEYDOWN (Vk_escape)) SendMessage (HWnd, Wm_close,0,0);    } ReleaseDC (HWnd, HDC); returnMsg.wparam;}

Windows programming: Draw lines, simple collision detection, simple frame rate locking

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.