Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions samples/04_keyboard.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "../sdl"
require "../src/sdl"

SDL.init(SDL::Init::VIDEO)
at_exit { SDL.quit }
Expand All @@ -7,10 +7,10 @@ window = SDL::Window.new("SDL tutorial", 640, 480)

surfaces = {
default: SDL.load_bmp(File.join(__DIR__, "data", "press.bmp")),
up: SDL.load_bmp(File.join(__DIR__, "data", "up.bmp")),
down: SDL.load_bmp(File.join(__DIR__, "data", "down.bmp")),
left: SDL.load_bmp(File.join(__DIR__, "data", "left.bmp")),
right: SDL.load_bmp(File.join(__DIR__, "data", "right.bmp")),
up: SDL.load_bmp(File.join(__DIR__, "data", "up.bmp")),
down: SDL.load_bmp(File.join(__DIR__, "data", "down.bmp")),
left: SDL.load_bmp(File.join(__DIR__, "data", "left.bmp")),
right: SDL.load_bmp(File.join(__DIR__, "data", "right.bmp")),
}

bmp = surfaces[:default]
Expand Down
4 changes: 2 additions & 2 deletions samples/05_stretching.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "../sdl"
require "../src/sdl"

def load_bmp(name, window)
path = File.join(__DIR__, "data", name)
Expand All @@ -21,6 +21,6 @@ loop do
end
end

bmp.blit_scaled(window.surface, dstrect: SDL::Rect[20, 20, 600, 440])
bmp.blit_scaled(window.surface, dstrect: SDL::Rect[0, 0, window.width, window.height])
window.update
end
10 changes: 5 additions & 5 deletions samples/08_geometry.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "../sdl"
require "../src/sdl"

SDL.init(SDL::Init::VIDEO)
at_exit { SDL.quit }
Expand All @@ -20,20 +20,20 @@ loop do

# centered red rectangle
renderer.draw_color = SDL::Color[255, 0, 0, 255]
renderer.fill_rect(width / 4, height / 4, width / 2, height / 2)
renderer.fill_rect(width // 4, height // 4, width // 2, height // 2)

# outlined green rectangle
renderer.draw_color = SDL::Color[0, 255, 0, 255]
renderer.draw_rect(width / 6, height / 6, width * 2 / 3, height * 2 / 3)
renderer.draw_rect(width // 6, height // 6, width * 2 // 3, height * 2 // 3)

# blue horizontal line
renderer.draw_color = SDL::Color[0, 0, 255, 255]
renderer.draw_line(0, height / 2, width, height / 2)
renderer.draw_line(0, height // 2, width, height // 2)

# vertical line of yellow dots
renderer.draw_color = SDL::Color[255, 255, 0, 255]
0.step(by: 4, to: height) do |i|
renderer.draw_point(width / 2, i)
renderer.draw_point(width // 2, i)
end

renderer.present
Expand Down
5 changes: 4 additions & 1 deletion samples/09_viewport.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ require "../src/image"
SDL.init(SDL::Init::VIDEO)
at_exit { SDL.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)

Expand All @@ -17,7 +20,7 @@ loop do
end

# clear sreen in white
renderer.draw_color = SDL::Color[255, 255, 0, 255]
renderer.draw_color = SDL::Color[255, 255, 255, 255]
renderer.clear

# top left
Expand Down
3 changes: 2 additions & 1 deletion samples/13_alpha_blending.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ renderer = SDL::Renderer.new(window)

background = SDL::IMG.load(File.join(__DIR__, "data", "fadein.png"), renderer)
modulated = SDL::IMG.load(File.join(__DIR__, "data", "fadeout.png"), renderer)
modulated.blend_mode = SDL::BlendMode::BLEND
a = 255

loop do
case event = SDL::Event.wait
case event = SDL::Event.poll
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you want to wait for an event, otherwise we keep redrawing the same image in a tight loop. We'd loop on poll if there was some animation running with FPS limitation. No need to burn the CPU.

when SDL::Event::Quit
break
when SDL::Event::Keyboard
Expand Down
14 changes: 8 additions & 6 deletions samples/14_animated_sprites_and_vsync.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ 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 = SDL::IMG.load(File.join(__DIR__, "data", "foo_sprite.png"), renderer)
sprite = SDL::IMG.load(File.join(__DIR__, "data", "foo_sprite.png"))
sprite.color_key = {0, 255, 255}
sprite_texture = SDL::Texture.from(sprite, renderer)
sprite_clips = StaticArray(SDL::Rect, 4).new do |i|
SDL::Rect.new(i * 64, 0, 64, 305)
SDL::Rect.new(i * 64, 0, 64, 205)
end

frame = 0
Expand All @@ -25,10 +27,10 @@ loop do
renderer.draw_color = SDL::Color[255, 255, 255, 255]
renderer.clear

current_clip = sprite_clips[frame / slowdown]
x = (window.width - current_clip.w) / 2
y = (window.height - current_clip.h) / 2
renderer.copy(sprite, current_clip, SDL::Rect[x, y, current_clip.w, current_clip.h])
current_clip = sprite_clips[frame // slowdown]
x = (window.width - current_clip.w) // 2
y = (window.height - current_clip.h) // 2
renderer.copy(sprite_texture, current_clip, SDL::Rect[x, y, current_clip.w, current_clip.h])

renderer.present

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 @@ -28,8 +28,8 @@ loop do
renderer.draw_color = SDL::Color[255, 255, 255, 255]
renderer.clear

x = (window.width - arrow.width) / 2
y = (window.height - arrow.height) / 2
x = (window.width - arrow.width) // 2
y = (window.height - arrow.height) // 2
renderer.copy(arrow, dstrect: SDL::Rect[x, y, arrow.width, arrow.height], angle: degrees, flip: flip)

renderer.present
Expand Down
4 changes: 2 additions & 2 deletions samples/16_ttf.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ loop do
color = SDL::Color[255, 0, 0, 255]
surface = font.render_shaded("The quick brow fox jumps over the lazy dog.", color, renderer.draw_color)

x = (window.width - surface.width) / 2
y = (window.height - surface.height) / 2
x = (window.width - surface.width) // 2
y = (window.height - surface.height) // 2
renderer.copy(surface, dstrect: SDL::Rect[x, y, surface.width, surface.height])

renderer.present
Expand Down