7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Exrayを使ってElixirで円を描画してみる

Last updated at Posted at 2025-01-10

Elixirで手軽に描画する方法があります
それはExrayを使うことです
Exrayはraylibを参照してます

いま時点ではExrayは2Dしか動かないです

検証済み環境

  • Ubuntu 22.04(実機)
  • Windows 11 + WSL2(Ubuntu 22.04)

※macOSは動かなかった

作成方法

$ mix new hoge
$ cd hoge

を見ながら

exrayを追加

mix.exs
# 省略
  defp deps do
    [
+     {:exray, "~> 0.6.0"}
    ]
  end
# 省略

ライブラリ取得&コンパイル

$ mix deps.get
$ mix compile.exray

ソースを書く

hexdocsのソースに円を追加しただけです

のソースをコピーして修正

lib/hoge.ex
- defmodule SuperCoolGame do
+ defmodule Hoge do

  # We import Exray.Core.Window, like we did in IEX.
  import Exray.Core.Window
  # But, we also import Exray.Core.Drawing, to have access to begin_draw() and end_draw() calls.
  # This is to poll for window_should_close?() events, as without end_draw() no inputs will be polled at all.
  import Exray.Core.Drawing
  # We also need to import Exray.Core.Timing in order to set our FPS. This is VERY important.
  import Exray.Core.Timing
+ alias Exray.Utils.Colors
+ alias Exray.Shapes.Basic


  def run(width \\ 200, height \\ 200, title \\ "Hello World!") do
    init_window(width, height, title)
    set_target_fps(60) # <-- SUPER important! Call this just after you init_window, or segfaults are gonna happen a lot.
    main_loop()

    # As another layer of safety, if for some reason our window *didn't* close, we can do it here in this check.
    if window_should_close?() do
      close_window()
    end

    :ok # :ok, because we're :ok :)
  end

  defp main_loop() do

    # Stay out of update and draw unless our window is ready to update and draw.
    unless window_is_ready?(), do: main_loop()

    # If at any point we press Raylib's default "Quit Application" key, (ESCAPE), stop looping and exit.
    unless window_should_close?() do
      update()
      draw()
      main_loop()
    end
  end

  defp update() do
    # Nothing yet!
  end

  defp draw() do
    # Before drawing, we'll clear the background with the Exray.Utils.Colors.black function result- Which is %Exray.Structs.Color{r: 0, g: 0, b: 0, a: 255}.
-   clear_background(Exray.Utils.Colors.black())
+   clear_background(Colors.white)
    begin_drawing()

    # Nothing yet!
+   Basic.draw_circle(100, 100, 50.0, Colors.red)

    end_drawing()
  end

end

実行

$ mix run --eval "Hoge.run()"

image.png

基本的な描画

を参照してください

今回はdraw_circleを使いました

色はこちらに定義されてます

今回のソース

おわり

7
2
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
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?