Skip to content

Instantly share code, notes, and snippets.

@ongaeshi
Created June 27, 2020 15:30
Show Gist options
  • Save ongaeshi/d831ecd82d4978366800b8d0003afa6b to your computer and use it in GitHub Desktop.
Save ongaeshi/d831ecd82d4978366800b8d0003afa6b to your computer and use it in GitHub Desktop.
def wait(wait = 1)
1.upto(wait) do |e|
yield e if block_given?
Fiber.yield
end
end
def say(str, w = 120)
print str
wait w
end
@f = Fiber.new do
say 'mrubyの使い方'
clear
say "開きます。\n"
say 'mrb_state* mrb = mrb_open();'
clear
s = <<-EOS
文字列経由でスクリプトを実行します。
mrb_state* mrb = mrb_open();
EOS
say s.chomp
say 'mrb_value ret = mrb_load_string(mrb, “1 + 2”);'
clear
s = <<-EOS
結果を数値に変換して表示します。
mrb_state* mrb = mrb_open();
mrb_value ret = mrb_load_string(mrb, “1 + 2”);
EOS
say s.chomp
say 'printf("value=%d\n", mrb_fixnum(ret));'
clear
s = <<-EOS
閉じて終わりです!
mrb_state* mrb = mrb_open();
mrb_value ret = mrb_load_string(mrb, “1 + 2”);
printf("value=%d\\n", mrb_fixnum(ret));
EOS
say s.chomp
say 'mrb_close(mrb);'
end
def mainloop
@f.resume if @f.alive?
end
#include <Siv3D.hpp> // OpenSiv3D v0.4.3
#include <mruby.h>
#include "mruby/array.h"
#include <mruby/compile.h>
#include <mruby/dump.h>
#include <mruby/string.h>
extern "C" void siv3d_Print(const char* str)
{
Print << Unicode::UTF8ToUTF32(str);
}
static mrb_value clear(mrb_state* mrb, mrb_value self)
{
ClearPrint();
return mrb_nil_value();
}
void Main()
{
bool isStart = false;
mrb_state* mrb = mrb_open();
FILE* fp;
if ((fp = fopen("./hello.rb", "r")) != NULL)
{
mrb_load_file(mrb, fp);
}
struct RClass* krn = mrb->kernel_module;
mrb_define_method(mrb, krn, "clear", clear, MRB_ARGS_NONE());
// 背景を黒色にする
Scene::Resize(640, 480);
Scene::SetBackground(Palette::Black);
// 大きさ 60 のフォントを用意
const Font font(60);
// 猫のテクスチャを用意
// const Texture cat(Emoji(U"🐈"));
// const Texture cat(Emoji(U"👀"));
// const Texture cat(Emoji(U"🎃"));
const Texture cat(Emoji(U"👺"));
// 猫の座標
Vec2 catPos(450, 200);
while (System::Update())
{
if (KeySpace.down()) {
isStart = true;
}
if (!isStart) {
continue;
}
mrb_funcall(mrb, mrb_top_self(mrb), "mainloop", 0);
if (mrb->exc)
{
Scene::SetBackground(Palette::Black);
mrb_value msg = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
const char* cstr = mrb_string_value_ptr(mrb, msg);
Print << Unicode::FromUTF8(cstr);
}
// 地面
Rect(0, 300, 800, 300).draw(ColorF(0.2, 0.3, 0.4));
// 猫を表示する
cat.draw(catPos);
// 反射するテクスチャ
constexpr Size imageSize = Emoji::ImageSize;
cat(0, imageSize.y * 0.25, imageSize.x, imageSize.y * 0.75).flipped().draw(catPos.x, catPos.y + imageSize.y, Arg::top = AlphaF(0.5), Arg::bottom = AlphaF(0.0));
//// テキストを画面の中心に描く
//font(U"Hello, Siv3D!🐣").drawAt(Scene::Center(), Palette::Black);
}
mrb_close(mrb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment