📄 openglisoengine.pas
字号:
unit OpenGLIsoEngine;
{****************************************************************************
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is IsoEditMdi.
The Initial Developer of the Original Code is
Putoon
Contributor(s): Chris Bruner of Crystal Software (Canada) Inc.
******************************************************************************}
interface
uses
Classes, IsoEngine, OpenGL, Windows, isoMath, Graphics, SysUtils;
type
{ TOpenGLImageList }
TImageData = array[0..MaxListSize] of TColor;
PImageData = ^TImageData;
TImageInfo = record
Width: Integer;
Height: Integer;
ImageData: PImageData;
Name: string;
TransparentColor: TColor;
end;
TOpenGLImageList = class(TPersistent)
private
FImages: array of TImageInfo;
function GetCount: Integer;
function GetName(Index: Integer): string;
procedure SetName(Index: Integer; const Value: string);
function GetImage(Index: Integer): TImageInfo;
function GetTransparentColor(Index: Integer): TColor;
procedure SetTransparentColor(Index: Integer; const Value: TColor);
public
constructor Create; virtual;
destructor Destroy; override;
procedure LoadBMPFromStream(ImageIndex: Integer; Stream: TStream; const Version: string);
procedure SaveBMPToStream(ImageIndex: Integer; Stream: TStream; const Version: string);
function AddImage: Integer;
procedure LoadFromStream(Stream: TStream; const Version: string);
procedure SaveToStream(Stream: TStream; const Version: string);
property Count: Integer read GetCount;
property Names[Index: Integer]: string read GetName write SetName;
property Images[Index: Integer]: TImageInfo read GetImage;
property TransparentColors[Index: Integer]: TColor read GetTransparentColor write SetTransparentColor;
end;
{ TOpenGLIsoMap required an active rendering context, which should be in the
default (startup) state, this rendering context should be active when one
of the TOpenGLIsoMap methods is called. }
{ TOpenGLIsoMap }
TOpenGLIsoMap = class(TComponent)
private
FImageList: TOpenGLImageList;
FOnFlip: TNotifyEvent;
FHeight: Integer;
FWidth: Integer;
function GetMapHeight: Integer;
function GetMapWidth: Integer;
function _GetImageColor(ImageIndex: Integer): TColor;
function _GetCell(X, Y: TGridInt): TIsoCell;
function GetCellHeight: Integer;
function GetCellWidth: Integer;
function GetXOffset: Integer;
function GetYOffset: Integer;
procedure SetXOffset(const Value: Integer);
procedure SetYOffset(const Value: Integer);
function GetImageName(ImageIndex: integer): string;
procedure SetImageName(ImageIndex: integer; const Value: string);
function GetImageHeight(ImageIndex: integer): integer;
function GetImageWidth(ImageIndex: integer): integer;
function GetMapName: string;
procedure SetMapName(const Value: string);
function GetImageCount: integer;
function GetIsoExt: string;
function GetScrollXOffset: Integer;
function GetScrollYOffset: Integer;
procedure SetScrollXOffset(const Value: Integer);
procedure SetScrollYOffset(const Value: Integer);
procedure SetHeight(const Value: Integer);
procedure SetWidth(const Value: Integer);
protected
// IsoMap plugin methods.
procedure IsoMapCanDraw(var ResultBoolean: Boolean);
procedure IsoMapDrawImage(var IsoCell : TIsoCell; var ImageIndex : Integer; Cellx,Celly : TGridInt; x,y,Layer, PatternIndex : Integer);
procedure IsoMapDrawGrid(x,y : TGridInt; animation : Integer);
procedure IsoMapFlip;
procedure IsoMapCls;
procedure IsoMapGetImageCount(var ResultInt: Integer);
procedure IsoMapGetImageHeight(ImageIndex : integer; var ResultInt : Integer);
procedure IsoMapGetImageWidth(ImageIndex : integer; var ResultInt : Integer);
procedure IsoMapGetSurfaceHeight(var ResultInt : Integer);
procedure IsoMapGetSurfaceWidth(var ResultInt: Integer);
procedure IsoMapGetPixel(ImageIndex,x,y : Integer; var color : TColor);
procedure IsoMapLoadImageListFromStream(Stream : TStream; Version : string);
procedure IsoMapSaveImageListToStream(Stream : TStream; Version : string);
procedure IsoMapLoadBMPFromStream(Index : integer; Stream : TStream; Version : string);
procedure IsoMapSaveBmpToStream(Index : integer; Stream : TStream; Version : string);
procedure IsoMapGetImageTransparentColor(Index : Integer; var Color : TColor);
procedure IsoMapSetImageTransparentColor(Index : Integer; Color : TColor);
procedure IsoMapGetImageName(Index : Integer; var Name : string);
procedure IsoMapSetImageName(Index: Integer; Name: string);
public
FIsoMap: TIsoMap;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// IsoMap methods.
procedure DrawIsoMap();
procedure LoadFromFile(filename: string);
procedure SaveToFile(filename: string);
procedure SetMapSize(MaxX, MaxY: TGridInt);
procedure SetCellSize(const Width, Height: Integer);
procedure Cls; // clear the screen to background colour
// Contained objects.
property IsoMap: TIsoMap read FIsoMap write FIsoMap;
property ImageList: TOpenGLImageList read FImageList write FImageList;
// OpenGL control properties.
property OnFlip: TNotifyEvent read FOnFlip write FOnFlip;
property Width: Integer read FWidth write SetWidth;
property Height: Integer read FHeight write SetHeight;
// IsoMap properties.
property MapName : string read GetMapName Write SetMapName;
property MapWidth: Integer read GetMapWidth;
property MapHeight: Integer read GetMapHeight;
property ImageColor[ImageIndex: Integer]: TColor read _GetImageColor;
property Cell[X, Y: TGridInt]: TIsoCell read _GetCell;
property CellWidth: Integer read GetCellWidth;
property CellHeight: Integer read GetCellHeight;
property XOffset: Integer read GetXOffset write SetXOffset;
property YOffset: Integer read GetYOffset write SetYOffset;
property ImageName[ImageIndex: integer] : string read GetImageName Write SetImageName;
property ImageHeight[ImageIndex: integer]: integer read GetImageHeight;
property ImageWidth[ImageIndex: integer]: integer read GetImageWidth;
property ImageCount: integer read GetImageCount;
property Ext : string read GetIsoExt;
function CellAt(Point: TPoint): TCellsCoord; // what Cell is at a point in the "world"
property ScrollXOffset : Integer read GetScrollXOffset Write SetScrollXOffset;
property ScrollYOffset : Integer read GetScrollYOffset Write SetScrollYOffset;
end;
implementation
//uses
// glUtils;
{ TOpenGLIsoMap }
function TOpenGLIsoMap.CellAt(Point: TPoint): TCellsCoord;
begin
Result := IsoMap.CellAt(Point);
end;
procedure TOpenGLIsoMap.Cls;
begin
IsoMap.Cls;
end;
constructor TOpenGLIsoMap.Create(AOwner: TComponent);
begin
inherited;
FImageList := TOpenGLImageList.Create;
FIsoMap := TIsoMap.Create(Self);
IsoMap.OnCanDraw := IsoMapCanDraw;
IsoMap.OnDrawImage := IsoMapDrawImage;
IsoMap.OnDrawGrid := IsoMapDrawGrid;
IsoMap.OnFlip := IsoMapFlip;
IsoMap.OnCls := IsoMapCls;
IsoMap.OnGetImageCount := IsoMapGetImageCount;
IsoMap.OnGetImageWidth := IsoMapGetImageWidth;
IsoMap.OnGetImageHeight := IsoMapGetImageHeight;
IsoMap.OnGetSurfaceWidth := IsoMapGetSurfaceWidth;
IsoMap.OnGetSurfaceHeight := IsoMapGetSurfaceHeight;
IsoMap.OnGetPixel := IsoMapGetPixel;
IsoMap.OnLoadImageListFromStream := IsoMapLoadImageListFromStream;
IsoMap.OnSaveImageListToStream := IsoMapSaveImageListToStream;
IsoMap.OnLoadBmpFromStream := IsoMapLoadBMPFromStream;
IsoMap.OnSaveBmpToStream := IsoMapSaveBmpToStream;
IsoMap.OnGetImageTransparentColor := IsoMapGetImageTransparentColor;
IsoMap.OnSetImageTransparentColor := IsoMapSetImageTransparentColor;
IsoMap.OnGetImageName := IsoMapGetImageName;
IsoMap.OnSetImageName := IsoMapSetImageName;
// Set OpenGL state.
glViewport(0,0,Width,Height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(0.0,Width,Height,0.0,-1.0,1.0); // Create Ortho 640x480 View (0,0 At Top Left)
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
glColor3f(1.0, 1.0, 1.0);
end;
destructor TOpenGLIsoMap.Destroy;
begin
FreeAndNil(FIsoMap);
FreeAndNil(FImageList);
inherited;
end;
procedure TOpenGLIsoMap.DrawIsoMap;
begin
IsoMap.DrawIsoMap;
end;
function TOpenGLIsoMap.GetCellHeight: Integer;
begin
Result := IsoMap.CellHeight;
end;
function TOpenGLIsoMap.GetCellWidth: Integer;
begin
Result := IsoMap.CellWidth;
end;
function TOpenGLIsoMap.GetImageCount: integer;
begin
Result := FImageList.GetCount;
end;
function TOpenGLIsoMap.GetImageHeight(ImageIndex: integer): integer;
begin
Result := FImageList.Images[ImageIndex].Height;
end;
function TOpenGLIsoMap.GetImageName(ImageIndex: integer): string;
begin
Result := FImageList.Images[ImageIndex].Name;
end;
function TOpenGLIsoMap.GetImageWidth(ImageIndex: integer): integer;
begin
Result := FImageList.Images[ImageIndex].Width;
end;
function TOpenGLIsoMap.GetIsoExt: string;
begin
Result := '.DXM';
end;
function TOpenGLIsoMap.GetMapHeight: Integer;
begin
Result := IsoMap.MapHeight;
end;
function TOpenGLIsoMap.GetMapName: string;
begin
Result := IsoMap.MapName;
end;
function TOpenGLIsoMap.GetMapWidth: Integer;
begin
Result := IsoMap.MapHeight;
end;
function TOpenGLIsoMap.GetScrollXOffset: Integer;
begin
Result := IsoMap.ScrollXOffset;
end;
function TOpenGLIsoMap.GetScrollYOffset: Integer;
begin
Result := IsoMap.ScrollYOffset;
end;
function TOpenGLIsoMap.GetXOffset: Integer;
begin
Result := IsoMap.XOffset;
end;
function TOpenGLIsoMap.GetYOffset: Integer;
begin
Result := IsoMap.YOffset;
end;
procedure TOpenGLIsoMap.IsoMapCanDraw(var ResultBoolean: Boolean);
begin
{ DONE : Return False if you sometimes want to prevent drawing. }
ResultBoolean := True;
end;
procedure TOpenGLIsoMap.IsoMapCls;
begin
{ DONE : Clear the background. }
glClear(GL_COLOR_BUFFER_BIT);
end;
procedure TOpenGLIsoMap.IsoMapDrawGrid(x, y: TGridInt; animation: Integer);
begin
{ TODO : Draw Grid. }
end;
procedure TOpenGLIsoMap.IsoMapDrawImage(var IsoCell: TIsoCell;
var ImageIndex: Integer; Cellx, Celly: TGridInt; x, y, Layer,
PatternIndex: Integer);
begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -