#!/usr/bin/env ruby require "erb" TEMPLATE = <] vertices = [ <% vertices.flatten.each do |coord| -%> <%= coord %>, <% end -%> ]; immutable ushort[<%= indices.size %>] indices = [ <% indices.each do |index| -%> <%= index %>, <% end -%> ]; <% object_ranges.each_pair do |obj_name, obj_ranges| -%> immutable char_desc_t[] <%= obj_name %> = [ <% obj_ranges.each do |obj_range| -%> <% obj_verts = indices[*obj_range].map {|i| vertices[i]} -%> <% c_x = (obj_verts.map {|v| v[0]}.min + obj_verts.map {|v| v[0]}.max) / 2.0 -%> <% c_y = (obj_verts.map {|v| v[1]}.min + obj_verts.map {|v| v[1]}.max) / 2.0 -%> {<%= obj_range[0] %>, <%= obj_range[1] %>, <%= c_x %>, <%= c_y %>}, <% end -%> ]; <% end -%> immutable float width = <%= vertices.map {|v| v[0]}.max - vertices.map {|v| v[0]}.min %>; immutable float height = <%= vertices.map {|v| v[1]}.max - vertices.map {|v| v[1]}.min %>; EOF def main(obj_fname, d_fname) objects = {} object_ranges = {} vertices = [] indices = [] cur_obj = nil File.read(obj_fname).each_line do |line| if line =~ /^v\s+(\S+)\s+(\S+)/ vertices << [$1.to_f, $2.to_f] elsif line =~ /^o\s+(..)-(\d+)/ obj_class, obj_class_index = $1, $2.to_i objects[obj_class] ||= [] objects[obj_class][obj_class_index] ||= [] cur_obj = objects[obj_class][obj_class_index] elsif line =~ /^o\s+(.*)/ raise "Do not know how to handle object #{$1.inspect}" elsif line =~ /^f\s+(\d+)\s+(\d+)\s+(\d+)/ cur_obj << [$1, $2, $3].map {|v| v.to_i - 1} elsif line =~ /^l\s+(\d+)\s+(\d+)/ cur_obj << [$1, $2].map {|v| v.to_i - 1} end end objects.each_pair do |obj_name, obj_array| object_ranges[obj_name] = obj_array.map do |obj_indices| obj_indices.flatten! base_index = indices.size indices += obj_indices [base_index, obj_indices.size] end end File.open(d_fname, "w") do |fh| fh.puts ERB.new(TEMPLATE, nil, "<>-").result(binding) end end main(*ARGV)