3
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を使ってグラフィック描画してみる その3 〜 raylibの定義から円を描く 〜

Last updated at Posted at 2024-12-10

前回

無理やり円を描きました

今回

raylib本来の円を描く命令を使います
rayexには円を描く命令が定義してないので追加したいと思います

rayexを改造して円を描く命令を定義する

draw_lineを真似してdraw_circleを追加します

c_src/rayex/rayex.c
// 省略

 UNIFEX_TERM draw_line(UnifexEnv *env, int start_x, int start_y, int end_x,
                       int end_y, color c) {
   DrawLine(start_x, start_y, end_x, end_y, COLOR(c));
   return draw_pixel_result_ok(env);
 }

+ UNIFEX_TERM draw_circle(UnifexEnv *env, int center_x, int center_y, double radius,
+                        color c) {
+  DrawCircle(center_x, center_y, radius, COLOR(c));
+  return draw_pixel_result_ok(env);
+ }

// 省略
c_src/rayex/rayex.spec.exs
spec draw_line(start_x :: int, start_y :: int, end_x :: int, end_y :: int, color :: color) ::
       :ok :: label

+ spec draw_circle(center_x :: int, center_y :: int, radius :: float, color :: color) ::
+       :ok :: label

lib/rayex/shapes.ex
 # 省略
   @spec draw_line(integer(), integer(), integer(), integer(), S.Color.t()) :: :ok
   defdelegate draw_line(start_x, start_y, end_x, end_y, color), to: Raylib

+  @doc "Draw a circle"
+  @spec draw_circle(integer(), integer(), float(), S.Color.t()) :: :ok
+  defdelegate draw_circle(center_x, center_y, radius, color), to: Raylib
# 省略

実行側のプログラムを作る

ローカルにあるrayexに変更します

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"}
+      {:rayex, path: "../rayex", override: true}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

円を描くプログラム
draw_circleを呼び出します

lib/rayex_circle.ex
defmodule RayexCircle do
  use Rayex

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

  defp main_loop(true) do
    begin_drawing()
    draw_circle(400, 400, 100.0, %{r: 255, g: 161, b: 0, a: 255})

    end_drawing()

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

  defp main_loop(_), do: nil
end

mix exeコマンド追加

lib/mix/tasks/exe.ex
defmodule Mix.Tasks.Exe do
  use Mix.Task

  def run(_arg) do
    RayexCircle.main()
  end
end

実行

$ mix exe

image.png

3
0
0

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
3
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?