Title: Custom resource files for games
Question: What I can do to make a custom resource file for a game ???
Answer:
//---------------------------------------------------------------------------
// Author : Digital Survivor [Esteban Rodrguez Nieto | Jos Plano]
// Email : plmad666@gmail.com | jose.plano@gmail
// Web site : www.ds-studios.com.ar
//---------------------------------------------------------------------------
Unit UPrincipal;
Interface
Uses
JPEG, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
Buttons, ImgList, ComCtrls, ToolWin, DirectoryTree;
// The JPEG unit is included with Delphi 4 & 5 and it makes possible that the
// Image control loads the JPG file
Type
TPrincipal = Class (TForm)
BJPG : TButton;
BRec : TButton;
Procedure BJPGClick (Sender : TObject);
Procedure BRecClick (Sender : TObject);
Private { Private declarations }
Public { Public declarations }
End;
Var
Principal : TPrincipal;
Implementation
{$R *.DFM}
Uses
DrvUtils;
Type
TJPGRecord = Record // This is an example record that stores name, size,
// date and the bytes of the JPG file
Name : String [255];
Date : String [10];
Size : LongWord; // In Delphi 4.0 use Cardinal instead of LongWord
ImgBytes : Pointer;
End;
Const
CRecFile = 'D:\Resource.Rec';
CJPGFile = 'D:\1.Jpg';
LenName = 255;
LenDate = 10;
Procedure TPrincipal.BJPGClick (Sender : TObject);
Var
FHandle : Integer;
Resource : TJPGRecord;
Begin
// This procedure stores any file directly into a record file
FHandle := FileOpen (CJPGFile, fmOpenRead); // Opens the JPG file and assign a handle
With Resource Do
Begin
Name := ExtractFileName (CJPGFile); // Fills with the name of the JPG file
Date := DateToStr (FileDateToDateTime (FileAge (CJPGFile))); // Fills with the date of the JPG file
Size := FileSeek (FHandle, 0, 2); // Fills with the size of the JPG file
GetMem (ImgBytes, Size); // Assign memory for the pointer
FileSeek (FHandle, 0, 0); // Moves to the begining of the file
FileRead (FHandle, ImgBytes^, Size); // Reads JPG File bytes
FileClose (FHandle); // Frees the handle of the JPG file
FHandle := FileCreate (CRecFile); // Creates an open a new record file
FileWrite (FHandle, Name, LenName); // Writes Name
FileWrite (FHandle, Date, LenDate); // Writes Date
FileWrite (FHandle, Size, SizeOf (Size)); // Writes Size
FileWrite (FHandle, ImgBytes^, Size); // Writes the JPG image
FileClose (FHandle);
FreeMem (ImgBytes, Size); // Frees the pointer
End;
End;
Procedure TPrincipal.BRecClick (Sender : TObject);
Var
FHandle : Integer;
Resource : TJPGRecord;
JPGImage : TJPEGImage;
MStream : TMemoryStream;
Begin
// This procedure reads the JPG file stored into the record file and display it
FHandle := FileOpen (CRecFile, fmOpenRead); // Opens the record file and assign a handle
With Resource Do
Begin
FileSeek (FHandle, 0, 0); // Moves to the begining of the file
FileRead (FHandle, Name, LenName); // Reads the name stored in the record file
FileRead (FHandle, Date, LenDate); // Reads the date stored in the record file
FileRead (FHandle, Size, SizeOf (Size)); // Reads the file size stored in the record file
GetMem (ImgBytes, Size); // Assign memory for the pointer
FileRead (FHandle, ImgBytes^, Size); // Reads the JPG file stored in the record file
FileClose (FHandle); // Frees the handle of the JPG file
MStream := TMemoryStream.Create; // Create the memory stream
MStream.Write (ImgBytes^, Size); // Fills the memory stream with the pointer
MStream.Seek (0, 0); // Moves to the begining of the memory stream
JPGImage := TJPEGImage.Create;
JPGImage.LoadFromStream (MStream);
//Image.Picture.Assign (JPGImage);
FreeMem (ImgBytes, Size); // Frees the pointer
JPGImage.Free; // Frees the object
MStream.Free; // Frees the stream
End;
End;
End.