jacius / rubygame

Flexible cross-platform game programming library for Ruby

This URL has Read+Write access

rubygame / samples / demo_gl.rb
100755 152 lines (122 sloc) 3.343 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env ruby
 
# This demonstrates the use of ruby-opengl alongside rubygame to produce
# hardware-accelerated three-dimensional graphics.
#
# Please note that rubygame itself does not perform any OpenGL functions,
# it only allows ruby-opengl to use the Screen as its viewport. You MUST
# have ruby-opengl installed to run this demo!
 
require 'rubygame'
 
begin
  require 'opengl'
rescue LoadError
  puts "ATTENTION: This demo requires the opengl extension for ruby."
  raise
end
 
WIDE = 640
HIGH = 480
 
Rubygame.init
 
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)
 
Rubygame::Screen.set_mode([WIDE,HIGH], 16, [Rubygame::OPENGL])
queue = Rubygame::EventQueue.new()
clock = Rubygame::Clock.new { |c| c.target_framerate = 60 }
 
ObjectSpace.garbage_collect
GL::Viewport( 0, 0, WIDE, HIGH )
 
GL::MatrixMode( GL::PROJECTION )
GL::LoadIdentity( )
GLU::Perspective( 35, WIDE/(HIGH.to_f), 3, 10)
 
GL::MatrixMode( GL::MODELVIEW )
GL::LoadIdentity( )
 
GL::Enable(GL::DEPTH_TEST)
GL::DepthFunc(GL::LESS)
 
GL::ShadeModel(GL::FLAT)
 
color =
  [[ 1.0, 1.0, 0.0],
  [ 1.0, 0.0, 0.0],
  [ 0.0, 0.0, 0.0],
  [ 0.0, 1.0, 0.0],
  [ 0.0, 1.0, 1.0],
  [ 1.0, 1.0, 1.0],
  [ 1.0, 0.0, 1.0],
  [ 0.0, 0.0, 1.0]]
 
cube =
  [[ 0.5, 0.5, -0.5],
  [ 0.5, -0.5, -0.5],
  [-0.5, -0.5, -0.5],
  [-0.5, 0.5, -0.5],
  [-0.5, 0.5, 0.5],
  [ 0.5, 0.5, 0.5],
  [ 0.5, -0.5, 0.5],
  [-0.5, -0.5, 0.5]]
 
cube_list = 1
 
GL::NewList(cube_list,GL::COMPILE_AND_EXECUTE)
  GL::PushMatrix()
GL::Begin(GL::QUADS)
GL::Color(1.0, 0.0, 0.0);
GL::Vertex(cube[0]);
GL::Vertex(cube[1]);
GL::Vertex(cube[2]);
GL::Vertex(cube[3]);
 
GL::Color(0.0, 1.0, 0.0);
GL::Vertex(cube[3]);
GL::Vertex(cube[4]);
GL::Vertex(cube[7]);
GL::Vertex(cube[2]);
 
GL::Color(0.0, 0.0, 1.0);
GL::Vertex(cube[0]);
GL::Vertex(cube[5]);
GL::Vertex(cube[6]);
GL::Vertex(cube[1]);
 
GL::Color(0.0, 1.0, 1.0);
GL::Vertex(cube[5]);
GL::Vertex(cube[4]);
GL::Vertex(cube[7]);
GL::Vertex(cube[6]);
 
GL::Color(1.0, 1.0, 0.0);
GL::Vertex(cube[5]);
GL::Vertex(cube[0]);
GL::Vertex(cube[3]);
GL::Vertex(cube[4]);
 
GL::Color(1.0, 0.0, 1.0);
GL::Vertex(cube[6]);
GL::Vertex(cube[1]);
GL::Vertex(cube[2]);
GL::Vertex(cube[7]);
    GL::End()
 GL::PopMatrix()
GL::EndList()
 
angle = 0
 
catch(:rubygame_quit) do
loop do
queue.each do |event|
case event
when Rubygame::KeyDownEvent
case event.key
when Rubygame::K_ESCAPE
throw :rubygame_quit
when Rubygame::K_Q
throw :rubygame_quit
        end
      when Rubygame::QuitEvent
        throw :rubygame_quit
      end
end
 
    GL.ClearColor(0.0, 0.0, 0.0, 1.0);
GL.Clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
 
GL::MatrixMode(GL::MODELVIEW);
    GL::LoadIdentity( )
    GL::Translate(0, 0, -4)
    GL::Rotate(45, 0, 1, 0)
    GL::Rotate(45, 1, 0, 0)
    GL::Rotate(angle, 0.0, 0.0, 1.0)
    GL::Rotate(angle*2, 0.0, 1.0, 0.0)
 
    GL::CallList(cube_list)
 
Rubygame::GL.swap_buffers()
ObjectSpace.garbage_collect
 
    angle += clock.tick()/50.0
    angle -= 360 if angle >= 360
 
end
end