Forums Archived

The forums are being archived. See this topic for more information.

rubygame + ruby-opengl

Post about games, applications, etc. made with Rubygame. Feel free to promote your own projects here!

rubygame + ruby-opengl

Postby brandon » Thu Mar 11, 2010 9:54 pm

Here's a small starter program for using rubygame along with direct calls to OpenGL. This program requires the ruby-opengl to be installed.
Code: Select all
require 'rubygame'

require 'gl'
require 'glu'

include Gl
include Glu

include Rubygame
include Rubygame::Events

class Game

  include EventHandler::HasEventHandler

  def initialize
    init_screen
    init_clock
    init_queue
    init_hooks
    init_gl
  end

  def init_screen
    @screen = Screen.new [800, 600], 0, [HWSURFACE, DOUBLEBUF, OPENGL]
    @screen.title = 'Particles!'
  end

  def init_clock
    @clock = Clock.new
    @clock.target_framerate = 30
  end

  def init_queue
    @queue = EventQueue.new
    @queue.enable_new_style_events
  end

  def init_hooks
    make_magic_hooks :escape => :quit
  end

  def init_gl
    glClearColor 0.0, 0.0, 0.0, 0.0
    resize 800, 600
  end

  def go
    catch :quit do
      loop do
        handle_events
        draw
      end
    end
  end

  def handle_events
    @queue.each do |e|
      handle e
    end
  end

  def resize(w, h)
    h = 1 if h == 0
    glViewport 0, 0, w, h
    glMatrixMode GL_PROJECTION
    glLoadIdentity
    gluPerspective 45.0, w.to_f / h.to_f, 1.0, 1000.0
    glMatrixMode GL_MODELVIEW
  end

  def draw
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glMatrixMode GL_MODELVIEW
    glLoadIdentity

    glTranslated 0.0, 0.0, -20.0
    glBegin GL_QUADS
      glVertex2i  5,  5
      glVertex2i -5,  5
      glVertex2i -5, -5
      glVertex2i  5, -5
    glEnd

    GL.swap_buffers
  end

  def quit
    puts 'Quitting!'
    throw :quit
  end

end

Game.new.go
Rubygame.quit

Notes
- In order to get around a NameError, I renamed gl.rb under rubygame to sdl_gl.rb (and the corresponding reference in rubygame.rb:36). Rubygame's gl.rb was getting required by the ruby-opengl gem, instead of the gl.so file.
- The OPENGL flag must be passed to Screen.new
brandon
 
Posts: 9
Joined: Wed Mar 10, 2010 7:07 pm

Re: rubygame + ruby-opengl

Postby jacius » Fri Mar 12, 2010 5:58 am

Nicely done! This is definitely a much cleaner style than the old demo_gl.rb and demo_gl_tex.rb samples! :lol:
User avatar
jacius
Site Admin
 
Posts: 131
Joined: Fri Feb 06, 2009 11:13 pm

Re: rubygame + ruby-opengl

Postby LionKing » Fri Oct 01, 2010 7:45 am

that was great.

i've finished it and it works.

thanks for the information guys.
LionKing
 
Posts: 1
Joined: Fri Oct 01, 2010 7:34 am


Return to Creations

Who is online

Users browsing this forum: No registered users and 1 guest

cron