This is not a detailed tutorial about DirectX, but an explanation of how to use DELPHI to develop DirectX games. Because books about DirectX, whether online or in bookstores, basically describe them in C++ or VC. The information on using DELPHI to develop games is Very little, the purpose of this article is to enable readers to learn how to use existing information to learn and develop games.
This article is intended for readers who have a certain understanding of DirectX but do not know how to develop DirectX games under DELPHI.
Recommended reference materials:
<<Game Programming Guide>>,<<DirectX Chinese Collection>>
Can DELPHI develop games?
The answer is of course. Many people in many online game forums believe that only C++ or VC can be used to develop games. DELPHI is only suitable for desktop applications. People with these views are advised to disassemble and look at the code compiled by DELPHI and VC, or look at the game "Age of Miracles". "Age of Miracles" was developed with DELPHI. The speed and graphics are better than Age of Empires.DELP HI is completely object-oriented, can embed assembly, and supports MMX instructions (MMX registers in DELPHI are mm0-mm7). It is completely suitable for the needs of game development. In fact, regardless of VC, DELPHI is just a tool, as long as you have good internal skills, you can make it A good program or game.
Preparation:
There are currently two options for developing DirectX games with DELPHI. One is to use jedi's DirectX declaration (http://www.delphi-jedi.org). The other is to use DelphiX controls. Here we are going to use jedi's Direct X declaration package is used to develop DirectX games. The reason why I choose the DirectX declaration package is because it uses the SDK method to develop games. If I need to switch to other languages in the future, I don’t have to relearn DirectX. As for the DelphiX control, I have never used it and have no say. But I don’t use Japanese products;-)
First go to the following address to download the DirectX declaration package (http://kuga.51.net/download/files/directx7.rar), and extract it to your custom directory. Then select Tools->Environment Options in DELPHI, Select the Library tab in the opened window and click the button behind Library Path. A Directories window will pop up, then click Greyed items denoted invalid button on the right side of path. Select the directory to which the DirectX declaration is extracted. Then click the ADD button to add the directory where the DirectX declaration is to the Library path of DELPHI. You can directly reference the unit in the DirectX declaration in uses. This declaration The package comes with several examples that can be used as a reference for getting started.
Debugging experience:
When developing a full-screen game, it is best to set the screen resolution during design to the same resolution as the game to avoid damaging the screen by frequently switching resolutions during debugging.
It is best to develop full-screen games under WIN2000/XP. Otherwise, when debugging under 98, the game will enter an infinite loop or an exception will occur. The machine will easily crash. When a full-screen game enters an infinite loop under 2000/XP, you can press ALT+ TAB switches to DELPHI (but at this time Since DirectX games are full-screen and occupy the screen, there will be no changes on the screen, so you need to try a few more times). Press CTRL+F2 to end the game. If it is abnormal, switch to DELPHI and press Enter first and then press CTRL+F2 can end the debugging game.
Notice:
If you are using DELPHI7, please change line 145 {$IFDEF VER140} in DirectDraw.pas to {$IFDEF VER150} for normal compilation and interpretation.
It is better to use API to create the main game window instead of using VCL's TFORM class.
First let's take a look at the code snippet that initializes a DirectDraw object using C++ and DELPHI.
c++ version:
BOOL InitDDraw( )
{
LPDIRECTDRAW7 lpDD; //Pointer to DirectDraw object
if (DirectDrawCreateEx (NULL, (void **)&lpDD, IID_IDirectDraw7, NULL) != DD_OK )
return FALSE; {Create DirectDraw object}
{The if (xxx != DD_OK) method is used for error detection, which is the most commonly used method}
if (lpDD->SetCoOperativeLevel(hwnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN) != DD_OK )
return FALSE; {Set DirectDraw control level}
if ( lpDD->SetDisplayMode( 640, 480, 32, 0, DDSDM_STANDARDVGAMODE ) != DD_OK )
return FALSE; {set display mode}
}
DELPHI version:
function TForm1.InitDirectDraw: Boolean;
var
lpDD: IDirectDraw7;
begin
Result := False; {Assume initialization failed}
{Create DirectDraw object}
if DirectDrawCreateEx(nil, lpDD, IID_IDIRECTDRAW7, nil) <> DD_OK then
exit;
{Set the control level of DirectDraw. The first parameter is the handle of the DirectDraw window. Here, the control level is set to full screen plus exclusive mode}
if lpDD.SetCooperativeLevel(Hwnd, DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN) <> DD_OK then
exit;
{Set the display mode. The first and second parameters are the resolution. The third parameter is used to set the number of color digits in the display mode.
The fourth parameter sets the refresh rate of the screen. 0 is the default value. The only valid value of the fourth parameter is DDSDM_STANDARDVGAMODE}
if lpDD.SetDisplayMode(640, 480, 32, 0, DDSDM_STANDARDVGAMODE) <> DD_OK then
exit;
Result := True;
end;
It can be seen that these two pieces of code are exactly the same except for syntax and object names. As long as we understand this, we can refer to VC or C++ information and then use DELPHI to make our own games. The objects in the DirectX declaration in DELPHI The name, structure name and VC are different. The general correspondence is as follows:
DELPHI VC
DirectDraw objectIDirectDraw7 LPDIRECTDRAW7
Page object IDirectDrawSurface7 LPDIRECTDRAWSURFACE7
DirectDraw page description TDDSurfaceDesc2 DDSURFACEDESC2
Basically, only the prefixes are different. Due to space, not all objects and structures are listed here.