gosu_texture_packer 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af48671121a22d94b41d7515d590a35d5cb69efa
4
- data.tar.gz: 4a78ca27ca52ab4e8d9b0bb8d6318e09c8d26aec
3
+ metadata.gz: 046d95ce5242ce706baeace9c3f6c19352653e69
4
+ data.tar.gz: e42056791386d088d652c4aa92b1e88e3870b058
5
5
  SHA512:
6
- metadata.gz: 1376f1f0358f8ca3f3550c92163a13ef170c208b451f7c43a6058e1ee4dea1d8606e82672a9b81abec3544890d22c19cf1a05a3f2d4dba7b5e717a9d56c15d01
7
- data.tar.gz: b5a6936182ef3d2029e07ef4b7908e51557262f54fab2991ad7308191077affb0ff2a43969015f2e4fe6f34a6a985848c4c53fe1ba1a03655c20a1b0ca3fe2cb
6
+ metadata.gz: c4c5d8fe9097783475afc48cceab860d3e69ddeafdbbc1c00f9e2e566541f1bb8fcc8d8806888ea8fbb1ad1b5a41291f253c79d19f6959b0675428ae72eaa9e7
7
+ data.tar.gz: fe16383639f2bd4fb094c4f34498eeb5d8b5850eec8d847d3ba5d4d4b974575f9b0918bf33fe31ad6a1d00cb8593871f096abd500c146bb6f9a46535f4a80a46
data/README.md CHANGED
@@ -28,6 +28,17 @@ tile = tileset.frame(frame_names.first)
28
28
  tile.draw(0, 0, 0) # tile is Gosu::Image
29
29
  ```
30
30
 
31
+ You may find that your tiles have "bleeding edges" when you stack them together. To avoid that,
32
+ either use `--extrude 1` or `--reduce-border-artifacts` when exporting Texture Packer tileset, or
33
+ provide `:precise` mode parameter to `load_json`:
34
+
35
+ ```ruby
36
+ tileset = Gosu::TexturePacker.load_json(gosu_window, '/path/to/tileset.json', :precise)
37
+ ```
38
+
39
+ However, loading with `:precise` mode will significantly increase memory consumption and reduce
40
+ load time, so use it carefully.
41
+
31
42
  ## Example Code
32
43
 
33
44
  Run
@@ -14,7 +14,7 @@ class GameWindow < Gosu::Window
14
14
  super(WIDTH, HEIGHT, false)
15
15
  self.caption = 'Click space to toggle random redraws'
16
16
  json_path = File.join(ROOT_DIR, 'spec', 'files', 'ground.json')
17
- @tileset = Gosu::TexturePacker.load_json(self, json_path)
17
+ @tileset = Gosu::TexturePacker.load_json(self, json_path, :precise)
18
18
  end
19
19
 
20
20
  def needs_cursor?
@@ -4,8 +4,8 @@ require 'gosu_texture_packer/tileset'
4
4
 
5
5
  module Gosu
6
6
  module TexturePacker
7
- def self.load_json(window, path)
8
- Tileset.load_json(window, path)
7
+ def self.load_json(window, path, mode = :fast)
8
+ Tileset.load_json(window, path, mode)
9
9
  end
10
10
  end
11
11
  end
@@ -1,18 +1,18 @@
1
1
  require 'json'
2
- require 'rmagick'
3
2
  module Gosu
4
3
  module TexturePacker
5
4
  class Tileset
6
5
 
7
- def self.load_json(window, json)
8
- self.new(window, json)
6
+ def self.load_json(window, json, mode = :fast)
7
+ self.new(window, json, mode)
9
8
  end
10
9
 
11
- def initialize(window, json)
10
+ def initialize(window, json, mode)
11
+ @mode = mode
12
12
  @window = window
13
13
  @json = JSON.parse(File.read(json))
14
14
  @source_dir = File.dirname(json)
15
- @main_image = Magick::ImageList.new(image_file).first
15
+ @main_image = build_main_image(mode)
16
16
  @tile_cache = {}
17
17
  end
18
18
 
@@ -33,8 +33,24 @@ module Gosu
33
33
 
34
34
  private
35
35
 
36
+ def build_main_image(mode)
37
+ case mode
38
+ when :fast
39
+ Gosu::Image.new(@window, image_file, true)
40
+ when :precise
41
+ require 'rmagick' unless defined?(Magick)
42
+ Magick::ImageList.new(image_file).first
43
+ else
44
+ raise "Unsupported mode #{mode}. Use :fast or :precise."
45
+ end
46
+ end
47
+
36
48
  def build_tile(f)
37
- Gosu::Image.new(@window, @main_image, true, f['x'], f['y'], f['w'], f['h'])
49
+ if @mode == :fast
50
+ @main_image.subimage(f['x'], f['y'], f['w'], f['h'])
51
+ else
52
+ Gosu::Image.new(@window, @main_image, true, f['x'], f['y'], f['w'], f['h'])
53
+ end
38
54
  end
39
55
 
40
56
  def image_file
@@ -48,7 +64,6 @@ module Gosu
48
64
  def frames
49
65
  @json['frames']
50
66
  end
51
-
52
67
  end
53
68
  end
54
69
  end
@@ -1,5 +1,5 @@
1
1
  module Gosu
2
2
  module TexturePacker
3
- VERSION = "0.1.2"
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  end
@@ -7,51 +7,65 @@ RSpec.describe Gosu::TexturePacker::Tileset do
7
7
  let(:game_window) { Gosu::Window.new(640, 480, false) }
8
8
  let(:target_frame) { 'alientech.png' }
9
9
  let(:tileset) { File.join(media_dir, '_UI.json') }
10
+ let(:mode) { :fast }
10
11
 
11
12
  subject(:instance) do
12
- tileset_class.load_json(game_window, tileset)
13
+ tileset_class.load_json(game_window, tileset, mode)
13
14
  end
14
15
 
15
- describe '.load_json' do
16
- it 'loads existing file' do
17
- expect { subject }.to_not raise_error
16
+ shared_examples 'tileset' do
17
+
18
+ describe '.load_json' do
19
+ it 'loads existing file' do
20
+ expect { subject }.to_not raise_error
21
+ end
18
22
  end
19
- end
20
23
 
21
- describe '#frame_list' do
22
- subject(:frame_list) { instance.frame_list }
24
+ describe '#frame_list' do
25
+ subject(:frame_list) { instance.frame_list }
23
26
 
24
- it 'contains all the frames' do
25
- expect(frame_list.size).to be 514
26
- end
27
+ it 'contains all the frames' do
28
+ expect(frame_list.size).to be 514
29
+ end
27
30
 
28
- it 'includes target frame' do
29
- expect(frame_list).to include target_frame
31
+ it 'includes target frame' do
32
+ expect(frame_list).to include target_frame
33
+ end
30
34
  end
31
- end
32
35
 
33
- # Frame format in JSON:
34
- # {"frame"=>{"x"=>129, "y"=>0, "w"=>32, "h"=>32}, "rotated"=>false, "trimmed"=>false,
35
- # "spriteSourceSize"=>{"x"=>0, "y"=>0, "w"=>32, "h"=>32}, "sourceSize"=>{"w"=>32, "h"=>32}}
36
- describe '#frame' do
37
- subject(:loaded_frame) { instance.frame(target_frame) }
38
- let(:source_size) { instance.send(:frames)[target_frame]['sourceSize'] }
36
+ # Frame format in JSON:
37
+ # {"frame"=>{"x"=>129, "y"=>0, "w"=>32, "h"=>32}, "rotated"=>false, "trimmed"=>false,
38
+ # "spriteSourceSize"=>{"x"=>0, "y"=>0, "w"=>32, "h"=>32}, "sourceSize"=>{"w"=>32, "h"=>32}}
39
+ describe '#frame' do
40
+ subject(:loaded_frame) { instance.frame(target_frame) }
41
+ let(:source_size) { instance.send(:frames)[target_frame]['sourceSize'] }
39
42
 
40
- it 'loads' do
41
- expect { subject }.to_not raise_error
42
- end
43
+ it 'loads' do
44
+ expect { subject }.to_not raise_error
45
+ end
43
46
 
44
- it 'produces a Gosu::Image' do
45
- should be_a(Gosu::Image)
46
- end
47
+ it 'produces a Gosu::Image' do
48
+ should be_a(Gosu::Image)
49
+ end
47
50
 
48
- it 'produces correctly sized image' do
49
- expect(loaded_frame.width).to be source_size['w']
50
- expect(loaded_frame.height).to be source_size['h']
51
- end
51
+ it 'produces correctly sized image' do
52
+ expect(loaded_frame.width).to be source_size['w']
53
+ expect(loaded_frame.height).to be source_size['h']
54
+ end
52
55
 
53
- it 'caches the image' do
54
- expect(instance.frame(target_frame)).to eq instance.frame(target_frame)
56
+ it 'caches the image' do
57
+ expect(instance.frame(target_frame)).to eq instance.frame(target_frame)
58
+ end
55
59
  end
56
60
  end
61
+
62
+ context 'default mode' do
63
+ it_behaves_like 'tileset'
64
+ end
65
+
66
+ context 'precise mode' do
67
+ let(:mode) { :precise }
68
+ it_behaves_like 'tileset'
69
+ end
70
+
57
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gosu_texture_packer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Varaneckas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-18 00:00:00.000000000 Z
11
+ date: 2014-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu