diff --git a/public/usage-examples-images-gifs/graphics/option_scale_bmp-1-basic.gif b/public/usage-examples-images-gifs/graphics/option_scale_bmp-1-basic.gif new file mode 100644 index 000000000..44c9fa969 Binary files /dev/null and b/public/usage-examples-images-gifs/graphics/option_scale_bmp-1-basic.gif differ diff --git a/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic-oop.cs b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic-oop.cs new file mode 100644 index 000000000..d63155235 --- /dev/null +++ b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic-oop.cs @@ -0,0 +1,129 @@ +// I am scaling a bitmap at draw time with OptionScaleBmp (OOP form). +// I am pressing A to make smaller; I am pressing D to make bigger; I am pressing R to reset; +// I am pressing SPACE to toggle outline; I am pressing ESC to quit. + +using SplashKitSDK; + +namespace GraphicsExamples +{ + public class OptionScaleBmpDemo + { + private Bitmap _stickerBitmap; // I am keeping the sticker bitmap. + private double _currentScale = 1.0; // I am tracking the live scale. + private const double ScaleStep = 0.1; // I am changing scale in small steps. + private const double MinScale = 0.2; // I am clamping to a minimum scale. + private const double MaxScale = 3.0; // I am clamping to a maximum scale. + private bool _showOutline = true; // I am toggling a bounding outline. + + private static Bitmap MakeStickerBitmap() + { + // I am creating a small procedural bitmap so no external file is needed. + int stickerWidth = 128; + int stickerHeight = 128; + Bitmap bmp = SplashKit.CreateBitmap("sticker", stickerWidth, stickerHeight); + SplashKit.ClearBitmap(bmp, SplashKit.RGBAColor(255, 255, 255, 0)); + + // I am drawing a light grid to show scale changes. + for (int y = 0; y < stickerHeight; y += 16) + { + SplashKit.DrawLineOnBitmap(bmp, SplashKit.RGBColor(220, 220, 220), 0, y, stickerWidth, y); + } + for (int x = 0; x < stickerWidth; x += 16) + { + SplashKit.DrawLineOnBitmap(bmp, SplashKit.RGBColor(220, 220, 220), x, 0, x, stickerHeight); + } + + // I am drawing a circle and a border so the sticker is easy to see. + SplashKit.FillCircleOnBitmap(bmp, SplashKit.RGBColor(33, 150, 243), stickerWidth / 2, stickerHeight / 2, 36); + SplashKit.DrawCircleOnBitmap(bmp, SplashKit.ColorBlack(), stickerWidth / 2, stickerHeight / 2, 36); + SplashKit.DrawRectangleOnBitmap(bmp, SplashKit.ColorBlack(), 1, 1, stickerWidth - 2, stickerHeight - 2); + + return bmp; + } + + public OptionScaleBmpDemo() + { + // I am opening the window with a short title; I am drawing instructions inside the window. + SplashKit.OpenWindow("Option Scale Bmp - live", 800, 480); + _stickerBitmap = MakeStickerBitmap(); + } + + public void Run() + { + // I am centering my drawing around the window middle. + double centerX = 800 / 2.0; + double centerY = 480 / 2.0; + + while (!SplashKit.QuitRequested()) + { + SplashKit.ProcessEvents(); + + // I am handling the controls. + if (SplashKit.KeyTyped(KeyCode.EscapeKey)) + { + break; + } + if (SplashKit.KeyTyped(KeyCode.AKey)) + { + _currentScale = _currentScale - ScaleStep; + if (_currentScale < MinScale) + { + _currentScale = MinScale; + } + } + if (SplashKit.KeyTyped(KeyCode.DKey)) + { + _currentScale = _currentScale + ScaleStep; + if (_currentScale > MaxScale) + { + _currentScale = MaxScale; + } + } + if (SplashKit.KeyTyped(KeyCode.RKey)) + { + _currentScale = 1.0; + } + if (SplashKit.KeyTyped(KeyCode.SpaceKey)) + { + _showOutline = !_showOutline; + } + + // I am clearing the frame to white. + SplashKit.ClearScreen(SplashKit.ColorWhite()); + + // I am drawing the sticker centered with the current scale applied. + double drawX = centerX - SplashKit.BitmapWidth(_stickerBitmap) / 2.0; + double drawY = centerY - SplashKit.BitmapHeight(_stickerBitmap) / 2.0; + SplashKit.DrawBitmap(_stickerBitmap, drawX, drawY, SplashKit.OptionScaleBmp(_currentScale, _currentScale)); + + // I am drawing an outline that matches the scaled size. + if (_showOutline) + { + double outlineWidth = SplashKit.BitmapWidth(_stickerBitmap) * _currentScale; + double outlineHeight = SplashKit.BitmapHeight(_stickerBitmap) * _currentScale; + SplashKit.DrawRectangle(SplashKit.ColorNavy(), + centerX - outlineWidth / 2.0, + centerY - outlineHeight / 2.0, + outlineWidth, + outlineHeight); + } + + // I am drawing the UI hints. + SplashKit.DrawText("A: smaller D: bigger R: reset SPACE: outline ESC: quit", + SplashKit.RGBColor(0, 0, 128), 16, 16); + SplashKit.DrawText("Scale: " + _currentScale.ToString("0.0") + " x", + SplashKit.ColorBlack(), 16, 40); + + SplashKit.RefreshScreen(60); + } + + // I am freeing the bitmap before I quit. + SplashKit.FreeBitmap(_stickerBitmap); + } + + public static void Main() + { + new OptionScaleBmpDemo().Run(); + } + } +} \ No newline at end of file diff --git a/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.cpp b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.cpp new file mode 100644 index 000000000..614af6364 --- /dev/null +++ b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.cpp @@ -0,0 +1,123 @@ +// I am scaling a bitmap at draw time with option_scale_bmp. +// I am pressing A to make smaller; I am pressing D to make bigger; I am pressing R to reset; +// I am pressing SPACE to toggle outline; I am pressing ESC to quit. + +#include "splashkit.h" +#include + +static bitmap make_sticker_bitmap() +{ + // I am creating a small procedural bitmap so no external file is needed. + const int stickerWidth = 128; + const int stickerHeight = 128; + bitmap bmp = create_bitmap("sticker", stickerWidth, stickerHeight); + clear_bitmap(bmp, rgba_color(255, 255, 255, 0)); // I am making the background transparent. + + // I am drawing a light grid to show scale changes. + for (int y = 0; y < stickerHeight; y += 16) + { + draw_line_on_bitmap(bmp, rgb_color(220, 220, 220), 0, y, stickerWidth, y); + } + for (int x = 0; x < stickerWidth; x += 16) + { + draw_line_on_bitmap(bmp, rgb_color(220, 220, 220), x, 0, x, stickerHeight); + } + + // I am drawing a circle and a border so the sticker is easy to see. + fill_circle_on_bitmap(bmp, rgb_color(33, 150, 243), stickerWidth / 2, stickerHeight / 2, 36); + draw_circle_on_bitmap(bmp, COLOR_BLACK, stickerWidth / 2, stickerHeight / 2, 36); + draw_rectangle_on_bitmap(bmp, COLOR_BLACK, 1, 1, stickerWidth - 2, stickerHeight - 2); + + return bmp; +} + +int main() +{ + // I am opening the window with a short title; I am drawing instructions inside the window. + open_window("Option Scale Bmp - live", 800, 480); + + // I am building the sticker once and I am reusing it each frame. + bitmap stickerBitmap = make_sticker_bitmap(); + + // I am tracking the current scale and the valid range. + double currentScale = 1.0; + const double scaleStep = 0.1; + const double minScale = 0.2; + const double maxScale = 3.0; + + // I am toggling an outline so the scaled bounds are obvious. + bool showOutline = true; + + // I am centering my drawing around the window middle. + const double centerX = 800 / 2.0; + const double centerY = 480 / 2.0; + + while (!quit_requested()) + { + process_events(); + + // I am handling the controls. + if (key_typed(ESCAPE_KEY)) + { + break; + } + if (key_typed(A_KEY)) + { + currentScale = currentScale - scaleStep; + if (currentScale < minScale) + { + currentScale = minScale; + } + } + if (key_typed(D_KEY)) + { + currentScale = currentScale + scaleStep; + if (currentScale > maxScale) + { + currentScale = maxScale; + } + } + if (key_typed(R_KEY)) + { + currentScale = 1.0; + } + if (key_typed(SPACE_KEY)) + { + showOutline = !showOutline; + } + + // I am clearing the frame to white. + clear_screen(COLOR_WHITE); + + // I am drawing the sticker centered with the current scale applied. + const double drawX = centerX - bitmap_width(stickerBitmap) / 2.0; + const double drawY = centerY - bitmap_height(stickerBitmap) / 2.0; + draw_bitmap(stickerBitmap, drawX, drawY, option_scale_bmp(currentScale, currentScale)); + + // I am drawing an outline that matches the scaled size. + if (showOutline) + { + const double outlineWidth = bitmap_width(stickerBitmap) * currentScale; + const double outlineHeight = bitmap_height(stickerBitmap) * currentScale; + draw_rectangle(COLOR_NAVY, + centerX - outlineWidth / 2.0, + centerY - outlineHeight / 2.0, + outlineWidth, + outlineHeight); + } + + // I am drawing the UI hints. + draw_text("A: smaller D: bigger R: reset SPACE: outline ESC: quit", + rgb_color(0, 0, 128), 16, 16); + + char info[96]; + std::snprintf(info, sizeof(info), "Scale: %.1f x", currentScale); + draw_text(info, rgb_color(0, 0, 0), 16, 40); + + refresh_screen(60); + } + + // I am freeing the bitmap before I quit. + free_bitmap(stickerBitmap); + return 0; +} \ No newline at end of file diff --git a/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.cs b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.cs new file mode 100644 index 000000000..d45d83d35 --- /dev/null +++ b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.cs @@ -0,0 +1,116 @@ +// I am scaling a bitmap at draw time with OptionScaleBmp. +// I am pressing A to make smaller; I am pressing D to make bigger; I am pressing R to reset; +// I am pressing SPACE to toggle outline; I am pressing ESC to quit. + +using SplashKitSDK; +using static SplashKitSDK.SplashKit; + +static Bitmap MakeStickerBitmap() +{ + // I am creating a small procedural bitmap so no external file is needed. + int stickerWidth = 128; + int stickerHeight = 128; + Bitmap bmp = CreateBitmap("sticker", stickerWidth, stickerHeight); + ClearBitmap(bmp, RGBAColor(255, 255, 255, 0)); // I am making the background transparent. + + // I am drawing a light grid to show scale changes. + for (int y = 0; y < stickerHeight; y += 16) + { + DrawLineOnBitmap(bmp, RGBColor(220, 220, 220), 0, y, stickerWidth, y); + } + for (int x = 0; x < stickerWidth; x += 16) + { + DrawLineOnBitmap(bmp, RGBColor(220, 220, 220), x, 0, x, stickerHeight); + } + + // I am drawing a circle and a border so the sticker is easy to see. + FillCircleOnBitmap(bmp, RGBColor(33, 150, 243), stickerWidth / 2, stickerHeight / 2, 36); + DrawCircleOnBitmap(bmp, ColorBlack(), stickerWidth / 2, stickerHeight / 2, 36); + DrawRectangleOnBitmap(bmp, ColorBlack(), 1, 1, stickerWidth - 2, stickerHeight - 2); + + return bmp; +} + +// I am opening the window with a short title; I am drawing instructions inside the window. +OpenWindow("Option Scale Bmp - live", 800, 480); + +// I am building the sticker once and I am reusing it each frame. +Bitmap stickerBitmap = MakeStickerBitmap(); + +// I am tracking the current scale and the valid range. +double currentScale = 1.0; +const double scaleStep = 0.1; +const double minScale = 0.2; +const double maxScale = 3.0; + +// I am toggling an outline so the scaled bounds are obvious. +bool showOutline = true; + +// I am centering my drawing around the window middle. +double centerX = 800 / 2.0; +double centerY = 480 / 2.0; + +while (!QuitRequested()) +{ + ProcessEvents(); + + // I am handling the controls. + if (KeyTyped(KeyCode.EscapeKey)) + { + break; + } + if (KeyTyped(KeyCode.AKey)) + { + currentScale = currentScale - scaleStep; + if (currentScale < minScale) + { + currentScale = minScale; + } + } + if (KeyTyped(KeyCode.DKey)) + { + currentScale = currentScale + scaleStep; + if (currentScale > maxScale) + { + currentScale = maxScale; + } + } + if (KeyTyped(KeyCode.RKey)) + { + currentScale = 1.0; + } + if (KeyTyped(KeyCode.SpaceKey)) + { + showOutline = !showOutline; + } + + // I am clearing the frame to white. + ClearScreen(ColorWhite()); + + // I am drawing the sticker centered with the current scale applied. + double drawX = centerX - BitmapWidth(stickerBitmap) / 2.0; + double drawY = centerY - BitmapHeight(stickerBitmap) / 2.0; + DrawBitmap(stickerBitmap, drawX, drawY, OptionScaleBmp(currentScale, currentScale)); + + // I am drawing an outline that matches the scaled size. + if (showOutline) + { + double outlineWidth = BitmapWidth(stickerBitmap) * currentScale; + double outlineHeight = BitmapHeight(stickerBitmap) * currentScale; + DrawRectangle(ColorNavy(), + centerX - outlineWidth / 2.0, + centerY - outlineHeight / 2.0, + outlineWidth, + outlineHeight); + } + + // I am drawing the UI hints. + DrawText("A: smaller D: bigger R: reset SPACE: outline ESC: quit", + RGBColor(0, 0, 128), 16, 16); + DrawText($"Scale: {currentScale:0.0} x", ColorBlack(), 16, 40); + + RefreshScreen(60); +} + +// I am freeing the bitmap before I quit. +FreeBitmap(stickerBitmap); \ No newline at end of file diff --git a/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.py b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.py new file mode 100644 index 000000000..5ee736b02 --- /dev/null +++ b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.py @@ -0,0 +1,91 @@ +# I am scaling a bitmap at draw time with option_scale_bmp. +# I am pressing A to make smaller; I am pressing D to make bigger; I am pressing R to reset; +# I am pressing SPACE to toggle outline; I am pressing ESC to quit. + +from splashkit import * +from splashkit import KeyCode + +# I am opening the window with a short title; I am drawing instructions inside the window. +open_window("Option Scale Bmp - live", 800, 480) + +def make_sticker_bitmap() -> bitmap: + # I am creating a small procedural bitmap so no external file is needed. + sticker_width, sticker_height = 128, 128 + bmp = create_bitmap("sticker", sticker_width, sticker_height) + clear_bitmap(bmp, rgba_color(255, 255, 255, 0)) # I am making the background transparent. + + # I am drawing a light grid to show scale changes. + for y in range(0, sticker_height, 16): + draw_line_on_bitmap(bmp, rgb_color(220, 220, 220), 0, y, sticker_width, y) + for x in range(0, sticker_width, 16): + draw_line_on_bitmap(bmp, rgb_color(220, 220, 220), x, 0, x, sticker_height) + + # I am drawing a circle and a border so the sticker is easy to see. + fill_circle_on_bitmap(bmp, rgb_color(33, 150, 243), sticker_width // 2, sticker_height // 2, 36) + draw_circle_on_bitmap(bmp, rgb_color(0, 0, 0), sticker_width // 2, sticker_height // 2, 36) + draw_rectangle_on_bitmap(bmp, rgb_color(0, 0, 0), 1, 1, sticker_width - 2, sticker_height - 2) + return bmp + +# I am building the sticker once and I am reusing it each frame. +sticker_bitmap = make_sticker_bitmap() + +# I am tracking the current scale and the valid range. +current_scale = 1.0 +scale_step = 0.1 +min_scale = 0.2 +max_scale = 3.0 + +# I am toggling an outline so the scaled bounds are obvious. +show_outline = True + +# I am centering my drawing around the window middle. +center_x = 800 / 2.0 +center_y = 480 / 2.0 + +while not quit_requested(): + process_events() + + # I am handling the controls. + if key_typed(KeyCode.escape_key): + break + if key_typed(KeyCode.a_key): + current_scale = current_scale - scale_step + if current_scale < min_scale: + current_scale = min_scale + if key_typed(KeyCode.d_key): + current_scale = current_scale + scale_step + if current_scale > max_scale: + current_scale = max_scale + if key_typed(KeyCode.r_key): + current_scale = 1.0 + if key_typed(KeyCode.space_key): + show_outline = not show_outline + + # I am clearing the frame to white. + clear_screen(rgb_color(255, 255, 255)) + + # I am drawing the sticker centered with the current scale applied. + draw_x = center_x - bitmap_width(sticker_bitmap) / 2.0 + draw_y = center_y - bitmap_height(sticker_bitmap) / 2.0 + draw_bitmap(sticker_bitmap, draw_x, draw_y, option_scale_bmp(current_scale, current_scale)) + + # I am drawing an outline that matches the scaled size. + if show_outline: + outline_width = bitmap_width(sticker_bitmap) * current_scale + outline_height = bitmap_height(sticker_bitmap) * current_scale + draw_rectangle(rgb_color(0, 0, 128), + center_x - outline_width / 2.0, + center_y - outline_height / 2.0, + outline_width, + outline_height) + + # I am drawing the UI hints and the current scale. + draw_text("A: smaller D: bigger R: reset SPACE: outline ESC: quit", + rgb_color(0, 0, 128), 16, 16) + draw_text(f"Scale: {current_scale:0.1f} x", rgb_color(0, 0, 0), 16, 40) + + refresh_screen() + delay(16) + +# I am freeing the bitmap before I quit. +free_bitmap(sticker_bitmap) \ No newline at end of file diff --git a/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.txt b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.txt new file mode 100644 index 000000000..634b63067 --- /dev/null +++ b/src/assets/usage-examples-code/graphics/option_scale_bmp/option_scale_bmp-1-basic.txt @@ -0,0 +1 @@ +I am scaling a centered bitmap at draw time and I am letting you change the scale live with keys. \ No newline at end of file