sgl 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. data/ChangeLog +234 -0
  2. data/Manifest.txt +94 -0
  3. data/Rakefile +16 -5
  4. data/examples/cocoa1-basic.rb +14 -0
  5. data/examples/cocoa10-transparent.rb +26 -0
  6. data/examples/cocoa11-application.rb +13 -0
  7. data/examples/cocoa2-draw.rb +23 -0
  8. data/examples/cocoa3-affine.rb +29 -0
  9. data/examples/cocoa4-font.rb +24 -0
  10. data/examples/cocoa5-image.rb +22 -0
  11. data/examples/cocoa6-sound.rb +24 -0
  12. data/examples/cocoa7-movie.rb +40 -0
  13. data/examples/cocoa8-movieoverlay.rb +42 -0
  14. data/examples/cocoa9-streaming.rb +40 -0
  15. data/examples/opengl1-basic.rb +14 -0
  16. data/examples/opengl11-application.rb +13 -0
  17. data/examples/opengl2-draw.rb +23 -0
  18. data/examples/opengl3-affine.rb +29 -0
  19. data/examples/sample1.rb +11 -0
  20. data/examples/sample10.rb +16 -0
  21. data/examples/sample11.rb +22 -0
  22. data/examples/sample11a.rb +33 -0
  23. data/examples/sample12.rb +30 -0
  24. data/examples/sample12a.rb +35 -0
  25. data/examples/sample13.rb +116 -0
  26. data/examples/sample15.rb +24 -0
  27. data/examples/sample16.rb +31 -0
  28. data/examples/sample16a.rb +35 -0
  29. data/examples/sample6.rb +18 -0
  30. data/examples/sample7.rb +16 -0
  31. data/examples/sample9.rb +20 -0
  32. data/examples/sgl-sound1.rb +6 -0
  33. data/examples/sgl-sound2.rb +19 -0
  34. data/examples/sine.ogg +0 -0
  35. data/examples/testgl.rb +169 -0
  36. data/lib/sgl/bass-api.rb +202 -0
  37. data/lib/sgl/bass-sample.rb +106 -0
  38. data/lib/sgl/bass.rb +46 -0
  39. data/lib/sgl/cocoa-app.rb +43 -0
  40. data/lib/sgl/cocoa-color.rb +65 -0
  41. data/lib/sgl/cocoa-draw.rb +72 -0
  42. data/lib/sgl/cocoa-event.rb +229 -0
  43. data/lib/sgl/cocoa-media.rb +144 -0
  44. data/lib/sgl/cocoa-notuse.rb +493 -0
  45. data/lib/sgl/cocoa-window.rb +203 -0
  46. data/lib/sgl/cocoa.rb +8 -0
  47. data/lib/sgl/notuse-objs.rb +358 -0
  48. data/lib/sgl/opengl-app.rb +38 -0
  49. data/lib/sgl/opengl-color.rb +44 -0
  50. data/lib/sgl/opengl-draw.rb +260 -0
  51. data/lib/sgl/opengl-event.rb +325 -0
  52. data/lib/sgl/opengl-modules.rb +22 -0
  53. data/lib/sgl/opengl-window.rb +224 -0
  54. data/lib/sgl/opengl.rb +7 -0
  55. data/lib/sgl/sgl-basic.rb +61 -0
  56. data/lib/sgl/sgl-button.rb +135 -0
  57. data/lib/sgl/sgl-client.rb +21 -0
  58. data/lib/sgl/sgl-color.rb +82 -0
  59. data/lib/sgl/sgl-connect.rb +9 -0
  60. data/lib/sgl/sgl-objlib.rb +0 -0
  61. data/lib/sgl/sgl-objs.rb +0 -0
  62. data/lib/sgl/sgl-server.rb +58 -0
  63. data/lib/sgl/sgl-sound.rb +24 -0
  64. data/lib/sgl/sgl-spring.rb +216 -0
  65. data/lib/sgl/sgl-timer.rb +72 -0
  66. data/lib/sgl/sgl.rb +5 -0
  67. data/lib/sgl/version.rb +2 -2
  68. data/scripts/makemanifest.rb +20 -0
  69. data/test/test_bass.rb +27 -0
  70. data/test/test_cocoa_app.rb +5 -5
  71. data/test/test_helper.rb +9 -1
  72. data/test/test_module_ruby16.rb +22 -21
  73. data/test/test_opengl_app.rb +4 -5
  74. data/test/test_opengl_basic.rb +3 -4
  75. data/test/test_opengl_fullscreen.rb +4 -6
  76. data/test/test_opengl_novice.rb +3 -6
  77. data/test/test_sgl.rb +4 -27
  78. data/test/test_sound.rb +15 -0
  79. data/website/challenge1.html +91 -0
  80. data/website/challenge1.txt +30 -0
  81. data/website/challenge2.html +144 -0
  82. data/website/challenge2.txt +73 -0
  83. data/website/challenge3.html +63 -0
  84. data/website/challenge3.txt +15 -0
  85. data/website/cocoa.html +88 -0
  86. data/website/cocoa.txt +35 -0
  87. data/website/description.html +138 -0
  88. data/website/description.txt +90 -0
  89. data/website/exercise1.html +264 -0
  90. data/website/exercise1.txt +192 -0
  91. data/website/exercise2.html +683 -0
  92. data/website/exercise2.txt +597 -0
  93. data/website/exhibition.html +84 -0
  94. data/website/exhibition.txt +24 -0
  95. data/website/howto.html +134 -0
  96. data/website/howto.txt +56 -0
  97. data/website/index.html +17 -9
  98. data/website/index.txt +13 -4
  99. data/website/sound.html +141 -0
  100. data/website/sound.txt +87 -0
  101. data/website/stylesheets/grade.png +0 -0
  102. data/website/stylesheets/screen.css +21 -20
  103. metadata +109 -3
@@ -0,0 +1,229 @@
1
+ # Copyright (C) 2004-2007 Kouichirou Eto, All rights reserved.
2
+ # License: Ruby License
3
+
4
+ module SGL
5
+ # callback functions
6
+ def setup() end
7
+ def onMouseDown(x,y) end
8
+ def onMouseUp(x,y) end
9
+ def onKeyDown(k) end
10
+ def onKeyUp(k) end
11
+ def display() end
12
+
13
+ # mainloop
14
+ def mainloop
15
+ $__a__.set_setup { setup }
16
+ $__a__.set_mousedown {|x, y| onMouseDown(x, y) }
17
+ $__a__.set_mouseup {|x, y| onMouseUp(x, y) }
18
+ $__a__.set_keydown {|k| onKeyDown(k) }
19
+ $__a__.set_keyup {|k| onKeyUp(k) }
20
+ $__a__.set_display { display }
21
+ $__a__.mainloop
22
+ end
23
+
24
+ # get status functions
25
+ def mouseX() $__a__.mouseX; end
26
+ def mouseY() $__a__.mouseY; end
27
+ def mouseDown() $__a__.mouseDown; end
28
+ def keynum() $__a__.keynum; end
29
+
30
+ class Application
31
+ def initialize_event
32
+ # block setting
33
+ @setup_done = nil
34
+ @display_drawing = nil
35
+ @display_overlay_drawing = nil
36
+ @block = {}
37
+ @delay_time = 1.0/60
38
+ @runtime = nil
39
+
40
+ # status setting
41
+ @mouseX, @mouseY = 0, 0
42
+ @mouseDown = 0
43
+ @keynum = 0
44
+ end
45
+ attr_accessor :runtime # for test
46
+
47
+ # get status
48
+ attr_reader :mouseX, :mouseY
49
+ attr_reader :mouseDown
50
+ attr_reader :keynum
51
+
52
+ # setup
53
+ def set_setup(&b)
54
+ return unless block_given?
55
+ @block[:setup] = Proc.new
56
+ end
57
+
58
+ def do_setup
59
+ setup_pre
60
+ @block[:setup].call if @block[:setup]
61
+ setup_post
62
+ end
63
+
64
+ def setup_pre
65
+ # do nothing
66
+ end
67
+
68
+ def setup_post
69
+ @setup_done = true
70
+ end
71
+ private :setup_pre, :setup_post
72
+
73
+ # display
74
+ def display_all(rect) # callback
75
+ return if @win.nil?
76
+ return if @setup_done.nil?
77
+ return if @display_drawing
78
+ @display_drawing = true
79
+ display_bg(rect)
80
+ display_pre
81
+ do_display
82
+ display_post
83
+ @display_drawing = nil
84
+ end
85
+
86
+ def set_display(&b)
87
+ return unless block_given?
88
+ @block[:display] = Proc.new
89
+ end
90
+
91
+ def set_display_overlay(&b)
92
+ return unless block_given?
93
+ @block[:display_overlay] = Proc.new
94
+ end
95
+
96
+ def do_display
97
+ return if @win.nil?
98
+ display_pre
99
+ @block[:display].call if @block[:display]
100
+ display_post
101
+ end
102
+
103
+ def display_pre
104
+ return if @win.nil?
105
+ pos = @win.mouseLocationOutsideOfEventStream
106
+ @mouseX, @mouseY = pos.x.to_i, pos.y.to_i
107
+ set_cur_color # set back current color
108
+ end
109
+
110
+ def display_bg(rect)
111
+ set_cur_bg
112
+ OSX::NSRectFill(rect)
113
+ end
114
+
115
+ def display_clear_bg(rect)
116
+ set_color(0, 0, 0, 0) # full transparent
117
+ OSX::NSRectFill(rect)
118
+ end
119
+
120
+ def display_post
121
+ # do nothing
122
+ end
123
+ private :display_pre, :display_bg, :display_clear_bg, :display_post
124
+
125
+ # sub view
126
+ def display_mov(rect)
127
+ display_clear_bg(rect)
128
+ set_cur_color # set back current color
129
+ end
130
+
131
+ def display_overlay_all(rect)
132
+ return if @win.nil?
133
+ return if @setup_done.nil?
134
+ return if @display_overlay_drawing
135
+ @display_overlay_drawing = true
136
+ display_clear_bg(rect)
137
+ set_cur_color # set back current color
138
+ @block[:display_overlay].call if @block[:display_overlay]
139
+ @display_overlay_drawing = nil
140
+ end
141
+
142
+ # mainloop
143
+ def mainloop
144
+ starttime = Time.now
145
+
146
+ do_setup
147
+
148
+ @thread = Thread.start {
149
+ loop {
150
+ need_display
151
+ delay
152
+
153
+ if check_runtime_finished(starttime)
154
+ stop
155
+ break
156
+ end
157
+ }
158
+ }
159
+
160
+ run
161
+ end
162
+
163
+ def check_runtime_finished(starttime)
164
+ diff = Time.now - starttime
165
+ return (@runtime && @runtime < diff)
166
+ end
167
+
168
+ def need_display
169
+ return if @app.nil?
170
+ @bgview.setNeedsDisplay(true) if @bgview
171
+ @oview.setNeedsDisplay(true) if @oview
172
+ end
173
+
174
+ def delay
175
+ sleep @delay_time
176
+ end
177
+ private :need_display, :delay
178
+
179
+ # mouse events
180
+ def set_mousedown(&b)
181
+ return unless block_given?
182
+ @block[:mousedown] = Proc.new
183
+ end
184
+
185
+ def do_mousedown
186
+ @mouseDown = 1
187
+ @block[:mousedown].call(@mouseX, @mouseY) if @block[:mousedown]
188
+ end
189
+
190
+ def set_mouseup(&b)
191
+ return unless block_given?
192
+ @block[:mouseup] = Proc.new
193
+ end
194
+
195
+ def do_mouseup
196
+ @mouseDown = 0
197
+ @block[:mouseup].call(@mouseX, @mouseY) if @block[:mouseup]
198
+ end
199
+
200
+ # key events
201
+ def set_keydown(&b)
202
+ return unless block_given?
203
+ @block[:keydown] = Proc.new
204
+ end
205
+
206
+ def do_keydown(e)
207
+ calc_keynum(e)
208
+ @block[:keydown].call(@keynum) if @block[:keydown]
209
+ end
210
+
211
+ def set_keyup(&b)
212
+ return unless block_given?
213
+ @block[:keyup] = Proc.new
214
+ end
215
+
216
+ def do_keyup(e)
217
+ calc_keynum(e)
218
+ @keynum = 0
219
+ @block[:keyup].call(@keynum) if @block[:keyup]
220
+ end
221
+
222
+ def calc_keynum(e)
223
+ #input = event.characters
224
+ input = e.characters
225
+ @keynum = input.to_s[0]
226
+ end
227
+ private :calc_keynum
228
+ end
229
+ end
@@ -0,0 +1,144 @@
1
+ # Copyright (C) 2004-2007 Kouichirou Eto, All rights reserved.
2
+ # License: Ruby License
3
+
4
+ require "kconv"
5
+
6
+ module SGL
7
+ def movie(*a) $__a__.movie(*a); end
8
+ def image(*a) $__a__.image(*a); end
9
+ def font(*a) $__a__.font(*a); end
10
+ def sound(*a) $__a__.sound(*a); end
11
+
12
+ class Application
13
+ def movie(u)
14
+ if /\Ahttp:\/\// =~ u || /\Artsp:\/\// =~ u
15
+ url = OSX::NSURL.URLWithString_(u)
16
+ else
17
+ url = OSX::NSURL.fileURLWithPath_(u)
18
+ end
19
+ mov = OSX::NSMovie.alloc.initWithURL(url, :byReference, true)
20
+ # Place the movie on the out of screen.
21
+ obj = NSMovieView.alloc.initWithFrame([-100.0, -100.0, 10.0, 10.0])
22
+ obj.setApp(self)
23
+ obj.setMovie(mov)
24
+ obj.showController(false, :adjustingSize, false)
25
+ view = @options[:movie] ? @movview : @bgview
26
+ # This "p" is necessary to show the movie. I don't know why.
27
+ p [@options[:movie], view]
28
+ # p view # This does not work.
29
+ # dummy = view.inspect # This does not work also.
30
+ view.addSubview(obj)
31
+ obj
32
+ end
33
+
34
+ def image(file)
35
+ img = NSImage.alloc.initWithContentsOfFile(file)
36
+ img.setApp(self)
37
+ img
38
+ end
39
+
40
+ def font(*a)
41
+ NSFont.new(self, *a)
42
+ end
43
+
44
+ def sound(file)
45
+ url = OSX::NSURL.fileURLWithPath_(file)
46
+ snd = NSSound.alloc.initWithContentsOfURL(url, :byReference, true)
47
+ snd
48
+ end
49
+ end
50
+
51
+ class NSMovieView < OSX::NSMovieView
52
+ include FrameTranslator
53
+
54
+ def setApp(app)
55
+ @app = app
56
+ @playing = false
57
+ end
58
+
59
+ def rect(a,b,c,d)
60
+ frame(*to_xywh(a, b, c, d))
61
+ #frame(*@app.to_xywh(a, b, c, d))
62
+ end
63
+
64
+ def frame(a,b,c,d)
65
+ setFrame([a, b, c, d])
66
+ end
67
+
68
+ def play
69
+ return if @playing
70
+ @playing = true
71
+ start_
72
+ end
73
+
74
+ def stop
75
+ return if ! @playing
76
+ @playing = false
77
+ stop_
78
+ end
79
+
80
+ def goBegin() gotoBeginning_; end
81
+ def goEnd() gotoEnd_; end
82
+ def forward() stepForward_; end
83
+ def back() stepBack_; end
84
+ def loop=(a) setLoopMode(a); end
85
+ def rate=(r) setRate(r/100.0); end
86
+ def volume=(v) setVolume(v/100.0); end
87
+ end
88
+
89
+ class NSImage < OSX::NSImage
90
+ include FrameTranslator
91
+
92
+ def setApp(app) @app = app; end
93
+
94
+ def rect(a,b,c,d)
95
+ frame(*to_xywh(a, b, c, d))
96
+ end
97
+
98
+ def frame(x,y,w,h)
99
+ drawInRect([x,y,w,h],
100
+ :fromRect, [0,0,size.width,size.height],
101
+ :operation, OSX::NSCompositeSourceOver,
102
+ :fraction, @app.get_cur_color_alpha)
103
+ end
104
+ end
105
+
106
+ class NSFont
107
+ def initialize(w, n="Helvetica", s=0.0)
108
+ @app, @name, @size = w, n, s.abs
109
+ end
110
+ attr_accessor :name
111
+ attr_reader :size
112
+
113
+ def size=(s)
114
+ @size = s.abs
115
+ end
116
+
117
+ def text(x, y, str)
118
+ return unless str.is_a? String
119
+ str = NKF.nkf("-m0 -s", str)
120
+ str = OSX::NSMutableAttributedString.alloc.initWithString(str)
121
+ str.addAttribute(OSX::NSFontAttributeName(),
122
+ :value, OSX::NSFont.fontWithName(@name, :size, @size),
123
+ :range, [0,str.length])
124
+ color = @app.make_cur_color
125
+ str.addAttribute(OSX::NSForegroundColorAttributeName(),
126
+ :value, color,
127
+ :range, [0,str.length])
128
+ str.drawAtPoint([x, y])
129
+ end
130
+
131
+ def show_fixed() show(OSX::NSFixedPitchFontMask); end
132
+ def show_all() show; end
133
+
134
+ private
135
+ def show(mask=0)
136
+ fmgr = OSX::NSFontManager.sharedFontManager
137
+ fonts = fmgr.availableFontNamesWithTraits(mask).to_a.map{|i| i.to_s }.sort
138
+ puts fonts
139
+ end
140
+ end
141
+
142
+ class NSSound < OSX::NSSound
143
+ end
144
+ end