Using DirectDraw -- http://www.microsoft.com/china/MSDN/library/archives/technic/develop/vj/051 in Java

Source: Internet
Author: User
 

Use DirectDraw in Java

Note:DirectDraw is an integral part of Microsoft DirectX SDK. Java DirectX is included in Java 2.0 SDK. In Java, use a set of classes in the com. Ms. DirectX package installed together with DirectX to access DirectDraw.

Introduction

This article will discuss some advantages, structures, and usage of the DirectDraw SDK for Java. In the past, Animation programs needed to be written in C ++ (or assembly language) because the animation needed to be quickly processed. An animation is implemented by rapidly and continuously displaying a series of images (or frames. In order to make the animation do not flash, at least 12 frames per second are required. A higher Frame Rate produces a smoother Animation: for example, the animation frame rate is usually 24 frames per second. To meet the minimum requirement of 12 frames per second, 640 MB of image data must be processed per second in 480,256 '000000' color mode. It is very difficult to process such a quantity of video data. DirectDraw now provides video data processing capabilities for Java developers.

Some basic steps are required to start using the DirectDraw API. Here we will list them as follows and explain them in the following sections:

  1. Create a DirectDraw object.
  2. Set the collaboration level.
  3. Create a required surface.
  4. Load the required bitmap.
  5. Display surface.

DirectDraw object

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

Dd = new DirectDraw ();

Once a DirectDraw object is created, you must set the collaboration level.

Collaboration level

You must set the control level of DirectDraw in the upper-level window. At the simplest level, DirectDraw has the same functions as other programs and is limited to Windows 95 or Windows NT windows. This is the normal operation of DirectDraw. You can set the normal collaboration level through the following statement:

Dd. setcooperativelevel (hwnd, ddscl_normal );

Note:To enable the program to take advantage of more advanced DirectDraw features, such as changing the display mode or modifying the behavior of the DirectDraw surface, the exclusive mode is required. However, advanced features are beyond the scope of this article. For more information about using advanced features, see the DirectX documentation.

Create a surface

DirectDraw uses the surface to represent the displayed content. The surface can be stored in both the Display memory and the system memory. However, if the display shows that the hardware does not have enough memory to store the surface, DirectDraw will simulate the surface in the system memory. The biggest advantage of DirectDraw is that it is always provided to developers in the form of linear memory areas. Even if the display mode set by the program is not linear, it still provides linear memory areas for developers. Consider the following example. modex shows that the memory is configured with a series of planes (Fig 1 ). In order to draw a point, you must protect the appropriate memory level and correct address. The directdrawsurface object handles all these details.

Figure 1. Mode xDisplay memory organization in.

A DirectDraw surface can contain multiple memory buffers, allowing you to build a surface that can be switched. Switching the surface allows the program to take advantage of the advantages of dual buffering technology. In the double buffer method, the program draws some content in the background buffer. After the painting, it quickly switches the background buffer or copies it to the foreground buffer to display the screen. The dual-buffer method is very fast, especially when both the current and background buffers are displayed in the memory. In this case, the switch operation does not even consume any CPU time.

The user based on the DirectDraw program always sees the original surface ). To change the content you see, you can simply change the content on the original surface. Generally, you need to create one or more background surfaces that contain images displayed at different times. Figure 2 illustrates a complex dual-buffered raw surface. The original surface consists of a background buffer, a foreground buffer, and four background surfaces. Each background surface contains a bitmap of different rotation stages. To make the cylinder move up, the program copies each bitmap from the background surface to a position in the background buffer of the original surface. The program switches the background buffer to the foreground buffer whenever the replication of each cylinder is complete.

Figure 2.Relationship between two buffered Surfaces

The implementation of DirectDraw in Java includes two classes specially designed for surface work. The ddsurfacedesc class is used to describe the properties of the created surface. The directdrawsurface class contains definitions, methods, and variables for DirectDraw surface work.

To create a DirectDraw surface, whether it is the original or background surface, you must first create a ddsurfacedesc object, set the surface properties, and pass the surface description structure toCreatesurface ()Method. The following statement creates an original surface composed 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 raw dual-buffer 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 describes how to create a background surface with a 320 pixel width of 200 rows:

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 );

Mount the bitmap to the DirectDraw surface.

Bitmap is an image that can be used by a program to display the image content. Each of the four cylinders in Figure 2 is a bitmap. The DirectDraw class in Java provides the directdrawbitmap class for bitmap operations. After the directdrawbitmap object is created, it can be copied to the DirectDraw surface. The following sentence reads a bitmap file named frntback.bmp into 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

// 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

// Bitmap data.

Display surface

When all surfaces are created and initialized (for example, copying bitmap to a surface), the program can copy them to the original surface when it needs to display them. This kind of surface replication uses the directdrawsurface object'sBLTMethod. In the default operation mode, if the switching program is busy (switching is in progress ),DDS. BLT ()Method returns an error code immediately. Therefore, it should be used in a loopDDS. BLT ()Method, or specifyDDS. BLT ()Ddblt_wait flag of the method. The second option has changed.DDS. BLT ()So that this method waits until the switchover can be performed or another error occurs.

The following code illustrates how to use directdrawsurfaceBLTMethod. First, there are two points to be clarified. If the mode of the display card changes or the program uses exclusive access to the display card to release all the surface memory allocated on the current video card, directdrawsurface will be lost. Recovery is required when the surface is lost. Recovery can be completed in two steps. First, callRestoreMethod to re-allocate the surface memory and re-Add the directdrawsurface object. Then, reconstruct the relevant surface content.

RC. Left = 0;

RC. Top = 0;

RC. Right = BM. Width ();

RC. Bottom = BM. Height ();

// Show the off-screen surface on

// 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)

{

// Undetermined error; quit the loop.

Done = 1;

}

} While (done = 0 );

Other information

This document provides the background and basic steps for initializing and using the DirectDraw SDK in Java. To obtain the DirectX SDK and related documents of Java, download microsoft SDK for Java 2.0 (including DirectX SDK) from http://www.microsoft.com/java ). It also contains several sample programs that are useful for creating animations using DirectX for Java. These examples are as follows:

Example

Directory

Sample content

Ddex3

Ddraw/ddex3/ddraw.html

Basic use of DirectDraw

Flipcube

D3d/flipcube/d3d.html

Basic use of 3D instant Mode

Viewer

D3drm/Viewer/direct3drm.html

Full use of direct3d persistence Mode

Castle

D3drm/Castle/castle.html

High Performance of direct3d persistence Mode

Directinput

Dinput/ddex3/dinput.html

Joystick, mouse, keyboard, and game keyboard

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.