Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions public/usage-examples/camera/move_camera_to-1-example-oop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using SplashKitSDK;

namespace MoveCameraToExample
{
public class Program
{
public static void Main()
{
// Open a window
Window window = SplashKit.OpenWindow("Move Camera To Example", 800, 600);

// Create a player bitmap and sprite
Bitmap playerBmp = SplashKit.CreateBitmap("player", 40, 40);
playerBmp.Clear(Color.BrightGreen);
Sprite player = SplashKit.CreateSprite(playerBmp);

// Position the player further out in the game world
player.X = 1000;
player.Y = 1000;

while (!window.CloseRequested)
{
// Handle input to move the player
SplashKit.ProcessEvents();

if (SplashKit.KeyDown(KeyCode.LeftKey)) player.X -= 5;
if (SplashKit.KeyDown(KeyCode.RightKey)) player.X += 5;
if (SplashKit.KeyDown(KeyCode.UpKey)) player.Y -= 5;
if (SplashKit.KeyDown(KeyCode.DownKey)) player.Y += 5;

// Center camera on player when SPACE is pressed
if (SplashKit.KeyTyped(KeyCode.SpaceKey))
{
// Calculate the top-left position for the camera to center the player
double targetX = player.X + player.Width / 2 - SplashKit.ScreenWidth() / 2;
double targetY = player.Y + player.Height / 2 - SplashKit.ScreenHeight() / 2;

// Move the camera to the calculated point
SplashKit.MoveCameraTo(targetX, targetY);
}

// Reset camera to origin when M is pressed
if (SplashKit.KeyTyped(KeyCode.MKey))
{
SplashKit.MoveCameraTo(0, 0);
}

// Clear the screen
window.Clear(Color.Black);

// Draw some world markers to visualize the camera move
window.FillRectangle(Color.White, 0, 0, 20, 20);
window.DrawText("World (0,0)", Color.White, 5, 25);

window.FillRectangle(Color.Red, 1000, 1000, 20, 20);
window.DrawText("World (1000,1000)", Color.Red, 1000, 1025);

// Draw the sprite (automatically uses camera offset)
player.Draw();

// Draw HUD (Heads-Up Display) directly to the screen
window.FillRectangle(Color.DimGray, 0, 0, 260, 80, SplashKit.OptionToScreen());
window.DrawText($"Camera Position: {SplashKit.PointToString(Camera.Position)}", Color.White, 10, 10, SplashKit.OptionToScreen());
window.DrawText($"Player World Pos: ({(int)player.X}, {(int)player.Y})", Color.White, 10, 30, SplashKit.OptionToScreen());
window.DrawText("Press SPACE to center on player", Color.White, 10, 50, SplashKit.OptionToScreen());
window.DrawText("Press M to move camera to (0,0)", Color.White, 10, 65, SplashKit.OptionToScreen());

window.Refresh(60);
}

SplashKit.CloseAllWindows();
}
}
}
66 changes: 66 additions & 0 deletions public/usage-examples/camera/move_camera_to-1-example-top-level.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Open a window
OpenWindow("Move Camera To Example", 800, 600);

// Create a player bitmap and sprite
Bitmap playerBmp = CreateBitmap("player", 40, 40);
ClearBitmap(playerBmp, ColorBrightGreen());
Sprite player = CreateSprite(playerBmp);

// Position the player further out in the game world
SpriteSetX(player, 1000);
SpriteSetY(player, 1000);

while (!QuitRequested())
{
// Handle input to move the player
ProcessEvents();

if (KeyDown(KeyCode.LeftKey)) SpriteSetX(player, SpriteX(player) - 5);
if (KeyDown(KeyCode.RightKey)) SpriteSetX(player, SpriteX(player) + 5);
if (KeyDown(KeyCode.UpKey)) SpriteSetY(player, SpriteY(player) - 5);
if (KeyDown(KeyCode.DownKey)) SpriteSetY(player, SpriteY(player) + 5);

// Center camera on player when SPACE is pressed
if (KeyTyped(KeyCode.SpaceKey))
{
// Calculate the top-left position for the camera to center the player
double targetX = SpriteX(player) + SpriteWidth(player) / 2 - ScreenWidth() / 2;
double targetY = SpriteY(player) + SpriteHeight(player) / 2 - ScreenHeight() / 2;

// Move the camera to the calculated point
MoveCameraTo(targetX, targetY);
}

// Reset camera to origin when M is pressed
if (KeyTyped(KeyCode.MKey))
{
MoveCameraTo(0, 0);
}

// Clear the screen
ClearScreen(ColorBlack());

// Draw some world markers to visualize the camera move
FillRectangle(ColorWhite(), 0, 0, 20, 20);
DrawText("World (0,0)", ColorWhite(), 5, 25);

FillRectangle(ColorRed(), 1000, 1000, 20, 20);
DrawText("World (1000,1000)", ColorRed(), 1000, 1025);

// Draw the sprite (automatically uses camera offset)
DrawSprite(player);

// Draw HUD (Heads-Up Display) directly to the screen
FillRectangle(ColorDimGray(), 0, 0, 260, 80, OptionToScreen());
DrawText($"Camera Position: {PointToString(CameraPosition())}", ColorWhite(), 10, 10, OptionToScreen());
DrawText($"Player World Pos: ({(int)SpriteX(player)}, {(int)SpriteY(player)})", ColorWhite(), 10, 30, OptionToScreen());
DrawText("Press SPACE to center on player", ColorWhite(), 10, 50, OptionToScreen());
DrawText("Press M to move camera to (0,0)", ColorWhite(), 10, 65, OptionToScreen());

RefreshScreen(60);
}

CloseAllWindows();
70 changes: 70 additions & 0 deletions public/usage-examples/camera/move_camera_to-1-example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "splashkit.h"

int main()
{
// Open a window
open_window("Move Camera To Example", 800, 600);

// Create a player bitmap and sprite
// This allows us to have an object to follow in the world
bitmap player_bmp = create_bitmap("player", 40, 40);
clear_bitmap(player_bmp, COLOR_BRIGHT_GREEN);
sprite player = create_sprite(player_bmp);

// Position the player further out in the game world
sprite_set_x(player, 1000);
sprite_set_y(player, 1000);

while (!quit_requested())
{
// Handle input to move the player
process_events();

if (key_down(LEFT_KEY)) sprite_set_x(player, sprite_x(player) - 5);
if (key_down(RIGHT_KEY)) sprite_set_x(player, sprite_x(player) + 5);
if (key_down(UP_KEY)) sprite_set_y(player, sprite_y(player) - 5);
if (key_down(DOWN_KEY)) sprite_set_y(player, sprite_y(player) + 5);

// Center camera on player when SPACE is pressed
if (key_typed(SPACE_KEY))
{
// Calculate the top-left position for the camera to center the player
double target_x = sprite_x(player) + sprite_width(player) / 2 - screen_width() / 2;
double target_y = sprite_y(player) + sprite_height(player) / 2 - screen_height() / 2;

// Move the camera to the calculated point
move_camera_to(target_x, target_y);
}

// Reset camera to origin when M is pressed
if (key_typed(M_KEY))
{
move_camera_to(0, 0);
}

// Clear the screen
clear_screen(COLOR_BLACK);

// Draw some world markers to visualize the camera move
fill_rectangle(COLOR_WHITE, 0, 0, 20, 20);
draw_text("World (0,0)", COLOR_WHITE, 5, 25);

fill_rectangle(COLOR_RED, 1000, 1000, 20, 20);
draw_text("World (1000,1000)", COLOR_RED, 1000, 1025);

// Draw the sprite (automatically uses camera offset)
draw_sprite(player);

// Draw HUD (Heads-Up Display) directly to the screen
fill_rectangle(COLOR_DIM_GRAY, 0, 0, 260, 80, option_to_screen());
draw_text("Camera Position: " + point_to_string(camera_position()), COLOR_WHITE, 10, 10, option_to_screen());
draw_text("Player World Pos: (" + std::to_string((int)sprite_x(player)) + ", " + std::to_string((int)sprite_y(player)) + ")", COLOR_WHITE, 10, 30, option_to_screen());
draw_text("Press SPACE to center on player", COLOR_WHITE, 10, 50, option_to_screen());
draw_text("Press M to move camera to (0,0)", COLOR_WHITE, 10, 65, option_to_screen());

refresh_screen(60);
}

close_all_windows();
return 0;
}
74 changes: 74 additions & 0 deletions public/usage-examples/camera/move_camera_to-1-example.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
program MoveCameraToExample;
uses
SplashKit;

var
PlayerBmp: Bitmap;
Player: Sprite;
TargetX, TargetY: Double;

begin
// Open a window
OpenWindow('Move Camera To Example', 800, 600);

// Create a player bitmap and sprite
PlayerBmp := CreateBitmap('player', 40, 40);
ClearBitmap(PlayerBmp, ColorBrightGreen());
Player := CreateSprite(PlayerBmp);

// Position the player further out in the game world
SpriteSetX(Player, 1000);
SpriteSetY(Player, 1000);

while not QuitRequested() do
begin
// Handle input to move the player
ProcessEvents();

if KeyDown(LeftKey) then SpriteSetX(Player, SpriteX(Player) - 5);
if KeyDown(RightKey) then SpriteSetX(Player, SpriteX(Player) + 5);
if KeyDown(UpKey) then SpriteSetY(Player, SpriteY(Player) - 5);
if KeyDown(DownKey) then SpriteSetY(Player, SpriteY(Player) + 5);

// Center camera on player when SPACE is pressed
if KeyTyped(SpaceKey) then
begin
// Calculate the top-left position for the camera to center the player
TargetX := SpriteX(Player) + SpriteWidth(Player) / 2 - ScreenWidth() / 2;
TargetY := SpriteY(Player) + SpriteHeight(Player) / 2 - ScreenHeight() / 2;

// Move the camera to the calculated point
MoveCameraTo(TargetX, TargetY);
end;

// Reset camera to origin when M is pressed
if KeyTyped(MKey) then
begin
MoveCameraTo(0, 0);
end;

// Clear the screen
ClearScreen(ColorBlack());

// Draw some world markers to visualize the camera move
FillRectangle(ColorWhite(), 0, 0, 20, 20);
DrawText('World (0,0)', ColorWhite(), 5, 25);

FillRectangle(ColorRed(), 1000, 1000, 20, 20);
DrawText('World (1000,1000)', ColorRed(), 1000, 1025);

// Draw the sprite (automatically uses camera offset)
DrawSprite(Player);

// Draw HUD (Heads-Up Display) directly to the screen
FillRectangle(ColorDimGray(), 0, 0, 260, 80, OptionToScreen());
DrawText('Camera Position: ' + PointToString(CameraPosition()), ColorWhite(), 10, 10, OptionToScreen());
DrawText('Player World Pos: (' + FloatToStr(SpriteX(Player)) + ', ' + FloatToStr(SpriteY(Player)) + ')', ColorWhite(), 10, 30, OptionToScreen());
DrawText('Press SPACE to center on player', ColorWhite(), 10, 50, OptionToScreen());
DrawText('Press M to move camera to (0,0)', ColorWhite(), 10, 65, OptionToScreen());

RefreshScreen(60);
end;

CloseAllWindows();
end.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions public/usage-examples/camera/move_camera_to-1-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from splashkit import *

# Open a window
open_window("Move Camera To Example", 800, 600)

# Create a player bitmap and sprite
player_bmp = create_bitmap("player", 40, 40)
clear_bitmap(player_bmp, color_bright_green())
player = create_sprite(player_bmp)

# Position the player further out in the game world
sprite_set_x(player, 1000)
sprite_set_y(player, 1000)

while not quit_requested():
# Handle input to move the player
process_events()

if key_down(KeyCode.left_key):
sprite_set_x(player, sprite_x(player) - 5)
if key_down(KeyCode.right_key):
sprite_set_x(player, sprite_x(player) + 5)
if key_down(KeyCode.up_key):
sprite_set_y(player, sprite_y(player) - 5)
if key_down(KeyCode.down_key):
sprite_set_y(player, sprite_y(player) + 5)

# Center camera on player when SPACE is pressed
if key_typed(KeyCode.space_key):
# Calculate the top-left position for the camera to center the player
target_x = sprite_x(player) + sprite_width(player) / 2 - screen_width() / 2
target_y = sprite_y(player) + sprite_height(player) / 2 - screen_height() / 2

# Move the camera to the calculated point
move_camera_to(target_x, target_y)

# Reset camera to origin when M is pressed
if key_typed(KeyCode.m_key):
move_camera_to(0, 0)

# Clear the screen
clear_screen(color_black())

# Draw some world markers to visualize the camera move
fill_rectangle(color_white(), 0, 0, 20, 20)
draw_text_no_font_no_size("World (0,0)", color_white(), 5, 25)

fill_rectangle(color_red(), 1000, 1000, 20, 20)
draw_text_no_font_no_size("World (1000,1000)", color_red(), 1000, 1025)

# Draw the sprite (automatically uses camera offset)
draw_sprite(player)

# Draw HUD (Heads-Up Display) directly to the screen
fill_rectangle(color_dim_gray(), 0, 0, 260, 80, option_to_screen())
draw_text_no_font_no_size_with_options(f"Camera Position: {point_to_string(camera_position())}", color_white(), 10, 10, option_to_screen())
draw_text_no_font_no_size_with_options(f"Player World Pos: ({int(sprite_x(player))}, {int(sprite_y(player))})", color_white(), 10, 30, option_to_screen())
draw_text_no_font_no_size_with_options("Press SPACE to center on player", color_white(), 10, 50, option_to_screen())
draw_text_no_font_no_size_with_options("Press M to move camera to (0,0)", color_white(), 10, 65, option_to_screen())

refresh_screen_with_target_fps(60)

close_all_windows()
1 change: 1 addition & 0 deletions public/usage-examples/camera/move_camera_to-1-example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This example demonstrates how to move the camera to a specific point using SplashKit. The camera centers on a player sprite when the spacebar is pressed, showing the camera's position on screen.
Loading