diff --git a/samples/06_image.cr b/samples/06_image.cr index 3687105..5450007 100644 --- a/samples/06_image.cr +++ b/samples/06_image.cr @@ -5,10 +5,10 @@ SDL.init(SDL::Init::VIDEO) at_exit { SDL.quit } window = SDL::Window.new("SDL Tutorial", 640, 480) -IMG.init(IMG::Init::PNG) -at_exit { IMG.quit } +SDL::IMG.init(SDL::IMG::Init::PNG) +at_exit { SDL::IMG.quit } -png = IMG.load(File.join(__DIR__, "data", "loaded.png")) +png = SDL::IMG.load(File.join(__DIR__, "data", "loaded.png")) png = png.convert(window.surface) loop do diff --git a/samples/07_texture.cr b/samples/07_texture.cr index 27ff871..29978aa 100644 --- a/samples/07_texture.cr +++ b/samples/07_texture.cr @@ -7,8 +7,8 @@ at_exit { SDL.quit } window = SDL::Window.new("SDL Tutorial", 640, 480) renderer = SDL::Renderer.new(window) -IMG.init(IMG::Init::PNG) -texture = IMG.load(File.join(__DIR__, "data", "loaded.png"), renderer) +SDL::IMG.init(SDL::IMG::Init::PNG) +texture = SDL::IMG.load(File.join(__DIR__, "data", "loaded.png"), renderer) loop do case event = SDL::Event.wait diff --git a/samples/09_viewport.cr b/samples/09_viewport.cr index 08fda1b..f8b0b6a 100644 --- a/samples/09_viewport.cr +++ b/samples/09_viewport.cr @@ -8,7 +8,7 @@ window = SDL::Window.new("SDL tutorial", 640, 480) renderer = SDL::Renderer.new(window) width, height = window.size -png = IMG.load(File.join(__DIR__, "data", "loaded.png"), renderer) +png = SDL::IMG.load(File.join(__DIR__, "data", "loaded.png"), renderer) loop do case event = SDL::Event.wait diff --git a/samples/10_colorkey.cr b/samples/10_colorkey.cr index 2c7e0a3..789479e 100644 --- a/samples/10_colorkey.cr +++ b/samples/10_colorkey.cr @@ -4,17 +4,17 @@ require "../src/image" SDL.init(SDL::Init::VIDEO) at_exit { SDL.quit } -IMG.init(IMG::Init::PNG) -at_exit { IMG.quit } +SDL::IMG.init(SDL::IMG::Init::PNG) +at_exit { SDL::IMG.quit } SDL.set_hint(SDL::Hint::RENDER_SCALE_QUALITY, "1") window = SDL::Window.new("SDL tutorial", 640, 480) renderer = SDL::Renderer.new window -background = IMG.load(File.join(__DIR__, "data", "background.png"), renderer) +background = SDL::IMG.load(File.join(__DIR__, "data", "background.png"), renderer) # image has a colored background, declare the color as transparent: -image = IMG.load(File.join(__DIR__, "data", "foo.png")) +image = SDL::IMG.load(File.join(__DIR__, "data", "foo.png")) image.color_key = {0, 255, 255} foo = SDL::Texture.from(image, renderer) diff --git a/samples/11_sprites.cr b/samples/11_sprites.cr index 208a7db..feeddea 100644 --- a/samples/11_sprites.cr +++ b/samples/11_sprites.cr @@ -2,12 +2,12 @@ require "../src/sdl" require "../src/image" SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit } -IMG.init(IMG::Init::PNG); at_exit { IMG.quit } +SDL::IMG.init(SDL::IMG::Init::PNG); at_exit { SDL::IMG.quit } window = SDL::Window.new("SDL tutorial", 640, 480) renderer = SDL::Renderer.new(window) -image = IMG.load(File.join(__DIR__, "data", "sprites.png")) +image = SDL::IMG.load(File.join(__DIR__, "data", "sprites.png")) image.color_key = {0, 255, 255} sprite = SDL::Texture.from(image, renderer) diff --git a/samples/12_color_modulation.cr b/samples/12_color_modulation.cr index fe7b861..8af9d21 100644 --- a/samples/12_color_modulation.cr +++ b/samples/12_color_modulation.cr @@ -2,12 +2,12 @@ require "../src/sdl" require "../src/image" SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit } -IMG.init(IMG::Init::PNG); at_exit { IMG.quit } +SDL::IMG.init(SDL::IMG::Init::PNG); at_exit { SDL::IMG.quit } window = SDL::Window.new("SDL tutorial", 640, 480) renderer = SDL::Renderer.new(window) -image = IMG.load(File.join(__DIR__, "data", "full.png"), renderer) +image = SDL::IMG.load(File.join(__DIR__, "data", "full.png"), renderer) r = g = b = 255 loop do diff --git a/samples/13_alpha_blending.cr b/samples/13_alpha_blending.cr index c27194c..7f29065 100644 --- a/samples/13_alpha_blending.cr +++ b/samples/13_alpha_blending.cr @@ -2,13 +2,13 @@ require "../src/sdl" require "../src/image" SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit } -IMG.init(IMG::Init::PNG); at_exit { IMG.quit } +SDL::IMG.init(SDL::IMG::Init::PNG); at_exit { SDL::IMG.quit } window = SDL::Window.new("SDL tutorial", 640, 480) renderer = SDL::Renderer.new(window) -background = IMG.load(File.join(__DIR__, "data", "fadein.png"), renderer) -modulated = IMG.load(File.join(__DIR__, "data", "fadeout.png"), renderer) +background = SDL::IMG.load(File.join(__DIR__, "data", "fadein.png"), renderer) +modulated = SDL::IMG.load(File.join(__DIR__, "data", "fadeout.png"), renderer) a = 255 loop do diff --git a/samples/14_animated_sprites_and_vsync.cr b/samples/14_animated_sprites_and_vsync.cr index ba76050..0c92809 100644 --- a/samples/14_animated_sprites_and_vsync.cr +++ b/samples/14_animated_sprites_and_vsync.cr @@ -2,13 +2,13 @@ require "../src/sdl" require "../src/image" SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit } -IMG.init(IMG::Init::PNG); at_exit { IMG.quit } +SDL::IMG.init(SDL::IMG::Init::PNG); at_exit { SDL::IMG.quit } width, height = 640, 480 window = SDL::Window.new("SDL tutorial", 640, 480) renderer = SDL::Renderer.new(window, SDL::Renderer::Flags::ACCELERATED | SDL::Renderer::Flags::PRESENTVSYNC) -sprite = IMG.load(File.join(__DIR__, "data", "foo_sprite.png"), renderer) +sprite = SDL::IMG.load(File.join(__DIR__, "data", "foo_sprite.png"), renderer) sprite_clips = StaticArray(SDL::Rect, 4).new do |i| SDL::Rect.new(i * 64, 0, 64, 305) end diff --git a/samples/15_rotation_and_flipping.cr b/samples/15_rotation_and_flipping.cr index 2b5da47..3606e0e 100644 --- a/samples/15_rotation_and_flipping.cr +++ b/samples/15_rotation_and_flipping.cr @@ -2,12 +2,12 @@ require "../src/sdl" require "../src/image" SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit } -IMG.init(IMG::Init::PNG); at_exit { IMG.quit } +SDL::IMG.init(SDL::IMG::Init::PNG); at_exit { SDL::IMG.quit } window = SDL::Window.new("SDL tutorial", 640, 480) renderer = SDL::Renderer.new(window, SDL::Renderer::Flags::ACCELERATED | SDL::Renderer::Flags::PRESENTVSYNC) -arrow = IMG.load(File.join(__DIR__, "data", "arrow.png"), renderer) +arrow = SDL::IMG.load(File.join(__DIR__, "data", "arrow.png"), renderer) degrees = 0 flip = SDL::Renderer::Flip::NONE diff --git a/samples/16_ttf.cr b/samples/16_ttf.cr index ecc4dd0..b577cb9 100644 --- a/samples/16_ttf.cr +++ b/samples/16_ttf.cr @@ -2,12 +2,12 @@ require "../src/sdl" require "../src/ttf" SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit } -TTF.init; at_exit { TTF.quit } +SDL::TTF.init; at_exit { SDL::TTF.quit } window = SDL::Window.new("SDL tutorial", 640, 480) renderer = SDL::Renderer.new(window, SDL::Renderer::Flags::ACCELERATED | SDL::Renderer::Flags::PRESENTVSYNC) -font = TTF::Font.new(File.join(__DIR__, "data", "lazy.ttf"), 28) +font = SDL::TTF::Font.new(File.join(__DIR__, "data", "lazy.ttf"), 28) loop do case event = SDL::Event.wait diff --git a/src/image.cr b/src/image.cr index 0d0a5e4..7d19f27 100644 --- a/src/image.cr +++ b/src/image.cr @@ -1,113 +1,115 @@ require "./lib_img" require "./sdl" -module IMG - # :nodoc: - alias Error = SDL::Error +module SDL + module IMG + # :nodoc: + alias Error = SDL::Error - alias Init = LibIMG::Init + alias Init = LibIMG::Init - enum Type - BMP - CUR - GIF - ICO - JPG - LBM - PCX - PNG - PNM - TGA - TIF - XCF - XPM - XV - WEBP - end - - def self.init(flags : Init) - ret = LibIMG.init(flags) - unless (ret & flags.value) == flags.value - raise SDL::Error.new("IMG_Init failed to init #{flags}") + enum Type + BMP + CUR + GIF + ICO + JPG + LBM + PCX + PNG + PNM + TGA + TIF + XCF + XPM + XV + WEBP end - end - def self.quit - LibIMG.quit - end - - # Loads the image from *path* using the optional *type* hint as a - # `SDL::Surface`. - def self.load(path, type : Type? = nil) - SDL::RWops.open(path, "rb") do |rwops| - if type - surface = LibIMG.load_typed_rw(rwops, 1, type.to_s) - raise Error.new("IMG_LoadTyped_RW") unless surface - else - surface = LibIMG.load_rw(rwops, 1) - raise Error.new("IMG_Load_RW") unless surface + def self.init(flags : Init) + ret = LibIMG.init(flags) + unless (ret & flags.value) == flags.value + raise SDL::Error.new("IMG_Init failed to init #{flags}") end - SDL::Surface.new(surface) end - end - # Loads the image from *path* using the optional *type* hint as a - # `SDL::Texture` for *renderer*. - def self.load(path, renderer : SDL::Renderer, type : Type? = nil) - SDL::RWops.open(path, "rb") do |rwops| - if type - texture = LibIMG.load_texture_typed_rw(renderer, rwops, 1, type.to_s) - raise Error.new("IMG_LoadTextureTyped_RW") unless texture - else - texture = LibIMG.load_texture_rw(renderer, rwops, 1) - raise Error.new("IMG_LoadTexture_RW") unless texture - end - SDL::Texture.new(texture) + def self.quit + LibIMG.quit end - end - {% for type in Type.constants %} - # Loads the {{type.id}} image from *path* as a `SDL::Surface`. - def self.load_{{type.downcase.id}}(path) + # Loads the image from *path* using the optional *type* hint as a + # `SDL::Surface`. + def self.load(path, type : Type? = nil) SDL::RWops.open(path, "rb") do |rwops| - surface = LibIMG.load_{{type.downcase.id}}_rw(rwops, 1) - raise Error.new("IMG_Load{{type.id}}_RW") unless surface + if type + surface = LibIMG.load_typed_rw(rwops, 1, type.to_s) + raise Error.new("IMG_LoadTyped_RW") unless surface + else + surface = LibIMG.load_rw(rwops, 1) + raise Error.new("IMG_Load_RW") unless surface + end SDL::Surface.new(surface) end end - # Returns true if the image at *path* is a {{type.id}} file. - def self.{{type.downcase.id}}?(path) + # Loads the image from *path* using the optional *type* hint as a + # `SDL::Texture` for *renderer*. + def self.load(path, renderer : SDL::Renderer, type : Type? = nil) SDL::RWops.open(path, "rb") do |rwops| - LibIMG.is_{{type.downcase.id}}(rwops) == 1 + if type + texture = LibIMG.load_texture_typed_rw(renderer, rwops, 1, type.to_s) + raise Error.new("IMG_LoadTextureTyped_RW") unless texture + else + texture = LibIMG.load_texture_rw(renderer, rwops, 1) + raise Error.new("IMG_LoadTexture_RW") unless texture + end + SDL::Texture.new(texture) end end - {% end %} - #class File - # def initialize(path) - # @rwops = SDL::RWops.new(path, "rb") - # end + {% for type in Type.constants %} + # Loads the {{type.id}} image from *path* as a `SDL::Surface`. + def self.load_{{type.downcase.id}}(path) + SDL::RWops.open(path, "rb") do |rwops| + surface = LibIMG.load_{{type.downcase.id}}_rw(rwops, 1) + raise Error.new("IMG_Load{{type.id}}_RW") unless surface + SDL::Surface.new(surface) + end + end + + # Returns true if the image at *path* is a {{type.id}} file. + def self.{{type.downcase.id}}?(path) + SDL::RWops.open(path, "rb") do |rwops| + LibIMG.is_{{type.downcase.id}}(rwops) == 1 + end + end + {% end %} + + #class File + # def initialize(path) + # @rwops = SDL::RWops.new(path, "rb") + # end - # # Loads the file as a `SDL::Surface`. - # def load - # surface = LibIMG.load_rw(@rwops, 1) - # raise Error.new("IMG_Load_RW") unless surface - # SDL::Surface.new(surface) - # end + # # Loads the file as a `SDL::Surface`. + # def load + # surface = LibIMG.load_rw(@rwops, 1) + # raise Error.new("IMG_Load_RW") unless surface + # SDL::Surface.new(surface) + # end - # # Loads the file as a `SDL::Texture` for *renderer*. - # def load(renderer : SDL::Renderer) - # texture = LibIMG.load_texture_rw(renderer, @rwops, 1) - # raise Error.new("IMG_LoadTexture_RW") unless texture - # SDL::Texture.new(texture) - # end + # # Loads the file as a `SDL::Texture` for *renderer*. + # def load(renderer : SDL::Renderer) + # texture = LibIMG.load_texture_rw(renderer, @rwops, 1) + # raise Error.new("IMG_LoadTexture_RW") unless texture + # SDL::Texture.new(texture) + # end - # {% for type in Type.constants %} - # # Returns true if the file is a {{type.id}} file. - # def {{type.downcase.id}}? - # LibIMG.is_{{type.downcase.id}}(@rwops) == 1 - # end - # {% end %} - #end + # {% for type in Type.constants %} + # # Returns true if the file is a {{type.id}} file. + # def {{type.downcase.id}}? + # LibIMG.is_{{type.downcase.id}}(@rwops) == 1 + # end + # {% end %} + #end + end end diff --git a/src/ttf.cr b/src/ttf.cr index 7e5b25e..da9b403 100644 --- a/src/ttf.cr +++ b/src/ttf.cr @@ -2,243 +2,245 @@ require "./lib_ttf" require "./sdl" require "./color" -module TTF - alias Error = SDL::Error - alias Color = SDL::Color +module SDL + module TTF + alias Error = SDL::Error + alias Color = SDL::Color - def self.init - if LibTTF.was_init == 1 - return - end - unless LibTTF.init == 0 - raise SDL::Error.new("TTF_Init failed to init") + def self.init + if LibTTF.was_init == 1 + return + end + unless LibTTF.init == 0 + raise SDL::Error.new("TTF_Init failed to init") + end end - end - - def self.quit - LibTTF.quit if LibTTF.was_init == 1 - end - - class Font - alias Style = LibTTF::Style - enum Mode - SOLID - SHADED - BLENDED - BLENDED_WRAPPED + def self.quit + LibTTF.quit if LibTTF.was_init == 1 end - def self.open(path) - font = new(path) - begin - yield font - ensure - font.close - end - end + class Font + alias Style = LibTTF::Style - def initialize(path, point_size = 16, index = nil) - @rwops = SDL::RWops.new(path, "rb") - if index - @font = LibTTF.open_font_index_rw(@rwops, 1, point_size, index) - else - @font = LibTTF.open_font_rw(@rwops, 1, point_size) + enum Mode + SOLID + SHADED + BLENDED + BLENDED_WRAPPED end - @closed = false - end - def finalize - close unless @closed - end + def self.open(path) + font = new(path) + begin + yield font + ensure + font.close + end + end - def kerning=(allowed : Bool) - LibTTF.set_font_kerning(self, allowed ? 1 : 0) - end + def initialize(path, point_size = 16, index = nil) + @rwops = SDL::RWops.new(path, "rb") + if index + @font = LibTTF.open_font_index_rw(@rwops, 1, point_size, index) + else + @font = LibTTF.open_font_rw(@rwops, 1, point_size) + end + @closed = false + end - def style - Style.new(LibTTF.get_font_style(self)) - end + def finalize + close unless @closed + end - def style=(style : Style) - LibTTF.set_font_style(self, style) - end + def kerning=(allowed : Bool) + LibTTF.set_font_kerning(self, allowed ? 1 : 0) + end - def outline - LibTTF.get_font_outline(self) - end + def style + Style.new(LibTTF.get_font_style(self)) + end - def outline=(outline) - LibTTF.set_font_outline(self, outline) - end + def style=(style : Style) + LibTTF.set_font_style(self, style) + end - # Returns the maximum pixel height of all glyphs of the font. - def height - LibTTF.font_height(self) - end + def outline + LibTTF.get_font_outline(self) + end - # Returns the maximum pixel ascent of all glyphs of the font. - def ascent - LibTTF.font_ascent(self) - end + def outline=(outline) + LibTTF.set_font_outline(self, outline) + end - # Returns the maximum pixel descent of all glyphs of the font. - def descent - LibTTF.font_descent(self) - end + # Returns the maximum pixel height of all glyphs of the font. + def height + LibTTF.font_height(self) + end - # Returns the reccomended pixel height of a rendered line of text of the - # font. - def line_skip - LibTTF.font_line_skip(self) - end + # Returns the maximum pixel ascent of all glyphs of the font. + def ascent + LibTTF.font_ascent(self) + end - # Returns the number of faces (sub-fonts) available in the font. - def faces - LibTTF.font_faces(self) - end + # Returns the maximum pixel descent of all glyphs of the font. + def descent + LibTTF.font_descent(self) + end - def face_fixed_width? - LibTTF.font_face_is_fixed_width(self) == 1 - end + # Returns the reccomended pixel height of a rendered line of text of the + # font. + def line_skip + LibTTF.font_line_skip(self) + end - def face_family_name - if chars = LibTTF.font_face_family_name(self) - String.new(chars) + # Returns the number of faces (sub-fonts) available in the font. + def faces + LibTTF.font_faces(self) end - end - def face_style_name - if chars = LibTTF.font_face_style_name(self) - String.new(chars) + def face_fixed_width? + LibTTF.font_face_is_fixed_width(self) == 1 end - end - def glyph_provided?(char : UInt16) - LibTTF.glyph_is_provided(self, char) == 1 - end + def face_family_name + if chars = LibTTF.font_face_family_name(self) + String.new(chars) + end + end - record GlyphMetrics, - char : UInt16, - min_x : Int32, - min_y : Int32, - max_x : Int32, - max_y : Int32, - advance : Int32 + def face_style_name + if chars = LibTTF.font_face_style_name(self) + String.new(chars) + end + end - def glyph_metrics(char : UInt16) - unless glyph_provided?(char) - return + def glyph_provided?(char : UInt16) + LibTTF.glyph_is_provided(self, char) == 1 end - if LibTTF.glyph_metrics(self, char, out minx, out maxx, out miny, out maxy, out advance) == 0 - Metrics.new(char, minx, miny, maxx, maxy, advance) - else - raise Error.new("TTF_GlyphMetrics") + + record GlyphMetrics, + char : UInt16, + min_x : Int32, + min_y : Int32, + max_x : Int32, + max_y : Int32, + advance : Int32 + + def glyph_metrics(char : UInt16) + unless glyph_provided?(char) + return + end + if LibTTF.glyph_metrics(self, char, out minx, out maxx, out miny, out maxy, out advance) == 0 + Metrics.new(char, minx, miny, maxx, maxy, advance) + else + raise Error.new("TTF_GlyphMetrics") + end end - end - # Returns the `{width, height}` of *text* if rendered with the current font. - def size_of(text : String, ascii = false) - if ascii - ret = LibTTF.size_ascii(self, text, out width, out height) - raise Error.new("TTF_SizeText") unless ret == 0 - else - ret = LibTTF.size_utf8(self, text, out width, out height) - raise Error.new("TTF_SizeUTF8") unless ret == 0 + # Returns the `{width, height}` of *text* if rendered with the current font. + def size_of(text : String, ascii = false) + if ascii + ret = LibTTF.size_ascii(self, text, out width, out height) + raise Error.new("TTF_SizeText") unless ret == 0 + else + ret = LibTTF.size_utf8(self, text, out width, out height) + raise Error.new("TTF_SizeUTF8") unless ret == 0 + end + {width, height} end - {width, height} - end - # Returns the `width` of *text* if rendered with the current font. - def width_of(text : String, ascii = false) - if ascii - ret = LibTTF.size_ascii(self, text, out width, nil) - raise Error.new("TTF_SizeText") unless ret == 0 - else - ret = LibTTF.size_utf8(self, text, out width, nil) - raise Error.new("TTF_SizeUTF8") unless ret == 0 + # Returns the `width` of *text* if rendered with the current font. + def width_of(text : String, ascii = false) + if ascii + ret = LibTTF.size_ascii(self, text, out width, nil) + raise Error.new("TTF_SizeText") unless ret == 0 + else + ret = LibTTF.size_utf8(self, text, out width, nil) + raise Error.new("TTF_SizeUTF8") unless ret == 0 + end + width end - width - end - # Returns the `height` of *text* if rendered with the current font. - def height_of(text : String, ascii = false) - if ascii - ret = LibTTF.size_ascii(self, text, nil, out height) - raise Error.new("TTF_SizeText") unless ret == 0 - else - ret = LibTTF.size_utf8(self, text, nil, out height) - raise Error.new("TTF_SizeUTF8") unless ret == 0 + # Returns the `height` of *text* if rendered with the current font. + def height_of(text : String, ascii = false) + if ascii + ret = LibTTF.size_ascii(self, text, nil, out height) + raise Error.new("TTF_SizeText") unless ret == 0 + else + ret = LibTTF.size_utf8(self, text, nil, out height) + raise Error.new("TTF_SizeUTF8") unless ret == 0 + end + height end - height - end - # Renders text using this font as an `SDL::Surface` using the "slow and - # nice" shaded mode. - def render(text : String, color, background, ascii = false) - render_shaded(text, color, background, ascii) - end + # Renders text using this font as an `SDL::Surface` using the "slow and + # nice" shaded mode. + def render(text : String, color, background, ascii = false) + render_shaded(text, color, background, ascii) + end - # Renders text using this font as an `SDL::Surface` using the "quick and - # dirty" solid mode. - def render_solid(text : String, color, ascii = false) - if ascii - surface = LibTTF.render_text_solid(self, text, Color.from(color)) - raise Error.new("TTF_RenderText_Solid") unless surface - else - surface = LibTTF.render_utf8_solid(self, text, Color.from(color)) - raise Error.new("TTF_RenderUTF8_Solid") unless surface - end - SDL::Surface.new(surface) - end + # Renders text using this font as an `SDL::Surface` using the "quick and + # dirty" solid mode. + def render_solid(text : String, color, ascii = false) + if ascii + surface = LibTTF.render_text_solid(self, text, Color.from(color)) + raise Error.new("TTF_RenderText_Solid") unless surface + else + surface = LibTTF.render_utf8_solid(self, text, Color.from(color)) + raise Error.new("TTF_RenderUTF8_Solid") unless surface + end + SDL::Surface.new(surface) + end - # Renders text using this font as an `SDL::Surface` using the "slow and - # nice" shaded mode. - def render_shaded(text : String, color, background, ascii = false) - if ascii - surface = LibTTF.render_text_shaded(self, text, Color.from(color), Color.from(background)) - raise Error.new("TTF_RenderText_Shaded") unless surface - else - surface = LibTTF.render_utf8_shaded(self, text, Color.from(color), Color.from(background)) - raise Error.new("TTF_RenderUTF8_Shaded") unless surface - end - SDL::Surface.new(surface) - end + # Renders text using this font as an `SDL::Surface` using the "slow and + # nice" shaded mode. + def render_shaded(text : String, color, background, ascii = false) + if ascii + surface = LibTTF.render_text_shaded(self, text, Color.from(color), Color.from(background)) + raise Error.new("TTF_RenderText_Shaded") unless surface + else + surface = LibTTF.render_utf8_shaded(self, text, Color.from(color), Color.from(background)) + raise Error.new("TTF_RenderUTF8_Shaded") unless surface + end + SDL::Surface.new(surface) + end - # Renders text using this font as an `SDL::Surface` using the "slow, slow, - # slow but ultra nice over another image" blended mode. - def render_blended(text : String, color, ascii = false) - if ascii - surface = LibTTF.render_text_blended(self, text, Color.from(color)) - raise Error.new("TTF_RenderText_Blended") unless surface - else - surface = LibTTF.render_utf8_blended(self, text, Color.from(color)) - raise Error.new("TTF_RenderUTF8_Blended") unless surface - end - SDL::Surface.new(surface) - end + # Renders text using this font as an `SDL::Surface` using the "slow, slow, + # slow but ultra nice over another image" blended mode. + def render_blended(text : String, color, ascii = false) + if ascii + surface = LibTTF.render_text_blended(self, text, Color.from(color)) + raise Error.new("TTF_RenderText_Blended") unless surface + else + surface = LibTTF.render_utf8_blended(self, text, Color.from(color)) + raise Error.new("TTF_RenderUTF8_Blended") unless surface + end + SDL::Surface.new(surface) + end - # Renders text using this font as an `SDL::Surface` using the blended - # wrapped mode. - #def render_blended_wrapper(text : String, color, wrap_length, ascii = false) - # if ascii - # surface = LibTTF.render_text_blended_wrapped(self, text, Color.from(color), wrap_length) - # raise Error.new("TTF_RenderText_Blended_Wrapped") unless surface - # else - # surface = LibTTF.render_utf8_blended_wrapped(self, text, Color.from(color), wrap_length) - # raise Error.new("TTF_RenderUTF8_Blended_Wrapped") unless surface - # end - # SDL::Surface.new(surface) - #end - - def close - @closed = true - LibTTF.close_font(self) - @rwops.close - end + # Renders text using this font as an `SDL::Surface` using the blended + # wrapped mode. + #def render_blended_wrapper(text : String, color, wrap_length, ascii = false) + # if ascii + # surface = LibTTF.render_text_blended_wrapped(self, text, Color.from(color), wrap_length) + # raise Error.new("TTF_RenderText_Blended_Wrapped") unless surface + # else + # surface = LibTTF.render_utf8_blended_wrapped(self, text, Color.from(color), wrap_length) + # raise Error.new("TTF_RenderUTF8_Blended_Wrapped") unless surface + # end + # SDL::Surface.new(surface) + #end + + def close + @closed = true + LibTTF.close_font(self) + @rwops.close + end - def to_unsafe - @font + def to_unsafe + @font + end end end end