rage 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ # Viewport constructs and methods
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # Licensed under the GPLv3+ http://www.gnu.org/licenses/gpl.txt
5
+
6
+ module RAGE
7
+
8
+ # Viewport is the end user's view into the world. It is represented on
9
+ # the screen via glViewport and has a camera attached to it managing
10
+ # the viewport's projection matrix.
11
+ class Viewport
12
+
13
+ # x,y pos, width, height
14
+ attr_accessor :x, :y, :width, :height
15
+
16
+ # Camera attached to viewport
17
+ attr_accessor :camera
18
+
19
+ # Create new Viewport with any of the following optional args,
20
+ # * :x x coordinate of viewport on screen, defaults to 0
21
+ # * :y y coordinate of viewport on screen, defaults to 0
22
+ # * :width width of viewport on screen or :max, defaults to :max
23
+ # * :height height of viewport on screen or :max, defaults to :max
24
+ # * :camera camera to attach to viewport, if none provided one is auto created
25
+ def initialize(args = {})
26
+ @x = args[:x] if args.has_key? :x
27
+ @y = args[:y] if args.has_key? :y
28
+ @width = args[:width] if args.has_key? :width
29
+ @height = args[:height] if args.has_key? :height
30
+ @camera = args[:camera] if args.has_key? :camera
31
+
32
+ @x = 0 if @x.nil?
33
+ @y = 0 if @y.nil?
34
+ @width = :max if @width.nil?
35
+ @height = :max if @height.nil?
36
+ @camera = Camera.new if @camera.nil?
37
+ end
38
+
39
+ # Invoked during draw cycle to draw viewport and associated camera
40
+ def draw
41
+ w = @width == :max ? Window.width : @width
42
+ h = @height == :max ? Window.height : @height
43
+
44
+ Gl.glViewport(@x,@y, w, h)
45
+ @camera.draw
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,58 @@
1
+ # Window constructs and methods
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # Licensed under the GPLv3+ http://www.gnu.org/licenses/gpl.txt
5
+
6
+ module RAGE
7
+
8
+ # Manages game window
9
+ class Window
10
+
11
+ # Screen width / height, and handle
12
+ class_attr :width, :height, :screen
13
+
14
+ # Viewports arrays
15
+ class_attr :viewports
16
+
17
+ # Set attributes on window, which may include
18
+ # * :width
19
+ # * :height
20
+ def self.set_attrs(args = {})
21
+ @@width = args[:width] if args.has_key? :width
22
+ @@height = args[:height] if args.has_key? :height
23
+
24
+ @@viewports = []
25
+ end
26
+
27
+ # Create a viewport using specified args and add it to
28
+ def self.create_viewport(vp_args = {}, &block)
29
+ vp = viewport vp_args, &block
30
+ viewports << vp
31
+ return vp
32
+ end
33
+
34
+ # Display window
35
+ def self.show
36
+ SDL.init(SDL::INIT_VIDEO)
37
+ SDL.setGLAttr(SDL::GL_DOUBLEBUFFER,1)
38
+ @@screen = SDL::Screen.open(width,height,32,SDL::OPENGL)
39
+
40
+ @@black = ResourcesManager.instance.load_resource :type => :color, :color => [0,0,0]
41
+ end
42
+
43
+ # Invoked during draw cycle to draw window
44
+ def self.draw
45
+ Gl.glClear(Gl::GL_COLOR_BUFFER_BIT | Gl::GL_DEPTH_BUFFER_BIT)
46
+ @@screen.fill_rect(0,0,width,height,@@black.color)
47
+ end
48
+
49
+ # Invoked during draw cycle to refresh window
50
+ def self.refresh
51
+ @@screen.update_rect(0,0,0,0)
52
+ SDL.GLSwapBuffers()
53
+ #Gl.glFlush();
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,112 @@
1
+ # location module tests
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # Licensed under the GPLv3+ http://www.gnu.org/licenses/gpl.txt
5
+
6
+ require File.dirname(__FILE__) + '/spec_helper'
7
+
8
+ describe Location do
9
+
10
+ it "should default to origin coordinates" do
11
+ loc = Location.new
12
+ loc.x.should == 0
13
+ loc.y.should == 0
14
+ loc.z.should == 0
15
+ end
16
+
17
+ it "should accept and set location parameters" do
18
+ mesh = MockMesh.new
19
+ loc = Location.new :x => 1, :y => 2, :z => -5.32, :mesh => mesh
20
+ loc.x.should == 1
21
+ loc.y.should == 2
22
+ loc.z.should == -5.32
23
+ loc.mesh.should == mesh
24
+ end
25
+
26
+ it "should return location boundaries" do
27
+ loc = Location.new :x => 3.14, :y => 1.59, :z => 17
28
+ loc.boundaries.should == [3.14, 1.59, 17, 3.14, 1.59, 17]
29
+ end
30
+
31
+ it "should return mesh boundaries" do
32
+ mesh = MockMesh.new
33
+ mesh.boundaries = [10, 20, -30, 44.2, -11.11, -0.93]
34
+ loc = Location.new :mesh => mesh, :x => 12.9, :y => -49, :z => 20.2462
35
+ boundaries = loc.boundaries
36
+ boundaries[0].should == 12.9 + 10
37
+ boundaries[1].should == -49 + 20
38
+ boundaries[2].should == 20.2462 + -30
39
+ boundaries[3].should == 12.9 + 44.2
40
+ boundaries[4].should == -49 + -11.11
41
+ boundaries[5].should == 20.2462 + -0.93
42
+ end
43
+
44
+
45
+ it "should draw associated mesh" do
46
+ mesh = MockMesh.new
47
+ loc = Location.new :mesh => mesh
48
+ loc.draw
49
+ mesh.draw_called.should be_true
50
+ end
51
+
52
+ end
53
+
54
+ describe LocationsManager do
55
+
56
+ it "should clear locations" do
57
+ LocationsManager.instance.clear
58
+ LocationsManager.instance.locations.size.should == 0
59
+ end
60
+
61
+ it "should manage locations" do
62
+ loc = Location.new
63
+ LocationsManager.instance.clear
64
+ LocationsManager.instance.add loc
65
+ LocationsManager.instance.locations.size.should == 1
66
+ LocationsManager.instance.locations.include?(loc).should be_true
67
+ end
68
+
69
+ it "should return boundaries of managed coordinate system" do
70
+ loc1 = Location.new :x => 10, :y => 10, :z => 10
71
+ loc2 = Location.new :x => -10, :y => -10, :z => -10
72
+ loc3 = Location.new :x => 5, :y => 5, :z => 5
73
+ loc4 = Location.new :x => 10, :y => 25, :z => -5
74
+
75
+ LocationsManager.instance.clear
76
+ LocationsManager.instance.add loc1
77
+ LocationsManager.instance.add loc2
78
+ LocationsManager.instance.add loc3
79
+ LocationsManager.instance.add loc4
80
+
81
+ xmax, ymax, zmax, xmin, ymin, zmin = LocationsManager.instance.boundaries
82
+ xmax.should == 10
83
+ ymax.should == 25
84
+ zmax.should == 10
85
+ xmin.should == -10
86
+ ymin.should == -10
87
+ zmin.should == -10
88
+ end
89
+
90
+ it "should return average center" do
91
+ # TODO LocationsManager average center test
92
+ end
93
+
94
+ it "should return mean center" do
95
+ # TODO LocationsManager mean center test
96
+ end
97
+
98
+ end
99
+
100
+ class MockMesh
101
+ attr_accessor :draw_called
102
+ attr_accessor :location
103
+ attr_accessor :boundaries
104
+
105
+ def initialize
106
+ @draw_called = false
107
+ end
108
+
109
+ def draw
110
+ @draw_called = true
111
+ end
112
+ end
@@ -0,0 +1,84 @@
1
+ # mesh module tests
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # Licensed under the GPLv3+ http://www.gnu.org/licenses/gpl.txt
5
+
6
+ dir = File.expand_path(File.dirname(__FILE__))
7
+ require dir + '/spec_helper'
8
+
9
+ describe Mesh do
10
+ before(:each) do
11
+ @test_x3d = Loader.load "file://#{dir}/content/cube.x3d"
12
+ end
13
+
14
+ it "should parse x3d xsd and data" do
15
+ mesh = Mesh.new @test_x3d
16
+ Mesh.x3d_schema.should_not be_nil
17
+ Mesh.x3d_schema_classes.include?(X3D).should be_true
18
+
19
+ mesh.instance_variable_get(:@scene).should_not be_nil
20
+ mesh.instance_variable_get(:@tf).should_not be_nil
21
+ mesh.instance_variable_get(:@translation).should_not be_nil
22
+ mesh.instance_variable_get(:@translation).should == [0, 0, 0]
23
+ mesh.instance_variable_get(:@scale).should_not be_nil
24
+ mesh.instance_variable_get(:@scale).should == [9.065643, 9.065643, 9.065643]
25
+ mesh.instance_variable_get(:@coord_index).should_not be_nil
26
+ mesh.instance_variable_get(:@coord_index).size.should == 24
27
+ mesh.instance_variable_get(:@coord_index)[0...4].should == [0, 1, 2, 3]
28
+ mesh.instance_variable_get(:@coord_point).should_not be_nil
29
+ mesh.instance_variable_get(:@coord_point).size.should == 24
30
+ mesh.instance_variable_get(:@coord_point)[0...3].should == [1, 1, -1]
31
+ end
32
+
33
+ it "should be associated with location when shown" do
34
+ mesh = Mesh.new @test_x3d
35
+ LocationsManager.instance.clear
36
+ location = mesh.show_at :x => 100, :y => 100, :z => -500
37
+ location.should_not be_nil
38
+ location.x.should == 100
39
+ location.y.should == 100
40
+ location.z.should == -500
41
+ LocationsManager.instance.locations.size.should == 1
42
+ end
43
+
44
+ it "should allow updates to associated location" do
45
+ mesh = Mesh.new @test_x3d
46
+ LocationsManager.instance.clear
47
+ mesh.show_at :x => 100, :y => 100, :z => -500
48
+ location = mesh.show_at :x => 200
49
+ location.x.should == 200
50
+ location.y.should == 100
51
+ location.z.should == -500
52
+ LocationsManager.instance.locations.size.should == 1
53
+ end
54
+
55
+ it "should return absolute center" do
56
+ mesh = Mesh.new @test_x3d
57
+ mesh.op_translation = [10, 20, 30]
58
+ mesh.instance_variable_set(:@translation, [10, 20, 30])
59
+
60
+ center = mesh.center
61
+ center[0].should == 10 * 2
62
+ center[1].should == 20 * 2
63
+ center[2].should == 30 * 2
64
+ end
65
+
66
+ it "should return total scale" do
67
+ mesh = Mesh.new @test_x3d
68
+ mesh.op_scale = [10, 20, 30]
69
+ mesh.instance_variable_set(:@scale, [10, 20, 30])
70
+
71
+ scale = mesh.scale
72
+ scale[0].should == 10 * 10
73
+ scale[1].should == 20 * 20
74
+ scale[2].should == 30 * 30
75
+ end
76
+
77
+ it "should return boundaries" do
78
+ mesh = Mesh.new @test_x3d
79
+ boundaries = mesh.boundaries
80
+ boundaries[0...3].should == [1,1,1]
81
+ boundaries[3...6].should == [-1,-1,-1]
82
+ end
83
+
84
+ end
@@ -0,0 +1,13 @@
1
+ # rage project spec helper
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # Licensed under the GPLv3+ http://www.gnu.org/licenses/gpl.txt
5
+
6
+ require 'rubygems'
7
+ require 'spec'
8
+
9
+ CURRENT_DIR=File.dirname(__FILE__)
10
+ $: << File.expand_path(CURRENT_DIR + "/../lib")
11
+
12
+ require 'rage'
13
+ include RAGE
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rage
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Mohammed Morsi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-20 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: The Ruby Advanced Gaming Engine
17
+ email: movitto@yahoo.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - bin/mesh-viewer.rb
26
+ - lib/rage/color.rb
27
+ - lib/rage/resource.rb
28
+ - lib/rage/input.rb
29
+ - lib/rage/dsl.rb
30
+ - lib/rage/viewport.rb
31
+ - lib/rage/window.rb
32
+ - lib/rage/location.rb
33
+ - lib/rage/common.rb
34
+ - lib/rage/camera.rb
35
+ - lib/rage/loader.rb
36
+ - lib/rage/game.rb
37
+ - lib/rage/mesh.rb
38
+ - lib/rage.rb
39
+ - COPYING
40
+ - LICENSE
41
+ - Rakefile
42
+ - README.rdoc
43
+ - spec/spec_helper.rb
44
+ - spec/location_spec.rb
45
+ - spec/mesh_spec.rb
46
+ has_rdoc: true
47
+ homepage: http://morsi.org/projects/RAGE
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.8.1
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.3
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: The Ruby Advanced Gaming Engine
74
+ test_files: []
75
+