public
Description: Very-high-level game programming framework, built on Rubygame & Ruby-OpenGL
Homepage: http://blog.rubygame.org/rebirth
Clone URL: git://github.com/jacius/rebirth.git
rebirth / lib / rebirth / view.rb
100644 105 lines (88 sloc) 2.805 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#--
#
# This file is one part of:
#
# Rebirth - Game development library for Ruby
# Copyright (C) 2008 John Croisant
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License, version 2.1, as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#++
 
 
require 'rubygame'
require 'singleton'
 
 
module Rebirth
 
  # View represents the viewport (aka the window).
  # This is where the game will be displayed to the player.
  #
  # Only one View can be open at a time. See #open, #open?.
  #
  class View
 
    include Singleton
 
    @@gl_setup = false
 
    # Open the View with the given size. If the View is already
    # open, it is re-opened with the new size.
    #
    # size:: the size of the view, in pixels. (Array, required)
    #
    # Returns:: the view (View)
    #
    # Example::
    # view = View.open([640,480])
    #
    def self.open( size )
      setup_gl
      @@screen = Rubygame::Screen.new(size, 16, [Rubygame::OPENGL])
      @@size = Rubygame::Screen.get_surface.size.freeze
      return instance
    end
 
    # True if the View is already open. Otherwise, false.
    def self.open?
      Rubygame::Screen.get_surface
      true
    rescue Rubygame::SDLError
      false
    end
 
    # Close the view. Usually, you don't need to call this, because
    # the view will be closed automatically when the game exits.
    def self.close
      Rubygame.quit
      @@size = nil
      @@gl_setup = false
    end
 
    # call-seq:
    # size -> [w,h]
    #
    # Returns the size of the View in pixels.
    # If the View is not open, returns nil.
    def self.size
      @@size
    end
 
    class << self
 
      private
 
      # Setup the OpenGL attributes prior to opening the view.
      # Does nothing if they've already been set up.
      def setup_gl # :nodoc:
        unless @@gl_setup
          Rubygame.init
          Rubygame::GL.set_attrib(Rubygame::GL::RED_SIZE, 5)
          Rubygame::GL.set_attrib(Rubygame::GL::GREEN_SIZE, 6)
          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)
          @@gl_setup = true
        end
      end
    end
 
  end
 
end