diff options
| -rw-r--r-- | .travis.yml | 12 | ||||
| -rw-r--r-- | README.md | 37 | ||||
| -rw-r--r-- | Rakefile | 47 | ||||
| -rw-r--r-- | ext/ruby2d/extconf.rb | 103 | ||||
| -rw-r--r-- | ext/ruby2d/ruby2d.c | 17 | ||||
| -rw-r--r-- | lib/ruby2d/dsl.rb | 4 | ||||
| -rw-r--r-- | lib/ruby2d/text.rb | 27 | ||||
| -rw-r--r-- | lib/ruby2d/version.rb | 2 | ||||
| -rw-r--r-- | lib/ruby2d/window.rb | 36 | ||||
| -rw-r--r-- | ruby2d.gemspec | 1 | ||||
| -rw-r--r-- | spec/color_spec.rb | 24 | ||||
| -rw-r--r-- | spec/dsl_spec.rb | 29 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 14 | ||||
| -rw-r--r-- | tests/testcard.rb | 19 |
14 files changed, 209 insertions, 163 deletions
diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..41063cd --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +os: osx +osx_image: xcode7.1 +language: ruby +rvm: + - 2.2.4 +before_install: + - sw_vers + - brew update + - brew tap simple2d/tap + - brew install simple2d +script: + - rake @@ -1,36 +1,31 @@ # Welcome to Ruby 2D! -This is the Ruby 2D gem. [Check out the website](http://www.ruby2d.com) for help getting started, documentation, news, and more. +[](https://travis-ci.org/ruby2d/ruby2d) -## Running Tests +This is the [Ruby 2D gem](https://rubygems.org/gems/ruby2d). Check out the [website](http://www.ruby2d.com) for help getting started, documentation, news, and more. See where we're headed in the [roadmap](http://www.ruby2d.com/roadmap) and learn about [contributing](http://www.ruby2d.com/contribute). If you encounter any issues, ping the [mailing list](https://groups.google.com/d/forum/ruby2d). -Ruby 2D uses both RSpec and custom tests. Run `bundle install` to get all development dependences. +## Development -### Getting the Test Media - -To keep the size of this repository small, media needed for tests are checked into the Simple 2D [`test_media`](https://github.com/simple2d/test_media) repo and referenced as a [Git submodule](http://git-scm.com/book/en/v2/Git-Tools-Submodules). After cloning this repo, init the submodule and get its contents by using: - -```bash -git submodule init -git submodule update --remote -``` - -Alternatively, you can clone the repo and update the submodule in one step: +To work on the gem locally, first clone this repo with the test media [Git submodule](http://git-scm.com/book/en/v2/Git-Tools-Submodules): ```bash git clone --recursive https://github.com/ruby2d/ruby2d.git ``` -Simply run `git submodule update --remote` anytime to get the latest changes from `test_media` (i.e. when there's a new commit available). +Along with cloning this repo, the above will grab the contents of the [`test_media`](https://github.com/simple2d/test_media) repo and place it in the `tests/media` directory. Simply run `git submodule update --remote` anytime to get the latest changes from `test_media` (i.e. when there's a new commit available). If you've already cloned this repo without the `--recursive` flag, make sure to run `git submodule init` before updating the submodule. -### Available Tests +Next, install [Bundler](http://bundler.io) and run `bundle install` to get the required development gems. -The current tests available are: +Finally, install Simple 2D by following the instructions in the [README](https://github.com/simple2d/simple2d). -- [`testcard.rb`](tests/testcard.rb) – A graphical card, similar to [testcards from TV](http://en.wikipedia.org/wiki/Testcard), with the goal of making sure all visual and inputs are working properly. +## Tests -Run a test using `rake <name_of_test>`, for example: +Ruby 2D uses a combination of automated tests via [RSpec](http://rspec.info) and manual, interactive tests to verify visual, audio, and input functionality. Build the gem and run all automated tests with `rake`. Run other tests using `rake <name_of_test>`, such as `rake testcard`. Run `rake -T` to see a list of all available tests. -```bash -rake testcard -``` +## Preparing a Release + +1. Update the Simple 2D minimum version required in [extconf.rb](ext/ruby2d/extconf.rb) +2. Run tests on all supported platforms +3. Update the version number in [`version.rb`](lib/ruby2d/version.rb), commit changes +4. Create a [new release](https://github.com/ruby2d/ruby2d/releases) in GitHub, with tag in the form `v#.#.#` +5. Push to [rubygems.org](rubygems.org) with `gem push ruby2d-#.#.#.gem` @@ -1,30 +1,49 @@ require 'rspec/core/rake_task' require './lib/ruby2d/version' -task default: 'all' +# Helpers + +class String + def colorize(c); "\e[#{c}m#{self}\e[0m" end + def bold; colorize('1') end + def blue; colorize('1;34') end + def red; colorize('1;31') end +end + +# Simple 2D is required for these tasks +if `which simple2d`.empty? + puts "Simple 2D not found!".red + puts "Install before running Rake tasks." + exit +end + +def run_cmd(cmd) + print "\n==> ".blue, cmd.bold, "\n\n" + system cmd +end def run_test(file) Rake::Task['build'].invoke Rake::Task['spec'].invoke + print "==> ".blue, "running tests/#{file}.rb".bold, "\n" system "( cd tests/ ; ruby #{file}.rb )" end -desc "Run the specs" +# Tasks + +task default: 'all' + +desc "Run the RSpec tests" RSpec::Core::RakeTask.new do |t| + print "\n==> ".blue, "running RSpec".bold, "\n\n" t.pattern = "spec/*spec.rb" end -desc "Build Gem" +desc "Build gem" task :build do - puts "==> uninstall gem" - system "gem uninstall ruby2d --executables" - - puts "==> build gem" - system "gem build ruby2d.gemspec --verbose" - - puts "==> install gem" - system "gem install ruby2d-#{Ruby2D::VERSION}.gem "\ - "--local --verbose -- --no-brew" + run_cmd "gem uninstall ruby2d --executables" + run_cmd "gem build ruby2d.gemspec --verbose" + run_cmd "gem install ruby2d-#{Ruby2D::VERSION}.gem --local --verbose" end desc "Run testcard" @@ -32,12 +51,12 @@ task :testcard do run_test 'testcard' end -desc "Run input" +desc "Run input tests" task :input do run_test 'input' end -desc "Run controller" +desc "Run controller tests" task :controller do run_test 'controller' end diff --git a/ext/ruby2d/extconf.rb b/ext/ruby2d/extconf.rb index 721c4fb..ac9d521 100644 --- a/ext/ruby2d/extconf.rb +++ b/ext/ruby2d/extconf.rb @@ -1,84 +1,83 @@ require 'mkmf' +S2D_VERSION = '0.2.1' # Simple 2D minimum version required +$errors = [] + class String def colorize(c); "\e[#{c}m#{self}\e[0m" end - def error; colorize('1;31') end - def bold; colorize('1') end + def bold; colorize('1') end + def red; colorize('1;31') end end -def print_errors(errors) +def print_errors puts " -#{"== Ruby 2D Installation Errors =============================".bold} - - #{"Ruby 2D found some problems and was not installed:".error} - - #{errors.join("\n ")} - -#{"============================================================".bold}" +#{"== #{"Ruby 2D Installation Errors".red} =====================================\n"} + #{$errors.join("\n ")}\n +#{"===================================================================="}" +end + +def check_s2d_version + unless Gem::Version.new(`simple2d --version`) >= Gem::Version.new(S2D_VERSION) + $errors << "Simple 2D needs to be updated for this version of Ruby 2D." << + "Run the following, then try reinstalling this gem:\n" << + " simple2d update".bold + print_errors + exit + end end -errors = [] # Install Simple 2D on supported platforms # OS X if RUBY_PLATFORM =~ /darwin/ - unless ARGV.include? "--no-brew" - # Simple 2D not installed - if `which simple2d`.empty? - # Homebrew not installed, print and quit - if `which brew`.empty? - errors << "Ruby 2D uses a library called Simple 2D." << - "On OS X, this can be installed using Homebrew." << - "Install Homebrew, then try installing this gem again.\n" << - "Learn more at http://brew.sh" - print_errors(errors) - exit - # Install Simple 2D using Homebrew - else - `brew tap simple2d/tap` - `brew install simple2d` - end + + # Simple 2D not installed + if `which simple2d`.empty? + + # Homebrew not installed + if `which brew`.empty? + $errors << "Ruby 2D uses a native library called Simple 2D." << + "On OS X, this can be installed using Homebrew.\n" << + "First, install #{"Homebrew".bold}. See instructions at #{"http://brew.sh".bold}" << + "Then, run the following:\n" << + " brew tap simple2d/tap".bold << + " brew install simple2d".bold + print_errors + exit - # Simple 2D installed, update to latest version + # Homebrew installed, instruct to install Simple 2D else - # Homebrew not installed - if `which brew`.empty? - # TODO: Check for latest version manually and update - # Homebrew installed, get latest version of Simple 2D - else - # An alternative, but slower and updates all formulas: - # `brew update` - # `brew upgrade simple2d` - - `brew untap simple2d/tap` - `brew tap simple2d/tap` - `brew upgrade simple2d` - end + $errors << "Ruby 2D uses a native library called Simple 2D." << + "Install with Homebrew using:\n" << + " brew tap simple2d/tap".bold << + " brew install simple2d".bold + print_errors + exit end end # Linux elsif RUBY_PLATFORM =~ /linux/ - # If Simple 2D not installed + + # Simple 2D not installed if `which simple2d`.empty? - errors << "Ruby 2D uses a library called Simple 2D." << - "There's a script to make installation easy on Linux.\n" << - "Follow the instructions in the README to get started:" << - " https://github.com/simple2d/simple2d" - print_errors(errors) + $errors << "Ruby 2D uses a native library called Simple 2D.\n" << + "To install Simple 2D on Linux, follow the instructions" << + "in the README: #{"https://github.com/simple2d/simple2d".bold}" + print_errors exit end -# Windows -elsif RUBY_PLATFORM =~ /mingw/ - puts "Ruby 2D doesn't support Windows yet :(" - exit + $CFLAGS << ' -std=c99' end +# Simple 2D installed, check version +check_s2d_version + # Configure Simple 2D and create Makefile $LDFLAGS << ' ' << `simple2d --libs` -$LDFLAGS.gsub!(/\n/, ' ') # remove newlines in flags, they cause problems +$LDFLAGS.gsub!(/\n/, ' ') # Remove newlines in flags, they cause problems create_makefile('ruby2d/ruby2d') diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c index 24baeb8..281f402 100644 --- a/ext/ruby2d/ruby2d.c +++ b/ext/ruby2d/ruby2d.c @@ -20,21 +20,21 @@ static VALUE c_data_klass; // Structures for Ruby 2D classes struct image_data { - S2D_Image* img; + S2D_Image *img; }; struct text_data { - S2D_Text* txt; + S2D_Text *txt; }; struct sound_data { - S2D_Sound* snd; + S2D_Sound *snd; }; /* - * Function pointer to close the Simple 2D window + * Function pointer to free the Simple 2D window */ -static void close_window() { - S2D_Close(window); +static void free_window() { + S2D_FreeWindow(window); } @@ -253,9 +253,10 @@ static VALUE ruby2d_show(VALUE s) { char *title = RSTRING_PTR(rb_iv_get(self, "@title")); int width = NUM2INT(rb_iv_get(self, "@width")); int height = NUM2INT(rb_iv_get(self, "@height")); + int flags = 0; window = S2D_CreateWindow( - title, width, height, update, render, 0 + title, width, height, update, render, flags ); window->on_key = on_key; @@ -264,7 +265,7 @@ static VALUE ruby2d_show(VALUE s) { S2D_Show(window); - atexit(close_window); + atexit(free_window); return Qnil; } diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb index ef8f384..93fedb3 100644 --- a/lib/ruby2d/dsl.rb +++ b/lib/ruby2d/dsl.rb @@ -1,10 +1,6 @@ # dsl.rb module Ruby2D::DSL - def hello - puts "hi" - end - def get(sym) Ruby2D::Application.get(sym) end diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index 2dd36e9..93c2862 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -5,20 +5,17 @@ module Ruby2D attr_accessor :x, :y, :size, :text - def initialize(x=0, y=0, size=20, text="Hello World!", font="Arial", c="white") - if font.include? '.' - unless File.exists? font - raise Error, "Cannot find font file!" - else - @font = font - end + def initialize(x=0, y=0, size=20, msg="Hello World!", font="Arial", c="white") + + if File.exists? font + @font = font else @font = resolve_path(font) end @type_id = 4 @x, @y, @size = x, y, size - @text, @color = text, c + @text, @color = msg, c update_color(c) if defined? Ruby2D::DSL @@ -44,14 +41,18 @@ module Ruby2D private def resolve_path(font) - # TODO: Consider CSS names, like 'serif', 'san-serif', 'monospace' if RUBY_PLATFORM =~ /darwin/ font_path = "/Library/Fonts/#{font}.ttf" - unless File.exists? font_path - raise Error, "Cannot find system font!" - end + else + # Linux + font_path = "/usr/share/fonts/truetype/#{font}.ttf" + end + + unless File.exists? font_path + raise Error, "Cannot find system font" + else + font_path end - font_path end def update_color(c) diff --git a/lib/ruby2d/version.rb b/lib/ruby2d/version.rb index c5011e0..eede3ba 100644 --- a/lib/ruby2d/version.rb +++ b/lib/ruby2d/version.rb @@ -1,5 +1,5 @@ # version.rb module Ruby2D - VERSION = '0.2.0' + VERSION = '0.2.1' end diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index 1b3698e..5889c5d 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -10,7 +10,6 @@ module Ruby2D @fps_cap = fps @fps = 60 @vsync = vsync - @objects = [] @keys = {} @keys_down = {} @@ -20,34 +19,21 @@ module Ruby2D def get(sym) case sym - when :window - return self - when :title - return @title - when :width - return @width - when :height - return @height - when :fps - return @fps - when :mouse_x - return @mouse_x - when :mouse_y - return @mouse_y + when :window; self + when :title; @title + when :width; @width + when :height; @height + when :fps; @fps + when :mouse_x; @mouse_x + when :mouse_y; @mouse_y end end def set(opts) - valid_keys = [:title, :width, :height] - valid_opts = opts.reject { |k| !valid_keys.include?(k) } - if !valid_opts.empty? - @title = valid_opts[:title] - @width = valid_opts[:width] - @height = valid_opts[:height] - return true - else - return false - end + # Store new window attributes, or ignore if nil + @title = opts[:title] || @title + @width = opts[:width] || @width + @height = opts[:height] || @height end def add(o) diff --git a/ruby2d.gemspec b/ruby2d.gemspec index 95c2662..008cfcd 100644 --- a/ruby2d.gemspec +++ b/ruby2d.gemspec @@ -4,7 +4,6 @@ require 'ruby2d/version' Gem::Specification.new do |s| s.name = 'ruby2d' s.version = Ruby2D::VERSION - s.date = '2016-01-01' s.author = 'Tom Black' s.email = '@blacktm' s.summary = 'Ruby 2D' diff --git a/spec/color_spec.rb b/spec/color_spec.rb index 964bc6f..deafe81 100644 --- a/spec/color_spec.rb +++ b/spec/color_spec.rb @@ -1,37 +1,31 @@ require 'ruby2d' - RSpec.describe Ruby2D::Color do describe '#is_valid?' do - it 'determines if a color string is valid' do - expect(Ruby2D::Color.is_valid? 'red').to be true - expect(Ruby2D::Color.is_valid? 'balloons').to be false + expect(Ruby2D::Color.is_valid? 'red').to eq true + expect(Ruby2D::Color.is_valid? 'balloons').to eq false end it 'determines if an array is a valid color' do - expect(Ruby2D::Color.is_valid? [1.0, 0, 0, 1.0]).to be true - expect(Ruby2D::Color.is_valid? [1.0, 0, 0]).to be false + expect(Ruby2D::Color.is_valid? [1.0, 0, 0, 1.0]).to eq true + expect(Ruby2D::Color.is_valid? [1.0, 0, 0]).to eq false end it 'prevents allow color values out of range' do - expect(Ruby2D::Color.is_valid? [1.0, 0, 0.0, 255]).to be true - expect(Ruby2D::Color.is_valid? [1.2, 0, 0, 0]).to be false - expect(Ruby2D::Color.is_valid? [-0.1, 0, 0, 0]).to be false - expect(Ruby2D::Color.is_valid? [255, 255, 256, 255]).to be false - expect(Ruby2D::Color.is_valid? [-1, 0, 127, 255]).to be false + expect(Ruby2D::Color.is_valid? [1.0, 0, 0.0, 255]).to eq true + expect(Ruby2D::Color.is_valid? [1.2, 0, 0, 0]).to eq false + expect(Ruby2D::Color.is_valid? [-0.1, 0, 0, 0]).to eq false + expect(Ruby2D::Color.is_valid? [255, 255, 256, 255]).to eq false + expect(Ruby2D::Color.is_valid? [-1, 0, 127, 255]).to eq false end - end - describe '#new' do - it 'raises error on bad color' do expect { Ruby2D::Color.new 42 }.to raise_error Ruby2D::Error end - end end diff --git a/spec/dsl_spec.rb b/spec/dsl_spec.rb new file mode 100644 index 0000000..6d3d8b4 --- /dev/null +++ b/spec/dsl_spec.rb @@ -0,0 +1,29 @@ +require 'ruby2d' + +RSpec.describe Ruby2D::DSL do + + describe '#get' do + it 'gets the default window attributes' do + expect(get :width).to eq 640 + expect(get :height).to eq 480 + expect(get :title).to eq "Ruby 2D" + end + end + + describe '#set' do + it 'sets a single window attribute' do + set width: 300 + expect(get :width).to eq 300 + expect(get :height).to eq 480 + expect(get :title).to eq "Ruby 2D" + end + + it 'sets multiple window attributes at a time' do + set width: 800, height: 600, title: "Hello tests!" + expect(get :width).to eq 800 + expect(get :height).to eq 600 + expect(get :title).to eq "Hello tests!" + end + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 82b6100..d93bff8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,4 +6,18 @@ RSpec.configure do |config| config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end + + # Silence output when running tests + original_stderr = $stderr + original_stdout = $stdout + config.before(:all) do + # Redirect stderr and stdout + $stderr = File.open(File::NULL, "w") + $stdout = File.open(File::NULL, "w") + end + config.after(:all) do + # Restore + $stderr = original_stderr + $stdout = original_stdout + end end diff --git a/tests/testcard.rb b/tests/testcard.rb index 1ab2267..e5b5f8a 100644 --- a/tests/testcard.rb +++ b/tests/testcard.rb @@ -17,10 +17,10 @@ Rectangle.new(50, 0, 50, 100, [0, 1.0, 0, 1.0]) Rectangle.new(100, 0, 50, 100, [0, 0, 1.0, 1.0]) # Color strings -Rectangle.new(150, 0, 50, 50, 'black') -Rectangle.new(200, 0, 50, 50, 'gray') -Rectangle.new(250, 0, 50, 50, 'silver') -Rectangle.new(300, 0, 50, 50, 'white') +Square.new( 150, 0, 50, 'black') +Square.new( 200, 0, 50, 'gray') +Square.new( 250, 0, 50, 'silver') +Square.new( 300, 0, 50, 'white') Rectangle.new(350, 0, 50, 50, 'navy') Rectangle.new(400, 0, 50, 50, 'blue') Rectangle.new(450, 0, 50, 50, 'aqua') @@ -129,16 +129,17 @@ Quad.new( ) # Images -Image.new(580, 180, "media/image.png") -Image.new(580, 290, "media/image.jpg") -Image.new(580, 400, "media/image.bmp") +Image.new(590, 180, "media/image.png") +Image.new(590, 290, "media/image.jpg") +Image.new(590, 400, "media/image.bmp") # Text Text.new(0, 250) # Default message -t = Text.new(0, 275, 30, "Hello Ruby 2D!") # Custom message -t.color = 'red' +t = Text.new(0, 275, 30, "Hello Ruby 2D!", "media/bitstream_vera/vera.ttf") +t.color = 'red' # Doesn't work yet fps = Text.new(0, 325, 20) + # Pointer for mouse pointer = Square.new(0, 0, 10, 'white') |
