8
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Elixirでraylibを使ってグラフィック描画してみる

Last updated at Posted at 2024-12-09

raylibとは?

raylib is a simple and easy-to-use library to enjoy videogames programming.
つまり、ゲームプログラムを作るライブラリ

Elixirでraylib使うには?

Rayexがあります

しかし、いま時点で立方体や線を描画程度しかできません
RayexのC言語のラッパーの部分を追加すれば可能だと認識してます

事前準備

実験してみる

mix.exs
defmodule RayexExperiment.MixProject do
  use Mix.Project

  def project do
    [
      app: :rayex_experiment,
      version: "0.1.0",
      elixir: "~> 1.17",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
+     {:rayex, "~> 0.0.3"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

lib/rayex_experiment.ex
defmodule RayexExperiment do
  use Rayex

  def main() do
    init_window(800, 800, "window name")
    main_loop(true)
  end

  defp main_loop(true) do
    begin_drawing()
    draw()
    end_drawing()

    Process.sleep(10)
    main_loop(!window_should_close())
  end

  defp main_loop(_), do: nil

  defp draw() do
    x1 = Enum.random(1..800)
    y1 = Enum.random(1..800)
    x2 = Enum.random(1..800)
    y2 = Enum.random(1..800)

    draw_line(x1, y1, x2, y2, %{r: 255, g: 161, b: 0, a: 255})
  end
end

実行

まず、iexを起動

$ iex -S mix

iexを起動後に

iex(1)> RayexExperiment.main

ポイント

  • init_window(800, 800, "window name") で画面サイズを指定します
  • 描画前はbegin_drawing()
  • 描画後はend_drawing()
  • 後は適度にProcess.sleep入れます
  • このソースはdraw_lineを使ってランダムで線を描画してます

ハマったこと

deps/rayex/c_src/rayex/rayex.cでエラーでビルドできないので

deps/rayex/c_src/rayex/rayex.c
UNIFEX_TERM is_sound_ready(UnifexEnv *env, UnifexPayload *payload) {
  Sound sound = get_sound_unifex_payload(env, payload);
-  bool res = IsSoundReady(sound);
+  bool res =  false;
  return is_sound_ready_result(env, res);
}

に変更でビルドしました

操作動画

8
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?