[ Pobierz całość w formacie PDF ]
.The core job of the graphics engine presentedin this chapter is to automate this process so you don't have to think about it.THermesChart: Working with a Tiled WorldSome of the most interesting games consist of large worlds that fill up screen after screen with information.Classicexamples of this kind of game are Heroes of Might and Magic, WarCraft, and Civilization.The THermesChartcomponent shows how to work with one of these worlds.Tiled worlds are made up of bitmaps that consist of lots of tiny tiles that can be combined in various ways to create maps.A picture of one of the tiled bitmaps is shown in Figure 28.4, and a small portion of the world created from these tiles isshown in Figure 28.5.Figure 28.4.The tiles from which a tiled world is made.Figure 28.5.A tiled world created by the THermesChart component from the bitmaps shown in Figure 28.4.The declaration for the THermesChart component is shown in Listing 28.10, although the test program for it is shownin Listings 28.11 and 28.12.Listing 28.10.The declaration for the THermesChart class and its parent.struct TSpecialRect{bool IsCreature;RECT R1;RECT R2;};class THermesTiler : public TScene{typedef TScene inherited;friend THermesChart;private:int FMaxMapRows;int FMaxMapCols;int FBitmapWidth;int FBitmapHeight;System::AnsiString FTileMap;file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch28.htm (29 of 67) [10/10/2000 1:16:07 AM] Ch 28 -- Game ProgrammingIDirectDrawSurface* FTileSurface;protected:__fastcall virtual ~THermesTiler(void);virtual void __fastcall DrawScene(void);virtual void __fastcall SetupSurfaces(System::TObject* Sender);virtual TSpecialRect __fastcall GetRect(int Col, int Row) = 0;virtual bool __fastcall MoveGrid(int Col, int Row, bool CallFlip) = 0;public:__fastcall virtual THermesTiler(Classes::TComponent* AOwner);virtual void __fastcall DestroyObjects(void);Windows::TRect __fastcall MapTypeToTileRect(int MapType);virtual long __fastcall RestoreSurfaces(void);__published:__property System::AnsiString TileMap ={read=FTileMap, write=FTileMap, nodefault};__property int BitmapWidth ={read=FBitmapWidth, write=FBitmapHeight, nodefault};__property int BitmapHeight ={read=FBitmapHeight, write=FBitmapHeight, nodefault};};typedef void __fastcall (__closure *TMoveHeroEvent)(System::TObject* Sender,const tagPOINT &NewPos, int NewType, bool &MoveOk);class THermesChart : public THermesTiler{file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch28.htm (30 of 67) [10/10/2000 1:16:07 AM] Ch 28 -- Game Programmingtypedef THermesTiler inherited;private:Creatures1::TCreature* FHero;bool FHeroActive;TMoveHeroEvent FOnHeroMove;bool __fastcall CheckHeroPos(Creatures1::TCreatureList* HeroList,int Col, int Row);Windows::TRect __fastcall MapTypeToCreature(int Col, int Row);virtual TSpecialRect __fastcall GetRect(int Col, int Row);virtual bool __fastcall MoveGrid(int Col, int Row, bool CallFlip);protected:void __fastcall MoveHero(int NewCol, int NewRow);virtual void __fastcall SetupSurfaces(System::TObject* Sender);public:__fastcall virtual ~THermesChart(void);void __fastcall Move(int Value);__published:__property bool HeroActive = {read=FHeroActive, write=FHeroActive, nodefault};__property TMoveHeroEvent OnHeroMove = {read=FOnHeroMove, write=FOnHeroMove};public:__fastcall virtual THermesChart(Classes::TComponent* AOwner): Mercury2::THermesTiler(AOwner) { }};Listing 28.11.The header file for the TilerTest program.///////////////////////////////////////// Main.hfile:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch28.htm (31 of 67) [10/10/2000 1:16:07 AM] Ch 28 -- Game Programming// Project: TilerTest1// Copyright (c) 1997 by Charlie Calvert//#ifndef MainH#define MainH#include#include#include#include#include "Creatures1.hpp"#include#include "Mercury2.h"class TForm1 : public TForm{__published:THermes *Hermes1;THermesChart *HermesChart1;TFileCreatureList *FileCreatureList1;TMainMenu *MainMenu1;TMenuItem *Run1;void __fastcall Run1Click(TObject *Sender);void __fastcall FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);private:public:__fastcall TForm1(TComponent* Owner);};extern TForm1 *Form1;#endiffile:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch28.htm (32 of 67) [10/10/2000 1:16:07 AM] Ch 28 -- Game ProgrammingListing 28.12.The main source for the TestTiler program.///////////////////////////////////////// Main.cpp// Project: TilerTest1// Copyright (c) 1997 by Charlie Calvert//#include#pragma hdrstop#include "Main.h"#pragma link "Creatures1"#pragma link "Mercury2"#pragma resource "*.dfm"TForm1 *Form1;__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner){}void __fastcall TForm1::Run1Click(TObject *Sender){Hermes1->Run();}void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,TShiftState Shift){if (Shift.Contains(ssAlt) && (Key == `X'))file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch28.htm (33 of 67) [10/10/2000 1:16:07 AM] Ch 28 -- Game ProgrammingClose();elseHermesChart1->Move(Key);}By now, you should be getting used to the fact that these components are very easy to use.The only custom code you haveto write is to define how to exit the program:if (Shift.Contains(ssAlt) && (Key == `X'))Close();Other than this one simple statement, all the other "coding" involves nothing more than changing a few properties.This particular example uses bitmaps that are stored in a DLL called BitDll.dll.To create a DLL of this type, simplybuild a resource file containing the bitmaps you want to use and then add the RC file to a DLL project.You can create aDLL by choosing File | New | DLL from the BCB menu.Here is the RC code for a sample resource that contains multiple bitmaps:Back BITMAP "PANEL4.BMP"TileMap BITMAP "TILEMAP.BMP"City BITMAP "FLOOR1.BMP"Dirs BITMAP "COMPDIRS.BMP"Treasure BITMAP "TREASURE.BMP"Sage1 BITMAP "SAGE1.BMP"You can access the various bitmaps in the DLL by name.For example, you should set the BackgroundBitmapproperty of the THermesChart to Back and the TileMap property to TileMap.As long as the DLLName property ispointing to a valid DLL, you need do nothing else.All TScene descendants know how to read bitmaps from either a fileon disk or from a DLL.You'll discover several obvious advantages to using a DLL rather than a raw BMP file:You can ship one file with your game rather than a series of files.The DLL helps to conceal your bitmaps from prying eyes.Other developers will know how to get at them, but mostusers won't be able to find them.You can ship a series of DLLs with your projects and then load and unload the DLLs as needed to access variousdifferent bitmaps.If you switch scenes during the run of a program, the DLL associated with one scene will beunloaded from memory, and the DLL associated with the new scene will be loaded.The tiled world that you create depends on two interrelated binary files.One binary file contains the screen map that youcreate, and the second contains a list of creatures that inhabit the map [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • odbijak.htw.pl