Skip to content

Instantly share code, notes, and snippets.

@mswieboda
Last active March 29, 2019 17:05
Show Gist options
  • Save mswieboda/fc8769f2a5f5a8f961b4bc6253aa8193 to your computer and use it in GitHub Desktop.
Save mswieboda/fc8769f2a5f5a8f961b4bc6253aa8193 to your computer and use it in GitHub Desktop.
Crystal Game Development
require "cray"
module CrayTest
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 768
LibRay.init_window SCREEN_WIDTH, SCREEN_HEIGHT, "Example: input"
@@icon = LibRay.load_image File.join(__DIR__, "circle.png")
@@circle = LibRay.load_texture_from_image(@@icon)
LibRay.set_window_icon(@@icon)
@@circle_pos = LibRay::Vector2.new(x: SCREEN_WIDTH/2 - @@circle.width/2, y: SCREEN_HEIGHT/2 - @@circle.height/2)
@@bullets = [] of LibRay::Vector2
@@bullet = LibRay::Vector2.new
SPEED = 300
TEXT = "Use WASD to move the red circle!"
FONT_SIZE = 20
def self.input_events
delta = LibRay.get_frame_time
if LibRay.key_down? LibRay::KEY_W
@@circle_pos.y -= delta * SPEED
end
if LibRay.key_down? LibRay::KEY_S
@@circle_pos.y += delta * SPEED
end
if LibRay.key_down? LibRay::KEY_A
@@circle_pos.x -= delta * SPEED
end
if LibRay.key_down? LibRay::KEY_D
@@circle_pos.x += delta * SPEED
end
if LibRay.key_released? LibRay::KEY_SPACE
@@bullets << LibRay::Vector2.new(x: @@circle_pos.x + (@@circle.width / 2), y: @@circle_pos.y - (@@circle.height / 4))
@@bullet = LibRay::Vector2.new(x: @@circle_pos.x + (@@circle.width / 2), y: @@circle_pos.y - (@@circle.height / 4))
end
end
def self.movement
delta = LibRay.get_frame_time
@@bullets.each do |bullet|
bullet.y -= delta * SPEED
end
@@bullet.y -= delta * SPEED
end
def self.draw
LibRay.begin_drawing
LibRay.clear_background LibRay::BLACK
LibRay.draw_fps 0, 0
text_size = LibRay.measure_text(TEXT, FONT_SIZE)
LibRay.draw_text(
TEXT,
SCREEN_WIDTH - text_size,
0,
FONT_SIZE,
LibRay::GREEN
)
# draw circle
LibRay.draw_texture_v(@@circle, @@circle_pos, LibRay::WHITE)
# draw bullets
@@bullets.each do |bullet|
LibRay.draw_rectangle(bullet.x, bullet.y, 3, 10, LibRay::GREEN)
end
LibRay.draw_rectangle(@@bullet.x, @@bullet.y, 3, 10, LibRay::GREEN)
LibRay.end_drawing
end
def self.application_loop
while !LibRay.window_should_close?
input_events()
movement()
draw()
end
end
def self.run
application_loop()
LibRay.unload_texture(@@circle)
LibRay.close_window
end
end
CrayTest.run

Intro to Crystal Game Development

asdf version manager

Crystal versioning like rbenv, to switch Crystal versions between directories. Not necessary, but thought it would be good to set up in case the Crystal version I try gets out of date soon.

https://asdf-vm.com/#/core-manage-asdf-vm

Once asdf is installed, you can list all available Crystal versions and install one:

$ asdf list-all crystal
# lists all crystal vers available

$ asdf install crystal 0.27.2

# once we create a new project we'll use
$ asdf local crystal 0.27.2

# to set the local crystal version or use `global` to set for everything

init Crystal app

$ crystal init app cray-test

add cray dependency

follow https://gitlab.com/Zatherz/cray to cray dependency:

Add this to your application's shard.yml:

dependencies:
  cray:
    gitlab: zatherz/cray

then run

$ shards install

to install the newly added cray dependency.

install raylib via Homebrew

see https://github.com/raysan5/raylib/wiki/Working-on-macOS

$ brew install raylib

run via:

$ crystal run src/hello_world.cr

(copied hello_world.cr from the examples folder of the cray github: https://gitlab.com/Zatherz/cray

Compiling not working. Difficulties in communicating via Raylib to crystal, maybe the binaries are not matched correctly?

15:34:36 ~/code/crystal/cray-test (master)✗ $ crystal build src/examples/hello-world.cr
Undefined symbols for architecture x86_64:
  "_GetDefaultFont", referenced from:
      ___crystal_main in _main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of command failed with code: 1: `cc "${@}" -o '/Users/matt/code/crystal/cray-test/hello-world'  -rdynamic  -L/usr/local/Cellar/raylib/2.0.0/lib -lraylib -lpcre -lgc -lpthread /usr/local/Cellar/crystal/0.27.2/src/ext/libcrystal.a -levent -liconv -ldl -L/usr/lib -L/usr/local/lib`

switched to glove game lib to see if that would work

https://github.com/ddfreyne/glove

Examples: https://github.com/ddfreyne/inari

made a new app, and copied the example code in for displaying a card (image texture):

require "glove"

if full_path = Process.executable_path
  Dir.cd(File.dirname(full_path))
end

card =
  Glove::Entity.new.tap do |e|
    e << Glove::Components::Texture.new("assets/card.png")
    e << Glove::Components::Transform.new.tap do |t|
      t.width = 140_f32
      t.height = 190_f32
      t.translate_x = 400_f32
      t.translate_y = 300_f32
    end
  end

scene =
  Glove::Scene.new.tap do |scene|
    scene.spaces << Glove::Space.new.tap do |space|
      space.entities << card
    end
  end

game = Glove::EntityApp.new(800, 600, "Inari")
game.clear_color = Glove::Color::WHITE
game.replace_scene(scene)
game.run

Glove requires copying over shader files, and any assets, so I reused/modified the Makefile structure from https://github.com/ddfreyne/inari so just with

$ make
$ cd dist/releases/cli
$ ./glove-test

the app opens in a new window, displaying the card image!

I didn't end up liking the Glove library structure too much, so tried going back to Cray, and was successful

back to Cray

Turns out the bindings to Crystal, the get_default_font wasn't working, so I removed the usage of the fonts and switched to use the RayLib methods that don't specify font, and use the default Cray font.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment