Skip to content

Instantly share code, notes, and snippets.

@Reputeless
Last active December 3, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Reputeless/6d2b76a7350d4820c9e46dfc876f0a84 to your computer and use it in GitHub Desktop.
Save Reputeless/6d2b76a7350d4820c9e46dfc876f0a84 to your computer and use it in GitHub Desktop.
OpenSiv3D Script Examples by Ryo Suzuki
To the extent possible under law, the person who associated CC0 with
OpenSiv3D Script Examples has waived all copyright and related or neighboring rights
to OpenSiv3D Script Examples
You should have received a copy of the CC0 legalcode along with this
work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
Graphics::SetBackground(ColorF(0.3, 0.4, 0.5));
const Rect blockA(100, 180, 150, 200);
const Rect blockB(400, 80, 150, 200);
blockA.rounded(10)
.drawShadow(Vec2(0, 3), 10, 3).draw(ColorF(0.4, 0.9, 0.5));
blockB.rounded(10)
.drawShadow(Vec2(0, 3), 10, 3).draw(ColorF(0.4, 0.9, 0.5));
const Vec2 src = blockA.rightCenter();
const Vec2 dst = blockB.leftCenter();
Bezier3(src, Vec2((src.x + dst.x)/2, src.y),
Vec2((src.x + dst.x)/2, dst.y), dst)
.draw(7, Palette::White)
.draw(4, Palette::Orange);
Circle(src, 15)
.drawShadow(Vec2(0, 0), 8, 8, ColorF(1.0, 0.5, 0.0, 0.5))
.draw(Palette::Orange);
Circle(dst, 15).draw(Palette::Orange)
.drawShadow(Vec2(0, 0), 8, 8, ColorF(1.0, 0.5, 0.0, 0.5))
.draw(Palette::Orange);
void Main()
{
Graphics::SetBackground(Palette::White);
const Vec2 c = Window::Center();
while (System::Update())
{
for (int32 i = 0; i < 60; ++i)
{
const Vec2 pos = OffsetCircular(c, 210, ToRadians(i * 6));
Circle(pos, i % 5 == 0 ? 6 : 3).draw(ColorF(0.25));
}
const DateTime t = DateTime::Now();
const double h = (t.hour + t.minute / 60.0) * ToRadians(30);
Line(c, c + Circular(110, h)).draw(LineStyle::RoundCap, 18, ColorF(0.1));
const double m = (t.minute + t.second / 60.0) * ToRadians(6);
Line(c, c + Circular(190, m)).draw(LineStyle::RoundCap, 8, ColorF(0.1));
const double s = (t.second + t.milliseconds / 1000.0) * ToRadians(6);
Line(c, c + Circular(190, s)).draw(3, ColorF(0.8, 0.2, 0.2));
Circle(c, 230).drawFrame(20, ColorF(0.8));
}
}
class Cell
{
int16 previous;
int16 current;
Cell() {}
Cell(int16 p, int16 c)
{
previous = p;
current = c;
}
};
void RandomFill(Grid<Cell>& grid)
{
grid.fill(Cell(0 , 0));
for (size_t y = 1; y < grid.height() - 2; ++y)
{
for (size_t x = 1; x < grid.width() - 2; ++x)
{
grid[x, y] = Cell(0, RandomBool(0.5) ? 0 : 1);
}
}
}
void Update(Grid<Cell>& grid)
{
for (size_t y = 0; y < grid.height(); ++y)
{
for (size_t x = 0; x < grid.width(); ++x)
{
grid[x, y].previous = grid[x, y].current;
}
}
for (size_t y = 1; y < grid.height() - 2; ++y)
{
for (size_t x = 1; x < grid.width() - 2; ++x)
{
int32 n = 0;
n += grid[x - 1, y - 1].previous;
n += grid[x, y - 1].previous;
n += grid[x + 1, y - 1].previous;
n += grid[x - 1, y].previous;
n += grid[x + 1, y].previous;
n += grid[x - 1, y + 1].previous;
n += grid[x, y + 1].previous;
n += grid[x + 1, y + 1].previous;
const int32 c = grid[x, y].previous;
grid[x, y].current = ((c == 0 && n == 3) || (c == 1 && (n == 2 || n == 3))) ? 1 : 0;
}
}
}
void Main()
{
Graphics::SetBackground(ColorF(0.5));
const int32 width = 80;
const int32 height = 80;
Grid<Cell> grid(width + 2, height + 2);
RandomFill(grid);
while (System::Update())
{
Window::SetTitle(Format(Profiler::FPS()));
for (size_t y = 1; y < grid.height() - 2; ++y)
{
for (size_t x = 1; x < grid.width() - 2; ++x)
{
const Cell cell = grid[x, y];
Rect(x * 7, y * 7, 7)
.draw(cell.current == 1 ? Color(0, 255, 0) : Color(0));
}
}
Update(grid);
}
}
Image CreateSquareImage()
{
const Image image("example/windmill.png");
const int32 size = Min(image.width(), image.height());
return image.clipped((image.width() - size) / 2, (image.height() - size) / 2, size, size);
}
bool Swappable(int32 a, int32 b)
{
return (a / 4 == b / 4 && Abs(a - b) == 1) || (a % 4 == b % 4 && Abs(a - b) == 4);
}
void Main()
{
Graphics::SetBackground(ColorF(0.8, 0.9, 1.0));
const int32 cellSize = 120;
const Point offset(20, 40);
const Texture texture(CreateSquareImage(), TextureDesc::Mipped);
Optional<size_t> grabbed;
Array<int32> pieces;
for (int32 i = 0; i <= 15; ++i)
pieces << i;
Array<int32> offsets = { -4, -1, 1, 4 };
{
int32 pos15 = 15;
for (int32 i = 0; i < 1000; ++i)
{
const int32 to = pos15 + offsets.choice();
if (InRange(to, 0, 15) && Swappable(pos15, to))
{
std::swap(pieces[pos15], pieces[to]);
pos15 = to;
}
}
}
while (System::Update())
{
Rect(offset, 4 * cellSize)
.drawShadow(Vec2(0, 2), 12, 8)
.draw(ColorF(0.25))
.drawFrame(0, 8, ColorF(0.3, 0.5, 0.7));
if (!MouseL.pressed())
{
grabbed.reset();
}
for (size_t i = 0; i < 16; ++i)
{
const int32 pieceID = pieces[i];
const Rect rect = Rect(i % 4 * cellSize, i / 4 * cellSize, cellSize).movedBy(offset);
if (pieceID == 15)
{
if (grabbed && rect.mouseOver() && Swappable(i, grabbed.value()))
{
std::swap(pieces[i], pieces[grabbed.value()]);
grabbed = i;
}
continue;
}
if (rect.leftClicked())
{
grabbed = i;
}
rect(texture.uv(pieceID % 4 * 0.25, pieceID / 4 * 0.25, 0.25, 0.25))
.draw()
.drawFrame(1, 0, ColorF(1.0, 0.75));
if (grabbed && grabbed.value() == i)
{
rect.draw(ColorF(1.0, 0.5, 0.0, 0.3));
}
if (rect.mouseOver())
{
Cursor::RequestStyle(CursorStyle::Hand);
}
}
texture.resized(240)
.draw(offset.x + cellSize * 4 + 24, offset.y)
.drawFrame(0, 4, ColorF(0.3, 0.5, 0.7));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment