24 hours study DX fourth-hour - Timer, Title

xiaoxiao2021-03-06  56

/ * ================================================================================================================================================================ ================= 学 3 hours =============================================================================================================================================================================================== ======================================================== * /

#include "stdafx.h" // # include "resource.h" #define initguid # include

#include

#define min_pos 0.0 # Define max_pos 50000.0 # define safitelease (x) f (x) {x-> release (); x = null;}

// Function declares BOOL INITWINDOW (Hinstance Hinstance, Int ncmdshow); LRESULT CALLBACK WINPROC (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM); BOOL LOAD_IMAGES (); void render_frame ();

// Variable Description HWND HWND; // Window Handle

/ * ================================================================================================================================================================ ============================= LPDIRECTDRAW7 and LPDIRECTDRAWSURFACE7 type (7 is the version number) is predefined in ddraw.h header files Point to the long pointer of IDirectDraw7 and iDirectdrawSurface7 (previously added LP represents long point), from the later "->" instead of "." It can also be seen.

DD is the abbreviation of DirectDraw, and DDS is the abbreviation of DirectDrawSurface, so habits we are using the variables from LPDD and LPDDSxxx.

Although VC.NET comes with DirectX SDK is version 8.1, because Microsoft stops from DirectX 8.0 to update DirectDraw, DirectDRAW currently has 7.0. ============================================================================================================================================================================================================= ======================================= * / lpdirectdraw7 lpdd = null; // DirectDRAW object pointer lpdirectdrawsurface7 lpddsprimary = null; // DirectDraw Main page pointer LPDIRECTDRAWSURFACE7 LPDDSBuffer = NULL; // DirectDraw Background Cache Pointer LPDIRECTDRAWSURFACE7 Back_Surf [3] = {Null, NULL, NULL}; Rect RECT

Double X_POS = 25000; // Player Location Double Move_Rate = 0.0; // Player Sports

Void MakeRect (int in in =}; reference.top = =} {? reference.top;}

LPDIRECTDRAWSURFACE7 bitmap_surface (LPCTSTR file_name) {HDC hdc; HBITMAP bit; LPDIRECTDRAWSURFACE7 surf; // load BMP bit = (HBITMAP) LoadImage (NULL, file_name, IMAGE_BITMAP, 0,0, ?? LR_DEFAULTSIZE | LR_LOADFROMFILE)?????; • If (! bit) // load failed, return null ?? return null;? // Get BMP size? Bitmap bitmap;? getObject (bit, sizeof (bitmap), & bitmap);? // Save BMP size? int Surf_width = bitmap.bmwidth;? int surf_height = bitmap.bmheight;

? // Create a page? HRESULT DDRVAL;? DDSURFACEDESC2 DDSD;? // Empty DDSurfaceDesc2 structure? ZeromeMory (& DDSD, SIZEOF (DDSD)); DDSD.dwsize = sizeof (ddsurfaceDesc2);? // Set the padding mark? DDSD.DWFLAGS = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;?????? // offscreen page ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; // specify the width and height ddsd.dwWidth = 1024; ddsd.dwHeight = 768; // create offscreen Page? DDRVAL = LPDD-> CreateSurface (& DDSD, & Surf, null) ;? if (ddrval! = Dd_ok) {?? // failed, release BMP ?? deleteObject (bit); ?? Return NULL;?}? Else // Create a success? {?? // get DC ?? Surf-> getDC (& HDC); ?? // Get consistent with DC ?? HDC bit_dc = createcompatibleDC (HDC); ?? // ?? SELECTOBJECT (Bit_DC, Bit); ?? // Transfer BMP ?? Bitblt (HDC, 0, 0, Surf_Width, Surf_Height, Bit_DC, 0, 0, Srccopy); ?? // Release DC ?? Surf-> ReleaseDC (HDC); ?? DELETEDC (Bit_DC);?}? // Release BMP? DeleteObject (bit);? // Return the off-screen page pointer? Return Surf;} BOOL LOAD_IMAGES () {? // Load Background? Back_Surf [0] = bitmap_surface ("E: / CJD / Material / PIC / World of Warcraft / 1.BMP" );?if (! Back_surf [0]) ?? Return False;? Back_surf [1] = Bitmap_surface ("E: / CJD / Material / PIC / World of Warcraft / 2.BMP"??? ((! "? R f false;? Back_surf [2] = bitmap_surface (" E: / CJD / Material / PIC / World of Warcraft /3.bmp ");? IF (! back_surf [2]) ?? RETURN FALSE;? RETURN TRUE;

/ / Draw 1 桢 function void render_frame () {? Rect RCT; / / Transfer image area? Long tile_pos; // Tile location? // Take Title? For (int i = 0; i <3; i ) {?? // calculate Title position ?? Tile_pos = 1024 * i (x_pos) / 3072) * 3072; ?? IF (Tile_POS 3072

??? Tile_pos = 3072;

??//on the screen?

?? IF (tile_pos 1024> = x_pos) {

??? // Yes, calculate the relative position on the left

??? INT screen_x = Tile_POS-X_POS;

??? // If you need it, please cut

??? rct.left = 0;

??? rct.top = 0;

??? rct.right = 1024;

??? RCT.BOTTOM = 768;

??? // Test cut

??? IF (Screen_x <0) {

???? rct.left- = screen_x;

???? screen_x = 0;

???} else if (screen_x rct.right> 1024) {

???? rct.right = 1024-screen_x;

???}

??? // transfer bitmap

??? LPDDSBuffer-> Bltfast (Screen_x, 0, Back_Surf [i], & rct, ddbltfast_wait);

??}

?

? // change the page

• LPDDSPRIMARY-> FLIP (0, DDFLIP_WAIT);

}

// ******************************************************** *********** // Function: initddraw () // Function: DX initialization function // ********************** ************************************************************************************** BOOL INITDDRAW () {? DDSurfaceDesc2 DDSD; // DirectDraw Page description? / * ??? Before all work, remember to join the two lib files ??? In the Object / Library Modules column of the LINK page under menu Project-Setting ??? "DDRAW.LIB" and " DXGUID.LIB, the latter can not join, and use #define initguid instead? If you want to use DirectDraw, you must create a DirectDraw object, which is the core of the DirectDraw interface. ??? Use the DirectDrawCreateex () function to create a DirectDraw object, which is defined in DDRAW.H, which is as follows:

?????? HRESULT WINAPI DirectDrawCreateex (?????? Guid Far * lpguid, // Pointer to the GUID of the DirectDRAW interface, NULL indicates the default (current) ?????? lpvoid * LPLPDD, ?? // Used to accept the address of the initial DirectDraw object? ?????? REFIID IID, ?????? // IID_IDIRECTDRAW7, current version ?????? IUNKNOWN FAR * PUNKOUTER? // Null ? Retain ???); ?????? All DirectDRAW functions are the HRESULT type, which is a 32-bit value. ??? function call successfully expressed "DD_OK", all the error value flags are "DDERR", such as: ??? DDERR_DIRECTDRAWALREADYCREATED ??? DDERR_OUTOFMEMORY? * /? // Use IF here (XXX! = DD_OK) Method for error detection, this is the most common method ???? IF (DirectDrawCreateex (NULL, (LPVOID *) & LPDD, IID_IDIRECTDRAW7, NULL)! = DD_OK) ?? Return false; // Create a DirectDRAW object? After the DirectDrawCreate function calls successfully, the LPDD has point to a DirectDraw object. It is the highest level leader of the entire DirectDRAW interface, and the following steps are under its control. • We use idirectdraw7 :: setCooperativeEvel () to set the DirectDraw program to the system's control level. The prototype is as follows:? HRESULT SETCOOPERATIVELEVEL (HWND HWND, DWORD DWFLAGS)? The first parameter is a window handle, we give it hwnd, allow the DirectDRAW object to contact the main window. ???? The second parameter is the control level flag. Here is DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN, indicating that we expect DirectDraw to work in exclusive and full-screen. ????? The control level describes how DirectDRAW is working with the display device and system. • DirectDraw control level is generally used to determine that the application is running in full screen mode (must be used simultaneously with exclusive mode), or runs in window mode. However, the control level of DirectDraw can also set the following two: ???? (1) allows Press Ctrl Alt DEL to restart (only for exclusive mode, DDSCL_AllowReboot). ??? (2) Does not allow minimization or restore of the DirectDraw application (DDSCL_NOWIDOWCHANGES). ????? Ordinary control level (DDSCL_NORMAL) indicates that our DirectDraw application will run in the form of a window. Under this control level, we will not change the display resolution, or make a change operation (this is an important operation). Other than this, we cannot call functions that will have a fierce reaction to the memory, such as LOCK (). ???? When the application is a full screen and the exclusive control level, we can fully utilize hardware resources. • At this point, other applications can still create a page, using DirectDraw or GDI functions, just unable to change the display mode.

? * / ???? if (! LpDD-> SetCooperativeLevel (hWnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | ?? DDSCL_ALLOWREBOOT) = DD_OK) ??????? return FALSE; // set the control level DirectDraw / * Next?? We use iDirectdraw7 :: setDisplayMode () to set the display mode, its original shape is:? HRESULT SETDISPLAYMODE (? DWORD DWPIDTH, DWORD DWHEIGHT, / / ​​DWWIDTH AND DWHEIGHT to set the width and height of the display mode.? DWORD DWBPP, // DWBPP is used to set the number of color bits of the display mode. • DWORD dwrefreshrate, // dwrefreshrate Set the refresh rate of the screen, 0 is the use of the default value.? DWORD dwflags // dwflags Now the only valid value is DDSDM_Standardvgamode, ?????? ?????????? // This value is used for VGA Mode 13 in 320 × 240 × 8 mode, ?????? // We usually set it to 0??? ??); ps: EnumdisplayModes () function with the IDirectDraw7 interface can get the currently available mode list. Please check the help of DX? * / ???? if (LPDD-> SetDisplayMode (1024, 768, 32, 85, 0)! = DD_OK) ??????? Return False; // Setting the display mode ??? / * ?? Before creating a page, first you need to fill a DDSurfaceDesc2 structure, which is the abbreviation of DirectDraw Surface Description, meaning the page description of DirectDraw. ?? It is very large, it can only make a simplest introduction. What should you pay attention to, you must empty it before filling this structure! ? * /

? // Start creating the home page, first empty the page description? ZeromeMory (& DDSD, SIZEOF (DDSD));

? // Plip page description ??? DDSD.DWSIZE = SIZEOF (DDSD); // Give the size of the DWSIZE page? DDSD.DWFLAGS = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; // Have a background cache ??? DDSD.DDSCAPS.DWCAPS = DDSCAPS_PRIMARFACE | DDSCAPS_FLIP | ?? DDSCAPS_COMPLEX; // Main page, there is a background cache, change the page chain? DDSD.DWBACKBUFFERCOUNT = 1; // The number of times the back built cache is 1 ??? / *? Createsurface () The first parameter is the first parameter of the function is The address of the DDSurfaceDesc2 structure of the page information is filled. This is & DDSD ;? The second parameter is the address of the receiving the home page pointer, here is & lpddsprimary; the third parameter must now be null, preserved for the function . ??? If the function call is successful, LPDDSPrimary will become a legitimate homepage object. • Since the working mode of the program has been set in front, it is the entire display screen, which is actually our entire display screen. • The graphic drawn on the main page will immediately reflect on our display screen. ? * / ???? IF (LPDD-> CreateSurface (& DDSD, & LPDDSPRIMARY, NULL)! = DD_OK) ?????? Return False; // Create the main page

??? DDSD.DDSCAPS.DWCAPS = DDSCAPS_BACKBuffer; // Background Cache page ??? / *? When you create the main page, the additional page (here is the background cache page) also created at the same time, but they are not called by you CreateSurface () returned? You have to get this interface through the getattachedSurface () function,? * / ??? if (LPDDSPRIMARY-> GetAttachedSurface (& DDSD.DDSCAPS, & LPDDSBUFFER)! = DD_OK) ?? Return False; // Created Backstage Cache page? // Load picture? If (! Loading_images ()) ?? Return false;} void cleanup (void) {? // release interface? SafeRelease (back_surf [0]) ;? SafeRelease Back_surf [1]) ;? SafeRelease (back_surf [2]); ?? SafeRelease (LPDDSBuffer) ;? SafeRelease (LPDDSPRIMARY); / / Delete Main page? SafeRelease (LPDD); // Delete DDRAW object? // ps: The order of creation is just the opposite}

// ******************************************************** ************ // Function: WinMain () // Function: Windows program portfolio function. Create a main window, process message loop // ************************************************* ****************** INT PASCAL WINMAIN (Hinstance Hinstance, Hinstance HpRevinstance, LPSTR LPCMDLINE, INT NCMDSHOW) {? msg msg; // message? longlong cur_time; // Current Time? DWORD TIME_COUNT; / / Each mill diacephic number? Longlong perf_cnt; // Timer execution frequency? BOOL Perf_flag = false; // Decide to use the sign of the timer? Longlong next_time = 0; // Rendering the next Time? Longlong last_time = 0; // Time time? Double Time_ELAPSED; // After starting the time after the time? Double Time_scale; // Time factor conversion coefficient

? if (! @Stance, ncmdshow) Return false; // Create a main window? // Returns false and exit the program at the same time if creating unsuccessful?? // Performance counter Can not be available? ? If (QueryPerformanceFrequency ((LARGE_INTEGER *) & perf_cnt)) {?? // available, disposed ?? perf_flag = TRUE flag and parameter counters;? ?? time_count = perf_cnt / 30; ?? QueryPerformanceCounter ((LARGE_INTEGER *) & next_time); ?? Time_scale = 1.0 / perf_cnt ;?} else {?? // no performance counter ?? // next_time = TimeGetTime (); ?? //time_scale=0.001; ?? // time_count = 33; ?}? // Save the last time? Last_time = next_time ;? // Enter the message loop:? For (;;)? {? I (PEEKMESSAGE (& MSG, NULL, 0, 0, PM_REMOVE))? {? ? if (msg.Message == wm_quit) Break; ?? TranslateMessage (& msg); ?? DispatchMessage (& msg) ;?} else {?? // According to the timer flag, different functions is used to calculate the last 桢How long is it ?? if (perf_flag) ??? QueryperFormanceCounter (Large_integer *) & cur_time); ?? // else ??? // cur_time = timegettime (); ?? // Can't draw a picture time? ?? i (cur_time> next_time) {??? Time_ELAPSED = (CUR_TIME-LAST_TIME) * TIME_SCALE; ?????? // Save the last time time ??? Last_time = cur_time; ??? // Mobile Screen Location ??? x_pos = Move_rate * time_elapsed; ??? ing (x_pos

???? x_pos = min_pos;

???? Move_rate = 0;

???}

??? f (x_pos> max_pos) {

???? x_pos = max_pos;

???? Move_rate = 0;

???}

??? // painting

??? render_frame ();

??? // Set the time of the next

??? next_time = cur_time time_count;

??}

?

?

?? Return msg.wparam;

}

// ******************************************************** *********** // Function: initwindow () // Function: Create window // ********************** **********************************************

Static Bool INITWINDOW (Hinstance Hinstance, INT NCMDSHOW) {? // Define Window Style:? WNDCLASS WC;? wc.style = NULL; / / Form Class Style? wc.lpfnWndProc = (WinProc) WinProc; // Window Message Processing function pointer? Wc.cbclsextra = 0; // Assign an additional byte number after the window structure? Wc.cbWndextra = 0; // All-byte number after assigning the window instance? Wc.hinstance = Hinstance; // The handle of the application corresponding to the window? Wc.hicon = null; // window icon? Wc.hcursor = loadcursor (null, idc_arrow); // Window mouse? Wc.hbrbackground = Createsolidbrush (RGB (0, 0, 0)); // Black background? Wc.lpszMenuname = null; // makeintResource (iDR_Menu); // window menu resource name? Wc.lpszclassname = "my_test"; // window class name? RegisterClass & wc); // Register window ??? // Create a window in accordance with the parameters ???? hwnd = createWindow ("my_test", // Create the name of the window class used by the window? "24 hours learning DX_ 4th Hours ", // window title ?? WS_POPUP | WS_MAXIMIZE, / / ​​window style, defined as the X, Y coordinate of the normal type 0, 0, // window position, and the width of the width, Height ?? null, // parent window handle ?? null, // menu handle ?? hinstance, // application handle ?? NULL); IF (! Hwnd) Return False;? Showwindow (hwnd, ncmdshow); / / Display window? UpdateWindow (hwnd); // Refresh window? INITDDRAW (); // Initialization DX? Return True;}

// ******************************************************** *********** // Function: WinProc () // Function: Process Window Message // ********************** ***********************************************

转载请注明原文地址:https://www.9cbs.com/read-116992.html

New Post(0)