Skip to content

Commit

Permalink
Fix SDL examples crashes (#10470)
Browse files Browse the repository at this point in the history
  • Loading branch information
megatux authored Mar 7, 2021
1 parent b018f28 commit a94a823
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion samples/sdl/fire.cr
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class Screen
end

def put_pixel(point, color)
color = color.to_u32
color = color.to_u32!
offset = @surface.offset(point.x, point.y)
@surface[offset] = color

Expand Down
2 changes: 1 addition & 1 deletion samples/sdl/sdl/surface.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SDL::Surface
end

def []=(offset, color)
(@surface.value.pixels.as(UInt32*))[offset] = color.to_u32
(@surface.value.pixels.as(UInt32*))[offset] = color.to_u32!
end

def []=(x, y, color)
Expand Down
46 changes: 25 additions & 21 deletions samples/sdl/tv.cr
Original file line number Diff line number Diff line change
Expand Up @@ -130,34 +130,38 @@ color_maker = ColorMaker.new(delay)
rects = parse_rectangles
puts "Rects: #{rects.size}"

while true
SDL.poll_events do |event|
if event.type == LibSDL::QUIT || event.type == LibSDL::KEYDOWN
ms = SDL.ticks - start
puts "#{frames} frames in #{ms} ms"
puts "Average FPS: #{frames / (ms * 0.001)}"
SDL.quit
exit
begin
while true
SDL.poll_events do |event|
if event.type == LibSDL::QUIT || event.type == LibSDL::KEYDOWN
ms = SDL.ticks - start
puts "#{frames} frames in #{ms} ms"
puts "Average FPS: #{frames / (ms * 0.001)}"
SDL.quit
exit
end
end
end

surface.lock
surface.lock

(height // 10).times do |h|
(width // 10).times do |w|
rect = rects.find { |rect| rect.contains?(w, h) }
10.times do |y|
10.times do |x|
surface[x + 10 * w, y + 10 * h] = rect ? (rect.light? ? color_maker.light_color : color_maker.dark_color) : color_maker.black_color
(height // 10).times do |h|
(width // 10).times do |w|
rect = rects.find { |rect| rect.contains?(w, h) }
10.times do |y|
10.times do |x|
surface[x + 10 * w, y + 10 * h] = rect ? (rect.light? ? color_maker.light_color : color_maker.dark_color) : color_maker.black_color
end
end
end
end
end

color_maker.next
color_maker.next

surface.unlock
surface.flip
surface.unlock
surface.flip

frames += 1
frames += 1
end
ensure
SDL.quit
end

0 comments on commit a94a823

Please sign in to comment.