#!# #!# MyGame.rb #!# # Application shiz require 'Application.rb' # Let's dance... put on your red shoes and dance the blues app = Application.new() app.run() #!# #!# Application.rb #!# # Ruby Libraries require 'rubygame' # My shiz require 'Game.rb' # This class is our main app class, and controls basically everything class Application # Constructor def initialize() # Start Rubygame Rubygame.init() # Initialize some variables @exit = false @bpp = 16 # Create the screen Rubygame::GL.set_attrib(Rubygame::GL::RED_SIZE, 5) Rubygame::GL.set_attrib(Rubygame::GL::GREEN_SIZE, 5) Rubygame::GL.set_attrib(Rubygame::GL::BLUE_SIZE, 5) Rubygame::GL.set_attrib(Rubygame::GL::DEPTH_SIZE, 16) Rubygame::GL.set_attrib(Rubygame::GL::DOUBLEBUFFER, 1) @screen = Rubygame::Screen.new([640, 480], @bpp, [Rubygame::OPENGL]) @screen.title = 'MyGame' end # This function runs our application def run() game = Game.new() game.run() end end #!# #!# Game.rb #!# # Ruby Libraries require 'rubygame' require 'gl' # My Ssiz require 'Timer.rb' # This class is an instance of our game class Game # This is our constructor (hurr!) def initialize() @exit = false # This is what we use to know if we're still running the game loop @events = nil # Our event queue #@timer = nil # Our logic timer # Creating our event queue @events = Rubygame::EventQueue.new() @events.autofetch = true # Fire up the timer @timer = Timer.new() end # This is our game loop :) def run() while(!@exit) @timer.newFrame() input() logic() output() @timer.sync() end end # Input function for our game loop def input() # Iterate through our event queue @events.each do |event| case event when Rubygame::QuitEvent @exit = true end end end # Logic function for our game loop def logic() end # Output function for our game loop def output() # Swap the GL buffers, yo Rubygame::GL.swap_buffers() end end #!# #!# Timer.rb #!# require 'rubygame' # This is my own little timer class. I like it a lot. class Timer # Constructor def initialize(fps = 100) @clock = Rubygame::Clock.new() @lastTick = @clock.lifetime() @framesPerSecond = fps end # Tells the timer that we're on a new frame now def newFrame() @lastTick = @clock.lifetime() end # Syncs the program to our desired FPS def sync() while((@clock.lifetime() - @lastTick) < (1000 / @framesPerSecond)) Rubygame::Clock.delay(1) end end end