Permalink
Browse files

Version 0.0.1

  • Loading branch information...
0 parents commit 2a60a483b953ae831e737d895e822b824df8d15b @guillermo committed Sep 29, 2011
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in spritopia.gemspec
+gemspec
@@ -0,0 +1,11 @@
+require "bundler/gem_tasks"
+
+require 'minitest/unit'
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.pattern = "test/test_*.rb"
+end
+
+task :default => :test
+
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+
+require 'spritopia'
+
+Spritopia::CLI.start
@@ -0,0 +1,89 @@
+require "spritopia/version"
+require 'chunky_png'
+require 'json'
+
+module Spritopia
+ extend self
+ include ChunkyPNG
+
+ module CLI
+ extend self
+
+ def start
+ if ARGV.size != 1
+ show_usage
+ exit -1
+ else
+ Spritopia.process_file(ARGV.first)
+ end
+ end
+
+
+ private
+
+ def show_usage
+ puts "usage: #{$0} filename.sprite\n\n" <<
+ "\tfilename.sprite should be a file with a list of files, relatives to itself.\n" <<
+ "\tThe files should be .png"
+
+
+ end
+
+ end
+
+
+ def process_file(file)
+ raise "File should be a .sprite file" unless file =~ /\.sprite$/
+ basename = File.basename(file, ".sprite")
+ dir = File.dirname(file)
+ basefilename = File.join(dir, basename)
+
+ File.file?(file) or raise "The file #{file} did not exists"
+
+ # Get the list of the images
+
+ files = File.read(file).split("\n").map{|f| f.strip}
+
+ # Calculate height and width
+ sizes = files.map do |f|
+ image = Image.from_file(File.join(dir,f))
+ [image.width, image.height]
+ end
+
+ height = sizes.inject( 0 ) { |memo,i| memo+i[1] }
+ width = sizes.inject( 0 ) { |memo,i| memo > i[0] ? memo : i[0] }
+
+
+ # Generate the sprite
+ canvas = Image.new(width,height)
+
+ file_info = {}
+
+ files.inject(0) { |current_height, filename|
+ new_image = Image.from_file(File.join(dir,filename))
+ canvas.replace!(new_image, 0, current_height)
+
+ file_info[filename] = {:width => new_image.width, :height => new_image.height, :x => 0, :y => current_height, :x2 => new_image.width, :y2 => new_image.height + current_height }
+
+
+ current_height + new_image.height
+ }
+
+ # Save the sprite
+
+ puts File.basename(file,".sprite")
+
+ canvas.save(basefilename+".png")
+
+ # Save the json
+
+ File.open(basefilename + ".json", "w") do |f|
+ f.write file_info.to_json
+ end
+
+ true
+
+ end
+
+
+end
@@ -0,0 +1,3 @@
+module Spritopia
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "spritopia/version"
+
+Gem::Specification.new do |s|
+ s.name = "spritopia"
+ s.version = Spritopia::VERSION
+ s.authors = ["Guillermo Álvarez"]
+ s.email = ["guillermo@cientifico.net"]
+ s.homepage = ""
+ s.summary = %q{A sprite generator that don't output in css}
+ s.description = %q{Maybe some day it can, but not for the moment}
+
+ s.rubyforge_project = "spritopia"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_development_dependency "minitest"
+ s.add_development_dependency "rake"
+ s.add_runtime_dependency "chunky_png"
+ s.add_runtime_dependency "oily_png"
+ s.add_runtime_dependency "json"
+ s.add_runtime_dependency "thor"
+end
@@ -0,0 +1,3 @@
+images/100x100.png
+images/20x20.png
+images/350x150.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,27 @@
+require 'spritopia'
+require 'minitest/autorun'
+
+class SpritopiaTest < MiniTest::Unit::TestCase
+
+ def test_true
+ assert true
+ end
+
+ def test_it_works
+ sprite_file = full_path('cats.sprite')
+
+ assert_equal true, Spritopia.process_file(sprite_file), "Spritopia should return true"
+
+ assert_file_exists "cats.png", "cats.png should be generated"
+ assert_file_exists "cats.json", "cats.json should be generated"
+ end
+
+
+ def assert_file_exists(filename, message = nil)
+ assert File.file?(full_path(filename)), message
+ end
+
+ def full_path(filename)
+ File.expand_path("../sample/#{filename}",__FILE__)
+ end
+end

0 comments on commit 2a60a48

Please sign in to comment.