rubydraw 0.2.3 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -49,6 +49,27 @@ module Rubydraw
49
49
  end
50
50
  end
51
51
 
52
+ def self.vinfo #:nodoc:
53
+ SDL.GetVideoInfo
54
+ end
55
+
56
+ private_class_method :vinfo
57
+
58
+ # Returns the screen width.
59
+ def self.screen_width
60
+ vinfo.current_w
61
+ end
62
+
63
+ # Returns the screen height.
64
+ def self.screen_height
65
+ vinfo.current_h
66
+ end
67
+
68
+ # Returns the screen size.
69
+ def self.screen_dimensions
70
+ Point[screen_width, screen_height]
71
+ end
72
+
52
73
  # Enable/disable key repeating. After this method is called, instances of Rubydraw::Events::KeyPressed
53
74
  # wil be continually created, until the key is released.
54
75
  #
@@ -6,6 +6,18 @@ module Rubydraw
6
6
  # instance can return its numerical value, but only Rubydraw itself
7
7
  # should need it.
8
8
  class Color
9
+ # Returns true if all arguments are within the valid RGB color
10
+ # values (0-255).
11
+ def self.in_bounds?(*args)
12
+ args.each {|element|
13
+ if (0..255).include?(element)
14
+ return true
15
+ else
16
+ return false
17
+ end
18
+ }
19
+ end
20
+
9
21
  # Shorthand new method.
10
22
  def self.[](red, green, blue, alpha = 0)
11
23
  self.new(red, green, blue, alpha)
@@ -18,6 +30,9 @@ module Rubydraw
18
30
  #
19
31
  # TODO: Add other color specs, like HSV or maybe CYMK
20
32
  def initialize(red, green, blue, alpha = 255)
33
+ unless self.class.in_bounds?(red, green, blue, alpha)
34
+ raise IndexError, "One or more color values are out of bounds (must be between 0 and 255)"
35
+ end
21
36
  @red, @green, @blue, @alpha = red, green, blue, alpha
22
37
  calc_num_val
23
38
  end
@@ -31,8 +46,7 @@ module Rubydraw
31
46
  hex_green = (@green.to_s(16)).color_string
32
47
  hex_blue = (@blue.to_s(16)).color_string
33
48
  # Construct a hex string using the previously determined hex colors.
34
- # *Note:* it appears that SDL's (or maybe ruby-sdl-ffi's) color
35
- # is backwards. The order appears to be: +BBGGRRAA+
49
+ # *Note:* it appears that SDL's colors are in the format +BBGGRRAA+.
36
50
  color_str = hex_blue + hex_green + hex_red + hex_alpha
37
51
  @num_val = color_str.to_i(16)
38
52
  end
@@ -14,7 +14,7 @@ module Rubydraw
14
14
  elsif arg.is_a?(SDL::Surface)
15
15
  load_from_surface(arg)
16
16
  else
17
- raise TypeError, "Failed to load image: Expected String or SDL::Surface but got #{arg}"
17
+ raise TypeError, "Failed to load image: Expected String or SDL::Surface but got: #{arg}"
18
18
  end
19
19
  self
20
20
  end
@@ -45,20 +45,17 @@ module Rubydraw
45
45
  @sdl_image = surface
46
46
  end
47
47
 
48
- # Blit (copy) into the window at +position+ (see Rubydraw::Point).
48
+ # Blit (copy) into +surface at +position+ (see Rubydraw::Point).
49
49
  # No graphical effects are applied.
50
- #
51
- # Notice that you don't blit surfaces to other surfaces when using
52
- # Rubygame, but instead you draw things.
53
- def draw(window, position)
50
+ def blit(surface, position)
54
51
  source_rect = Rectangle[Point[0, 0], Point[width, height]]
55
- blit_rect = Rectangle[position, Point[window.width, window.height]]
56
- #source_rect = Rectangle[Point[0, 0], Point[window.width, window.height]]
57
- #blit_rect = Rectangle[position, Point[window.width, window.height]]
58
- SDL::BlitSurface(@sdl_image, source_rect.to_sdl, window.sdl_surface, blit_rect.to_sdl)
52
+ blit_rect = Rectangle[position, Point[surface.width, surface.height]]
53
+ SDL::BlitSurface(@sdl_image, source_rect.to_sdl, surface.to_sdl, blit_rect.to_sdl)
59
54
  self
60
55
  end
61
56
 
57
+ alias draw blit
58
+
62
59
  # Rotates and/or expands the image. Note that this modifies the image
63
60
  # itself.
64
61
  def rotozoom!(angle, zoom, smooth=false)
@@ -3,7 +3,7 @@ module Rubydraw
3
3
  # with the text to display. They can be drawn at a position on the
4
4
  # screen.
5
5
  class Text
6
- attr_accessor(:contents, :font, :color, :size)
6
+ attr_reader(:contents, :font, :color, :size)
7
7
 
8
8
  # Create a new drawable Text object with the given font and contents.
9
9
  def initialize(contents, color, font_name="Times New Roman", size = 25)
@@ -18,31 +18,39 @@ module Rubydraw
18
18
  unless File.exists?(font_path)
19
19
  raise "Font file '#{font_name}' does not exist; attemped to load from '#{font_path}'"
20
20
  end
21
- @drawable = SDL::TTF.OpenFont(font_path, size)
22
- raise(SDLError, "Failed to initialize font: #{SDL.GetError}") if @drawable.pointer.null?
21
+ sdl_text = SDL::TTF.OpenFont(font_path, size)
22
+ raise(SDLError, "Failed to initialize font: #{SDL.GetError}") if sdl_text.pointer.null?
23
+
24
+ sdl_color = @color.to_sdl
25
+ @sdl_surface = SDL::TTF.RenderText_Blended(sdl_text, @contents, sdl_color)
23
26
  end
24
27
 
25
- # Returns the sdl surface of this text object.
28
+ # Returns the SDL surface for this object.
26
29
  def sdl_surface
27
- sdl_color = @color.to_sdl
28
- SDL::TTF.RenderText_Blended(@drawable, @contents, sdl_color)
30
+ @sdl_surface
29
31
  end
30
32
 
31
- # Draw the font in the given window at a position.
32
- def draw(window, position)
33
- source_rect = Rectangle[Point[0, 0], Point[sdl_surface.w, sdl_surface.h]]
34
- blit_rect = Rectangle[position, Point[window.width, window.height]]
35
- SDL::BlitSurface(sdl_surface, source_rect.to_sdl, window.sdl_surface, blit_rect.to_sdl)
33
+ alias to_sdl sdl_surface
34
+
35
+ # Blit (copy) into +surface at +position+ (see Rubydraw::Point).
36
+ # No graphical effects are applied.
37
+ def blit(surface, position)
38
+ source_rect = Rectangle[Point[0, 0], Point[width, height]]
39
+ blit_rect = Rectangle[position, Point[surface.width, surface.height]]
40
+ SDL::BlitSurface(sdl_surface, source_rect.to_sdl, surface.to_sdl, blit_rect.to_sdl)
41
+ self
36
42
  end
37
43
 
44
+ alias draw blit
45
+
38
46
  # Returns the width.
39
47
  def width
40
- sdl_surface.w
48
+ @sdl_surface.w
41
49
  end
42
50
 
43
51
  # Returns the height.
44
52
  def height
45
- sdl_surface.h
53
+ @sdl_surface.h
46
54
  end
47
55
  end
48
56
  end
@@ -26,11 +26,27 @@ module Rubydraw
26
26
  end
27
27
 
28
28
  def width
29
- @screen.w
29
+ begin
30
+ return @screen.w
31
+ rescue NameError
32
+ if @width == 0
33
+ return Rubydraw.screen_width
34
+ else
35
+ return @width
36
+ end
37
+ end
30
38
  end
31
39
 
32
40
  def height
33
- @screen.h
41
+ begin
42
+ return @screen.h
43
+ rescue NameError
44
+ if @height == 0
45
+ return Rubydraw.screen_height
46
+ else
47
+ return @height
48
+ end
49
+ end
34
50
  end
35
51
 
36
52
  # Call this method to start updating and drawing.
@@ -122,6 +138,7 @@ module Rubydraw
122
138
  @screen
123
139
  end
124
140
 
141
+ alias to_sdl sdl_surface
125
142
 
126
143
  # Redefine Rubydraw::Window#tick with any code you want to be executed
127
144
  # every frame, like drawing functions.
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubydraw
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 3
10
- version: 0.2.3
5
+ version: 0.2.5
11
6
  platform: ruby
12
7
  authors:
13
8
  - J. Wostenberg
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-11-25 00:00:00 -07:00
13
+ date: 2011-11-29 00:00:00 -07:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,20 +21,10 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- - 4
33
24
  version: "0.4"
34
25
  type: :runtime
35
26
  version_requirements: *id001
36
- description: |-
37
-
38
- Rubydraw is a high level drawing/game library,
39
- like Gosu or Rubygame. Its only dependencies are
40
- ruby-sdl-ffi--which it uses to access SDL
41
- functions--and SDL itself (See README on how to
42
- install the latter).
27
+ description: "\n Rubydraw is a high level drawing/game library,\n like Gosu or Rubygame. Its only dependencies are\n ruby-sdl-ffi--which it uses to access SDL\n functions--and SDL itself (See README on how to\n install the latter)."
43
28
  email:
44
29
  executables: []
45
30
 
@@ -94,23 +79,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
79
  requirements:
95
80
  - - ">="
96
81
  - !ruby/object:Gem::Version
97
- hash: 3
98
- segments:
99
- - 0
100
82
  version: "0"
101
83
  required_rubygems_version: !ruby/object:Gem::Requirement
102
84
  none: false
103
85
  requirements:
104
86
  - - ">="
105
87
  - !ruby/object:Gem::Version
106
- hash: 3
107
- segments:
108
- - 0
109
88
  version: "0"
110
89
  requirements: []
111
90
 
112
91
  rubyforge_project:
113
- rubygems_version: 1.5.2
92
+ rubygems_version: 1.6.2
114
93
  signing_key:
115
94
  specification_version: 3
116
95
  summary: Rubydraw is a high level drawing/graphics library, like Gosu or Rubygame.