DirectDraw in Java

zhaozj2021-02-11  196

DirectDraw in Java

Note: DirectDraw® is an integral part of Microsoft DirectX® SDK. The Java version of DirectX is included in the Java 2.0 SDK. The DirectDraw is accessed through a set of COM.MS.DIRECTX packages installed with DirectX.

Introduction

This article will explore some advantages, structures, and use of DirectDraw SDK for Java. In the past, the use of animated programs need to be written with C (or assembled languages) because animations need to be quickly processed. Animation is implemented by quickly showing a series of pictures (or frames). In order to give the user to watch the animation, there is no flashing sense, at least the rate of 12 frames per second. Higher frame rates generate smoother animations: For example, the frame rate of cartoons is usually 24 frames per second. To reach the minimum 12 frame requirements, image data of 3.6M bytes is required per second in 640'480, 256 color mode. Handling such a number of video data is very difficult. DirectDraw now provides this video data processing capability for Java developers.

Start using the DirectDraw API requires some basic steps. It will now be listed below and will be explained in the following part:

Create a DirectDRAW object. Set collaboration level. Create a needed surface. Loading a bitmap you need. Show the surface.

DirectDraw object

To use DirectDraw in Java, you must create a DirectDraw object first:

DD = New DirectDraw ();

Once the DirectDraw object is created, you need to set the collaboration level.

Collaborative level

You must set the control level that DirectDraw on the highest layer window. At the simplest level, the functionality of DirectDraw is the same as other programs, which is limited to Windows® 95 or Windows NT® window. This is the normal level of DirectDraw. Normal collaboration level can be set by the following statement:

DD.SETCOOPERATIVELEVELEVEL (HWND, DDSCL_NORMAL);

Note To enable the program to utilize more advanced DirectDraw features, such as changing the display mode or modifying the behavior of the surface of DirectDraw, you need to use exclusive mode. However, the use of advanced features beyond the scope of this article. For more information on using advanced features, please refer to the DirectX document.

Create surface

DirectDraw uses the term surface (SURFACE) to display content. The surface can be placed in display memory or in system memory. However, if the display hardware does not have enough memory plans, DirectDRAW simulates the surface in the system memory. The biggest advantage of the DirectDraw surface is that it is always available to developers in the form of a linear memory area. Even if the display mode of the program is not linear, the surface is still a linear memory area provided by the developer. Considering the following example, Modex displays memory is configured in a series (Figure 1). To draw a point, you must defense the appropriate memory and the correct address. The DirectDrawSurface object has handled all of these details.

Figure 1. Display memory organization in Mode X.

A DirectDRAW surface can contain multiple memory buffers, allowing constructing surfaces that can be switched. The switching surface allows the program to utilize the advantages of double buffer technology. In the double buffer method, the program draws some content in the background buffer. After the painting, switch the background buffer to the front buffer, so that the screen is displayed. The double buffer method is very fast, especially when the current table and the background buffer are displayed in the memory. At this point, the switching operation does not even consume any CPU time.

Users based on the DirectDraw program are always the original surface (PRIMARY SURFACE). In order to change the content seen by the user, you can simply change the contents of the original surface. Typically, you need to create one or more background surfaces that contain pictures displayed at different times. Figure 2 illustrates a complex double buffer original surface. The original surface consists of a background buffer, a front buffer, and four back surfaces. Each background surface contains a bitmap of a different rotation stage. To make the cylinder, the program copies each bitmap from the background surface to a location in the background buffer of the original surface. Whenever the copy of each cylinder is completed, the program switches the background buffer to the front desk buffer. Figure 2. Relationship between double buffer surface

DirectDraw implements two classes that are specifically designed for surface work in Java. DDSurfaceDesc class is used to describe the properties of the created surface. The DirectDrawSurface class contains definitions, methods, and variables that work for the surface of the DirectDRAW.

To create a DirectDraw surface, whether it is the original surface or the background surface, you must first create a DDSurFaceDesc object, set surface properties, and pass the surface description structure to the createSurface () method of the DirectDraw object. The following statement creates a raw surface consisting of a buffer:

// CREATE A Primary Surface Containing A Single Buffer.

DDSD = New ddsurfacedesc ();

DDSD.FLAGS = DDSD_CAPS;

DDSD.DDSCAPS = DDSCAPS_PRIMARYSURFACE;

PDDS = DD.CREATESURFACE (DDSD);

The following statement creates a double buffer original surface:

// CREATE A Primary Surface Containing A Back // Buffer for Double Buffering.

DDSD = New ddsurfacedesc ();

DDSD.FLAGS = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;

DDSD.DDSCAPS = DDSCAPS_PRIMARYSURFACE | DDSCPAS_FLIP | DDSCAPS_COMPLEX;

Ddsd. backbuffercount = 1;

PDDS = DD.CREATESURFACE (DDSD);

The following statement explains how to create a 320 pixel width 200 rising background surface:

DDSD = New ddsurfacedesc ();

DDSD.FLAGS = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;

DDSD.DDSCAPS = DDSCAPS_OFFSCREENPLAIN

DDSD.WIDTH = 320;

DDSD.HEIGHT = 200;

PDDS = DD.CREATESURFACE (DDSD);

Put the bitmap into the surface of the DirectDRAW

The bitmap is a picture that can be used to display graphical content. Each of the four cylinders in Figure 2 is a bitmap. DirectDraw in Java provides the DirectDrawbitMap class for the operation bitmap file. After the DirectDrawbitMap object is created, you can copy it to the DirectDraw surface. The following statement reads a bitmap file called FRNTBACK.BMP to the DirectDRAW surface:

// read the bitmap file.

BM = New DirectDrawbitmap ();

BM.FileName ("FRNTBACK.BMP");

BM.INITWIDTH (DX);

BM.INITHEIGHT (DY);

IF (bm.loaded ()! = 0)

{

// Create a DirectDrawSurface for // this bitmap.

DDSD = New ddsurfacedesc ();

DDSD.FLAGS = DDSD_CAPS |

DDSD_HEIGHT | DDSD_WIDTH;

DDSD.DDSCAPS = DDSCAPS_OFFSCREENPLAIN

DDSD.WIDTH = Bm.width ();

Ddsd.height = bm.height ();

PDDS = DD.CREATESURFACE (DDSD);

Pdds.copybitmap (BM, 0, 0, 0, 0);

}

// off-screen surface pdds now contains the

// bitmap data.

Display surface

When all surfaces are created and initialized (for example, copying the bitmap to the surface), you can copy them to the original surface when you need to display them. This surface is copied by the BLT method of the DirectDrawSurface object. In the default mode of operation, if the switch is busy (switching), the DDS.BLT () method returns an error code immediately. Therefore, the dds.blt () method should be used in a cycle, or the DDBLT_WAIT flag of the DDS.BLT () method should be specified. The second option changed the behavior of DDS.BLT () such that this method waits until the switch can be performed or another error occurs.

The following code illustrates how to use the DirectDrawSurface BLT method. First, there are two points to clarify. If the mode change or the program is used to use exclusive access to the display card, DirectDrawSurface will be lost. Recovery is required when the surface is lost. Recovery can be done by two steps. First call the RESTORE method of the DirectDrawSurface object to reassign the surface memory and re-add the DirectDrawSurface object. Then, reconstruct the contents of the relevant surface.

Rc.Left = 0;

rc.top = 0;

rc.right = bm.width ();

Rc.Bottom = BM.HEIGHT ();

// show the off-screen surface on The OFF-Screen Surface

// Primary Surface.

DONE = 0;

DO

{

int Retval;

Retval = DDSPRIMARY.BLT (RC, DDSONE, RC, 0);

IF (retval == dd_ok)

{

// if the bitmap HAS Been successfully

// Copied, Exit Loop.

DONE = 1;

}

Else if (retval == dderr_surfacelost)

{

While (DDSPRIMARY.RESTORE ()! = DD_OK &&

Ddsone.restore ()! = DD_OK)

{

ReloadBitmap (DDSOne, SZBitmap);

}

}

Else IF (RetVal! = DDERR_WASSTILLDRAWING)

{

// Undetetd Error; Quit the loop.

DONE = 1;

}

} while (done == 0);

other information

This article provides the background and basic steps of initialization and use of DirectDraw SDK in Java. To get Java's DirectX SDK and related documents, you can download Microsoft SDK for Java 2.0 from http://www.microsoft.com/java/ (which contains DirectX SDK). It also includes several sample programs that are very useful for creating an animation using DirectX for Java. These examples are as follows: Sample Directory Sample Content DDEX3 DDRAW / DDEX3 / DDRAW.HTML DirectDraw Basic Using Flipcube D3D / Flipcube / D3D.html 3d Immediate Mode Basic Using Viewer D3DRM / Viewer / Direct3DRM.html Direct3D Keep Mode Complete User Castle D3DRM / CASTLE / CASTLE.HTML DIRECT3D Keep mode High Performance DirectInput Dinput / DDEX3 / DINPUT.HTML Joystick, Mouse, Keyboard, and Game Keyboard

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

New Post(0)