Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Ruby] Ray  (Read 2806 times)

0 Members and 1 Guest are viewing this topic.

Mon ouïe

  • Newbie
  • *
  • Posts: 27
    • View Profile
[Ruby] Ray
« on: February 08, 2011, 10:38:33 pm »
Hi,

Here's a project I've been working on for some time. I wrote the original version using the SDL, but rewrote it with the SFML to get hardware acceleration and some of the features the SFML has.

The rewrite didn't change the whole API though. This means that, unlike rbsfml, binding the methods of the SFML is neither a goal nor a requirement. The SFML is merely a mean to get whatever I want (though I am indeed influenced by the way the SFML works).

As an example, here's one of rbsfml's samples:
Code: [Select]
require 'sfml/system'
require 'sfml/window'
require 'sfml/graphics'

app = SFML::RenderWindow.new
app.create( [800, 600], "My Ruby SFML" )
app.framerate = 100
app.position = [300, 300]
input = app.input

shape = SFML::Shape.rectangle( [-10, -10, 20, 20], SFML::Color::White )

image = SFML::Image.new
image.create( 100, 100, [255, 0, 0] )
sprite = SFML::Sprite.new( image, [500, 500] )

text = SFML::Text.new( "This is a test!" )
text.position = [ 20, 20 ]

while app.open?
  while event = app.get_event
    if event.type == SFML::Event::Closed
      app.close
    end
  end
 
  app.clear
  shape.position = [input.mouseX, input.mouseY]
  app.draw shape
  app.draw sprite
  app.draw text
  app.display
end


This is how I'd write it with ray:
Code: [Select]
require 'ray'

Ray.game "My Ruby SFML", :size => [800, 600] do
  register do
    add_hook :quit, method(:exit!)
  end

  scene :test do
    @shape = Ray::Shape.rectangle([-10, -10, 20, 20], Ray::Color.white)

    img = Ray::Image.new(:w => 100, :h => 100).fill(Ray::Color.red).update
    @sprite = sprite(img, :at => [500, 500])

    always do
      @shape.position = [mouse_x, mouse_y]
    end

    @font = Ray::Font.default

    render do |win|
      @shape.draw_on  win
      @sprite.draw_on win

      @font.draw("This is a test!", :on => win, :at => [20, 20], :size => 12)
    end
  end

  push_scene :test
end


It uses a C extensions to bind the SFML's features, but an important part is still written just in Ruby.

As it requires the SFML 2, I wouldn't release it as a gem before the SFML 2's release.

I've written a file introducing to some of Ray's features, available on the github rebo where you can also find, aside from (of course) Ray's code, a few samples: https://github.com/Mon-Ouie/ray/tree/sfml (the master branch is still the version written with the SDL).