tea 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/COPYING +674 -0
  2. data/COPYING.LESSER +165 -0
  3. data/README.rdoc +93 -0
  4. data/doc/example/bitmap_draw.rb +21 -0
  5. data/doc/example/bitmap_load.rb +14 -0
  6. data/doc/example/bitmap_new.rb +19 -0
  7. data/doc/example/circle.rb +24 -0
  8. data/doc/example/circle_alpha.rb +45 -0
  9. data/doc/example/circle_alpha_bitmap.rb +51 -0
  10. data/doc/example/circle_bitmap.rb +18 -0
  11. data/doc/example/clip.rb +46 -0
  12. data/doc/example/event_app.rb +45 -0
  13. data/doc/example/event_keyboard.rb +43 -0
  14. data/doc/example/event_mouse.rb +85 -0
  15. data/doc/example/font_hello.rb +22 -0
  16. data/doc/example/font_word_wrap.rb +44 -0
  17. data/doc/example/grab.rb +28 -0
  18. data/doc/example/init.rb +10 -0
  19. data/doc/example/lines.rb +49 -0
  20. data/doc/example/lines_aa.rb +44 -0
  21. data/doc/example/lines_alpha.rb +33 -0
  22. data/doc/example/point.rb +26 -0
  23. data/doc/example/rect.rb +15 -0
  24. data/doc/example/rect_alpha.rb +75 -0
  25. data/doc/example/screen_set_mode.rb +18 -0
  26. data/doc/example/screen_update.rb +14 -0
  27. data/doc/example/sfont_hello.rb +22 -0
  28. data/doc/example/smile.png +0 -0
  29. data/doc/example/smile_bounce.rb +44 -0
  30. data/doc/example/smile_move.rb +58 -0
  31. data/doc/example/smile_move_2.rb +78 -0
  32. data/doc/example/sound.rb +101 -0
  33. data/doc/example/state_app.rb +33 -0
  34. data/doc/example/state_keyboard.rb +23 -0
  35. data/doc/example/state_mouse.rb +60 -0
  36. data/doc/key_constants.textile +129 -0
  37. data/doc/key_modifiers.textile +19 -0
  38. data/doc/reference.textile +421 -0
  39. data/lib/tea.rb +34 -0
  40. data/lib/tea/c_bitmap.rb +122 -0
  41. data/lib/tea/c_error.rb +11 -0
  42. data/lib/tea/c_font.rb +302 -0
  43. data/lib/tea/c_sound.rb +144 -0
  44. data/lib/tea/m_color.rb +50 -0
  45. data/lib/tea/m_event.rb +65 -0
  46. data/lib/tea/m_event_app.rb +96 -0
  47. data/lib/tea/m_event_dispatch.rb +54 -0
  48. data/lib/tea/m_event_keyboard.rb +311 -0
  49. data/lib/tea/m_event_mouse.rb +189 -0
  50. data/lib/tea/mix_blitting.rb +31 -0
  51. data/lib/tea/mix_clipping.rb +71 -0
  52. data/lib/tea/mix_grabbing.rb +86 -0
  53. data/lib/tea/mix_image_saving.rb +70 -0
  54. data/lib/tea/mix_primitive.rb +613 -0
  55. data/lib/tea/o_screen.rb +98 -0
  56. metadata +137 -0
@@ -0,0 +1,26 @@
1
+ # Test that Tea can plot points on bitmaps, e.g. the screen.
2
+ # Expected result is 4 points drawn as a rectangle in a 400x300 window for 2
3
+ # seconds.
4
+
5
+ require 'tea'
6
+
7
+ puts <<TEST
8
+ 4 coloured points should appear in a rectangle formation, in a 400x300 window
9
+ for 2 seconds. Point formation:
10
+
11
+ red --- green
12
+ | |
13
+ blue -- white
14
+ TEST
15
+
16
+ Tea.init
17
+ Tea::Screen.set_mode 400, 300
18
+
19
+ Tea::Screen[100, 75] = Tea::Color::RED
20
+ Tea::Screen[300, 75] = Tea::Color::GREEN
21
+ Tea::Screen[100, 225] = Tea::Color::BLUE
22
+ Tea::Screen[300, 225] = Tea::Color::WHITE
23
+
24
+ Tea::Screen.update
25
+
26
+ sleep 2
@@ -0,0 +1,15 @@
1
+ # Test that filled rectangles can be drawn.
2
+ # Expected results are a 400x300 window, with a filled yellow rectangle in it.
3
+
4
+ require 'tea'
5
+
6
+ puts <<TEST
7
+ A 400x300 window should appear with a yellow rectangle for 2 seconds.
8
+ TEST
9
+
10
+ Tea.init
11
+ Tea::Screen.set_mode 400, 300
12
+ Tea::Screen.rect 50, 50, 300, 200, Tea::Color::YELLOW
13
+ Tea::Screen.update
14
+
15
+ sleep 2
@@ -0,0 +1,75 @@
1
+ # Test that alpha for rectangles works.
2
+ # Expected results are:
3
+ #
4
+ # * center: red rectangle (background)
5
+ # * top-left: translucent green rectangle
6
+ # * bottom-left: translucent white rectangle
7
+ # * bottom-right: solid blue rectangle
8
+
9
+ require 'tea'
10
+
11
+ puts <<TEST
12
+ You should see:
13
+
14
+ * center: red rectangle (background)
15
+ * top-left: translucent green rectangle
16
+ * bottom-left: translucent white rectangle
17
+ * bottom-right: solid blue rectangle
18
+
19
+ A smaller box with these same rectangles should appear in the top-right. It
20
+ can be moved with the arrow keys.
21
+
22
+ Press ESC key to exit.
23
+ TEST
24
+
25
+ Tea.init
26
+ Tea::Screen.set_mode 400, 300
27
+
28
+ TL_GREEN = Tea::Color.mix( 0, 255, 0, 128)
29
+ TL_BLUE = Tea::Color.mix( 0, 0, 255, 128)
30
+ TL_WHITE = Tea::Color.mix(255, 255, 255, 128)
31
+
32
+ x, y = 205, 15
33
+ up, down, left, right = false, false, false, false
34
+ wait_for_event = true
35
+
36
+ b = Tea::Bitmap.new(180, 130, Tea::Color::CLEAR)
37
+ b.rect 45, 32, 90, 65, Tea::Color::RED
38
+ b.rect 0, 0, 90, 65, TL_GREEN, :mix => :blend
39
+ b.rect 90, 65, 90, 65, TL_BLUE, :mix => :replace
40
+ b.rect 0, 65, 90, 65, TL_WHITE
41
+
42
+ begin
43
+ Tea::Screen.clear
44
+
45
+ Tea::Screen.rect 100, 75, 200, 150, Tea::Color::RED
46
+ Tea::Screen.rect 0, 0, 200, 150, TL_GREEN, :mix => :blend
47
+ Tea::Screen.rect 200, 150, 200, 150, TL_BLUE, :mix => :replace
48
+ Tea::Screen.rect 0, 150, 200, 150, TL_WHITE
49
+
50
+ Tea::Screen.blit b, x, y
51
+
52
+ Tea::Screen.update
53
+ e = Tea::Event.get(wait_for_event)
54
+ case e
55
+ when Tea::Kbd::Down
56
+ case e.key
57
+ when Tea::Kbd::UP then up = true
58
+ when Tea::Kbd::DOWN then down = true
59
+ when Tea::Kbd::LEFT then left = true
60
+ when Tea::Kbd::RIGHT then right = true
61
+ end
62
+ when Tea::Kbd::Up
63
+ case e.key
64
+ when Tea::Kbd::UP then up = false
65
+ when Tea::Kbd::DOWN then down = false
66
+ when Tea::Kbd::LEFT then left = false
67
+ when Tea::Kbd::RIGHT then right = false
68
+ end
69
+ end
70
+ wait_for_event = !(up || down || left || right)
71
+ x -= 1 if left
72
+ x += 1 if right
73
+ y -= 1 if up
74
+ y += 1 if down
75
+ end until e.class == Tea::App::Exit || (e.class == Tea::Kbd::Down && e.key == Tea::Kbd::ESCAPE)
@@ -0,0 +1,18 @@
1
+ # Test if the screen can initialise.
2
+ # Expected results:
3
+ #
4
+ # * output of "screen size is 320x240"
5
+ # * a blank 320x240 display window for 2 seconds.
6
+
7
+ require 'tea'
8
+
9
+ puts <<TEST
10
+ You should see a 320x240 window for 2 seconds, and the text "screen size is 320x240"
11
+ TEST
12
+
13
+ Tea.init
14
+ Tea::Screen.set_mode 320, 240
15
+
16
+ puts "screen size is #{Tea::Screen.w}x#{Tea::Screen.h}"
17
+
18
+ sleep 2
@@ -0,0 +1,14 @@
1
+ # Test if the screen can be updated without problems.
2
+ # Expected result is a 320x240 graphical window shown for 2 seconds.
3
+
4
+ require 'tea'
5
+
6
+ puts <<TEST
7
+ You should see a 320x240 window appear for 2 seconds.
8
+ TEST
9
+
10
+ Tea.init
11
+ Tea::Screen.set_mode 320, 240
12
+ Tea::Screen.update
13
+
14
+ sleep 2
@@ -0,0 +1,22 @@
1
+ # This tests that SFonts can be loaded, rendered and drawn.
2
+ # Expected result: 'hello' in a small window for 5 seconds.
3
+
4
+ require 'tea'
5
+
6
+ puts <<TEST
7
+ You should see 'hello world' in a small window for 5 seconds.
8
+ TEST
9
+
10
+ Tea.init
11
+ Tea::Screen.set_mode 320, 240
12
+
13
+ font = Tea::Font.new('sfont.png', Tea::Font::SFONT)
14
+ message = 'hello world'
15
+
16
+ x = (Tea::Screen.w - font.string_w(message)) / 2
17
+ y = (Tea::Screen.h - font.h) / 2
18
+ font.draw_to Tea::Screen, x, y, message
19
+
20
+ Tea::Screen.update
21
+
22
+ sleep 5
Binary file
@@ -0,0 +1,44 @@
1
+ # This is a demo of 5 smiley faces bouncing around a small screen.
2
+
3
+ require 'tea'
4
+
5
+ puts <<TEST
6
+ You should see 5 smiley faces bouncing around on a 320x240 screen for 5 seconds.
7
+ TEST
8
+
9
+ class Smiley
10
+ def initialize(bitmap, start_x=0, start_y=0)
11
+ @bitmap = bitmap
12
+ @x = start_x
13
+ @y = start_y
14
+ @dx = rand() * 2 - 1
15
+ @dy = rand() * 2 - 1
16
+ end
17
+
18
+ def update
19
+ @x += @dx
20
+ @y += @dy
21
+ @dx = -@dx if @x < 0 || @x + @bitmap.w >= Tea::Screen.w
22
+ @dy = -@dy if @y < 0 || @y + @bitmap.h >= Tea::Screen.h
23
+ end
24
+
25
+ def draw
26
+ Tea::Screen.blit @bitmap, @x, @y
27
+ end
28
+ end
29
+
30
+ Tea.init
31
+ Tea::Screen.set_mode 320, 240
32
+
33
+ smile_bitmap = Tea::Bitmap.new('smile.png')
34
+ smiles = []
35
+ 5.times { smiles << Smiley.new(smile_bitmap, rand(288), rand(208)) }
36
+
37
+ start = Time.now
38
+ until Time.now >= start + 5
39
+ smiles.each { |s| s.update }
40
+ Tea::Screen.clear
41
+ smiles.each { |s| s.draw }
42
+ Tea::Screen.update
43
+ sleep 0.001
44
+ end
@@ -0,0 +1,58 @@
1
+ # Move the Smiley face with the arrow keys. Press Esc to exit.
2
+
3
+ require 'tea'
4
+
5
+ class Smile
6
+ def initialize
7
+ @bitmap = Tea::Bitmap.new('smile.png')
8
+ @x = (Tea::Screen.w - @bitmap.w) / 2
9
+ @y = (Tea::Screen.h - @bitmap.h) / 2
10
+ @dx = @dy = 0
11
+ end
12
+
13
+ def n(move) @dy += move ? -1 : 1 end
14
+ def s(move) @dy += move ? 1 : -1 end
15
+ def e(move) @dx += move ? 1 : -1 end
16
+ def w(move) @dx += move ? -1 : 1 end
17
+ def stopped?; @dx == 0 && @dy == 0; end
18
+
19
+ def update
20
+ @x += @dx
21
+ @y += @dy
22
+ end
23
+
24
+ def draw
25
+ Tea::Screen.blit @bitmap, @x, @y
26
+ end
27
+ end
28
+
29
+ Tea.init
30
+ Tea::Screen.set_mode 640, 480
31
+ player = Smile.new
32
+ wait_for_event = true
33
+ loop do
34
+ player.update
35
+ Tea::Screen.clear
36
+ player.draw
37
+ Tea::Screen.update
38
+
39
+ if e = Tea::Event.get(wait_for_event)
40
+ break if e.class == Tea::App::Exit
41
+
42
+ case e
43
+ when Tea::Kbd::Down then move = true
44
+ when Tea::Kbd::Up then move = false
45
+ else next
46
+ end
47
+
48
+ case e.key
49
+ when Tea::Kbd::UP then player.n(move)
50
+ when Tea::Kbd::DOWN then player.s(move)
51
+ when Tea::Kbd::RIGHT then player.e(move)
52
+ when Tea::Kbd::LEFT then player.w(move)
53
+ when Tea::Kbd::ESCAPE then break
54
+ end
55
+
56
+ wait_for_event = player.stopped?
57
+ end
58
+ end
@@ -0,0 +1,78 @@
1
+ # Move the Smiley with the arrow keys, and press Esc to exit. Identical to
2
+ # smile_move.rb, but uses Tea::Event::Dispatch in a controlling class to handle
3
+ # events.
4
+
5
+ require 'tea'
6
+
7
+ class Smile
8
+ def initialize
9
+ @bitmap = Tea::Bitmap.new('smile.png')
10
+ @x = (Tea::Screen.w - @bitmap.w) / 2
11
+ @y = (Tea::Screen.h - @bitmap.h) / 2
12
+ @dx = @dy = 0
13
+ end
14
+
15
+ def n(move) @dy += move ? -1 : 1 end
16
+ def s(move) @dy += move ? 1 : -1 end
17
+ def e(move) @dx += move ? 1 : -1 end
18
+ def w(move) @dx += move ? -1 : 1 end
19
+ def stopped?; @dx == 0 && @dy == 0; end
20
+
21
+ def update
22
+ @x += @dx
23
+ @y += @dy
24
+ end
25
+
26
+ def draw
27
+ Tea::Screen.blit @bitmap, @x, @y
28
+ end
29
+ end
30
+
31
+ class SmileControllingState
32
+ def initialize
33
+ @player = Smile.new
34
+ @done = false
35
+ end
36
+
37
+ def done?; @done; end
38
+ def update; @player.update; end
39
+ def need_update?; !@player.stopped?; end
40
+ def draw
41
+ Tea::Screen.clear
42
+ @player.draw
43
+ end
44
+
45
+ include Tea::Event::Dispatch
46
+
47
+ def kbd_down(e)
48
+ case e.key
49
+ when Tea::Kbd::UP then @player.n true
50
+ when Tea::Kbd::DOWN then @player.s true
51
+ when Tea::Kbd::LEFT then @player.w true
52
+ when Tea::Kbd::RIGHT then @player.e true
53
+ when Tea::Kbd::ESCAPE then @done = true
54
+ end
55
+ end
56
+
57
+ def kbd_up(e)
58
+ case e.key
59
+ when Tea::Kbd::UP then @player.n false
60
+ when Tea::Kbd::DOWN then @player.s false
61
+ when Tea::Kbd::LEFT then @player.w false
62
+ when Tea::Kbd::RIGHT then @player.e false
63
+ end
64
+ end
65
+ end
66
+
67
+ Tea.init
68
+ Tea::Screen.set_mode 640, 480
69
+ state = SmileControllingState.new
70
+ until state.done?
71
+ state.update
72
+ state.draw
73
+ Tea::Screen.update
74
+
75
+ if e = Tea::Event.get(!state.need_update?)
76
+ state.dispatch_event e
77
+ end
78
+ end
@@ -0,0 +1,101 @@
1
+ # A demo that attempts to test all of Tea::Sound's functionality.
2
+
3
+ require 'tea'
4
+
5
+ puts <<TEST
6
+ This is a simple sound demo. Up to 2 sounds can play simultaneously.
7
+
8
+ Keys:
9
+
10
+ [ - Switch to tone 1 (default)
11
+ ] - Switch to tone 2
12
+
13
+ Right - Play tone
14
+ Left - Stop
15
+ Down - Pause
16
+ Up - Resume (unpause)
17
+
18
+ Enter - Print tone's status (stopped/playing/paused)
19
+
20
+ p - Pause all
21
+ r - Resume (unpause) all
22
+ s - Stop all
23
+
24
+ = - Tone volume +1/8
25
+ - - Tone volume -1/8
26
+ + - Master volume +1/8
27
+ _ - Master volume -1/8
28
+
29
+ Esc - Exit demo
30
+ --
31
+ TEST
32
+
33
+ Tea.init
34
+ Tea::Screen.set_mode 100, 100
35
+
36
+ tone_sound = Tea::Sound.new('tone.wav')
37
+ tones = [tone_sound, tone_sound.clone]
38
+
39
+ which_tone = 0
40
+
41
+ done = false
42
+ until done
43
+ e = Tea::Event.get(true)
44
+ case e
45
+ when Tea::Kbd::Down
46
+ case e.key
47
+
48
+ when Tea::Kbd::OPEN_SQUARE_BRACKET
49
+ which_tone -= 1 if which_tone > 0
50
+ puts "Switched to tone ##{which_tone + 1}"
51
+ when Tea::Kbd::CLOSE_SQUARE_BRACKET
52
+ which_tone += 1 if which_tone < tones.length - 1
53
+ puts "Switched to tone ##{which_tone + 1}"
54
+
55
+ when Tea::Kbd::RIGHT
56
+ tones[which_tone].play
57
+ puts "Playing tone ##{which_tone + 1}"
58
+ when Tea::Kbd::LEFT
59
+ tones[which_tone].stop
60
+ puts "Stopping tone ##{which_tone + 1}"
61
+ when Tea::Kbd::DOWN
62
+ tones[which_tone].pause
63
+ puts "Pausing tone ##{which_tone + 1}"
64
+ when Tea::Kbd::UP
65
+ tones[which_tone].resume
66
+ puts "Resuming tone ##{which_tone + 1}"
67
+
68
+ when Tea::Kbd::ENTER
69
+ puts "Tone ##{which_tone + 1} is #{tones[which_tone].state.to_s}"
70
+
71
+ when Tea::Kbd::P
72
+ Tea::Sound.pause_all
73
+ puts "Pausing all sounds"
74
+ when Tea::Kbd::R
75
+ Tea::Sound.resume_all
76
+ puts "Resuming all sounds"
77
+ when Tea::Kbd::S
78
+ Tea::Sound.stop_all
79
+ puts "Stopping all sounds"
80
+
81
+ when Tea::Kbd::EQUALS
82
+ tones[which_tone].volume += 16
83
+ puts "Volume of tone ##{which_tone + 1} raised to #{tones[which_tone].volume} / 128"
84
+ when Tea::Kbd::MINUS
85
+ tones[which_tone].volume -= 16
86
+ puts "Volume of tone ##{which_tone + 1} lowered to #{tones[which_tone].volume} / 128"
87
+ when Tea::Kbd::PLUS
88
+ Tea::Sound.volume += 16
89
+ puts "Master volume raised to #{Tea::Sound.volume} / 128"
90
+ when Tea::Kbd::UNDERSCORE
91
+ Tea::Sound.volume -= 16
92
+ puts "Master volume lowered to #{Tea::Sound.volume} / 128"
93
+
94
+ when Tea::Kbd::ESCAPE
95
+ done = true
96
+
97
+ end
98
+ when Tea::App::Exit
99
+ done = true
100
+ end
101
+ end