Tech Talk: DirectDraw

Download Report

Transcript Tech Talk: DirectDraw

Tech Talk:
DirectDraw
Alex Riemann
University of Illinois,
Urbana-Champaign
February 13, 2000
Graphics Programming in DOS
Very Limited Video Hardware
 Required the developer to know what
features the video hardware provided
 Video Electronics Standards Association
(VESA)

Helped relieve this situation
 Provided semi-common interface to video
memory

Graphics Programming in
Windows
Common set of API’s to access graphics
subsystem
 Numerous layers of indirection between the
programmer and the hardware
 Graphics Device Interface (GDI) objects



Great for Circles, Squares, and other simple
objects
Far too slow to handle effects for video games!
Enter the Game SDK
Introduced to promote game
development on the Windows Platform
 Designed to shift responsibility away
from the developer and to the hardware
manufacturer


Soon became known as DirectX
What does DirectX provide?





Fast, low latency access to hardware
Hardware emulation for certain tasks (if
required)
A single set of API calls regardless of system
hardware
Multiplayer support that is indifferent to
transport media (TCP/IP, IPX, Null Modem,
etc.)
And so much more!
The Hardware Abstraction Layer
(HAL)
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 Hardware Emulation Layer
(HEL)
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

Components of DirectX








DirectDraw
Direct3D
DirectSound
DirectPlay
DirectInput
DirectSetup
DirectMusic
DirectShow
DirectDraw Basics
Provides an interface to the 2D
hardware available on modern video
cards
 Important Concepts

Graphics Modes
 The DirectDraw Object
 Primary DirectDraw Surface

Data Structure Initialization

Critical in DirectX

Example
DDSURFACEDESC ddsdesc;
ZeroMemory(&ddsdesc, sizeof(ddsdesc));
ddsdesc.dwSize = sizeof(ddsdesc);

Typically, DirectX structures require that
the dwSize parameter be initialized
Page Flipping
Requires addition of a Back Buffer
Surface
 Prevents “tearing” from occurring

No Page Flipping Demo
Page Flipping Demo
Page Flipping – Illustration
Display
Address
Surface 1
Front
Buffer
Display
Address
Surface 1
Front
Buffer
Surface 2
Back
Buffer
Monitor
Monitor
Surface 2
Back
Buffer
Before Page Flip
After Page Flip
The Surface
Typically “imagined” as a 2D surface,
however is actually a linear buffer
 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
MSDN – Your Friend
Exercise – Look up DDSurfaceDesc and
read the description of its member
variables
 Launch MSDN
 Important Member Variables

Pitch
 lpSurface
 ddckCKDestBlt, ddckCKSrcBlt

Blitting to a Surface
A blit is the process of copying an
image from one surface onto another
 Two Types of blitting in DirectDraw

Direct access of the surface to perform the
blit
 Using Blt and BltFast to perform the blitting
operation

Blt and BltFast
Used to fill surfaces or copy an image to
a surface
 BltFast has less options than Blt



BltFast provides a speed increase when the
HEL is being used (~10%)
Blt Demo
But I don’t want to have only
squares!

Loading Images into an Off-Screen Surface




Typically requires direct access of the screen
buffer to allow for custom file formats
Raw 24bpp images require no decompression,
however they are large
Bitmaps can be blitted through the GDI
The Lock() and Unlock() functions


Used to “grab” a surface to blt to
Must be used together or other functions will be unable
to access the surface!
File I/O

CreateFile, ReadFile, CloseHandle



CreateFile returns a handle to a new or existing
file so read/write operations can occur
ReadFile returns a specified number of bytes
CloseHandle closes the file
Transparent Blts

DDCOLORKEY Structure

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 either the Destination Color Key or the
Source Color Key
 Call Blt or BltFast using the appropriate flag to
create the transparent Blt

Transparent Blt Demo
Demo
 The selected color key is
RGB(255,0,255) – the pink in this
picture

Interfacing to the User

Win32 Messages




WM_MOUSEMOVE, WM_LBUTTONDOWN, etc.
Excellent for applications
Dependent upon message queue to handle user
input
Direct Input



Requires polling of the device
Can be synced to get new input every frame
Allows use of specialized game devices

USB Joysticks, Flight Pedals, etc.
Direct Input

USB Considerations
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
 Needs to “gracefully” recover from having
a device disconnected from the system

Multi-Threaded Application
Development

Threads rock!

Every programmer loves threads the first
time they use them
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
 Multi-Threaded Demo

Thread Synchronization
Critical Sections
 Kernel Objects


Ex. Mutex, Semaphore, Event, Thread,
Process and many more
Critical Section
A segment of code that needs to be executed
without being pre-empted
 Started by making a call to EnterCriticalSection
 Completed by calling LeaveCriticalSection
 System will appear “locked” during a critical
section



Important to keep these sections to a minimal size
Only use in areas that require it to be used
Kernel Objects
An object that is shared between
threads
 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

WAIT_FAILED, WAIT_OBJECT_0,
WAIT_TIMEOUT, WAIT_ABANDONED_0
Example – Buggy Code
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(hMutex, 1000);
If (WAIT_FAILED == dwWaitResult)
{ // Handle Failure }
Else
{ // Assume Success – Potential Bug! }
Example – Solid Code
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(hMutex, 1000);
If (WAIT_OBJECT_0 == dwWaitResult)
{ //
The mutex is now owned }
Else
{ // Handle Wait failure.
the 3 possible failures }
Differentiate between
Dangers of Multi-Threaded
Development

“Dueling” conditions between threads

Race Conditions
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


Starvation

Occurs when a single thread controls a
resource due to scheduling priority
Race Condition Example
Two threads A and B share a common
DirectDraw Surface
 Thread A locks 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

Starvation Example
Two threads – A and B
 A has higher priority than B
 They share a common resource
 A completes so quickly that it
immediately reacquires the resource
preventing B from executing

Application to Game Development

Allows background processing of data

Example:
One thread for system messages
 One thread for drawing the frame buffer
 One thread to handle input (user or network)


Potential increase in speed by using
multiprocessor systems

Quake 3
Basics to start programming
The latest DirectX SDK
 Make sure to add ddraw.lib to the link
option in Visual C++ (under the
Project->Settings menu)
 Have patience! Debugging DirectDraw
is difficult
 MSDN

Further Reading
Inside DirectX, Bargen and Donnelly.
Microsoft Press
 Win32 Multithreaded Programming,
Cohen and Woodring. O’Reilly.
 Programming Windows, Charles Petzold.
Microsoft Press. (No Windows
programmer should be without this!)

Contacts

Alex Riemann


[email protected]
Microsoft UIUC Recruiter - Tracy Foltz

[email protected]