ehb_game_lib 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9acd1ac23a1acd8cdafc3382002cca990d79c0eebdba0f681c9a5e499876fd05
4
- data.tar.gz: 54c26e41e8a034f231445bb0105a072ff1be27dd5e27c08df921c8ee63b23dae
3
+ metadata.gz: bed38ad4e4913133709910358b6cefecc19b31806dbebf56fa0634436854df16
4
+ data.tar.gz: 6f5f10d50d575fd6318fb46ef148b779b2cb1dae36f221f9bb1f8c1c49af9ae4
5
5
  SHA512:
6
- metadata.gz: 16e379f2da9c3bda6e0aabeb25b3dd76160d01ffd255bb01c154dd66788c19efc53980694ef45bdd2528b145bed0da9b73570be39205d417ff4730cacea35367
7
- data.tar.gz: 82e9484c27ead60e992b2069a0d2ba66480bd4f8b2aecbffcf33c4a9a447e233c92c10719c758716a079b40629d8865d1427c519776f97138eb761e0e06c830c
6
+ metadata.gz: 56b31e0d389e0623a6eac2cef4b665b97f0bca16b3130c7f191d7d66186c5d35fd8f24e28bd1ce329433441edfffa64a77c839e5cdadefa0514abd6c1052681d
7
+ data.tar.gz: 49d76a3c978287e715977ec2c1117a437b698e1ede1a8de4c6e8f3340037198fcf8900d3459931db172e0a7011050b5be0f4c3163886b215c13f6c326db48eba
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'ehb_game_lib/palettes/sprite'
5
+ require 'ehb_game_lib/text/bitmap_font'
6
+
7
+ module EhbGameLib
8
+ class MediaPath
9
+ class << self
10
+ # @return [EhbGameLib::MediaPath]
11
+ def default
12
+ @default ||= new
13
+ end
14
+ end
15
+
16
+ # @return [Array]
17
+ def paths
18
+ @paths ||= []
19
+ end
20
+
21
+ # @return [Pathname]
22
+ def find(subpath)
23
+ paths.each do |path|
24
+ full_path = path.to_pathname.expand_path.join(subpath)
25
+ return full_path if full_path.exist?
26
+ end
27
+ raise "Subpath \"#{subpath}\" not found (Searched in: #{paths})"
28
+ end
29
+
30
+ # @return [EhbGameLib::Text::BitmapFont]
31
+ def load_bitmap_font(subpath, line_height)
32
+ ::EhbGameLib::Text::BitmapFont.new(find(subpath).to_path, line_height)
33
+ end
34
+
35
+ # @return [Gosu::Image]
36
+ def load_gosu_image(subpath)
37
+ ::Gosu::Image.new(find(subpath).to_path, ::EhbGameLib::Palettes::Sprite.to_gosu_image_options)
38
+ end
39
+
40
+ # @return [EhbGameLib::Palettes::Sprite]
41
+ def load_sprite(subpath)
42
+ ::EhbGameLib::Palettes::Sprite.from_file(find(subpath))
43
+ end
44
+ end
45
+ end
@@ -1,20 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ehb_game_lib/palettes/palette'
4
+
3
5
  module EhbGameLib
4
6
  module Nes
5
- class Palette
6
- COLORS_COUNT = 16
7
+ # Reference: http://www.firebrandx.com/nespalette.html
8
+ class Palette < ::EhbGameLib::Palettes::Palette
9
+ COLUMNS = %w[grey blue indigo purple violet red brown orange yellow lawn_green lime_green
10
+ sea_green cyan black].freeze
11
+ ROWS = %w[dd d l ll].freeze
7
12
 
8
- def initialize(all_colors)
9
- @colors = Array.new(COLORS_COUNT) { |i| ::EhbGameLib::Nes::Color.new(all_colors, i) }
13
+ COLUMNS.each_with_index do |c_name, c_index|
14
+ ROWS.each_with_index do |r_name, r_index|
15
+ const_set("#{c_name}_#{r_name}".upcase, r_index * 0x10 + c_index)
16
+ end
10
17
  end
11
18
 
12
- def [](index)
13
- @colors[index % @colors.length]
14
- end
19
+ BLACK = BLACK_DD
20
+ WHITE = GREY_LL
21
+ TRANSPARENT = 0x3F
15
22
 
16
- def []=(index, value)
17
- @colors[index % @colors.length].color_index = value
23
+ def initialize
24
+ colors = ::EhbGameLib::Palettes::Color.array_from_file(
25
+ ::File.join(__dir__, 'pvm_style_d93_palette.pal')
26
+ )
27
+ colors[TRANSPARENT] = ::EhbGameLib::Palettes::Color.new(0x00, 0x00, 0x00, 0x00)
28
+ super(colors)
18
29
  end
19
30
  end
20
31
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EhbGameLib
6
+ module Palettes
7
+ require_sub __FILE__
8
+ end
9
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'gosu'
5
+ require 'rmagick'
6
+
7
+ module EhbGameLib
8
+ module Palettes
9
+ class Color
10
+ QUANTUM_MIN = 0
11
+ QUANTUM_MAX = 255
12
+ QUANTUM_RANGE = (QUANTUM_MIN..QUANTUM_MAX).freeze
13
+
14
+ class << self
15
+ def validate_quantum(quantum)
16
+ raise "Not a integer: #{quantum}" unless quantum.is_a?(::Integer)
17
+ raise 'Out of bounds' unless QUANTUM_RANGE.include?(quantum)
18
+
19
+ quantum
20
+ end
21
+
22
+ def quantum_to_magick_range(quantum)
23
+ ((quantum / QUANTUM_MAX.to_f) * ::Magick::QuantumRange).floor
24
+ end
25
+
26
+ # @return [Array<EhbGameLib::Palettes::Color>]
27
+ def array_from_file(file_path)
28
+ r = []
29
+ ::File.open(file_path.to_s) do |file|
30
+ while (b = file.read(3))
31
+ b = b.bytes.to_a
32
+ r << ::EhbGameLib::Palettes::Color.new(b[0], b[1], b[2])
33
+ end
34
+ end
35
+ r
36
+ end
37
+ end
38
+
39
+ enable_simple_cache
40
+ attr_reader :red, :green, :blue, :alpha
41
+
42
+ def initialize(red, green, blue, alpha = QUANTUM_MAX)
43
+ @red = self.class.validate_quantum(red)
44
+ @green = self.class.validate_quantum(green)
45
+ @blue = self.class.validate_quantum(blue)
46
+ @alpha = self.class.validate_quantum(alpha)
47
+ end
48
+
49
+ # @return [Gosu::Color]
50
+ def gosu_color
51
+ @gosu_color ||= ::Gosu::Color.rgba(red, green, blue, alpha)
52
+ end
53
+
54
+ # @return [Magick::Pixel]
55
+ def magick_pixel
56
+ @magick_pixel ||= ::Magick::Pixel.new(
57
+ *[red, green, blue, alpha].map { |n| self.class.quantum_to_magick_range(n) }
58
+ )
59
+ end
60
+
61
+ def names
62
+ Array(::Colorname.find(red, green, blue))
63
+ end
64
+
65
+ def rgb_to_s
66
+ values_to_s(red, green, blue)
67
+ end
68
+
69
+ def rgba_to_s
70
+ rgb_to_s + '|' + values_to_s(alpha)
71
+ end
72
+
73
+ def to_a
74
+ [red, green, blue, alpha]
75
+ end
76
+
77
+ def to_s
78
+ rgba_to_s
79
+ end
80
+
81
+ def to_html
82
+ '#' + rgb_to_s
83
+ end
84
+
85
+ private
86
+
87
+ def values_to_s(*values)
88
+ values.map { |i| i.to_s(16).rjust(2, '0') }.join.upcase
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'gosu'
4
+ require 'ehb_game_lib/palettes/color'
5
+
6
+ module EhbGameLib
7
+ module Palettes
8
+ class Palette
9
+ class << self
10
+ # @return [EhbGameLib::Palettes::Palette]
11
+ def from_file(file_path)
12
+ new(::EhbGameLib::Palettes::Color.array_from_file(file_path))
13
+ end
14
+ end
15
+
16
+ # @return [Array<Gosu::Color>]
17
+ attr_reader :colors
18
+
19
+ def initialize(colors)
20
+ colors.each_with_index do |color, index|
21
+ unless color.is_a?(::EhbGameLib::Palettes::Color)
22
+ raise "\##{index} is not a Palette::Color (#{color})"
23
+ end
24
+ end
25
+ @colors = colors.to_a.freeze
26
+ end
27
+
28
+ # @return [EhbGameLib::Palettes::Color]
29
+ def color(index)
30
+ colors.fetch(index)
31
+ end
32
+
33
+ # @return [EhbGameLib::Palettes::Color]
34
+ def [](index)
35
+ colors.fetch(index)
36
+ end
37
+
38
+ # @return [EhbGameLib::Palettes::Palette]
39
+ def sub(*indexes)
40
+ sub_class.new(indexes.map { |index| color(index) })
41
+ end
42
+
43
+ def sub_class
44
+ ::EhbGameLib::Palettes::Palette
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ehb_game_lib/media_path'
4
+ require 'rmagick'
5
+
6
+ module EhbGameLib
7
+ module Palettes
8
+ class Sprite
9
+ class << self
10
+ def from_file(file_path)
11
+ new(::Magick::Image.read(file_path.to_s).first)
12
+ end
13
+
14
+ def to_gosu_image_options
15
+ { retro: true, tileable: true }
16
+ end
17
+ end
18
+
19
+ common_constructor :magick_image
20
+
21
+ # @return Gosu::Image
22
+ def gosu_image
23
+ @gosu_image ||= begin
24
+ the_path = '/tmp/temp_magick_image.png'
25
+ magick_image.write(the_path)
26
+ ::Gosu::Image.new(the_path, self.class.to_gosu_image_options)
27
+ end
28
+ end
29
+
30
+ # @return [EhbGameLib::Palettes::Sprite]
31
+ def palette_image(palette)
32
+ r = magick_image.dup
33
+ r.colors.times.each do |index|
34
+ r.colormap(index, palette.color(index).magick_pixel)
35
+ end
36
+ self.class.new(r)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EhbGameLib
6
+ module Palettes
7
+ module SpriteSet
8
+ class Paletted
9
+ common_constructor :palette, :source_set
10
+
11
+ def [](name)
12
+ sprite(name)
13
+ end
14
+
15
+ def sprite(name)
16
+ sprites_hash[name.to_sym] ||= source_set.sprite(name).palette_image(palette)
17
+ end
18
+
19
+ def sprites
20
+ source_set.sprites.keys.map { |name| [name, sprite(name)] }.to_h
21
+ end
22
+
23
+ private
24
+
25
+ def sprites_hash
26
+ @sprites_hash ||= {}
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'ehb_game_lib/media_path'
5
+ require 'ehb_game_lib/palettes/sprite'
6
+
7
+ module EhbGameLib
8
+ module Palettes
9
+ module SpriteSet
10
+ class Source
11
+ enable_simple_cache
12
+ common_constructor :media_path, default: [::EhbGameLib::MediaPath.default]
13
+
14
+ def add(name, sprite)
15
+ sprites_hash[name.to_sym] = sprite
16
+ reset_cache :sprites
17
+ end
18
+
19
+ def load(subpath, name = nil)
20
+ name ||= ::File.basename(subpath, '.*')
21
+
22
+ add(name, media_path.load_sprite(subpath))
23
+ end
24
+
25
+ def [](name)
26
+ sprite(name)
27
+ end
28
+
29
+ def sprite(name)
30
+ sprites_hash.fetch(name.to_sym)
31
+ end
32
+
33
+ private
34
+
35
+ def sprites_uncached
36
+ sprites_hash.dup.freeze
37
+ end
38
+
39
+ def sprites_hash
40
+ @sprites_hash ||= {}
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EhbGameLib
4
- VERSION = '0.2.2'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ehb_game_lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo H. Bogoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-04 00:00:00.000000000 Z
11
+ date: 2021-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chingu
@@ -66,6 +66,26 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rmagick
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.1'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 4.1.2
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '4.1'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 4.1.2
69
89
  - !ruby/object:Gem::Dependency
70
90
  name: eac_ruby_gem_support
71
91
  requirement: !ruby/object:Gem::Requirement
@@ -135,10 +155,16 @@ files:
135
155
  - lib/ehb_game_lib/math/quadratic_equation.rb
136
156
  - lib/ehb_game_lib/math/rectable_object.rb
137
157
  - lib/ehb_game_lib/math/vector.rb
158
+ - lib/ehb_game_lib/media_path.rb
138
159
  - lib/ehb_game_lib/nes.rb
139
- - lib/ehb_game_lib/nes/all_colors_palette.rb
140
- - lib/ehb_game_lib/nes/color.rb
141
160
  - lib/ehb_game_lib/nes/palette.rb
161
+ - lib/ehb_game_lib/nes/pvm_style_d93_palette.pal
162
+ - lib/ehb_game_lib/palettes.rb
163
+ - lib/ehb_game_lib/palettes/color.rb
164
+ - lib/ehb_game_lib/palettes/palette.rb
165
+ - lib/ehb_game_lib/palettes/sprite.rb
166
+ - lib/ehb_game_lib/palettes/sprite_set/paletted.rb
167
+ - lib/ehb_game_lib/palettes/sprite_set/source.rb
142
168
  - lib/ehb_game_lib/patches/chingu/basic_game_object.rb
143
169
  - lib/ehb_game_lib/patches/gosu/font.rb
144
170
  - lib/ehb_game_lib/text.rb
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EhbGameLib
4
- module Nes
5
- class AllColorsPalette
6
- def initialize(pal_file)
7
- @colors = []
8
- File.open(pal_file) do |file|
9
- while (b = file.read(3))
10
- b = b.bytes.to_a
11
- @colors << Gosu::Color.rgba(b[0], b[1], b[2], 0xFF)
12
- end
13
- end
14
- @colors.freeze
15
- end
16
-
17
- def gosu_color(i)
18
- @colors[i]
19
- end
20
-
21
- def draw(x, y)
22
- s = 32
23
- @colors.each_with_index do |c, i|
24
- cx = x + (i % 16) * s
25
- cy = y + (i / 16) * s
26
- Gosu.draw_rect(cx, cy, s, s, c)
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EhbGameLib
4
- module Nes
5
- class Color
6
- attr_accessor :color_index
7
-
8
- BLACK = 0x0F
9
- YELLOW = 0x28
10
- RED = 0x05
11
- WHITE = 0x30
12
-
13
- def initialize(all_colors, color_index)
14
- @all_colors = all_colors
15
- @color_index = color_index
16
- end
17
-
18
- def gosu_color
19
- @all_colors.gosu_color(color_index)
20
- end
21
- end
22
- end
23
- end