Skip to content

Commit

Permalink
Move IMG and TTF to SDL namespace (ysbaddaden#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink authored and ysbaddaden committed Mar 8, 2017
1 parent ce64727 commit b04dfac
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 308 deletions.
6 changes: 3 additions & 3 deletions samples/06_image.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions samples/07_texture.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion samples/09_viewport.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions samples/10_colorkey.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions samples/11_sprites.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions samples/12_color_modulation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions samples/13_alpha_blending.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions samples/14_animated_sprites_and_vsync.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions samples/15_rotation_and_flipping.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions samples/16_ttf.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
178 changes: 90 additions & 88 deletions src/image.cr
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit b04dfac

Please sign in to comment.