From 742bb574dc506fbb7a8d1774c44736ad0e228c54 Mon Sep 17 00:00:00 2001 From: Tom Black Date: Sun, 6 Jan 2019 02:05:39 -0800 Subject: Raise errors if media can't be read Check if NULL pointers are returned by native functions loading media for images, sprites, audio, etc. If NULL, raise Ruby 2D error. --- lib/ruby2d/image.rb | 4 +++- lib/ruby2d/music.rb | 4 +++- lib/ruby2d/sound.rb | 4 +++- lib/ruby2d/sprite.rb | 4 +++- lib/ruby2d/text.rb | 4 +++- 5 files changed, 15 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb index 1341f89..67ce1b1 100644 --- a/lib/ruby2d/image.rb +++ b/lib/ruby2d/image.rb @@ -20,7 +20,9 @@ module Ruby2D @rotate = opts[:rotate] || 0 self.color = opts[:color] || 'white' self.opacity = opts[:opacity] if opts[:opacity] - ext_init(@path) + unless ext_init(@path) + raise Error, "Image `#{@path}` cannot be created" + end add end diff --git a/lib/ruby2d/music.rb b/lib/ruby2d/music.rb index ef8deb5..46aab0a 100644 --- a/lib/ruby2d/music.rb +++ b/lib/ruby2d/music.rb @@ -12,7 +12,9 @@ module Ruby2D end @path = path @loop = false - ext_init(path) + unless ext_init(@path) + raise Error, "Music `#{@path}` cannot be created" + end end # Play the music diff --git a/lib/ruby2d/sound.rb b/lib/ruby2d/sound.rb index 2c78525..2dae996 100644 --- a/lib/ruby2d/sound.rb +++ b/lib/ruby2d/sound.rb @@ -11,7 +11,9 @@ module Ruby2D raise Error, "Cannot find audio file `#{path}`" end @path = path - ext_init(path) + unless ext_init(@path) + raise Error, "Sound `#{@path}` cannot be created" + end end # Play the sound diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb index 94a6ea2..8405e5f 100644 --- a/lib/ruby2d/sprite.rb +++ b/lib/ruby2d/sprite.rb @@ -45,7 +45,9 @@ module Ruby2D @img_width = nil; @img_height = nil # Initialize the sprite - ext_init(@path) + unless ext_init(@path) + raise Error, "Sprite image `#{@path}` cannot be created" + end # The clipping rectangle @clip_x = opts[:clip_x] || 0 diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index a526672..d62bc0b 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -20,7 +20,9 @@ module Ruby2D unless File.exist? @font raise Error, "Cannot find font file `#{@font}`" end - ext_init + unless ext_init + raise Error, "Text `#{@text}` cannot be created" + end add end -- cgit v1.2.3