tung-tea 0.1.0 → 0.1.4

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.
@@ -0,0 +1,19 @@
1
+ # Test that Bitmaps can be created without an image file.
2
+ # Expected results are a red square inside a cyan square.
3
+
4
+ require 'tea'
5
+
6
+ puts <<TEST
7
+ You should see a red square in a cyan square for 2 seconds.
8
+ TEST
9
+
10
+ Tea.init
11
+ a = Tea::Bitmap.new(200, 200, 0x00ffffff)
12
+ Tea::Screen.set_mode 400, 300
13
+ b = Tea::Bitmap.new(150, 150, 0xff0000ff)
14
+
15
+ Tea::Screen.blit a, 100, 50
16
+ Tea::Screen.blit b, 125, 75
17
+ Tea::Screen.update
18
+
19
+ sleep 2
@@ -1,3 +1,5 @@
1
+ # Move the Smiley face with the arrow keys. Press Esc to exit.
2
+
1
3
  require 'tea'
2
4
 
3
5
  class Smile
@@ -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
@@ -34,9 +34,14 @@ Tea::PrimitiveDrawing:
34
34
 
35
35
  Note that colours are of the form 0xRRGGBBAA.
36
36
 
37
- h3. Tea::Bitmap.new(image_path)
37
+ h3. Tea::Bitmap.new(*args)
38
38
 
39
- Create a new Bitmap from an image file. May raise Tea::Error if it fails.
39
+ Create a new Bitmap. This can be done in 2 ways:
40
+
41
+ 1. Tea::Bitmap.new(image_path): load from an image file.
42
+ 2. Tea::Bitmap.new(width, height, color): create with the given size and colour.
43
+
44
+ ArgumentError is raised if you don't call it in one of these ways. Tea::Error may be raised if one of these initialisations fail.
40
45
 
41
46
  h3. Tea::Bitmap#w()
42
47
 
@@ -134,6 +139,52 @@ These returned event objects may have methods that give extra information about
134
139
 
135
140
  May raise Tea::Error if getting an event fails.
136
141
 
142
+ h2. Tea::Event::Dispatch mixin
143
+
144
+ This mixin gives the class a dispatch_event method that, when called, will call a specially-named method that matches the event's class name.
145
+
146
+ <pre>
147
+ require 'tea'
148
+
149
+ class MyEventHandler
150
+ attr_reader :done
151
+ def initialize; @done = false; end
152
+
153
+ include Tea::Event::Dispatch
154
+
155
+ def kbd_down(e)
156
+ puts "You typed: #{e.char}"
157
+ if e.key == Tea::Kbd::ESCAPE
158
+ puts 'Bye bye!'
159
+ @done = true
160
+ end
161
+ end
162
+ end
163
+
164
+ Tea.init
165
+ Tea::Screen.set_mode 400, 300
166
+
167
+ handler = MyEventHandler.new
168
+ until handler.done
169
+ handler.dispatch_event Tea::Event.get(true)
170
+ end
171
+ </pre>
172
+
173
+ This example shows:
174
+
175
+ 1. the Tea::Event::Dispatch mixin being included in a class,
176
+ 2. a sample event handling function kbd_down, matching Tea::Kbd::Down, and
177
+ 3. dispatch_event being called with an event passed into it.
178
+
179
+ Event names are matched to method names as follows:
180
+
181
+ 1. Get the original class name, e.g. Tea::Kbd::Down.
182
+ 2. Get rid of the 'Tea::' part, e.g. Kbd::Down.
183
+ 3. Substitute the '::' for '_', e.g. Kbd_Down.
184
+ 4. Convert to all lower case, e.g. kbd_down.
185
+
186
+ This mixin may not be needed for simpler games, but it can help when using classes to organise game code.
187
+
137
188
  h2. Tea::App
138
189
 
139
190
  As well as the app-related events that spawn from here, this module also provides some app-related status checking.
data/lib/tea/c_bitmap.rb CHANGED
@@ -12,21 +12,19 @@ module Tea
12
12
  # A Bitmap is a grid of pixels that holds graphics.
13
13
  class Bitmap
14
14
 
15
- # Create a new Bitmap from an image file.
15
+ # Create a new Bitmap. This can be done in 2 ways:
16
16
  #
17
- # May raise Tea::Error if it fails.
18
- def initialize(image_path)
19
- begin
20
- @buffer = SDL::Surface.load(image_path)
21
-
22
- # Optimise for the screen mode, potentially making screen blits faster.
23
- if Tea::Screen.mode_set?
24
- @buffer = @buffer.display_format_alpha
25
- else
26
- Tea::Screen.on_set_mode lambda { @buffer = @buffer.display_format_alpha }
27
- end
28
- rescue SDL::Error => e
29
- raise Tea::Error, e.message, e.backtrace
17
+ # (image_path):: loads from an image
18
+ # (width, height, color):: creates with the given size and color
19
+ #
20
+ # May raise ArgumentError if the arguments passed in don't match one of the
21
+ # above, or Tea::Error if the Bitmap creation fails.
22
+ def initialize(*args)
23
+ case args.length
24
+ when 1 then from_image(*args)
25
+ when 3 then fresh(*args)
26
+ else
27
+ raise ArgumentError, "wrong number of arguments (#{args.length} for 1 or 3)", caller
30
28
  end
31
29
  end
32
30
 
@@ -50,6 +48,53 @@ module Tea
50
48
  @buffer
51
49
  end
52
50
 
51
+ private
52
+
53
+ # Create a new Bitmap from an image file.
54
+ #
55
+ # May raise Tea::Error if it fails.
56
+ def from_image(image_path)
57
+ @buffer = SDL::Surface.load(image_path)
58
+
59
+ # Optimise for the screen mode, potentially making screen blits faster.
60
+ if Tea::Screen.mode_set?
61
+ @buffer = @buffer.display_format_alpha
62
+ else
63
+ Tea::Screen.on_set_mode lambda { @buffer = @buffer.display_format_alpha }
64
+ end
65
+ rescue SDL::Error => e
66
+ raise Tea::Error, e.message, e.backtrace
67
+ end
68
+
69
+ # Create a new Bitmap of the given size and initialized with the color.
70
+ #
71
+ # May raise Tea::Error if it fails.
72
+ def fresh(width, height, color)
73
+ if width < 1 || height < 1
74
+ raise Tea::Error, "can't create bitmap smaller than 1x1 (#{width}x#{height})", caller
75
+ end
76
+
77
+ # Default to little endian pixel format, because it's what Intel uses,
78
+ # and Intel arches are common.
79
+ rmask = 0x000000ff
80
+ gmask = 0x0000ff00
81
+ bmask = 0x00ff0000
82
+ amask = 0xff000000
83
+ @buffer = SDL::Surface.new(SDL::SWSURFACE | SDL::SRCALPHA,
84
+ width, height, 32,
85
+ rmask, gmask, bmask, amask)
86
+ rect 0, 0, w, h, color
87
+
88
+ # Optimise for the screen mode, now or later.
89
+ if Tea::Screen.mode_set?
90
+ @buffer = @buffer.display_format_alpha
91
+ else
92
+ Tea::Screen.on_set_mode lambda { @buffer = @buffer.display_format_alpha }
93
+ end
94
+ rescue SDL::Error => e
95
+ raise Tea::Error, e.message, e.backtrace
96
+ end
97
+
53
98
  end
54
99
 
55
100
  end
data/lib/tea/m_event.rb CHANGED
@@ -4,6 +4,7 @@ require 'sdl'
4
4
 
5
5
  require 'tea/c_error'
6
6
  require 'tea/m_event_app'
7
+ require 'tea/m_event_dispatch'
7
8
  require 'tea/m_event_keyboard'
8
9
  require 'tea/m_event_mouse'
9
10
 
@@ -2,15 +2,6 @@
2
2
 
3
3
  require 'sdl'
4
4
 
5
- # A hot-patch to Ruby/SDL for missing SDL::Event::Active#state constants.
6
- module SDL
7
- class Event
8
- APPMOUSEFOCUS = 0x01
9
- APPINPUTFOCUS = 0x02
10
- APPACTIVE = 0x04
11
- end
12
- end
13
-
14
5
  #
15
6
  module Tea
16
7
 
@@ -61,6 +52,11 @@ module Tea
61
52
 
62
53
  module Event
63
54
 
55
+ # APP constants rubysdl is missing. For internal use only.
56
+ APPMOUSEFOCUS_ = 0x01
57
+ APPINPUTFOCUS_ = 0x02
58
+ APPACTIVE_ = 0x04
59
+
64
60
  # Translates an app-related SDL::Event into an array of Tea::Event
65
61
  # objects. For internal use only.
66
62
  def Event.translate_app_event(sdl_event)
@@ -71,13 +67,13 @@ module Tea
71
67
  out_events.push App::Exit.new
72
68
 
73
69
  when SDL::Event::Active
74
- if (sdl_event.state & SDL::Event::APPACTIVE) != 0
70
+ if (sdl_event.state & APPACTIVE_) != 0
75
71
  out_events.push sdl_event.gain ? App::Restored.new : App::Minimized.new
76
72
  end
77
- if (sdl_event.state & SDL::Event::APPINPUTFOCUS) != 0
73
+ if (sdl_event.state & APPINPUTFOCUS_) != 0
78
74
  out_events.push sdl_event.gain ? Kbd::Gained.new : Kbd::Lost.new
79
75
  end
80
- if (sdl_event.state & SDL::Event::APPMOUSEFOCUS) != 0
76
+ if (sdl_event.state & APPMOUSEFOCUS_) != 0
81
77
  out_events.push sdl_event.gain ? Mouse::Gained.new : Mouse::Lost.new
82
78
  end
83
79
  end
@@ -0,0 +1,54 @@
1
+ # This file contains the event dispatch mixin.
2
+
3
+ require 'tea/c_error'
4
+
5
+ #
6
+ module Tea
7
+
8
+ module Event
9
+
10
+ # The Dispatch mixin gives any object the ability to handle Tea events with
11
+ # method calls. To use it, include the mixin in your class
12
+ #
13
+ # include Tea::Event::Dispatch
14
+ #
15
+ # define the handling methods (all optional)
16
+ #
17
+ # def app_exit; end
18
+ # def app_restored; end
19
+ # def app_minimized; end
20
+ # def kbd_down; end
21
+ # def kbd_up; end
22
+ # def mouse_move; end
23
+ # def mouse_down; end
24
+ # def mouse_up; end
25
+ # def mouse_scroll; end
26
+ #
27
+ # and when you need events handled, call your instance's dispatch_event
28
+ # method
29
+ #
30
+ # handler = MyEventHandler.new
31
+ # ...
32
+ # loop do
33
+ # event = Tea::Event.get
34
+ # handler.dispatch_event event if event
35
+ # ...
36
+ # end
37
+ #
38
+ # Tea will then call the method matching the event received.
39
+ module Dispatch
40
+
41
+ def dispatch_event(tea_event)
42
+ class_string = tea_event.class.to_s
43
+ unless class_string =~ /^Tea::(?:App|Kbd|Mouse)::[A-Za-z]+$/
44
+ raise Tea::Error, "Can't dispatch on class #{class_string}", caller
45
+ end
46
+ msg = class_string.split('::', 2)[1].sub('::', '_').downcase.intern
47
+ send msg, tea_event if respond_to?(msg)
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -2,14 +2,6 @@
2
2
 
3
3
  require 'sdl'
4
4
 
5
- # Hot-patch for Ruby/SDL for missing mouse wheel constants.
6
- module SDL
7
- module Mouse
8
- BUTTON_WHEELUP = 4
9
- BUTTON_WHEELDOWN = 5
10
- end
11
- end
12
-
13
5
  #
14
6
  module Tea
15
7
 
@@ -81,8 +73,8 @@ module Tea
81
73
  @x = sdl_event.x
82
74
  @y = sdl_event.y
83
75
  case sdl_event.button
84
- when SDL::Mouse::BUTTON_WHEELDOWN then @delta = 1
85
- when SDL::Mouse::BUTTON_WHEELUP then @delta = -1
76
+ when Event::BUTTON_WHEELDOWN_ then @delta = 1
77
+ when Event::BUTTON_WHEELUP_ then @delta = -1
86
78
  else
87
79
  raise Tea::Error, "Tea::Mouse::Scroll given an unexpected event: #{sdl_event.inspect}", caller
88
80
  end
@@ -159,6 +151,10 @@ module Tea
159
151
 
160
152
  module Event
161
153
 
154
+ # Missing mouse wheel button constants from rubysdl. For internal use only.
155
+ BUTTON_WHEELUP_ = 4
156
+ BUTTON_WHEELDOWN_ = 5
157
+
162
158
  # Convert a mouse-related SDL::Event into a Tea event. For internal use only.
163
159
  def Event.translate_mouse_event(sdl_event)
164
160
  out_events = []
@@ -170,7 +166,7 @@ module Tea
170
166
  case sdl_event.button
171
167
  when SDL::Mouse::BUTTON_LEFT, SDL::Mouse::BUTTON_MIDDLE, SDL::Mouse::BUTTON_RIGHT
172
168
  out_events.push Mouse::Down.new(sdl_event)
173
- when SDL::Mouse::BUTTON_WHEELDOWN, SDL::Mouse::BUTTON_WHEELUP
169
+ when BUTTON_WHEELDOWN_, BUTTON_WHEELUP_
174
170
  out_events.push Mouse::Scroll.new(sdl_event)
175
171
  end
176
172
  when SDL::Event::MouseButtonUp
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tung-tea
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-26 00:00:00 -07:00
12
+ date: 2009-07-28 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -36,6 +36,7 @@ files:
36
36
  - README.rdoc
37
37
  - doc/example/bitmap_draw.rb
38
38
  - doc/example/bitmap_load.rb
39
+ - doc/example/bitmap_new.rb
39
40
  - doc/example/circles.rb
40
41
  - doc/example/event_app.rb
41
42
  - doc/example/event_keyboard.rb
@@ -49,6 +50,7 @@ files:
49
50
  - doc/example/smile.png
50
51
  - doc/example/smile_bounce.rb
51
52
  - doc/example/smile_move.rb
53
+ - doc/example/smile_move_2.rb
52
54
  - doc/example/state_app.rb
53
55
  - doc/example/state_keyboard.rb
54
56
  - doc/example/state_mouse.rb
@@ -61,12 +63,14 @@ files:
61
63
  - lib/tea/m_blitting.rb
62
64
  - lib/tea/m_event.rb
63
65
  - lib/tea/m_event_app.rb
66
+ - lib/tea/m_event_dispatch.rb
64
67
  - lib/tea/m_event_keyboard.rb
65
68
  - lib/tea/m_event_mouse.rb
66
69
  - lib/tea/m_primitive_drawing.rb
67
70
  - lib/tea/screen.rb
68
71
  has_rdoc: false
69
72
  homepage: http://github.com/tung/tea
73
+ licenses:
70
74
  post_install_message:
71
75
  rdoc_options:
72
76
  - --charset=UTF-8
@@ -87,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
91
  requirements: []
88
92
 
89
93
  rubyforge_project:
90
- rubygems_version: 1.2.0
94
+ rubygems_version: 1.3.5
91
95
  signing_key:
92
96
  specification_version: 3
93
97
  summary: A simple game development library for Ruby.