Skip to content

Commit

Permalink
samples/core - separate pas files
Browse files Browse the repository at this point in the history
  • Loading branch information
turborium committed Dec 23, 2022
1 parent c4e023e commit 1b683ca
Show file tree
Hide file tree
Showing 26 changed files with 3,568 additions and 0 deletions.
165 changes: 165 additions & 0 deletions examples/core/core_2d_camera/core_2d_camera_src.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
(*******************************************************************************************
*
* raylib [core] example - 2d camera
*
* Example originally created with raylib 1.5, last time updated with raylib 3.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2016-2022 Ramon Santamaria (@raysan5)
*
********************************************************************************************)
unit core_2d_camera_src;

{$IFDEF FPC}{$MODE DELPHIUNICODE}{$ENDIF}

interface

uses
raylib;

procedure Main();

implementation

const
MAX_BUILDINGS = 100;

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
procedure Main();
const
ScreenWidth = 800;
ScreenHeight = 450;
var
Player: TRectangle;
Camera: TCamera2D;
Buildings: array [0..MAX_BUILDINGS - 1] of TRectangle;
BuildColors: array [0..MAX_BUILDINGS - 1] of TColor;
Spacing: Integer;
I: Integer;

begin
// Initialization
//---------------------------------------------------------------------------------------------
SetConfigFlags(FLAG_WINDOW_HIGHDPI or FLAG_MSAA_4X_HINT);
InitWindow(ScreenWidth, ScreenHeight, UTF8String('raylib [core] example - 2d camera'));

Player := TRectangle.Create(400, 280, 40, 40);

Spacing := 0;

SetRandomSeed(9);
for I := 0 to MAX_BUILDINGS - 1 do
begin
Buildings[I].Width := GetRandomValue(50, 200);
Buildings[I].Height := GetRandomValue(100, 800);
Buildings[I].Y := ScreenHeight - 130.0 - Buildings[I].Height;
Buildings[I].X := -6000.0 + Spacing;

Spacing := Spacing + Trunc(Buildings[I].Width);

BuildColors[I] := TColor.Create(GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255);
end;

Camera := Default(TCamera2D);
Camera.Target := TVector2.Create(Player.X + 20.0, Player.Y + 20.0);
Camera.Offset := TVector2.Create(ScreenWidth / 2.0, ScreenHeight / 2.0);
Camera.Rotation := 0.0;
Camera.Zoom := 1.0;

SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//---------------------------------------------------------------------------------------------

// Main game loop
while not WindowShouldClose() do // Detect window close button or ESC key
begin
// Update
//-------------------------------------------------------------------------------------------
// Player movement
if IsKeyDown(KEY_RIGHT) then
Player.X := Player.X + 2
else if IsKeyDown(KEY_LEFT) then
Player.X := Player.X - 2;

// Camera target follows player
Camera.Target := TVector2.Create(Player.X + 20, Player.Y + 20);

// Camera rotation controls
if IsKeyDown(KEY_A) then
Camera.Rotation := Camera.Rotation - 1
else if IsKeyDown(KEY_S) then
Camera.Rotation := Camera.Rotation + 1;

// Limit camera rotation to 80 degrees (-40 to 40)
if Camera.Rotation > 40 then
Camera.Rotation := 40
else if Camera.Rotation < -40 then
Camera.Rotation := -40;

// Camera zoom controls
Camera.Zoom := Camera.Zoom + (GetMouseWheelMove() * 0.05);

if Camera.Zoom > 3.0 then
Camera.Zoom := 3.0
else if Camera.Zoom < 0.05 then
Camera.Zoom := 0.05;

// Camera reset (zoom and rotation)
if IsKeyPressed(KEY_R) then
begin
Camera.Zoom := 1.0;
Camera.Rotation := 0.0;
end;
//-------------------------------------------------------------------------------------------

// Draw
//-------------------------------------------------------------------------------------------
BeginDrawing();

ClearBackground(RAYWHITE);

BeginMode2D(Camera);

DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);

for I := 0 to MAX_BUILDINGS - 1 do
DrawRectangleRec(Buildings[I], BuildColors[I]);

DrawRectangleRec(Player, RED);

DrawLine(Trunc(Camera.Target.X), -ScreenHeight * 10, Trunc(Camera.Target.X), ScreenHeight * 10, GREEN);
DrawLine(-ScreenWidth * 10, Trunc(Camera.Target.Y), ScreenWidth * 10, Trunc(Camera.Target.Y), GREEN);

EndMode2D();

DrawText(UTF8String('SCREEN AREA'), 640, 10, 20, RED);

DrawRectangle(0, 0, ScreenWidth, 5, RED);
DrawRectangle(0, 5, 5, ScreenHeight - 10, RED);
DrawRectangle(ScreenWidth - 5, 5, 5, ScreenHeight - 10, RED);
DrawRectangle(0, ScreenHeight - 5, ScreenWidth, 5, RED);

DrawRectangle(10, 10, 250, 113, Fade(SKYBLUE, 0.5));
DrawRectangleLines(10, 10, 250, 113, BLUE);

DrawText(UTF8String('Free 2d camera controls:'), 20, 20, 10, BLACK);
DrawText(UTF8String('- Right/Left to move Offset'), 40, 40, 10, DARKGRAY);
DrawText(UTF8String('- Mouse Wheel to Zoom in-out'), 40, 60, 10, DARKGRAY);
DrawText(UTF8String('- A / S to Rotate'), 40, 80, 10, DARKGRAY);
DrawText(UTF8String('- R to reset Zoom and Rotation'), 40, 100, 10, DARKGRAY);

EndDrawing();
//-------------------------------------------------------------------------------------------
end;

// De-Initialization
//---------------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//---------------------------------------------------------------------------------------------
end;

end.

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
(*******************************************************************************************
*
* raylib [core] example - 2d camera mouse zoom
*
* Example originally created with raylib 4.2, last time updated with raylib 4.2
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2022 Jeffery Myers (@JeffM2501)
*
********************************************************************************************)
unit core_2d_camera_mouse_zoom_src;

{$IFDEF FPC}{$MODE DELPHIUNICODE}{$ENDIF}

interface

procedure Main();

implementation

uses
SysUtils,
raylib,
raymath,
rlgl;

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
procedure Main();
const
ScreenWidth = 800;
ScreenHeight = 450;
ZoomIncrement = 0.125;
var
Camera: TCamera2D;
Delta, MouseWorldPos: TVector2;
Wheel: Single;
begin
// Initialization
//---------------------------------------------------------------------------------------------
SetConfigFlags(FLAG_WINDOW_HIGHDPI or FLAG_MSAA_4X_HINT);

InitWindow(ScreenWidth, ScreenHeight, UTF8String('raylib [core] example - 2d camera mouse zoom'));

Camera := Default(TCamera2D);
Camera.Zoom := 1.0;

SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//---------------------------------------------------------------------------------------------

// Main game loop
while not WindowShouldClose() do // Detect window close button or ESC key
begin
// Update
//-------------------------------------------------------------------------------------------
// Translate based on mouse right click
if IsMouseButtonDown(MOUSE_BUTTON_RIGHT) then
begin
Delta := GetMouseDelta();
Delta := Vector2Scale(Delta, -1.0 / Camera.Zoom);

Camera.Target := Vector2Add(Camera.Target, Delta);
end;

// Zoom based on mouse wheel
Wheel := GetMouseWheelMove();
if Wheel <> 0 then
begin
// Get the world point that is under the mouse
MouseWorldPos := GetScreenToWorld2D(GetMousePosition(), Camera);

// Set the offset to where the mouse is
Camera.Offset := GetMousePosition();

// Set the target to match, so that the camera maps the world space point
// under the cursor to the screen space point under the cursor at any zoom
Camera.Target := mouseWorldPos;

Camera.Zoom := Camera.Zoom + (Wheel * ZoomIncrement);
if Camera.Zoom < ZoomIncrement then
Camera.Zoom := ZoomIncrement;
end;

//-------------------------------------------------------------------------------------------

// Draw
//-------------------------------------------------------------------------------------------
BeginDrawing();

ClearBackground(BLACK);

BeginMode2D(Camera);

// Draw the 3d grid, rotated 90 degrees and centered around 0,0
// just so we have something in the XY plane
rlPushMatrix();
rlTranslatef(0, 25*50, 0);
rlRotatef(90, 1, 0, 0);
DrawGrid(100, 50);
rlPopMatrix();

// Draw a reference circle
DrawCircle(100, 100, 50, YELLOW);

EndMode2D();

DrawText(UTF8String('Mouse right button drag to move, mouse wheel to zoom'), 10, 10, 20, WHITE);

EndDrawing();
//-------------------------------------------------------------------------------------------
end;

// De-Initialization
//---------------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//---------------------------------------------------------------------------------------------
end;

end.

Loading

0 comments on commit 1b683ca

Please sign in to comment.