Skip to content

Commit

Permalink
more samples: alpha bending, animated sprites, rotatation & flipping
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Dec 26, 2016
1 parent 1fa9bfd commit c2f6b80
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
36 changes: 36 additions & 0 deletions samples/13_alpha_blending.cr
@@ -0,0 +1,36 @@
require "../src/sdl"
require "../src/image"

SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit }
IMG.init(IMG::Init::PNG); at_exit { 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)
a = 255

loop do
case event = SDL::Event.wait
when SDL::Event::Quit
break
when SDL::Event::Keyboard
case event.sym
when .w? then a += 32
when .s? then a -= 32
end if event.keydown?
end

a = a.clamp(0, 255)

renderer.draw_color = {255, 255, 255, 255}
renderer.clear

renderer.copy(background)

modulated.alpha_mod = a
renderer.copy(modulated)

renderer.present
end
35 changes: 35 additions & 0 deletions samples/14_animated_sprites_and_vsync.cr
@@ -0,0 +1,35 @@
require "../src/sdl"
require "../src/image"

SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit }
IMG.init(IMG::Init::PNG); at_exit { IMG.quit }

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_clips = StaticArray(SDL::Rect, 4).new do |i|
SDL::Rect.new(i * 64, 0, 64, 305)
end

frame = 0
slowdown = 6

loop do
case event = SDL::Event.poll
when SDL::Event::Quit
break
end

renderer.draw_color = {255, 255, 255, 255}
renderer.clear

current_clip = sprite_clips[frame / slowdown]
x = (window.surface.width - current_clip.w) / 2
y = (window.surface.height - current_clip.h) / 2
renderer.copy(sprite, current_clip, {x, y, current_clip.w, current_clip.h})

renderer.present

frame = (frame + 1) % (sprite_clips.size * slowdown)
end
36 changes: 36 additions & 0 deletions samples/15_rotation_and_flipping.cr
@@ -0,0 +1,36 @@
require "../src/sdl"
require "../src/image"

SDL.init(SDL::Init::VIDEO); at_exit { SDL.quit }
IMG.init(IMG::Init::PNG); at_exit { 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)
degrees = 0
flip = SDL::Renderer::Flip::NONE

loop do
case event = SDL::Event.wait
when SDL::Event::Quit
break
when SDL::Event::Keyboard
case event.sym
when .a? then degrees -= 60
when .d? then degrees += 60
when .q? then flip = SDL::Renderer::Flip::HORIZONTAL
when .w? then flip = SDL::Renderer::Flip::NONE
when .e? then flip = SDL::Renderer::Flip::VERTICAL
end if event.keydown?
end

renderer.draw_color = {255, 255, 255, 255}
renderer.clear

x = (window.surface.width - arrow.width) / 2
y = (window.surface.height - arrow.height) / 2
renderer.copy(arrow, dstrect: {x, y, arrow.width, arrow.height}, angle: degrees, flip: flip)

renderer.present
end
6 changes: 5 additions & 1 deletion src/rect.cr
Expand Up @@ -21,6 +21,10 @@ module SDL
Point.new(pt.x, pt.y)
end

def self.from(pt : Nil)
nil
end

# OPTIMIZE: avoid copy
def to_unsafe
pt = GC.malloc(sizeof(LibSDL::Point)).as(LibSDL::Point*)
Expand Down Expand Up @@ -58,7 +62,7 @@ module SDL

# OPTIMIZE: avoid copy
def to_unsafe
rect = GC.malloc(sizeof(LibSDL::Rect)).as(LibSDL::Rect*)
rect = Pointer(LibSDL::Rect).malloc
rect.value.x = x
rect.value.y = y
rect.value.w = w
Expand Down

0 comments on commit c2f6b80

Please sign in to comment.