Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more samples: alpha bending, animated sprites, rotatation & flipping
- Loading branch information
1 parent
1fa9bfd
commit c2f6b80
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters