rubydraw 0.1.5 → 0.1.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/examples/image_ex.rb CHANGED
@@ -24,5 +24,4 @@ class MyWindow < Rubydraw::Window
24
24
  end
25
25
  end
26
26
 
27
- w = MyWindow.new
28
- w.open
27
+ w = MyWindow.new.show
data/examples/sound_ex.rb CHANGED
@@ -18,4 +18,4 @@ class MyWindow < Rubydraw::Window
18
18
  end
19
19
  end
20
20
 
21
- MyWindow.new.open
21
+ MyWindow.new.show
@@ -19,7 +19,7 @@ w = gets.to_i
19
19
  print "Window width: "
20
20
  h = gets.to_i
21
21
 
22
- window = MyWindow.new(w, h).open
22
+ window = MyWindow.new(w, h).show
23
23
 
24
24
  # Wait for a while to let the user enjoy the window
25
25
  sleep 10
data/lib/ext/string.rb ADDED
@@ -0,0 +1,14 @@
1
+ class String
2
+ # Sets the size for this string, but does not modify the string.
3
+ # Appends 0 to the beginning if original is one in legnth. (Used
4
+ # in Rubydraw::Color#to_i)
5
+ def color_string
6
+ new_str = self[0..1]
7
+ if new_str.size == 1
8
+ result = "0" + new_str
9
+ else
10
+ result = new_str
11
+ end
12
+ result
13
+ end
14
+ end
data/lib/rubydraw.rb CHANGED
@@ -10,8 +10,10 @@ files = %w[
10
10
  event_queue
11
11
  keys
12
12
  point
13
+ color
13
14
  sdl_error]
14
15
  files.each {|f| require("rubydraw/" + f)}
16
+ require 'ext/string'
15
17
 
16
18
  # Rubydraw is a high level game/graphics library, like Gosu or Rubygame, and is written completely
17
19
  # in Ruby. Its only dependency is ruby-sdl-ffi, which it uses to access SDL functions. Also, thanks,
@@ -0,0 +1,50 @@
1
+ module Rubydraw
2
+ # Instances of Color are created with four arguments: Red, green,
3
+ # blue, and alpha. The last is 0 by default, and all should have
4
+ # values ranging from 0 to 255. Use this to specify colors instead
5
+ # hex numbers. Each color instance can return its numerical value,
6
+ # but only Rubydraw itself should need it.
7
+ class Color
8
+ #Red = 0x00_00_ff_00
9
+ #Green = 0x00_ff_00_00
10
+ #Blue = 0xff_00_00_00
11
+ #Black = 0x00_00_00_00
12
+ #White = 0xff_ff_ff_00
13
+ attr_reader(:red, :green, :blue, :alpha)
14
+
15
+ # Create a new color with the given red, green, blue, and alpha
16
+ # values. Alpha is 0 by default.
17
+ #
18
+ # TODO: Add other color specs, like HSV or maybe (but probably not) CYMK
19
+ def initialize(red, green, blue, alpha = 0)
20
+ @red, @green, @blue, @alpha = red, green, blue, alpha
21
+ end
22
+
23
+ # Convert this color to a numerical value. It only makes sense in
24
+ # hexidecimal or binary format; e.g. red would be equal to
25
+ # +0x0000ff00+.
26
+ def to_i
27
+ # Get the hex string for each value.
28
+ hex_alpha = (@alpha.to_s(16)).color_string
29
+ hex_red = (@red.to_s(16)).color_string
30
+ hex_green = (@green.to_s(16)).color_string
31
+ hex_blue = (@blue.to_s(16)).color_string
32
+ # Construct a hex string using the previously determined hex colors.
33
+ # *Note:* it appears that SDL's (or maybe ruby-sdl-ffi's) color
34
+ # is backwards. The order appears to be: +BBGGRRAA+
35
+ color_str = hex_blue + hex_green + hex_red + hex_alpha
36
+ color_str.to_i(16)
37
+ end
38
+
39
+ White = new(255, 255, 255)
40
+ Black = new(0, 0, 0)
41
+ # The primary colors
42
+ Red = new(255, 0, 0)
43
+ Green = new(0, 255, 0)
44
+ Blue = new(0, 0, 255)
45
+ # Secondary colors
46
+ Yellow = new(255, 255, 0)
47
+ Magenta = new(255, 0, 255)
48
+ Cyan = new(0, 255, 255)
49
+ end
50
+ end
@@ -6,7 +6,7 @@ module Rubydraw
6
6
  # when Rubydraw::Window#close is called.
7
7
  class Window
8
8
  # Create a new window.
9
- def initialize(width, height)
9
+ def initialize(width, height, bkg_color=Color::Black)
10
10
  unless height.is_a?(Numeric) and width.is_a?(Numeric)
11
11
  # Raise an error
12
12
  raise SDLError "Window width and height have to be a number."
@@ -19,10 +19,12 @@ module Rubydraw
19
19
  @event_queue = EventQueue.new
20
20
 
21
21
  @registered_actions = {}
22
+
23
+ @bkg_color = bkg_color
22
24
  end
23
25
 
24
26
  # Call this method to start updating and drawing.
25
- def open
27
+ def show
26
28
  @open = true
27
29
  # Behold, the main loop. Drumroll!
28
30
  @screen = SDL::SetVideoMode(@width, @height, 0, 0)
@@ -44,7 +46,13 @@ module Rubydraw
44
46
  # Clear the window's contents by filling it with black. A bit of a cheat; I don't
45
47
  # know if there is a better way to do this.
46
48
  def clear
47
- SDL::FillRect(@screen, nil, 0)
49
+ fill_with(@bkg_color)
50
+ end
51
+
52
+ # Fill the entire window with a Rubydraw::Color. Do this by feeding SDL the numeric
53
+ # verion of the color; see Rubydraw::Color#to_i
54
+ def fill_with(color)
55
+ SDL::FillRect(@screen, nil, color.to_i)
48
56
  end
49
57
 
50
58
  # Call this method to tell SDL to quit drawing. The loop (started in
@@ -55,6 +63,8 @@ module Rubydraw
55
63
  SDL.QuitSubSystem(SDL::INIT_VIDEO)
56
64
  end
57
65
 
66
+ alias quit close
67
+
58
68
  # Collect and handle new events by executing blocks in +@regestered_events+. See
59
69
  # Rubydraw::Window#register_action on how to use it.
60
70
  def handle_events
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubydraw
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 89
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 7
10
+ - 1
11
+ version: 0.1.7.1
11
12
  platform: ruby
12
13
  authors:
13
14
  - J. Wostenberg
@@ -15,7 +16,7 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2011-11-17 00:00:00 -07:00
19
+ date: 2011-11-18 00:00:00 -07:00
19
20
  default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
@@ -49,6 +50,7 @@ extra_rdoc_files: []
49
50
  files:
50
51
  - README
51
52
  - lib/rubydraw.rb
53
+ - lib/ext/string.rb
52
54
  - lib/rubydraw/window.rb
53
55
  - lib/rubydraw/image.rb
54
56
  - lib/rubydraw/sound.rb
@@ -57,6 +59,7 @@ files:
57
59
  - lib/rubydraw/event_queue.rb
58
60
  - lib/rubydraw/events.rb
59
61
  - lib/rubydraw/point.rb
62
+ - lib/rubydraw/color.rb
60
63
  - examples/window_ex.rb
61
64
  - examples/image_ex.rb
62
65
  - examples/sound_ex.rb