retmx 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/retmx.rb +22 -2
  2. data/lib/retmx_sdl.rb +137 -0
  3. metadata +2 -1
data/lib/retmx.rb CHANGED
@@ -6,7 +6,7 @@
6
6
  require 'rexml/document'
7
7
  require 'base64'
8
8
  require 'zlib'
9
- require 'csv'
9
+ require 'stringio'
10
10
 
11
11
  module RETMX
12
12
  include REXML
@@ -246,7 +246,27 @@ When the object has a gid set, then it is represented by the image of the tile w
246
246
  @map.tilesets.each {|k, t|
247
247
  if t.firstgid <= cell.gid
248
248
  cell.gid -= t.firstgid
249
- block.call(bx, by, t, cell)
249
+ block.call(self, bx, by, t, cell)
250
+ break
251
+ end
252
+ }
253
+ }
254
+ }
255
+ end
256
+
257
+ #This function render partial of layer
258
+ #:x: offset in tiles
259
+ #:y: offset in tiles
260
+ #:w: in tiles
261
+ #:h: in tiles
262
+ def render_partial(x, y, w, h, &block)
263
+ h.times {|by|
264
+ w.times {|bx|
265
+ cell = @data[((by + y ) * @height) + (bx + x)]
266
+ @map.tilesets.each {|k, t|
267
+ if t.firstgid <= cell.gid
268
+ cell.gid -= t.firstgid
269
+ block.call(self, bx, by, t, cell)
250
270
  break
251
271
  end
252
272
  }
data/lib/retmx_sdl.rb ADDED
@@ -0,0 +1,137 @@
1
+ =begin
2
+ #Libreria para leer archivo TMX de mapeditor.org
3
+ (C) 2011 Jovany Leandro G.C <info@manadalibre.org>
4
+
5
+ This file is part of retmx.
6
+
7
+ retmx is free software: you can redistribute it and/or modify
8
+ it under the terms of the GNU General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ any later version.
11
+
12
+ retmx is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU General Public License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19
+ =end
20
+
21
+
22
+ require 'sdl'
23
+ require 'retmx'
24
+
25
+ module RETMX
26
+
27
+ #Wrapper for RETMX and RubySDL
28
+ class TMXSDL
29
+ attr_reader :tmx
30
+
31
+ def initialize(tmx)
32
+
33
+ case tmx
34
+ when Map
35
+ @tmx = tmx
36
+ when String
37
+ @tmx = RETMX.load(tmx)
38
+ else
39
+ raise RuntimeError, "Need a RETMX::Map or path to .tmx" unless tmx.kind_of? Map
40
+ end
41
+
42
+
43
+ @tileset_images = {}
44
+ @tmx.tilesets.each {|k,t|
45
+ if File.exists? t.image.source or !@tileset_imagen.include? t.name
46
+ image = SDL::Surface.load(t.image.source)
47
+ image.setColorKey(SDL::SRCCOLORKEY, t.image.trans.hex) unless t.image.trans.nil?
48
+ @tileset_images[t.name] = image
49
+ end
50
+ }
51
+ end
52
+
53
+
54
+ #Blit all layers to +surface+ at (+dx+, +dy+)
55
+ #:dx: on tiles
56
+ #:dy: on tiles
57
+ def put_layers(surface, dx, dy)
58
+ raise RuntimeError, "Need SDL::Surface" unless surface.kind_of? SDL::Surface
59
+
60
+ @tmx.layers.each {|name, l|
61
+ put_layer(name, surface, dx, dy)
62
+ }
63
+ end
64
+
65
+ #Perfoms a blit from entire layer +name+ to +surface+ at (+dx+, +dy+)
66
+ #:dx: on tiles
67
+ #:dy: on tiles
68
+ def put_layer (name, surface, dx, dy)
69
+ raise NameError, "Can get layer of name #{name}" unless @tmx.layers.include? name
70
+ raise RuntimeError, "Need SDL::Surface" unless surface.kind_of? SDL::Surface
71
+
72
+ @tmx.layers[name].render {|layer, bx, by, tileset, index|
73
+ pos = tileset.at(index)
74
+ alpha_now = @tileset_images[tileset.name].alpha
75
+ image = @tileset_images[tileset.name]
76
+ image.setAlpha(SDL::SRCALPHA, layer.opacity * SDL::ALPHA_OPAQUE)
77
+
78
+ x = tileset.tilewidth * (bx + dx)
79
+ y = tileset.tileheight * (by + dy)
80
+
81
+ SDL::Surface.blit(image, pos.x, pos.y, pos.w, pos.h, surface, x, y)
82
+ image.setAlpha(SDL::SRCALPHA, alpha_now)
83
+ }
84
+ end
85
+
86
+ #Perfoms a blit from layer at (+sx+,+sy+,+sw+,+sh+) to +surface+ at (+dx+, +dy)
87
+ #:sx: on tiles
88
+ #:sy: on tiles
89
+ #:sw: on tiles
90
+ #:sh: on tiles
91
+ #:dx: on tiles
92
+ #:dy: on tiles
93
+ def blit_layer(name, sx, sy, sw, sh, surface, dx, dy)
94
+ raise NameError, "Can get layer of name #{name}" unless @tmx.layers.include? name
95
+ raise RuntimeError, "Need SDL::Surface" unless surface.kind_of? SDL::Surface
96
+
97
+ @tmx.layers[name].render_partial(sx, sy, sw, sh) { |layer, bx, by, tileset, index|
98
+ pos = tileset.at(index)
99
+ alpha_now = @tileset_images[tileset.name].alpha
100
+ image = @tileset_images[tileset.name]
101
+ image.setAlpha(SDL::SRCALPHA, layer.opacity * SDL::ALPHA_OPAQUE)
102
+
103
+ x = tileset.tilewidth * (bx + dx)
104
+ y = tileset.tileheight * (by + dy)
105
+
106
+ SDL::Surface.blit(image, pos.x, pos.y, pos.w, pos.h, surface, x, y)
107
+ image.setAlpha(SDL::SRCALPHA, alpha_now)
108
+ }
109
+ end
110
+ end
111
+ end
112
+
113
+ if $0 == __FILE__
114
+
115
+ abort "#{$0}: <file .tmx>" if ARGV.size != 1
116
+
117
+
118
+ SDL.init(SDL::INIT_VIDEO)
119
+ $quit = false
120
+
121
+ tmx = RETMX.load(ARGV[0])
122
+ tmxsdl = RETMX::TMXSDL.new(tmx)
123
+
124
+ $screen = SDL.setVideoMode(tmx.width * tmx.tilewidth, tmx.height * tmx.tileheight, 0, 0)
125
+
126
+ #Blit specific layer
127
+ #tmxsdl.blit_layer('Ground', 0,0,8,8, $screen, 0, 0)
128
+ tmxsdl.put_layers($screen, 0, 0)
129
+ $screen.flip
130
+ until $quit
131
+ e = SDL::Event.wait
132
+ case e
133
+ when SDL::Event::Quit
134
+ $quit = true
135
+ end
136
+ end
137
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: retmx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/retmx.rb
21
+ - lib/retmx_sdl.rb
21
22
  homepage: https://www.manadalibre.org/desarrollos/doku.php?id=videojuegos:librerias:retmx
22
23
  licenses: []
23
24
  post_install_message: