mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2025-02-05 20:45:51 +01:00
Update examples, extend SDL2 with streaming texture access
This commit is contained in:
parent
4a9c451743
commit
f9a9fdfecb
6 changed files with 174 additions and 24 deletions
|
@ -8,6 +8,7 @@ module PF
|
|||
@transform : Transform2d = Transform2d.new
|
||||
@angle = 0.0
|
||||
@size = 1.0
|
||||
@zoom = 0.5
|
||||
|
||||
def initialize(*args, **kwargs)
|
||||
super
|
||||
|
@ -17,6 +18,8 @@ module PF
|
|||
|
||||
def update(dt, event)
|
||||
@angle += 1.0 * dt
|
||||
@zoom, @size = -@zoom, @size.clamp(0.5..2.0) if @size > 2.0 || @size < 0.5
|
||||
@size = @size + @zoom * dt
|
||||
end
|
||||
|
||||
def draw
|
||||
|
|
|
@ -2,26 +2,14 @@ require "../src/game"
|
|||
require "../src/shape"
|
||||
require "../src/entity"
|
||||
require "../src/entity/circle_collision"
|
||||
require "../src/pixel_text"
|
||||
|
||||
module PF
|
||||
class Sprite
|
||||
# Redefine draw_point to wrap the coordinates
|
||||
def draw_point(x : Int32, y : Int32, color : UInt32)
|
||||
x = x % width
|
||||
y = y % height
|
||||
|
||||
x = width + x if x < 0
|
||||
y = height + y if y < 0
|
||||
|
||||
# super(x, y, color) # Undefined method super for Object??
|
||||
pixel_pointer(x, y).value = color
|
||||
end
|
||||
end
|
||||
|
||||
class Ball < Entity
|
||||
include CircleCollision
|
||||
|
||||
getter frame : Array(Vector(Float64, 2))
|
||||
getter color = Pixel.random
|
||||
|
||||
def initialize(size : Float64)
|
||||
@frame = Shape.circle(size.to_i32, size.to_i32)
|
||||
|
@ -31,28 +19,43 @@ module PF
|
|||
end
|
||||
|
||||
class Balls < Game
|
||||
ADD_BALL = 2.0
|
||||
@balls : Array(Ball) = [] of Ball
|
||||
@ball_clock = ADD_BALL
|
||||
@text = PF::PixelText.new("assets/pf-font.png")
|
||||
|
||||
def initialize(*args, **kwargs)
|
||||
super
|
||||
@text.color(PF::Pixel.new(255, 255, 255))
|
||||
add_ball
|
||||
end
|
||||
|
||||
15.times do
|
||||
position = Vector(Float64, 2).new(rand(0.0_f64..@width.to_f64), rand(0.0_f64..@height.to_f64))
|
||||
ball = Ball.new(rand(10.0..30.0))
|
||||
ball.position = position
|
||||
ball.velocity = Vector(Float64, 2).new(rand(-50.0..50.0), rand(-50.0..50.0))
|
||||
|
||||
@balls << ball
|
||||
end
|
||||
def add_ball
|
||||
position = Vector(Float64, 2).new(rand(0.0_f64..@width.to_f64), rand(0.0_f64..@height.to_f64))
|
||||
ball = Ball.new(rand(10.0..30.0))
|
||||
ball.position = position
|
||||
ball.velocity = Vector(Float64, 2).new(rand(-50.0..50.0), rand(-50.0..50.0))
|
||||
@balls << ball
|
||||
end
|
||||
|
||||
def update(dt, event)
|
||||
@ball_clock -= dt
|
||||
if @ball_clock < 0
|
||||
@ball_clock = ADD_BALL
|
||||
add_ball
|
||||
end
|
||||
|
||||
@balls.each do |b|
|
||||
b.update(dt)
|
||||
b.position = b.position % viewport # wrap coords
|
||||
|
||||
b.position.x = width + b.radius if b.position.x < -b.radius
|
||||
b.position.y = height + b.radius if b.position.y < -b.radius
|
||||
b.position.x = -b.radius if b.position.x > width + b.radius
|
||||
b.position.y = -b.radius if b.position.y > height + b.radius
|
||||
end
|
||||
|
||||
collission_pairs = [] of Tuple(Ball, Ball)
|
||||
|
||||
@balls.each do |a|
|
||||
@balls.each do |b|
|
||||
next if a == b
|
||||
|
@ -69,9 +72,10 @@ module PF
|
|||
def draw
|
||||
clear(10, 10, 30)
|
||||
@balls.each do |ball|
|
||||
fill_shape(Shape.translate(ball.frame, translation: ball.position).map(&.to_i32))
|
||||
fill_shape(Shape.translate(ball.frame, translation: ball.position).map(&.to_i32), ball.color)
|
||||
# draw_circle(ball.position.to_i32, ball.radius.to_i32, Pixel.green)
|
||||
end
|
||||
@text.draw_to(screen, "#{@balls.size}", 5, 5)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
25
examples/static.cr
Normal file
25
examples/static.cr
Normal file
|
@ -0,0 +1,25 @@
|
|||
require "../src/game"
|
||||
|
||||
module PF
|
||||
class Static < Game
|
||||
@buffer_size : Int32
|
||||
@buffer : Pointer(UInt32)
|
||||
|
||||
def initialize(*args, **kwargs)
|
||||
super
|
||||
@buffer_size = width * height
|
||||
@buffer = screen.pixel_pointer(0, 0)
|
||||
end
|
||||
|
||||
def update(dt, event)
|
||||
end
|
||||
|
||||
def draw
|
||||
0.upto(@buffer_size) do |n|
|
||||
(@buffer + n).value = PF::Pixel.random.to_u32
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
PF::Static.new(400, 300, 3).run!
|
59
spec/streaming_texture.cr
Normal file
59
spec/streaming_texture.cr
Normal file
|
@ -0,0 +1,59 @@
|
|||
require "../src/lib_sdl"
|
||||
require "../src/pixel"
|
||||
|
||||
FPS_INTERVAL = 1.0
|
||||
width = 400
|
||||
height = 300
|
||||
scale = 1
|
||||
|
||||
fps_lasttime : Float64 = Time.monotonic.total_milliseconds # the last recorded time.
|
||||
fps_current : UInt32 = 0 # the current FPS.
|
||||
fps_frames : UInt32 = 0 # frames passed since the last recorded fps.
|
||||
last_time : Float64 = Time.monotonic.total_milliseconds
|
||||
|
||||
begin
|
||||
SDL.init(SDL::Init::VIDEO)
|
||||
|
||||
window = SDL::Window.new("test", width * scale, height * scale, flags: SDL::Window::Flags::SHOWN)
|
||||
renderer = SDL::Renderer.new(window, flags: SDL::Renderer::Flags::ACCELERATED)
|
||||
raw_texture = LibSDL.create_texture(renderer, LibSDL::PixelFormatEnum::RGBA8888, LibSDL::TextureAccess::STREAMING, width, height)
|
||||
texture = SDL::Texture.new(raw_texture)
|
||||
|
||||
fps_frames = 0
|
||||
|
||||
loop do
|
||||
case event = SDL::Event.poll
|
||||
when SDL::Event::Quit
|
||||
break
|
||||
end
|
||||
|
||||
et = Time.monotonic.total_milliseconds
|
||||
|
||||
fps_frames += 1
|
||||
if fps_lasttime < et - FPS_INTERVAL * 1000
|
||||
fps_lasttime = et
|
||||
fps_current = fps_frames
|
||||
fps_frames = 0
|
||||
puts String.build { |io| io << fps_current << " fps" }
|
||||
end
|
||||
|
||||
last_time = et
|
||||
|
||||
pitch = uninitialized Int32
|
||||
pixels_pointer = uninitialized Void*
|
||||
|
||||
LibSDL.lock_texture(texture, nil, pointerof(pixels_pointer), pointerof(pitch))
|
||||
pixels = Slice.new(Pointer(UInt32).new(pixels_pointer.address), width * height)
|
||||
|
||||
0.upto(pixels.size - 1) do |n|
|
||||
pixels[n] = PF::Pixel.random.to_u32
|
||||
end
|
||||
|
||||
LibSDL.unlock_texture(texture)
|
||||
|
||||
renderer.copy(texture)
|
||||
renderer.present
|
||||
end
|
||||
ensure
|
||||
SDL.quit
|
||||
end
|
|
@ -1,5 +1,58 @@
|
|||
require "sdl"
|
||||
|
||||
@[Link("SDL2")]
|
||||
lib LibSDL
|
||||
# These macros don't work inside of the PixelFormatEnum
|
||||
|
||||
# macro define_pixelfourcc(a, b, c, d)
|
||||
# {{a}}.ord.to_u32 << 0 |
|
||||
# {{b}}.ord.to_u32 << 8 |
|
||||
# {{c}}.ord.to_u32 << 16 |
|
||||
# {{d}}.ord.to_u32 << 24
|
||||
# end
|
||||
|
||||
# macro define_pixelformat(type, order, layout, bits, bytes)
|
||||
# 1 << 28 | {{type}} << 24 | {{order}} << 20 | {{layout}} << 16 | {{bits}} << 8 | {{bytes}} << 0
|
||||
# end
|
||||
|
||||
enum PixelFormatEnum : UInt32
|
||||
UNKNOWN
|
||||
INDEX1LSB = 1 << 28 | PixelType::INDEX1 << 24 | BitmapOrder::SDL_BITMAPORDER_4321 << 20 | 0 << 16 | 1 << 8 | 0
|
||||
INDEX1MSB = 1 << 28 | PixelType::INDEX1 << 24 | BitmapOrder::SDL_BITMAPORDER_1234 << 20 | 0 << 16 | 1 << 8 | 0
|
||||
INDEX4LSB = 1 << 28 | PixelType::INDEX4 << 24 | BitmapOrder::SDL_BITMAPORDER_4321 << 20 | 0 << 16 | 4 << 8 | 0
|
||||
INDEX4MSB = 1 << 28 | PixelType::INDEX4 << 24 | BitmapOrder::SDL_BITMAPORDER_1234 << 20 | 0 << 16 | 4 << 8 | 0
|
||||
INDEX8 = 1 << 28 | PixelType::INDEX8 << 24 | 0 << 20 | 0 << 16 | 8 << 8 | 1
|
||||
RGB332 = 1 << 28 | PixelType::PACKED8 << 24 | PackedOrder::XRGB << 20 | PackedLayout::L332 << 16 | 8 << 8 | 1
|
||||
RGB444 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::XRGB << 20 | PackedLayout::L4444 << 16 | 12 << 8 | 2
|
||||
RGB555 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::XRGB << 20 | PackedLayout::L1555 << 16 | 15 << 8 | 2
|
||||
BGR555 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::XBGR << 20 | PackedLayout::L1555 << 16 | 15 << 8 | 2
|
||||
ARGB4444 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::ARGB << 20 | PackedLayout::L4444 << 16 | 16 << 8 | 2
|
||||
RGBA4444 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::RGBA << 20 | PackedLayout::L4444 << 16 | 16 << 8 | 2
|
||||
ABGR4444 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::ABGR << 20 | PackedLayout::L4444 << 16 | 16 << 8 | 2
|
||||
BGRA4444 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::BGRA << 20 | PackedLayout::L4444 << 16 | 16 << 8 | 2
|
||||
ARGB1555 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::ARGB << 20 | PackedLayout::L1555 << 16 | 16 << 8 | 2
|
||||
RGBA5551 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::RGBA << 20 | PackedLayout::L5551 << 16 | 16 << 8 | 2
|
||||
ABGR1555 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::ABGR << 20 | PackedLayout::L1555 << 16 | 16 << 8 | 2
|
||||
BGRA5551 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::BGRA << 20 | PackedLayout::L5551 << 16 | 16 << 8 | 2
|
||||
RGB565 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::XRGB << 20 | PackedLayout::L565 << 16 | 16 << 8 | 2
|
||||
BGR565 = 1 << 28 | PixelType::PACKED16 << 24 | PackedOrder::XBGR << 20 | PackedLayout::L565 << 16 | 16 << 8 | 2
|
||||
RGB888 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::XRGB << 20 | PackedLayout::L8888 << 16 | 24 << 8 | 4
|
||||
RGBX8888 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::RGBX << 20 | PackedLayout::L8888 << 16 | 24 << 8 | 4
|
||||
BGR888 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::XBGR << 20 | PackedLayout::L8888 << 16 | 24 << 8 | 4
|
||||
BGRX8888 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::BGRX << 20 | PackedLayout::L8888 << 16 | 24 << 8 | 4
|
||||
ARGB8888 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::ARGB << 20 | PackedLayout::L8888 << 16 | 32 << 8 | 4
|
||||
RGBA8888 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::RGBA << 20 | PackedLayout::L8888 << 16 | 32 << 8 | 4
|
||||
ABGR8888 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::ABGR << 20 | PackedLayout::L8888 << 16 | 32 << 8 | 4
|
||||
BGRA8888 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::BGRA << 20 | PackedLayout::L8888 << 16 | 32 << 8 | 4
|
||||
ARGB2101010 = 1 << 28 | PixelType::PACKED32 << 24 | PackedOrder::ARGB << 20 | PackedLayout::L2101010 << 16 | 32 << 8 | 4
|
||||
YV12 = 842094169
|
||||
IYUV = 1448433993
|
||||
YUY2 = 844715353
|
||||
UYVY = 1498831189
|
||||
YVYU = 1431918169
|
||||
end
|
||||
end
|
||||
|
||||
module SDL
|
||||
class Surface
|
||||
def pixels
|
||||
|
|
|
@ -17,8 +17,14 @@ module PF
|
|||
end
|
||||
end
|
||||
|
||||
# ditto
|
||||
def draw_rect(p1 : PF::Vector(Int, 2), p2 : PF::Vector(Int, 2), pixel : Pixel = Pixel.new)
|
||||
draw_rect(p1.x, p1.y, p2.x, p2.y, pixel)
|
||||
end
|
||||
|
||||
# ditto
|
||||
def draw_rect(size : PF::Vector(Int, 2), pixel : Pixel = Pixel.new)
|
||||
draw_rect(0, 0, size.x, size.y, pixel)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue