Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Bitmap Replace Color
- # @author : Gabriel "Gab!" Teles
- # @date : 2014/07/30
- # @website : http://www.mundorpgmaker.com.br/
- class Bitmap
- MemcpyPL = Win32API.new('msvcrt', 'memcpy', 'pll', 'v')
- MemcpyLP = Win32API.new('msvcrt', 'memcpy', 'lpl', 'v')
- def address
- ptr = [0].pack('L')
- MemcpyPL.call(ptr, object_id * 2 + 16, 4)
- MemcpyPL.call(ptr, ptr.unpack('L').first + 8, 4)
- MemcpyPL.call(ptr, ptr.unpack('L').first + 16, 4)
- return ptr.unpack('L').first
- end
- def getRawData
- buf = 0.chr * (width * height * 4)
- Bitmap::MemcpyPL.call(buf, address, buf.size)
- return buf
- end
- def setRawData(buf)
- raise(RuntimeError, "Tamanho Incorreto") if (buf.size != (width * height * 4))
- Bitmap::MemcpyLP.call(address, buf, buf.size)
- end
- def replaceColor(fC, tC)
- data = getRawData
- offset = 0
- f = [fC.blue, fC.green, fC.red, fC.alpha].pack('C4')
- t = [tC.blue, tC.green, tC.red, tC.alpha].pack('C4')
- (width * height).times{
- data[offset, 4] = t if data[offset, 4] == f
- offset += 4
- }
- setRawData(data)
- end
- end
- x = Bitmap.new(100, 100)
- x.fill_rect(0, 0, 50, 50, Color.new(255, 0, 0))
- x.fill_rect(50, 0, 50, 50, Color.new(0, 255, 0))
- x.fill_rect(0, 50, 50, 50, Color.new(0, 0, 255))
- x.fill_rect(50, 50, 50, 50, Color.new(255, 0, 255))
- x.replaceColor(Color.new(255, 0, 0), Color.new(255, 255, 0))
- y = Sprite.new
- y.bitmap = x
- loop { Graphics.update }
Advertisement
Add Comment
Please, Sign In to add comment