70 likes | 209 Views
GAM666 – Introduction To Game Programming. 2D Using DirectX 7. DirectDraw, the 2D component of DirectX, uses the term “surface” to mean an area of memory in which pixel data can be stored Each surface is an instance of a DirectDrawSurface7 COM object
E N D
GAM666 – Introduction To Game Programming 2D Using DirectX 7 • DirectDraw, the 2D component of DirectX, uses the term “surface” to mean an area of memory in which pixel data can be stored • Each surface is an instance of a DirectDrawSurface7 COM object • A surface has characteristics such as width and height (in pixels), bit depth (bits per pixel) and flags indicating such things as whether it occupies system memory or video card memory
GAM666 – Introduction To Game Programming 2D Using DirectX 7 • A 2D application always has at least one surface, the primary surface, which contains the data that appears on the screen • Typically, a second surface, called a backbuffer, is also used. This is identical to the primary buffer, except that it does not appear on the screen • The program writes to the offscreen backbuffer, and when the image is finished, the backbuffer is “flipped” with the primary buffer
GAM666 – Introduction To Game Programming 2D Using DirectX 7 • Each filling of the backbuffer and flipping with the primary buffer constitutes one frame of an animation • The game logic's job is to keep drawing slightly different frames, based on time and user input, fast enough that the user perceives smooth movement • Frame rates below around 30Hz (used for movies) are noticeably jerkier than higher frame rates • Frame rates above the monitor's refresh rate do not give any smoother visual appearance.
GAM666 – Introduction To Game Programming 2D Using DirectX 7 • CreateSurface() [DirectDraw7 member] is used to create a surface • A DDSURFACEDESC2 structure passed to CreateSurface() controls details about the creation of the surface • Since a surface is an instance of a COM interface, it must be Release()d after use • Besides the primary surface and back buffer, surfaces are used to store images that will be drawn onto the screen • If the program loses control of the screen, each surface must be Restore()d upon reactivation
GAM666 – Introduction To Game Programming 2D Using DirectX 7 • When the primary surface is created, a backbuffer can be requested • The backbuffer is then obtained by a subsequent call to the primary surface's GetAttachedSurface() function • When the primary surface is Release()d, the backbuffer is automatically released also • The primary surface's Flip() function switches the primary buffer with the back buffer (displaying the next frame)
GAM666 – Introduction To Game Programming 2D Using DirectX 7 • Surfaces can be drawn onto using regular Windows drawing techniques, by calling the member function GetDC(), which obtains a device context handle (HDC) – remember to ReleaseDC() this when done • Alternately, and much faster, one surface can be copied onto part or all of another surface by calling the Blt()member function [Block Transfer, blitting]
GAM666 – Introduction To Game Programming 2D Using DirectX 7 • A typical Windows program creates a main window (which has a window procedure), and then goes into a loop which handles messages as they arrive • A game modifies that loop by looking to see if any messages are pending, and if not then preparing and drawing the next frame • The timeGetTime() multimedia timer is often used to determine elapsed time to a high degree of accuracy