Download presentation
Presentation is loading. Please wait.
Published byDouglas Summers Modified over 9 years ago
1
Tech Talk: DirectDraw Alex Riemann University of Illinois, Urbana-Champaign February 13, 2000
2
Graphics Programming in DOS Very Limited Video Hardware Very Limited Video Hardware Required the developer to know what features the video hardware provided Required the developer to know what features the video hardware provided Video Electronics Standards Association (VESA) Video Electronics Standards Association (VESA) Helped relieve this situation Helped relieve this situation Provided semi-common interface to video memory Provided semi-common interface to video memory
3
Graphics Programming in Windows Common set of API’s to access graphics subsystem Common set of API’s to access graphics subsystem Numerous layers of indirection between the programmer and the hardware Numerous layers of indirection between the programmer and the hardware Graphics Device Interface (GDI) objects Graphics Device Interface (GDI) objects Great for Circles, Squares, and other simple objects Great for Circles, Squares, and other simple objects Far too slow to handle effects for video games! Far too slow to handle effects for video games!
4
Enter the Game SDK Introduced to promote game development on the Windows Platform Introduced to promote game development on the Windows Platform Designed to shift responsibility away from the developer and to the hardware manufacturer Designed to shift responsibility away from the developer and to the hardware manufacturer Soon became known as DirectX Soon became known as DirectX
5
What does DirectX provide? Fast, low latency access to hardware Fast, low latency access to hardware Hardware emulation for certain tasks (if required) Hardware emulation for certain tasks (if required) A single set of API calls regardless of system hardware A single set of API calls regardless of system hardware Multiplayer support that is indifferent to transport media (TCP/IP, IPX, Null Modem, etc.) Multiplayer support that is indifferent to transport media (TCP/IP, IPX, Null Modem, etc.) And so much more! And so much more!
6
The Hardware Abstraction Layer (HAL) Standard interface to any hardware device supported by Windows and DirectX Standard interface to any hardware device supported by Windows and DirectX The HAL is a very thin software layer provided by the vendor to interface with the hardware device The HAL is a very thin software layer provided by the vendor to interface with the hardware device
7
The Hardware Emulation Layer (HEL) Used to provide those functions in software that are not available in hardware Used to provide those functions in software that are not available in hardware A much slower solution, however it allows all systems to provide at least a minimal amount of DirectX functionality A much slower solution, however it allows all systems to provide at least a minimal amount of DirectX functionality
8
Components of DirectX DirectDraw DirectDraw Direct3D Direct3D DirectSound DirectSound DirectPlay DirectPlay DirectInput DirectInput DirectSetup DirectSetup DirectMusic DirectMusic DirectShow DirectShow
9
DirectDraw Basics Provides an interface to the 2D hardware available on modern video cards Provides an interface to the 2D hardware available on modern video cards Important Concepts Important Concepts Graphics Modes Graphics Modes The DirectDraw Object The DirectDraw Object Primary DirectDraw Surface Primary DirectDraw Surface
10
Data Structure Initialization Critical in DirectX Critical in DirectX Example Example Typically, DirectX structures require that the dwSize parameter be initialized Typically, DirectX structures require that the dwSize parameter be initialized DDSURFACEDESC ddsdesc; ZeroMemory(&ddsdesc, sizeof(ddsdesc)); ddsdesc.dwSize = sizeof(ddsdesc);
11
Page Flipping Requires addition of a Back Buffer Surface Requires addition of a Back Buffer Surface Prevents “tearing” from occurring Prevents “tearing” from occurring No Page Flipping Demo Page Flipping Demo
12
Page Flipping – Illustration Surface 1 Surface 2 Before Page Flip Display Address Front Buffer Back Buffer Monitor Surface 1 Surface 2 After Page Flip Display Address Front Buffer Back Buffer Monitor
13
The Surface Typically “imagined” as a 2D surface, however is actually a linear buffer Typically “imagined” as a 2D surface, however is actually a linear buffer Described by the DDSurfaceDesc structure Described by the DDSurfaceDesc structure A DDSURFACEDESC structure is used to either define a surface when CreateSurface is called or to describe the current condition of a buffer using GetSurfaceDesc A DDSURFACEDESC structure is used to either define a surface when CreateSurface is called or to describe the current condition of a buffer using GetSurfaceDesc
14
MSDN – Your Friend Exercise – Look up DDSurfaceDesc and read the description of its member variables Exercise – Look up DDSurfaceDesc and read the description of its member variables Launch MSDN Launch MSDN Launch MSDN Launch MSDN Important Member Variables Important Member Variables Pitch Pitch lpSurface lpSurface ddckCKDestBlt, ddckCKSrcBlt ddckCKDestBlt, ddckCKSrcBlt
15
Blitting to a Surface A blit is the process of copying an image from one surface onto another A blit is the process of copying an image from one surface onto another Two Types of blitting in DirectDraw Two Types of blitting in DirectDraw Direct access of the surface to perform the blit Direct access of the surface to perform the blit Using Blt and BltFast to perform the blitting operation Using Blt and BltFast to perform the blitting operation
16
Blt and BltFast Used to fill surfaces or copy an image to a surface Used to fill surfaces or copy an image to a surface BltFast has less options than Blt BltFast has less options than Blt BltFast provides a speed increase when the HEL is being used (~10%) BltFast provides a speed increase when the HEL is being used (~10%) Blt Demo Blt Demo Blt Demo Blt Demo
17
But I don’t want to have only squares! Loading Images into an Off-Screen Surface Loading Images into an Off-Screen Surface Typically requires direct access of the screen buffer to allow for custom file formats Typically requires direct access of the screen buffer to allow for custom file formats Raw 24bpp images require no decompression, however they are large Raw 24bpp images require no decompression, however they are large Bitmaps can be blitted through the GDI Bitmaps can be blitted through the GDI The Lock() and Unlock() functions The Lock() and Unlock() functions Used to “grab” a surface to blt to Used to “grab” a surface to blt to Must be used together or other functions will be unable to access the surface! Must be used together or other functions will be unable to access the surface!
18
File I/O CreateFile, ReadFile, CloseHandle CreateFile, ReadFile, CloseHandle CreateFile returns a handle to a new or existing file so read/write operations can occur CreateFile returns a handle to a new or existing file so read/write operations can occur ReadFile returns a specified number of bytes ReadFile returns a specified number of bytes CloseHandle closes the file CloseHandle closes the file
19
Transparent Blts DDCOLORKEY Structure DDCOLORKEY Structure Two member variables used to define a color space that will appear transparent Two member variables used to define a color space that will appear transparent Select the color key for a surface using the SetColorKey() Method Select the color key for a surface using the SetColorKey() Method Select either the Destination Color Key or the Source Color Key Select either the Destination Color Key or the Source Color Key Call Blt or BltFast using the appropriate flag to create the transparent Blt Call Blt or BltFast using the appropriate flag to create the transparent Blt
20
Transparent Blt Demo Demo Demo Demo The selected color key is RGB(255,0,255) – the pink in this picture The selected color key is RGB(255,0,255) – the pink in this picture
21
Interfacing to the User Win32 Messages Win32 Messages WM_MOUSEMOVE, WM_LBUTTONDOWN, etc. WM_MOUSEMOVE, WM_LBUTTONDOWN, etc. Excellent for applications Excellent for applications Dependent upon message queue to handle user input Dependent upon message queue to handle user input Direct Input Direct Input Requires polling of the device Requires polling of the device Can be synced to get new input every frame Can be synced to get new input every frame Allows use of specialized game devices Allows use of specialized game devices USB Joysticks, Flight Pedals, etc. USB Joysticks, Flight Pedals, etc.
22
Direct Input USB Considerations USB Considerations USB allows for up to 127 devices to be connected! USB allows for up to 127 devices to be connected! The user may have more than one mouse, joystick or keyboard connected to the system The user may have more than one mouse, joystick or keyboard connected to the system Needs to “gracefully” recover from having a device disconnected from the system Needs to “gracefully” recover from having a device disconnected from the system
23
Multi-Threaded Application Development Threads rock! Threads rock! Every programmer loves threads the first time they use them Every programmer loves threads the first time they use them A multi-threaded application can perform multiple tasks at “once” A multi-threaded application can perform multiple tasks at “once” Very useful to allow GUIs to appear “alive” as an intensive calculation occurs in the background Very useful to allow GUIs to appear “alive” as an intensive calculation occurs in the background Multi-Threaded Demo Multi-Threaded Demo Multi-Threaded Demo Multi-Threaded Demo
24
Thread Synchronization Critical Sections Critical Sections Kernel Objects Kernel Objects Ex. Mutex, Semaphore, Event, Thread, Process and many more Ex. Mutex, Semaphore, Event, Thread, Process and many more
25
Critical Section A segment of code that needs to be executed without being pre-empted A segment of code that needs to be executed without being pre-empted Started by making a call to EnterCriticalSection Started by making a call to EnterCriticalSection Completed by calling LeaveCriticalSection Completed by calling LeaveCriticalSection System will appear “locked” during a critical section System will appear “locked” during a critical section Important to keep these sections to a minimal size Important to keep these sections to a minimal size Only use in areas that require it to be used Only use in areas that require it to be used
26
Kernel Objects An object that is shared between threads An object that is shared between threads WaitForSingleObject and WaitForMultipleObject will wait until a specified kernel object is signaled WaitForSingleObject and WaitForMultipleObject will wait until a specified kernel object is signaled Important to thoroughly analyze the return value to prevent from misinterpreting the results Important to thoroughly analyze the return value to prevent from misinterpreting the results WAIT_FAILED, WAIT_OBJECT_0, WAIT_TIMEOUT, WAIT_ABANDONED_0 WAIT_FAILED, WAIT_OBJECT_0, WAIT_TIMEOUT, WAIT_ABANDONED_0
27
Example – Buggy Code DWORD dwWaitResult; dwWaitResult = WaitForSingleObject(hMutex, 1000); If (WAIT_FAILED == dwWaitResult) { // Handle Failure } Else { // Assume Success – Potential Bug! }
28
Example – Solid Code DWORD dwWaitResult; dwWaitResult = WaitForSingleObject(hMutex, 1000); If (WAIT_OBJECT_0 == dwWaitResult) { // The mutex is now owned } Else { // Handle Wait failure. Differentiate between the 3 possible failures }
29
Dangers of Multi-Threaded Development “Dueling” conditions between threads “Dueling” conditions between threads Race Conditions Race Conditions Exist where two or more threads are competing to share the same resource Exist where two or more threads are competing to share the same resource Very difficult to track – good design will prevent bugs of this nature Very difficult to track – good design will prevent bugs of this nature Starvation Starvation Occurs when a single thread controls a resource due to scheduling priority Occurs when a single thread controls a resource due to scheduling priority
30
Race Condition Example Two threads A and B share a common DirectDraw Surface Two threads A and B share a common DirectDraw Surface Thread A locks the surface Thread A locks the surface Before it is unlocked, A is pre-empted by B, which needs to access the surface Before it is unlocked, A is pre-empted by B, which needs to access the surface Solution: Use a critical section to prevent a conflict or consolidate the two threads Solution: Use a critical section to prevent a conflict or consolidate the two threads
31
Starvation Example Two threads – A and B Two threads – A and B A has higher priority than B A has higher priority than B They share a common resource They share a common resource A completes so quickly that it immediately reacquires the resource preventing B from executing A completes so quickly that it immediately reacquires the resource preventing B from executing
32
Application to Game Development Allows background processing of data Allows background processing of data Example: Example: One thread for system messages One thread for system messages One thread for drawing the frame buffer One thread for drawing the frame buffer One thread to handle input (user or network) One thread to handle input (user or network) Potential increase in speed by using multiprocessor systems Potential increase in speed by using multiprocessor systems Quake 3 Quake 3
33
Basics to start programming The latest DirectX SDK The latest DirectX SDK Make sure to add ddraw.lib to the link option in Visual C++ (under the Project->Settings menu) Make sure to add ddraw.lib to the link option in Visual C++ (under the Project->Settings menu) Have patience! Debugging DirectDraw is difficult Have patience! Debugging DirectDraw is difficult MSDN MSDN
34
Further Reading Inside DirectX, Bargen and Donnelly. Microsoft Press Inside DirectX, Bargen and Donnelly. Microsoft Press Win32 Multithreaded Programming, Cohen and Woodring. O’Reilly. Win32 Multithreaded Programming, Cohen and Woodring. O’Reilly. Programming Windows, Charles Petzold. Microsoft Press. (No Windows programmer should be without this!) Programming Windows, Charles Petzold. Microsoft Press. (No Windows programmer should be without this!)
35
Contacts Alex Riemann Alex Riemann Riemann@uiuc.edu Riemann@uiuc.edu Riemann@uiuc.edu Microsoft UIUC Recruiter - Tracy Foltz Microsoft UIUC Recruiter - Tracy Foltz Tracyfo@microsoft.com Tracyfo@microsoft.com Tracyfo@microsoft.com
36
Video Drivers Graphics Primitive Engine (GPE) Classes Graphics Primitive Engine (GPE) Classes Specific to Windows CE Devices Specific to Windows CE Devices Common set of classes that allows Windows to utilize the hardware on the device regardless of manufacturer Common set of classes that allows Windows to utilize the hardware on the device regardless of manufacturer Problem: Every device is unique. Writing Drivers requires the complete specification of the hardware device to create an optimized driver Problem: Every device is unique. Writing Drivers requires the complete specification of the hardware device to create an optimized driver
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.