说明: delphi hge map editeur un exemple de code qui monter comment créer un map éditeur avec HGE.
[demo] delphi hge map editeur-----------------------------
Url : http://codes-sources.commentcamarche.net/source/54262-demo-delphi-hge-map-editeurAuteur : happy05Date : 06/08/2013
Licence :
=========
Ce document intitulé [demo] delphi hge map editeur issu de CommentCaMarche
(codes-sources.commentcamarche.net) est mis à disposition sous les termes de
la licence Creative Commons. Vous pouvez copier, modifier des copies de cette
source, dans les conditions fixées par la licence, tant que cette note
apparat clairement.
Description :
=============
un exemple de code qui monter comment créer un map éditeur avec HG
E.
pour telecharger HGE:
http://hge.relishgames.com/
S
ource / Exemple :
program Ti
leMap;
{
~ ~ $
$$$$
____ $$$$ $$$ $
$$$$
____|/| . ~ $$$$$$ $$$
$$$$$$$
____|/| . : . $$$ $$$$ $$$
$$$$ $
____|/| '. . : . .' $ $$$$ $$
$$ $$$
____|/| ._ '._.-'''-._.' _. $$$$$$$$
$$ $$$$$
____|/| '-..' '..-' $$$$$$
$$$$$$ $$$$ $$$$$
____|/| --._ /. .\ _.-- ~
$$$$$$$$$$$$$$$$$ $$$
____|/| '/ \
$$$ $$$$$$$$$$$$ $
____|/| -----| `|----
- $$ $$$$$$$$$$$$$$$$$
____|/| _: ;_
$$ $$$$ *** $$$ $$$$$
____|/| --' \
/ '-- $ $$$$ ** $$$$ $$$
____|/| _.-''.
.''-._ $$$ ** $$$ $$
____|/| '
.''-. .-''. ' $$ *** $$$ $
____|/|
.' ' : ' '. $$ ** $$$
____|/|
' : ' $ *** $$
____|/|
' $ *** $$
____|/|
Titre: HGE MAP EDITEUR *** $
____|/
| Auteur: H@PPyZER5 ***
____|/|
E-mail: happy05@programmer.net ***
____|/|
|^~^~^~^~^~^~***`~~^~^~^~^~^|
_
___|/| |____|_|____|_|____|_
|____|_|
____|/| |____|_|__
__|_|____|_|____|_|
____|/|----------------------------------------------------
-|____|_|____|_|____|_|____|_|-------------------------------
|~~^~^~^~^~^~^~^~
^~^`~~^~^~^~^~^~^~^~^~^`~~^~~^~^~^~^~^~^~^~^~^`~~^~^~^~^~^~^~^~^~^`~~^~~^~^~^~^~
^~^~^~^~^`~~^~^~^`~~^~^
|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|
_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|
|____|_|____|
###___###___########_|_###_|_|###_|_|###_|_|___###_|#########_|_#######____|_|__
__|__|____|_|____|_|____|_|
|____|_|____|# #___# #_|# ##_|__|___# #_|_|# #_|__#
#_#_|_#_# #__# #_|_# #__# #___# #_|___|_|_____|____|_|____|_|____|_|
|____|_|_
___|# ##### #__# #_|_####__# #_|_|# #_|__# #__###__# #|_# ##### #_|# ######___|_
|____|___|____|_|____|_|____|_|
|____|_|____|# #___# #|_# #|__##_#_|# ###### #_
|_|# #__|#___# #__# #___# #_|# #_|__|____|_|____|__|____|_|____|_|____|_|
|____
|_|____|###___###__|######|_#__##########_|__###___|___###_|###_|_###__###__|_|_
___|_|____|__|____|_|____|_|____|_|
|____|_|____|_|____|_|____|_|____||____|_|_
___|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|__|
}
//-----------------------------------
{$R *.res}
uses
Windows,SysUtils,
Classes,Messages,Graphics,HGEImages,HGECanvas,HGEDef,
HGE,HGESpriteEngine,HGE
Sprite,bass;
{
==> http://hge.relishgames.com pour:HGE,HGEImages,HGECanvas,HGEDef,
HGESprit
eEngine,HGESprite,...
}
//-----------------------------------
type
TMa
pRec = record //...MapData: array of TMapRec;
X, Y : Integer; //position X,
Y
ImageName: string[10]; //nom de l'image
end;
//------------------------
-----------
TMapcellule=class(TSpriteEx) //cellule de map
public /
/Public declarations
procedure DoMove(const MoveCount: Single); override;
//bouger
procedure OnLMouseDown; override; //souris en bas
procedure O
nMouseLeave; override; // souris laisser
procedure OnMouseEnter; override;
// souris entrer
procedure OnMouseDrag; override; // souris traner
end;
//-----------------------------------
//----le ID pour la forme
const
ClassName = 'SimpleWinClass'; //
AppName = 'Tools'; //
//--------------
---------------------
{#### #### ## ##########
## ##
#### ## ##
## ## ## ## ## ##
## ##
## ## ########
## ## ######## ## ##
## ##
## ## ## ##
##### ##### #### #### ####}
var
HGE: IHGE = nil;//IHGE = interface
Images: THGEimages; //HGEImages
Canvas:
THGECanvas;//HGECanvas
Font: TSysFont;//HGE Font
SpriteEngine: TSpriteEng
ine;//HGESpriteEngine
Fs: TFileStream; //Fs:=TFileStream.Create(ExtractFilePa
th(ParamStr(0)) + FileName, fmCreate);
MapData: array of TMapRec;//tableau
UserRefreshRate, //MonitorFrequency
WorldX,//position x
WorldY,//position
y
OffsetW,OffsetH,//offset Width,Height
FileSize,//FileSize
i,//
Mo
nitorFrequency:Integer;
Mx, My,//mouse position x,y
Layer1Pos,Layer2Pos: /
/background
Single;
msg: TMsg; //message
DC: THandle;//GetDC(HGE.System
_GetState(HGE_HWND));
wndtools: HWND;//HWND = type LongWord;
//-------------
-------------
//----OnMouseEnter
procedure TMapcellule.OnMouseEnter;
begin
SetColor(255,150,255); //
end;
//----OnMouseLeave
procedure TMapce
llule.OnMouseLeave;
begin
Setcolor(255,255,255); //
end;
//----OnLMou
seDown
procedure TMapcellule.OnLMouseDown;
begin
// calculate final pixel off
set
OffsetW:= Round(X-Mx); //render OffsetW
OffsetH:= Round(Y-My); //r
ender OffsetH
X:= Mx + OffsetW; //position x
Y:= My + OffsetH;
//position y
end;
//----OnMouseDrag
procedure TMapcellule.OnMouseDrag;
begin
X:= Mx+OffsetW; //position x
Y:= My+OffsetH; //position y
e
nd;
//----DoMove
procedure TMapcellule.DoMove(const MoveCount: Single);
beg
in
DoMouseEvent;//OnMouseEnter,OnMouseDrag,...
ActiveRect:=Rect(Roun
d(X), Round(Y), Round(X+40), Round(Y+40));//HGESpriteEngine
end;
//-----------
---------------
{ __________________
| | | | ###### #
#### #### ############
| \ | | ## ### ##
## ##
| \____________| | ## ## ## ## ## ##
##
| | ##### ## ## ## ## ########
|
_______ | ## ######## ## ## ## ##
\ | _
| | ## ## ## ## ## ##
\___\_|_|___|_____| ######
###### ##### ##### ############}
//----SaveMap
procedure SaveMap
(FileName: string);
begin
Fs := TFileStream.Create(ExtractFilePath(ParamS
tr(0)) + FileName, fmCreate);//TFileStream class Create(AFilename, Mode, 0);
SetLength(MapData, SpriteEngine.Count);//System.Pointer,System.Byte
for
i := 0 to SpriteEngine.Count - 1 do
begin
MapData[i].ImageName:=Spr
iteEngine[i].ImageName; //ImageName
MapData[i].X:=Trunc(SpriteEngine[i].X)
; //position x
MapData[i].Y:=Trunc(SpriteEngine[i].Y); //position y
end;
FileSize:=SpriteEngine.Count;
Fs.WriteBuffer(FileSize, Size
Of(FileSize));//FileSize
WorldX:=Trunc(SpriteEngine.WorldX); //WorldX
WorldY:=Trunc(SpriteEngine.WorldY); //Worldy
Fs.WriteBuffer(Worl
dX, SizeOf(WorldX)); //x Voide Type,System.Integer
Fs.WriteBuffer(WorldY,
SizeOf(WorldY)); //y Voide Type,System.Integer
Fs.WriteBuffer(MapData[0],
SizeOf(TMapRec) * FileSize); //MapData[0], SizeOf(TMapRec) * FileSize
Fs.D
estroy; //nettoyer
end;
{
/\
//\\
// \\
// \\
(/_
_ __\)
||
|| loadMAP
}
//---------------
procedure LoadMap(Fil
eName: string);
begin
Fs := TFileStream.Create(ExtractFilePath(ParamStr(0
)) + FileName, fmOpenRead);
Fs.ReadBuffer(FileSize, SizeOf(FileSize));//Re
adBuffer
Fs.ReadBuffer(WorldX, SizeOf(WorldX));
Fs.ReadBuffer(WorldY
, SizeOf(WorldY));
SetLength(MapData, FileSize);
Fs.ReadBuffer(MapDa
ta[0], SizeOf(TMapRec) * FileSize);
Fs.Destroy;
end;
{
CreateMap
}
//
---------------
procedure CreateMap();
var
Tile: array of TMapcellule;//
tableau
begin
SetLength(Tile, FileSize);
for i := 0 to FileSize-1 d
o //FileSize - 200 //1355
begin
Tile[i]:= TMapcellule.Create(
SpriteEngine); // SpriteEngine
Tile[i].ImageName := MapData[i].ImageN
ame; //ImageName
Tile[i].Width := Tile[i].PatternWidth; //Width
Tile[i].Height := Tile[i].PatternHeight; //Height
Tile[i].X
:= MapData[i].X;// + OffsetX;
Tile[i].Y := MapData[i].Y;// + OffsetY;
end;
SpriteEngine.WorldX:=WorldX;
SpriteEngine.WorldY:=WorldY;
end;
{
____________
| IMG /__\
| ________ |
| |. ,;, .:| |
| |.('_'
)::| |
| |../_\.::| |
| |._| |_.:| |
| |________| |
|____________| LoadImage
s
}
//---------------
procedure LoadImages; //
var
FileSearchRec: TSear
chRec; //
begin
if FindFirst(ExtractFilePath(ParamStr(0)) + 'Gfx\'+ '*.png'
, faAnyfile, FileSearchRec) = 0 then
repeat
Images.LoadFromFile('Gfx\'+Fil
eSearchRec.Name);
until FindNext(FileSearchRec) <> 0;
FindClose(Fil
eSearchRec);
Images.LoadFromFile('Idle.png',110, 118);
Images.LoadFromFile
('Walk.png',110, 118);
Images.LoadFromFile('Jump.png',110, 118);
Images.Lo
adFromFile('Dead.png',110, 118);
Images.LoadFromFile('Enemy1.png',50, 40);
Images.LoadFromFile('Enemy2.png',40, 40);
Images.LoadFromFile('Enemy3.png',4
8, 35);
Images.LoadFromFile('Spring1.png',48, 48);
end;
{################
###### ## #### #### ############
## ##
## #### #### ## ##
## ## ## ## ##
## #### #### ## ##
########## ######## ## ##
## ## ## ## ########
## ## ## ## ######## ## ##
## ## ## ##
## ## ## ## ## ## ## #
# ##
######## ######## ######## ########## ## ###### ########
####}
function FrameFunc: Boolean;
begin
GetMouseEvent; //
HGE.Input_GetMousePos(Mx, My); // Grab mouse position
if HGE.Input_GetKeyS
tate(HGEK_UP) then
SpriteEngine.WorldY := SpriteEngine.WorldY- 3;
if HG
E.Input_GetKeyState(HGEK_Down) then
SpriteEngine.WorldY := SpriteEngine.Wo
rldY+ 3;
if HGE.Input_GetKeyState(HGEK_Left) then
SpriteEngine.WorldX :
= SpriteEngine.WorldX- 3;
if HGE.Input_GetKeyState(HGEK_Right) then
Spr
iteEngine.WorldX := SpriteEngine.WorldX+ 3;
if SpriteEngine.WorldX <- 11
00 then
SpriteEngine.WorldX :=- 1100;
if SpriteEngine.WorldX > 248
0 then
SpriteEngine.WorldX := 2480;
if SpriteEngine.WorldY > -20 t
hen
SpriteEngine.WorldY := -20;
if SpriteEngine.WorldY < -1600 the
n
SpriteEngine.WorldY := -1600;
if HGE.Input_KeyUp(HGEK_S)then //S
begin
SaveMap('Stage.map'); //SaveMap
end;
case HGE.Input_GetKey of
HGEK_ESCAPE:
begin
FreeAndNil(Canvas);
FreeAndNil(Ima
ges);
FreeAndNil(SpriteEngine);
FreeAndNil(Font);
Result :=
True;
Exit;
end;
end;
Result := False;
end;
{##########
############ #### ################ ############ ##########
##
## ## #### ## ## ## ## ## #
#
## ## ## ## ## ## ## ## ## ## ##
## ##
######## ######## ## ## ## ## ## #####
### ########
## ## ## ## ## ## ## ## ##
## ## ## ##
## ## ## ## #### ##
## ## ## ##
######## ############## ###### ##
########## ############ ######## ####}
function RenderFunc: Boolean;
begin
HGE.Gfx_BeginScene; // Begin rendering quads.
// This function m
ust be called
// before any actual rendering.
HGE.Gfx_Clear(0); // Clear s
creen with black color
Canvas.Draw(Images.Image['Back3'],0,0,0,Blend_Default)
;
Canvas.DrawPart(Images.Image['Back2'],0,310,Trunc(Layer2Pos)-***0,0,800,248,
$FFFFFFFF,Blend_Default);
Canvas.DrawPart(Images.Image['Back1'],0,330,Trunc(L
ayer1Pos)-***0,0,800,240,$FFFFFFFF,Blend_Default);
SpriteEngine.Draw;
Sprit
eEngine.Move(1);
// Font.Print(100,100,IntToStr(UserRefreshRate));
HGE.Gfx_
EndScene; //End rendering and update the screen
Result := False; //return f
alse
end;
{#### #### ## ###### #### ######
##
## ## #### ## #### ##
#### ####
## ## ## ## ## ##
## ## ## ## ## ## ## ##
## ##
## ## ## ## ######## ## ## ## ##
##
## ## ## ## ## ## ####
###### ## ###### ######
########## ###### ##}
//this defines the window procedure for our fram
e window
function WndProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARA
M): LRESULT; stdcall;
begin
Result := 0;
if uMsg = WM_DESTROY then
P
ostQuitMessage(0)
else
Result := DefWindowProc(hWnd, uMsg, wParam, lPara
m);
end;
var
wchge: TWndClassEx = (
lpfnWndProc : @WndProc;
lp
szClassName: ClassName;
);
wctools: TWndClassEx = (
cbSize : Size
Of(TWndClassEx); //class Size
style : CS_HREDRAW or CS_VREDRA
W; //lass styles
lpfnWndProc : @WndProc; //default win
dow procedure
cbClsExtra : 0; //class memory
cbWndExtra : 0; //window memory
hbrBackground:
HBRUSH(COLOR_BTNFACE + 1); //color
lpszMenuName : nil;
//menu
lpszClassName: ClassName; //registered class name
hIconSm : 0; //icon
);
begin //MAIN Pr
ogram Begin
//-----------------------------------------------------------
wctools.hInstance := HInstance; //application instance
wctools.hIcon :=
LoadIcon(0, IDI_APPLICATION);//icon
wctools.hCursor := LoadCursor(0, IDC_A
RROW); //cursor
RegisterClassEx(wctools); //Register
the Window Class
wndtools := CreateWindowEx(0, //styles
ClassName, //class name
App
Name, //title bar text
WS_OVERLAPPEDWINDOW,/
/normal window style
Integer(20), //horizontal p
osition
Integer(50), //vertical position
Integer(200), //width
Integer(5
00), //height
0, //owner window}
0, //menu}
H
Instance, //application instance
nil);
//additional information
//---------------------
//----TLabel d
ans la VCL
CreateWindow('Static', 'coming soon...',
WS_VISIBLE or
WS_CHILD, 10, 12, 300, 20, wndtools, 0,HInstance, nil);
//---------------------
---------
//----affichage
ShowWindow(wndtools, SW_SHOWNORMAL);
UpdateWi
ndow(wndtools);
//------------------------------
//----HGE
HGE := HGECreate
(HGE_VERSION);//Get HGE interface
//----frame function, render function et wi
ndow title ...
HGE.System_SetState(HGE_FRAMEFUNC,FrameFunc);//function FrameF
unc: Boolean;
HGE.System_SetState(HGE_RENDERFUNC,RenderFunc);//function Rende
rFunc: Boolean;
HGE.System_SetState(HGE_USESOUND, False);//USESOUND:=False
HGE.System_SetState(HGE_WINDOWED,true);//FormStyle
HGE.System_SetState(HGE_T
ITLE,'HAG MAP');//window title
//----video mode
HGE.System_SetState(HGE_SC
REENWIDTH, 800);//Wih
HGE.System_SetState(HGE_SCREENHEIGHT,600);//Heighdtt
HGE.System_SetState(HGE_SCREENBPP, 16);//BPP
HGE.System_SetState(HGE_TEXTURE
FILTER, False);//TEXTUREFILTER:=False
HGE.System_SetState(HGE_FPS,HGEFPS_VSYN
C);//frame function
HGE.System_SetState(HGE_HIDEMOUSE, False); //HIDEMOUSE:=F
alse
//----------------------------
Canvas := THGeCanvas.Create;//
Image
s := THGEImages.Create;//
SpriteEngine := TSpriteEngine.Create(nil); //
Sp
riteengine.Images := Images; //
SpriteEngine.Canvas := Canvas; //
DC
:= GetDC(HGE.System_GetState(HGE_HWND) ); //
MonitorFrequency := GetDeviceCa
ps(DC, VREFRESH);//
case MonitorFrequency of //
60..70:UserRefre
shRate:=60;
71..75:UserRefreshRate:=75;
76..85:UserRefresh
Rate:=85;
86..100:UserRefreshRate:=100;
101..120:UserRefre
shRate:=120;
end;
if (HGE.System_Initiate) then //
begin
//----char
gement de sound et texture...
Font:=TSysFont.Create; //
Font.CreateF
ont('arial',15,[]);//
LoadImages;//LoadImages
LoadMap('Stage1-1.map');
//Load map
CreateMap; //Create new instance of tilemap.
hge.System_GetState(HGE_HWND); //Returns internal system state value.
HGE
.System_Start; //S-t-a-r-t ...
end
else
begin
// If one of the data f
iles is not found, display
// an error message and shutdown.
MessageBo
x(0,PChar(HGE.System_GetErrorMessage),'Error',MB_OK or MB_ICONERROR or MB_SYSTEM
MODAL);
HGE.System_Shutdown; //Clean up and shutdown
HGE := nil; //
end;
while True do
begin
if not GetMessage(msg, 0, 0, 0) then //{the
standard message loop}
Break; //pause
TranslateMessage(
msg); //traduire message(msg) traduire n'importe quel WM_KEYDOWN clavier Msg ver
s un WM_CHAR message
DispatchMessage(msg); //envoyer message(msg) envoyer M
sg à l'adresse de "Window Procedure"
... ...