Use DirectX to move child graphics (Sprite animation)
This is what I learned from this book today, the book is called "Beginning DirectX9" This book is in English. Fortunately, I still can read English. The book gives a lot of DirectX programming beginners How to do DirectX programming. Today I saw a sprite (sub-graphic, also called Sprite Graphic) Chapter, which describes how to use DirectX to draw sprite graphics. The contents of the book are still relatively understandable. But there is a flaw, that is, there is no man who has the CD-ROM of this book. So our domestic readers have to read the PDF file. I was studying this book with some foundation, or 4 months ago, I didn't know how to correct the mistakes in the book. As I said above, there are still many flaws in the book. This will have to be corrected by our readers. On the basis of today's one-hour effort and my basic framework, I finally realized the effect of the book to be shown. Here is my code, the comments I translated and added, very understood. If you still do not understand, you can ask me alone or thread, I will be patient to answer the questions of everyone.
First of all is my MainFrame.cpp file.
Code:/*--------------------------------------------------------------------------- Shimin production e-mail:jiangcaiyang123@163.com file name: MainFrame.cpp Role: Loading sub-image (Sprite) ----------------------------- -----------------------------------------------*/ /*----------------------------------------------- ---------------------------*/ // header files #include <windows.h> #include <d3d9.h> #include <d3dx9.h> #include <stdio.h> // library Files #pragma comment ( lib, "d3d9.lib") #pragma Comment ( lib, "d3dx9.lib") // defined macros #define jclassname "optimized program" #define JCAPTION "program demo" #define window_width 640 #define &NBSP;WINDOW_HEIGHT&NBSP;480 #define fullscreen false// Full Screen Switch #define &NBSP;SAVE_RELEASE ( p ) if ( p ) p->release (); p = null; // adjusting compiler settings #pragma warning ( disable: 4100 ) #if FULLSCREEN // settings for full screen or not #define window_ Style ws_ex_topmost | ws_popup | ws_visible #else #define Window_style ws_caption | ws_sysmenu #endif /*--------------- -------------------------------------------------------------*/ // Global variables LPDIRECT3D9&NBSP;G_JD3D&NBSP;=&NBSP;NULL;//&NBSP;D3D structural body Lpdirect3ddevice9 g_jdevice &NBSP;=&NBSP;NULL;//&NBSP;D3D device structure lpd3dxfont g_fpsfont = null;// pointer to fps font Rect g_fpsfontpos = { window_width - 100, window_height - 15, Window_width, window_height };// fps Rectangle int g_framecount = 0;// Frame counter int g_ lasttime = 0;// record the time of the previous second int g_currenttime = 0;// record the current time char g_fpsstr[25] = { 0 };// string that records the current frame rate /*--------- -------------------------------------------------------------------*/ // Initialize the sub-graphic header file #include "Sprite.h" /*------------------------------------------------------------ ----------------*/ // Function declaration bool initialized3d ( HWND hWnd ) { D3DDISPLAYMODE displayMode; //creating D3D objects g_jd3d = direct3dcreate9 ( D3D_SDK_VERSION ); if ( g_jd3d == null ) return FALSE; // Get display mode if ( failed ( g_JD3D-> Getadapterdisplaymode ( D3DADAPTER_DEFAULT, &displayMode ) ) ) return FALSE; // d3d Display parameters D3DPRESENT_PARAMETERS jd3dpp; zeromemory ( &jd3dpp, sizeof ( jd3dpp ) ); // Initialize d3dpresent_parameters jd3dpp. windowed = ! fullscreen; &NBSP;JD3DPP. backbufferwidth = window_width; &NBSP;JD3DPP. backbufferheight = window_height; &NBSP;JD3DPP. Swapeffect = d3dswapeffect_discard; &NBSP;JD3DPP. backbufferformat = displaymode.format; &NBSP;JD3DPP. enableautodepthstencil = true; &NBSP;JD3DPP. autodepthstencilformat = d3dfmt_d16; // Create device if (FAILED ( g_jd3d->createdevice ( d3dadapter_default, d3ddevtype_hal, hwnd, d3dcreate_software_ vertexprocessing, &jd3dpp, &g_JDevice ) ) return FALSE; // Set rendering status g_jdevice->setrenderstate ( d3drs_ lighting, false ); g_jdevice->setrenderstate ( D3DRS_ZENABLE, D3DZB_TRUE ); // Create fonts if ( failed ( d3dxcreatefont ( g _jdevice, 15, 0, 1, 1, 0, default_charset, out_default_precis, default_quality, default_pitch | ff_dontcare, "Impact", & g_fpsfont ) ) ) return FALSE; if ( initsprites () == FALSE ) return false; return TRUE; } /*----------------------------------------------------------------------------*/ // Release All Resources void releasememory ( void ) { Save_release ( g_JD3D ); save_release ( g_JDevice ); save_release ( g_FPSFont ); } /*----------------------------------------------------------------------------*/ // Rendering Screen void renderscene ( void ) { // counter start count (in milliseconds) g_currentTime = GetTickCount (); if ( g_currentTime - g_lastTime > 1000 ) { sprintf_s ( g_FPSstr, 25, "Current fps:%d", g_framecount ); g_lastTime = g_currentTime; g_FrameCount = 0; } else g_FrameCount++; g_jdevice->clear ( 0, NULL, D3DCLEAR_TARGET | d3dclear_zbuffer, d3dcolor_xrgb ( 179, 221, 247), 1.0f, 0 );// Clear Screen // render sub-graphics rendersprite (); // Start Rendering g_jdevice->beginscene (); g_fpsfont->drawtext (&nbsP Null, g_fpsstr, -1, &g_fpsfontpos, dt_center, d3dcolor_xrgb ( 255, 255, 255 ) );// display font // End Rendering g_jdevice->endscene (); g_jdevice->present ( NULL, NULL, NULL, NULL ); } /*----------------------------------------------------------------------------*/ // callback function for handling messages Hresult callback myappproc ( HWND hWnd, uint msg, wparam wparam, lparam lparam ) { switch ( msg ) { case wm_destroy: PostQuitMessage ( 0 ); return 0; case wm_keydown: if ( wparam == vk_escape ) pOstquitmessage ( 0 ); return 0; } return (HRESULT) DefWindowProc ( hwnd, msg, wparam, lParam ); }; /*----------------------------------------------------------------------------*/ // Entrance to the program, main functions Int winapi winmain ( HINSTANCE hInst, HINSTANCE hprevinst, lpstr cmd, int show ) { // settings window Class structure and register it wndclassex jwndcls = { sizeof ( jWndCls ), cs_ classdc, myappproc, 0l, 0l, hinst, null, loadcursor ( null, idc_arrow) , 0, NULL, JCLASSNAME, NULL }; registerclassex ( &jWndCls ); // Setup window and display window hwnd hwnd = createwindow ( jclassname, jcaption, window_style, 100, 40, window_width, window_height, getdesktopwindow (), null, jwndcls.hinstance, null); if ( hWnd == NULL ) return FALSE; showwindow ( hWnd, SW_SHOWDEFAULT ); updatewindow ( hWnd ); // Initialization Settings if ( initialized3d ( hWnd ) == FALSE ) return FALSE; // go to message loop MSG msg; zeromemory ( &msg, sizeof ( msg ) ); while ( msg.message != WM_QUIT ) { if ( peekmessage ( &msg, null, 0u, 0u, pm_remove ) ) { translatemessage ( &msg ); dispatchmessage ( &msg ); } else { // Rendering Screen renderscene (); } } // program end, free memory all resources releasememory (); // Unblock window registration unregisterclass ( JCLASSNAME, jwndcls.hinstance ); return ( INT ) msg.wParam; }
Then there is my Sprite.h file:
Code:/*--------------------------------------------------------------------------- Shimin production e-mail:jiangcaiyang123@163.com file name: Sprite.h Role: Loading sub-image (Sprite) ---------------------------------- ------------------------------------------*/ // Precompiled #ifndef _sprite_h_ #define &NBSP;_SPRITE_H_ /*------------------------------------------------------------------- ---------*/ // header file #include <time.h> /*---------------------------- ------------------------------------------------*/ // define macros #define for sub-shapes sprite_width 48 #define &NBSP;SPRITE_HEIGHT&NBSP;48 #define SCRN_WIDTH window_width #define scrn_height window_height /*----------------- -----------------------------------------------------------*/ // To customize the structure of a sub-graph &NBSP;&NBsp Struct stsprite { stsprite (): MoveX ( 1 ), Movey ( 1 ) {}// Default constructors RECT srcRect; // locations int posX; // x coordinates int posY; // y coordinates int moveX; Specifies how many x pixels to go at a time int moveY; // specifies how many y pixels to walk at once } spritestruct[10]; /*----------------------------------------------------------------------------*/ // Global variables pointers to idirect3dsurface9* surface;// planes /*----------------- -----------------------------------------------------------*/ // Initialization sub-graphic bool Initsprites ( void ) { // genThe off-screen plane is used to display graphics if ( failed ( g_jdevice->createoffscreenplainsurface ( 480, 48, D3DFMT_X8R8G8B8, d3dpool_default, &surface, NULL ) ) ) return false; // Load sub-graphics if ( failed ( D3DXLoadSurfaceFromFile ( surface, NULL, NULL, "Sprite.tga", null, d3dx_default, 0, null ) ) ) return false; int i; srand ( ( unsigned int ) time ( NULL ) );// generate random numbers // Initialize 10 sub-graphs for ( i= 0; i< 10; i++ ) { spritestruct[i].srcrect.top = 0; spriteStruct[i].srcRect.left = i * SPRITE_WIDTH; spriteStruct[i].srcRect.right = spriteStruct[i].srcRect.left + Sprite_width; spriteStruct[i].srcRect.bottom = SPRITE_HEIGHT; &NBSP;&NBSP;SPRITESTRUCT[I].POSX = ABS (rand ()%scrn_width-sprite_width);
Spritestruct[i].posy = ABS (rand ()%scrn_height-sprite_height); } return true; }/*----------------------------------------------------------------------------*//render sub-shape void Rendersprite (Voi d) {idirect3dsurface9* backbuffer = NULL; int i; <