[demo] delphi hge map editeur

Description

un exemple de code qui monter comment créer un map éditeur avec HGE.
pour telecharger HGE: http://hge.relishgames.com/

Source / Exemple :


program TileMap;
{
                                  ~          ~                      $$$$$
____                                                    $$$$       $$$   $$$$$
____|/|                        .                   ~     $$$$$$     $$$   $$$$$$$
____|/|                    .   :   .                    $$$ $$$$   $$$  $$$$    $
____|/|                '.   .  :  .   .'                $    $$$$ $$$$ $$$
____|/|             ._   '._.-'''-._.'   _.                     $$$$$$$$$$   $$$$$
____|/|                '-..'         '..-'                    $$$$$$$$$$$$ $$$$ $$$$$
____|/|             --._ /.           .\ _.--         ~      $$$$$$$$$$$$$$$$$     $$$
____|/|                '/               \                   $$$  $$$$$$$$$$$$        $
____|/|            -----|              `|-----              $$  $$$$$$$$$$$$$$$$$
____|/|                _:               ;_                 $$   $$$$ *** $$$  $$$$$
____|/|             --'  \             /  '--              $   $$$$   **  $$$$   $$$
____|/|               _.-''.         .''-._                    $$$     **   $$$    $$
____|/|              '    .''-.   .-''.    '                   $$      ***   $$$    $
____|/|                 .'   '  :  '   '.                      $$       **    $$$
____|/|                    '    :   '                          $        ***    $$
____|/|                         '                              $        ***    $$
____|/|  Titre: HGE MAP EDITEUR                                         ***    $
____|/|  Auteur: H@PPyZERØ5                                             ***
____|/|  E-mail: happy05@programmer.net                                 ***
____|/|                                                     |^~^~^~^~^~^~***`~~^~^~^~^~^|
____|/|                                                     |____|_|____|_|____|_|____|_|
____|/|                                                     |____|_|____|_|____|_|____|_|
____|/|-----------------------------------------------------|____|_|____|_|____|_|____|_|-------------------------------
|~~^~^~^~^~^~^~^~^~^`~~^~^~^~^~^~^~^~^~^`~~^~~^~^~^~^~^~^~^~^~^`~~^~^~^~^~^~^~^~^~^`~~^~~^~^~^~^~^~^~^~^~^`~~^~^~^`~~^~^
|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|
|____|_|____|###___###___########_|_###_|_|###_|_|###_|_|___###_|#########_|_#######____|_|____|__|____|_|____|_|____|_|
|____|_|____|# #___# #_|# ##_|__|___# #_|_|# #_|__# #_#_|_#_# #__# #_|_# #__# #___# #_|___|_|_____|____|_|____|_|____|_|
|____|_|____|# ##### #__# #_|_####__# #_|_|# #_|__# #__###__# #|_# ##### #_|# ######___|_|____|___|____|_|____|_|____|_|
|____|_|____|# #___# #|_# #|__##_#_|# ###### #_|_|# #__|#___# #__# #___# #_|# #_|__|____|_|____|__|____|_|____|_|____|_|
|____|_|____|###___###__|######|_#__##########_|__###___|___###_|###_|_###__###__|_|____|_|____|__|____|_|____|_|____|_|
|____|_|____|_|____|_|____|_|____||____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|_|____|__|

}
//-----------------------------------
{$R *.res}
uses
  Windows,SysUtils,Classes,Messages,Graphics,HGEImages,HGECanvas,HGEDef,
  HGE,HGESpriteEngine,HGESprite,bass;
  {
  ==> http://hge.relishgames.com pour:HGE,HGEImages,HGECanvas,HGEDef,
  HGESpriteEngine,HGESprite,...
  }
//-----------------------------------

type
  TMapRec = 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 OnMouseLeave; override; // souris laisser
    procedure OnMouseEnter; override; // souris entrer
    procedure OnMouseDrag; override; // souris traîner
  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: TSpriteEngine;//HGESpriteEngine
  Fs: TFileStream; //Fs:=TFileStream.Create(ExtractFilePath(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,//
  MonitorFrequency: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 TMapcellule.OnMouseLeave;
begin
     Setcolor(255,255,255); //
end;

//----OnLMouseDown
procedure TMapcellule.OnLMouseDown;
begin
// calculate final pixel offset
    OffsetW:= Round(X-Mx); //render OffsetW
    OffsetH:= Round(Y-My); //render 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
end;

//----DoMove
procedure TMapcellule.DoMove(const MoveCount: Single);
begin
     DoMouseEvent;//OnMouseEnter,OnMouseDrag,...
     ActiveRect:=Rect(Round(X), Round(Y), Round(X+40), Round(Y+40));//HGESpriteEngine
end;
//--------------------------
{ __________________
 | |             |  |   ######        #    ####        #### ############
 | \             |  |  ##            ###    ##         ##   ##
 |  \____________|  |  ##          ##  ##    ##       ##    ##    ##
 |                  |   #####     ##    ##    ##     ##     ########
 |     _______      |       ##    ########     ##   ##      ##    ##
 \    |  _    |     |       ##   ##      ##     ## ##       ##
  \___\_|_|___|_____|  ######  ######    #####  #####       ############}

//----SaveMap
procedure SaveMap(FileName: string);
begin
     Fs := TFileStream.Create(ExtractFilePath(ParamStr(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:=SpriteEngine[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, SizeOf(FileSize));//FileSize
     WorldX:=Trunc(SpriteEngine.WorldX);     //WorldX
     WorldY:=Trunc(SpriteEngine.WorldY);     //Worldy
     Fs.WriteBuffer(WorldX, 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.Destroy; //nettoyer
end;
{
      /\
     //\\
    //  \\
   //    \\
  (/__  __\)
      ||
      || loadMAP
}
//---------------
procedure LoadMap(FileName: string);
begin
     Fs := TFileStream.Create(ExtractFilePath(ParamStr(0)) + FileName, fmOpenRead);
     Fs.ReadBuffer(FileSize, SizeOf(FileSize));//ReadBuffer
     Fs.ReadBuffer(WorldX, SizeOf(WorldX));
     Fs.ReadBuffer(WorldY, SizeOf(WorldY));
     SetLength(MapData, FileSize);
     Fs.ReadBuffer(MapData[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 do   //FileSize - 200 //1355
     begin
          Tile[i]:= TMapcellule.Create(SpriteEngine); // SpriteEngine
          Tile[i].ImageName := MapData[i].ImageName; //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     /__\
|  ________  |
| |. ,;, .:| |
| |.('_')::| |
| |../_\.::| |
| |._| |_.:| |
| |________| |
|____________| LoadImages
}
//---------------
procedure LoadImages;   //
var
  FileSearchRec: TSearchRec;  //
begin
  if FindFirst(ExtractFilePath(ParamStr(0)) + 'Gfx\'+ '*.png', faAnyfile, FileSearchRec) = 0 then
  repeat
  Images.LoadFromFile('Gfx\'+FileSearchRec.Name);
  until  FindNext(FileSearchRec) <> 0;
  FindClose(FileSearchRec);
  Images.LoadFromFile('Idle.png',110, 118);
  Images.LoadFromFile('Walk.png',110, 118);
  Images.LoadFromFile('Jump.png',110, 118);
  Images.LoadFromFile('Dead.png',110, 118);
  Images.LoadFromFile('Enemy1.png',50, 40);
  Images.LoadFromFile('Enemy2.png',40, 40);
  Images.LoadFromFile('Enemy3.png',48, 35);
  Images.LoadFromFile('Spring1.png',48, 48);
end;

{######################            ##    ####          ####  ############
   ##          ##      ##        ####      ####        ##      ##
   ##      ##  ##      ##        ##  ##    ####      ####      ##    ##
   ##########  ########        ##    ##    ##  ##  ##  ##      ########
   ##      ##  ##    ##        ########    ##  ##  ##  ##      ##    ##
   ##          ##      ##    ##      ##    ##    ##    ##      ##
 ########    ########    ########    ##########  ##  ######  ############}

function FrameFunc: Boolean;
begin
GetMouseEvent;               //
  HGE.Input_GetMousePos(Mx, My);  // Grab mouse position
   if HGE.Input_GetKeyState(HGEK_UP) then
     SpriteEngine.WorldY := SpriteEngine.WorldY- 3;
  if HGE.Input_GetKeyState(HGEK_Down) then
     SpriteEngine.WorldY := SpriteEngine.WorldY+ 3;
  if HGE.Input_GetKeyState(HGEK_Left) then
     SpriteEngine.WorldX := SpriteEngine.WorldX- 3;
  if HGE.Input_GetKeyState(HGEK_Right) then
     SpriteEngine.WorldX :=  SpriteEngine.WorldX+ 3;
  if  SpriteEngine.WorldX <- 1100 then
      SpriteEngine.WorldX :=- 1100;
  if  SpriteEngine.WorldX > 2480 then
      SpriteEngine.WorldX := 2480;
  if  SpriteEngine.WorldY > -20 then
      SpriteEngine.WorldY := -20;
  if  SpriteEngine.WorldY < -1600 then
      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(Images);
      FreeAndNil(SpriteEngine);
      FreeAndNil(Font);
      Result := True;
      Exit;
    end;
  end;
  Result := False;
end;

{##########    ############  ####      ################      ############  ##########
   ##      ##    ##            ####      ##    ##      ##      ##            ##      ##
   ##      ##    ##    ##      ##  ##    ##    ##        ##    ##    ##      ##      ##
   ########      ########      ##  ##    ##    ##        ##    ########      ########
   ##    ##      ##    ##      ##    ##  ##    ##        ##    ##    ##      ##    ##
   ##      ##    ##            ##      ####    ##      ##      ##            ##      ##
 ########    ##############  ######      ##  ##########      ############  ########    ####}

function RenderFunc: Boolean;
begin
  HGE.Gfx_BeginScene;   // Begin rendering quads.
  // This function must be called
  // before any actual rendering.
  HGE.Gfx_Clear(0); // Clear screen with black color
  Canvas.Draw(Images.Image['Back3'],0,0,0,Blend_Default);
  Canvas.DrawPart(Images.Image['Back2'],0,310,Trunc(Layer2Pos)-640,0,800,248,$FFFFFFFF,Blend_Default);
  Canvas.DrawPart(Images.Image['Back1'],0,330,Trunc(Layer1Pos)-640,0,800,240,$FFFFFFFF,Blend_Default);
  SpriteEngine.Draw;
  SpriteEngine.Move(1);
 // Font.Print(100,100,IntToStr(UserRefreshRate));
  HGE.Gfx_EndScene;   //End rendering and update the screen
  Result := False; //return false
end;

{####          ####          ##    ######  ####      ######
   ####        ##          ####      ##      ####      ##
   ####      ####          ##  ##    ##      ##  ##    ##
   ##  ##  ##  ##        ##    ##    ##      ##  ##    ##
   ##  ##  ##  ##        ########    ##      ##    ##  ##
   ##    ##    ##      ##      ##    ##      ##      ####
 ######  ##  ######  ######    ##########  ######      ##}

//this defines the window procedure for our frame window
function WndProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  Result := 0;
  if uMsg = WM_DESTROY then
    PostQuitMessage(0)
  else
    Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
end;

var
  wchge: TWndClassEx = (
    lpfnWndProc  : @WndProc;
    lpszClassName: ClassName;
);

wctools: TWndClassEx = (
    cbSize       : SizeOf(TWndClassEx);        //class Size
    style        : CS_HREDRAW or CS_VREDRAW;   //lass styles
    lpfnWndProc  : @WndProc;                   //default window 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 Program Begin

//-----------------------------------------------------------
  wctools.hInstance := HInstance;   //application instance
  wctools.hIcon     := LoadIcon(0, IDI_APPLICATION);//icon
  wctools.hCursor   := LoadCursor(0, IDC_ARROW);   //cursor
  RegisterClassEx(wctools);                        //Register the Window Class

  wndtools := CreateWindowEx(0,             //styles
                        ClassName,          //class name
                        AppName,            //title bar text
                        WS_OVERLAPPEDWINDOW,//normal window style
                        Integer(20),        //horizontal position
                         Integer(50),       //vertical position
                        Integer(200),       //width
                        Integer(500),       //height
                        0,                  //owner window}
                        0,                  //menu}
                        HInstance,          //application instance
                        nil);               //additional information
   //---------------------

   //----TLabel dans la VCL
   CreateWindow('Static', 'coming soon...',
         WS_VISIBLE or WS_CHILD, 10, 12, 300, 20, wndtools, 0,HInstance, nil);
//------------------------------
  //----affichage
  ShowWindow(wndtools, SW_SHOWNORMAL);
  UpdateWindow(wndtools);
//------------------------------
//----HGE
  HGE := HGECreate(HGE_VERSION);//Get HGE interface
  //----frame function, render function et window title ...
  HGE.System_SetState(HGE_FRAMEFUNC,FrameFunc);//function FrameFunc: Boolean;
  HGE.System_SetState(HGE_RENDERFUNC,RenderFunc);//function RenderFunc: Boolean;
  HGE.System_SetState(HGE_USESOUND, False);//USESOUND:=False
  HGE.System_SetState(HGE_WINDOWED,true);//FormStyle
  HGE.System_SetState(HGE_TITLE,'HAG MAP');//window title
  //----video mode
  HGE.System_SetState(HGE_SCREENWIDTH, 800);//Wih
  HGE.System_SetState(HGE_SCREENHEIGHT,600);//Heighdtt
  HGE.System_SetState(HGE_SCREENBPP, 16);//BPP
  HGE.System_SetState(HGE_TEXTUREFILTER, False);//TEXTUREFILTER:=False
  HGE.System_SetState(HGE_FPS,HGEFPS_VSYNC);//frame function
  HGE.System_SetState(HGE_HIDEMOUSE, False); //HIDEMOUSE:=False
 //----------------------------
  Canvas := THGeCanvas.Create;//
  Images := THGEImages.Create;//
  SpriteEngine := TSpriteEngine.Create(nil); //
  Spriteengine.Images := Images;   //
  SpriteEngine.Canvas := Canvas;  //
  DC   := GetDC(HGE.System_GetState(HGE_HWND) );  //
  MonitorFrequency := GetDeviceCaps(DC, VREFRESH);//
  case MonitorFrequency of   //
          60..70:UserRefreshRate:=60;
          71..75:UserRefreshRate:=75;
          76..85:UserRefreshRate:=85;
          86..100:UserRefreshRate:=100;
          101..120:UserRefreshRate:=120;
end;
  if (HGE.System_Initiate) then  //
  begin
    //----chargement de sound et texture...

    Font:=TSysFont.Create; //
    Font.CreateFont('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 files is not found, display
    // an error message and shutdown.
    MessageBox(0,PChar(HGE.System_GetErrorMessage),'Error',MB_OK or MB_ICONERROR or MB_SYSTEMMODAL);
  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 vers un WM_CHAR message
    DispatchMessage(msg); //envoyer message(msg) envoyer Msg à l'adresse de "Window Procedure"
end;
  ExitCode := msg.wParam; //sortie

end.

Conclusion :


pour les Gfx je les ai trouvé la: http://relishgames.com/forum/

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.