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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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 <cstdio>

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;
}
Original file line number Diff line number Diff line change
@@ -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);
Loading