Skip to content

Instantly share code, notes, and snippets.

@frostney
Created May 31, 2011 16:08
Show Gist options
  • Save frostney/1000772 to your computer and use it in GitHub Desktop.
Save frostney/1000772 to your computer and use it in GitHub Desktop.
Elysion - Custom template (If you don't want or need to use the default template shipped with Elysion)
(*
Elysion Custom Template
Compile with: fpc -Mdelphi myApplication.pas
Make sure the Elysion library source are added to the search paths of the compiler for example by compiling with fpc -Mdelphi -Fu./path/to/Elysion -Fi./path/to/Elysion myApplication.pas
(Created by Johannes Stein, 2011. This source code file is PUBLIC DOMAIN.)
*)
program myApplication;
// GUI mode on Windows
{$IFDEF FPC}
{$IFDEF WINDOWS}
{$APPTYPE GUI}
{$ENDIF}
{$ENDIF}
uses
ElysionApplication, //< This unit is always needed. It provides access to the application container and the current window(s)
ElysionGraphics, //< This unit provides access to graphical objects such as sprites and its derivates
ElysionTypes, //< Provides basic functions and data types
ElysionInput; //< This unit provides the Input API
const
// Window dimensions
WIDTH = 800;
HEIGHT = 480;
BITS = 32;
var
Sprite: TelSprite;
// Initialize the application and load assets
procedure Initialize();
begin
// Initializes the Elysion application container
Application.Initialize(WIDTH, HEIGHT, BITS);
// Show cursor
ActiveWindow.ShowCursor();
// Create sprite object
Sprite := TelSprite.Create;
// Make sure, myimage.png is found, it won't be drawn on the screen
Sprite.LoadFromFile('../path/to/myimage.png');
// Set position (makeV3f = make three-dimensional vector with float precision, the third and last parameter is optional and is set to 0 if nothing else has been specified)
Sprite.Position := makeV3f(50, 50); //< This sprite will now be drawn at X: 50 Y: 50 (Origin is the top-left corner)
end;
// Render/Draw procedure (All visual objects like sprites, text output, etc. should be drawn here)
procedure Draw();
begin
// Draw sprite
Sprite.Draw();
(**
* In this example we only have one object that needs to be drawn.
* Remember that we set the Z-coordinate to zero when we set the position in Initialize. If you have more than one visual object at the same Z-coordinate it the element that is drawn last might cover previously drawn elements. (Depending on how the X- and Y-coordinate have been defined.)
*
*)
end;
// Update procedure with DeltaTime parameter
// GameLogic should be handled here
procedure Update(dt: Double = 0.0);
begin
// Move sprite with arrow keys
if Input.Keyboard.isKeyDown(Key.Left) then Sprite.Left := Sprite.Left - 50 * dt;
if Input.Keyboard.isKeyDown(Key.Right) then Sprite.Left := Sprite.Left + 50 * dt;
if Input.Keyboard.isKeyDown(Key.Up) then Sprite.Top := Sprite.Top - 50 * dt;
if Input.Keyboard.isKeyDown(Key.Down) then Sprite.Top := Sprite.Top + 50 * dt;
(**
* Sprite.Left is the same as Sprite.Position.X
* Sprite.Top is the as Sprite.Position.Y
* Sprite.Left, Sprite.Top, Sprite.Right or Sprite.Bottom should be used for UI elements only, and Sprite.Position should be used for game-relevant elements such as actors.
*
* In this simple example it doesn't really matter which one we use, but keep this in mind for bigger projects, espacially if you plan on using cameras
*
*)
// Color the sprite
// Left-click: Random color
if Sprite.Click then
begin
Randomize;
Sprite.Color := makeCol(Random(255), Random(255), Random(255));
end;
// Right-click: Reset color
if Sprite.RightClick then Sprite.Color := makeCol(255, 255, 255);
// Quit application on ESC
if Input.Keyboard.isKeyHit(Key.Escape) then Application.Quit();
end;
begin
Initialize();
// Game Loop
while Application.Run do
begin
// Clears buffer
ActiveWindow.BeginScene();
// Render procedure
Draw();
// Update procedure
Update(ActiveWindow.DeltaTime);
// Flip surface
ActiveWindow.EndScene();
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment