DX-related code

Source: Internet
Author: User
Tags bool sprintf time interval


1. Empty window

#include "stdafx.h" #include <stdio.h>//global variable declaration hinstance hInst;
Hbitmap Man[7];
HDC HDC,MDC;
HWND hwnd;                 DWORD Tpre,tnow,tcheck;                    Declare three functions to record the time, Tpre record the time of the last drawing, Tnow record the time to prepare the drawing, Tcheck record the time of the start of the second int num,frame,fps; Num is used to record the number of pictures, frame is used to accumulate the number of times each screen update, FPS (frame per second) to record the number of times per second//global function declaration ATOM MyRegisterClass (HINSTANCE hinstance
);
BOOL InitInstance (hinstance, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

void Mypaint (hdc hdc);
                     The WinMain function, the program entry point function ************************************** int apientry WinMain (hinstance hinstance,

	HInstance hPrevInstance, LPSTR lpcmdline, int ncmdshow) {MSG msg;

	MyRegisterClass (HINSTANCE); Run the Initialize function if (!
	InitInstance (HINSTANCE, nCmdShow)) {return FALSE;
    }//Game Loop GetMessage (&msg,null,null,null);
  while (Msg.message!=wm_quit) {if (PeekMessage (&msg, NULL, 0,0, Pm_remove))      {TranslateMessage (&msg);
        DispatchMessage (&AMP;MSG);
			} else {tnow = GetTickCount ();
		if (tnow-tpre >= 100)//Redraw operation Mypaint (HDC) when this cycle runs 0.1 seconds from the last drawing time;
}} return Msg.wparam;

	}//**** design a window class, similar to the fill-in question, using the window structure ************************* ATOM myregisterclass (hinstance hinstance) {wndclassex Wcex; 
	wcex.cbsize = sizeof (wndclassex); 

Wcex.style = Cs_hredraw |
	Cs_vredraw;
	Wcex.lpfnwndproc = (WNDPROC) WNDPROC;
	Wcex.cbclsextra = 0;
	Wcex.cbwndextra = 0;
	Wcex.hinstance = hinstance;
	Wcex.hicon = NULL;
	Wcex.hcursor = NULL;
	Wcex.hcursor = LoadCursor (NULL, Idc_arrow);
	Wcex.hbrbackground = (hbrush) (color_window+1);
	Wcex.lpszmenuname = NULL;
	Wcex.lpszclassname = "Canvas";

	WCEX.HICONSM = NULL;
return RegisterClassEx (&AMP;WCEX); }//**** initialization function *************************************//load bitmap from File BOOL InitInstance (hinstance hinstance, int ncmdshow) {Cha
	R Filename[20] = "";

	int i;

	HInst = hinstance;HWnd = CreateWindow ("Canvas", "animated presentation", Ws_overlappedwindow, Cw_usedefault, 0, cw_usedefault, 0, NULL, NULL, HINSTANC

	E, NULL);
	if (!hwnd) {return FALSE;
	} MoveWindow (Hwnd,10,10,600,450,true);
	ShowWindow (HWnd, ncmdshow);

	UpdateWindow (HWND);
	HDC = GetDC (HWND);

	MDC = CreateCompatibleDC (HDC);
		Load each character bitmap for (i=0;i<7;i++) {sprintf (filename, "man%d.bmp", I);
	Man[i] = (HBITMAP) loadimage (null,filename,image_bitmap,640,480,lr_loadfromfile);
	num = 0;

	frame = 0;

	Mypaint (HDC);
return TRUE; }//**** custom drawing function *********************************//1. Calculate and display update times per second//2. Window map void Mypaint (hdc hdc) {char str[4 in the order of the chart numbers

	0] = "";
	if (num = = 7) num = 0;			       frame++; Screen update times plus 1 if (tnow-tcheck >= 1000)//Determine whether the drawing time has reached a 1-second interval from the previous second.
	If it is, assign the current ' frame ' value to ' fps ', indicate the number of times it has been updated in a second, then return the "frame" value back to 0 and set the start time for the next count of the number of images per second "ICheck".
		{fps = frame;
		frame = 0;
	Tcheck = Tnow;         } SelectObject (Mdc,man[num]); Select the pattern to be updated into the MDC, and then output a string showing the number of updates per second to the MDC, and finally the MDCThe content is pasted into the window.
	sprintf (str, "Display%d images per second", FPS);
	TextOut (Mdc,0,0,str,strlen (str));

	BitBlt (hdc,0,0,600,450,mdc,0,0,srccopy);     Tpre = GetTickCount ();
	Record the time of the drawing for the next game cycle to determine whether you have reached the time interval set by the screen update operation.
num++; }//****** Message processing function ********************************* LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPAR

	AM lParam) {int i;
			Switch (message) {case Wm_destroy://Window End Message DeleteDC (MDC);
			for (i=0;i<7;i++) DeleteObject (Man[i]);
			ReleaseDC (HWND,HDC);
			PostQuitMessage (0);
		Break
   Default://Other message return DefWindowProc (hWnd, message, WParam, LParam);
} return 0;
 }


2, code Snippets structure good habit
Code Snippet one  dx11demobases class header File
 
[CPP] View plaincopyprint?
#ifndef _demo_base_h_  
#define _demo_base_h_  
#include <d3d11.h>  
#include <d3dx11.h>  
# Include<dxerr.h>  
class Dx11demobase  
{public  
:  
   dx11demobase ();  
   Virtual ~dx11demobase ();  
   BOOL Initialize (HINSTANCE HINSTANCE, HWND hwnd);  
   void Shutdown ();  
   virtual bool Loadcontent ();  
   virtual void unloadcontent ();  
   virtual void Update (float dt) = 0;  
   virtual void Render () = 0;  
   Protected:  
   hinstance Hinstance_;  
   HWND Hwnd_;  
   D3d_driver_type Drivertype_;  
   D3d_feature_level Featurelevel_;  
   id3d11device* D3ddevice_;  
   id3d11devicecontext* D3dcontext_;  
   idxgiswapchain* Swapchain_;  
   id3d11rendertargetview* backbuffertarget_;  
};  
#endif The  




 
above code we can see the most streamlined D3d object, which exists in the class as a protected class member. Initializing variables outside of the class is a good programming habit and is much more efficient than calling the copy constructor starters and then calling the default constructor.

3, two-dimensional D3d11_texture2d_desc declaration form as follows:

The two-dimensional D3d11_texture2d_desc declaration form is as follows:

[CPP]View Plain copy print?      typedef struct D3D11_TEXTURE2D_DESC {UINT Width;      UINT Height;      UINT Miplevels;      UINT ArraySize;      Dxgi_format FORMAT;      Dxgi_sample_desc Sampledesc;      D3d11_usage USAGE;      UINT Bindflags;      UINT Cpuaccessflags;      UINT Miscflags; } D3d11_texture2d_desc4, Direct3D initialization of four parts /*****************************************************************************************  //  desc:  library file Definition section     //******************************************************************     #pragma  comment (lib, "D3d9.lib")    #pragma  comment ( LIB, "D3dx9.lib")      //********************************************************************   // name: direct3d_init ( )   // desc:  Initialize direct3d  // point: "Direct3D initialization Four"   //      1. Initializes one of the four parts, creating the Direct3D interface Object   //      2. Initialize four section Two, get Hardware device information   //       3. Initializes the four-part three, filling the structure   //      4. Initialize four Direct3D, create a device interface   //************************************************************************* ****************      hresult direct3d_init (hwnd hwnd)    {           //--------------------------------------------------------------------------------------        //  "Direct3D initialize one of four steps, create interface": Creating Direct3D Interface Objects,  To create Direct3D device Objects        //--------------------------------------------with the Direct3D object ------------------------------------------       lpdirect3d9  pd3d  = null; //direct3d Creation of Interface objects

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.