📄 ch28.htm
字号:
up a simple graphics engine. The key components in
this graphics engine are described
in Table 28.1.
<H4><FONT COLOR="#000077">Table 28.1. The key objects in the graphics engine.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Component</B></TD>
<TD
ALIGN="LEFT"><B>Purpose</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>THermes</TT></TD>
<TD ALIGN="LEFT">Get into and out of DirectX Exclusive mode</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD
ALIGN="LEFT"><TT>TScene</TT></TD>
<TD ALIGN="LEFT">Draw the background of a graphics scene</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>TSpriteScene</TT></TD>
<TD ALIGN="LEFT">Draw the background of a graphics scene that
supports sprites</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>THermesChart</TT></TD>
<TD ALIGN="LEFT">Create a tiled game scene</TD>
</TR>
</TABLE>
After I describe the graphics engine, I will describe the game engine that can
be
used in conjunction with these objects.
<H2><FONT COLOR="#000077">THermes: Getting Into and Out of Exclusive Mode</FONT></H2>
<P>DirectX is a Microsoft technology that allows you to write high-performance gaming,
simulation, and multimedia
programs. In this chapter, I focus on the <TT>IDirectDraw</TT>
interface, which is a portion of DirectX that gives you the ability to write directly
to the video buffer.</P>
<P>The first thing you find when working with DirectX is that, to take full
advantage
of it, you need to switch into a special video mode called Exclusive mode. For the
purposes of this book, this mode has a 640x480 screen dimension and 256 colors. Actually,
other screen sizes and color resolutions are available to you as a
DirectX programmer,
but <TT>THermes</TT> gives you access only to this one screen mode.</P>
<P>The first object in the graphics engine is designed to switch you into and out
of this mode. The class declaration for this object is shown in Listing 28.1.
You
can find the full source for the code shown here in a file called <TT>Mercury2.cpp</TT>
in the <TT>Utils</TT> directory on the CD that accompanies this book.</P>
<P>The <TT>Mercury</TT> unit relies on <TT>Creatures.pas</TT>, which ships with this
book, and <TT>DDraw.h</TT>, which ships in the included directory with BCB. You will
also find a Pascal version of the <TT>Mercury</TT> unit in the <TT>Units</TT> subdirectory
on the CD that accompanies this book.
<BLOCKQUOTE>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>NOTE: </B></FONT>To find some Web sites of interest to DirectX
programmers and full Pascal translations of DirectX 3.X headers, go to the following:<BR>
<A HREF="javascript:if(confirm('http://www.dkw.com/bstone/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.dkw.com/bstone/'" tppabs="http://www.dkw.com/bstone/">http://www.dkw.com/bstone/</A>
<HR>
</DL>
</BLOCKQUOTE>
<PRE></PRE>
<P>When you're looking at this code, keep these points in mind:
<UL>
<LI>I do not quote the entire source until the end of the chapter. Throughout the
main body of this chapter, I quote only the class
declarations, one at a time as
needed so that you can use them to grasp the structure of the portion of code currently
under discussion. At the end of the chapter, I quote the entire object.
<P>
<LI>You can install all the key objects in these
units as components and use them
directly in BCB, just as you would use any of the other components discussed in this
book.
<P>
<LI>You don't really have to understand this code, but you do need to know how to
use the components created when
these objects are installed on the Component Palette.
</UL>
<H4><FONT COLOR="#000077">Listing 28.1. Mercury2.cpp contains logic for handling
sprites, backgrounds, and DirectX. The key object in this file is THermes.</FONT></H4>
<PRE><FONT
COLOR="#0066FF">class THermes : public Classes::TComponent
{
typedef Classes::TComponent inherited;
friend THermesChart;
friend TDraw;
private:
bool FActive;
Creatures1::TFileCreatureList* FCreatureList;
HWND FHandle;
bool
FTimerOdd;
int FTimerInterval;
bool FExclusive;
Classes::TNotifyEvent FPaintProc;
TScene* FScene;
bool FUseTimer;
bool FFirstTime;
IDirectDraw* FDirectDraw;
IDirectDrawSurface* FBackSurface;
IDirectDrawClipper* FClipper;
IDirectDrawSurface* FPrimarySurface;
bool __fastcall CreatePrimary(void);
void __fastcall DDTest(long hr, System::AnsiString S);
void __fastcall InitBaseObjects(void);
bool __fastcall MakeItSo(long DDResult);
void __fastcall
SetScene(TScene* Scene);
bool __fastcall SetUpBack(void);
protected:
void __fastcall DrawBitmaps(void);
virtual long __fastcall RestoreSurfaces(void);
public:
__fastcall virtual THermes(Classes::TComponent* AOwner);
__fastcall
virtual ~THermes(void) {}
void __fastcall EndExclusive(void);
void __fastcall ErrorEvent( System::AnsiString S);
void __fastcall Flip(void);
virtual void __stdcall InitObjects(void);
virtual void __fastcall Run(void);
__property bool
Active = {read=FActive, write=FActive, nodefault};
__property IDirectDrawSurface* BackSurface =
{read=FBackSurface, write=FBackSurface, nodefault};
__property Classes::TNotifyEvent OnDrawBitmap =
{read=FPaintProc, write=FPaintProc};
__published:
__property Creatures1::TFileCreatureList* CreatureList =
{read=FCreatureList, write=FCreatureList, nodefault};
__property bool Exclusive = {read=FExclusive, write=FExclusive, nodefault};
__property TScene* Scene =
{read=FScene, write=SetScene, nodefault};
__property int TimerInterval =
{read=FTimerInterval, write=FTimerInterval, nodefault};
__property bool UseTimer = {read=FUseTimer, write=FUseTimer, nodefault};
</FONT></PRE>
<P><FONT
COLOR="#0066FF"><TT>};</TT> </FONT><BR>
<BR>
To install <TT>THermes</TT> and the other graphics engine objects, choose Component
| Install. Click the Add button, and then browse the <TT>Units</TT> directory from
the CD that accompanies this book.
Install both <TT>Creatures1.pas</TT> and <TT>Mercury2.cpp</TT>.</P>
<P>To use <TT>THermes</TT>, start a new project, drop a <TT>THermes</TT> object on
it, and create an <TT>OnKeyDown</TT> handler that closes the form if any key is pressed.
The code
for such a project is shown in Listings 28.2 and 28.3.
<H4><FONT COLOR="#000077"><BR>
Listing 28.2. The HermesTest1 project shows how to use THermes. The header for the
main unit is shown here.</FONT></H4>
<PRE><FONT
COLOR="#0066FF">///////////////////////////////////////
// Main.h
// Testing the THermes object
// Copyright (c) 1997 by Charlie Calvert
//
#ifndef MainH
#define MainH
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include "..\..\utils\Mercury2.h"
#include <vcl\Menus.hpp>
class TForm1 : public TForm
{
__published:
THermes *Hermes1;
TMainMenu *MainMenu1;
TMenuItem
*Run1;
void __fastcall FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
void __fastcall Run1Click(TObject *Sender);
private:
void __fastcall MyExceptions(TObject *Sender, Exception *E);
public:
__fastcall
TForm1(TComponent* Owner);
};
extern TForm1 *Form1;</FONT></PRE>
<P><FONT COLOR="#0066FF"><TT>#endif</TT></FONT>
<H4><FONT COLOR="#000077">Listing 28.3. The source for the main unit in the HermesTest1
project.</FONT></H4>
<PRE><FONT
COLOR="#0066FF">
///////////////////////////////////////
// Main.cpp
// The TestHermes project tests the THermes component
// Copyright (c) 1997 by Charlie Calvert
//
#include <vcl\vcl.h>
#pragma hdrstop
#include "Main.h"
#pragma link "Mercury2"
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Application->OnException = MyExceptions;
}
void __fastcall
TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if ((Shift.Contains(ssAlt)) && (Key == `E'))
throw Exception("Some Exception");
else
if (!Shift.Contains(ssAlt))
Close();
}
void
__fastcall TForm1::Run1Click(TObject *Sender)
{
Hermes1->Run();
}
void __fastcall TForm1::MyExceptions(TObject *Sender,
Exception *E)
{
Hermes1->EndExclusive();
ShowMessage(E->Message);</FONT></PRE>
<P><FONT
COLOR="#0066FF"><TT>}</TT> </FONT><BR>
<BR>
You should note that this project probably will not run correctly unless you add
<TT>Creatures.pas</TT> to it, as shown in the <TT>USEUNIT</TT> statement from the
project source:</P>
<PRE><FONT
COLOR="#0066FF">
//--------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
//--------------------------------------------------------------------------
USEFORM("Main.cpp",
Form1);
USERES("HermesTest1.res");
USEUNIT("..\..\Units\creatures1.pas");
//--------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -