Skip to content

Commit

Permalink
Rework: @[Extern] Color and Rect structs
Browse files Browse the repository at this point in the history
In order to avoid copying values and HEAP allocations, the C structs
are now Crystal structs with the `@[Extern]` decorator, so they're
identical/exchangeable with the SDL structs.
  • Loading branch information
ysbaddaden committed Jun 9, 2017
1 parent 50beac9 commit 99e071c
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 158 deletions.
2 changes: 1 addition & 1 deletion samples/05_stretching.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ loop do
end
end

bmp.blit_scaled(window.surface, dstrect: SDL::Rect.new(20, 20, 600, 440))
bmp.blit_scaled(window.surface, dstrect: SDL::Rect[20, 20, 600, 440])
window.update
end
4 changes: 2 additions & 2 deletions samples/07_texture.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ loop do
break
end

renderer.draw_color = {255, 0, 0, 255}
renderer.draw_color = SDL::Color[255, 0, 0, 255]
renderer.clear

renderer.copy(texture, dstrect: {20, 20, 600, 440})
renderer.copy(texture, dstrect: SDL::Rect[20, 20, 600, 440])

renderer.present
end
12 changes: 6 additions & 6 deletions samples/08_geometry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ loop do
break
end

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

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

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

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

# vertical line of yellow dots
renderer.draw_color = {255, 255, 0, 255}
renderer.draw_color = SDL::Color[255, 255, 0, 255]
0.step(by: 4, to: height) do |i|
renderer.draw_point(width / 2, i)
end
Expand Down
8 changes: 4 additions & 4 deletions samples/09_viewport.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ loop do
end

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

# top left
renderer.viewport = {20, 20, 290, 210}
renderer.viewport = SDL::Rect[20, 20, 290, 210]
renderer.copy(png)

# top left
renderer.viewport = {330, 20, 290, 210}
renderer.viewport = SDL::Rect[330, 20, 290, 210]
renderer.copy(png)

# bottom
renderer.viewport = {20, 250, 600, 210}
renderer.viewport = SDL::Rect[20, 250, 600, 210]
renderer.copy(png)

renderer.present
Expand Down
4 changes: 2 additions & 2 deletions samples/10_colorkey.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ loop do
end

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

renderer.copy(background)
renderer.copy(foo, dstrect: {240, 190, foo.width, foo.height})
renderer.copy(foo, dstrect: SDL::Rect[240, 190, foo.width, foo.height])

renderer.present
end
10 changes: 5 additions & 5 deletions samples/11_sprites.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ loop do
break
end

renderer.draw_color = {255, 255, 255, 255}
renderer.draw_color = SDL::Color[255, 255, 255, 255]
renderer.clear

renderer.copy(sprite, {0, 0, 100, 100}, {0, 0, 100, 100})
renderer.copy(sprite, {100, 0, 100, 100}, {width - 100, 0, 100, 100})
renderer.copy(sprite, {0, 100, 100, 100}, {0, height - 100, 100, 100})
renderer.copy(sprite, {100, 100, 100, 100}, {width - 100, height - 100, 100, 100})
renderer.copy(sprite, SDL::Rect[0, 0, 100, 100], SDL::Rect[0, 0, 100, 100])
renderer.copy(sprite, SDL::Rect[100, 0, 100, 100], SDL::Rect[width - 100, 0, 100, 100])
renderer.copy(sprite, SDL::Rect[0, 100, 100, 100], SDL::Rect[0, height - 100, 100, 100])
renderer.copy(sprite, SDL::Rect[100, 100, 100, 100], SDL::Rect[width - 100, height - 100, 100, 100])

renderer.present
end
2 changes: 1 addition & 1 deletion samples/12_color_modulation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ loop do
g = g.clamp(0, 255)
b = b.clamp(0, 255)

renderer.draw_color = {255, 255, 255, 255}
renderer.draw_color = SDL::Color[255, 255, 255, 255]
renderer.clear

image.color_mod = {r, g, b}
Expand Down
2 changes: 1 addition & 1 deletion samples/13_alpha_blending.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ loop do

a = a.clamp(0, 255)

renderer.draw_color = {255, 255, 255, 255}
renderer.draw_color = SDL::Color[255, 255, 255, 255]
renderer.clear

renderer.copy(background)
Expand Down
4 changes: 2 additions & 2 deletions samples/14_animated_sprites_and_vsync.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ loop do
break
end

renderer.draw_color = {255, 255, 255, 255}
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, {x, y, current_clip.w, current_clip.h})
renderer.copy(sprite, 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 @@ -25,12 +25,12 @@ loop do
end if event.keydown?
end

renderer.draw_color = {255, 255, 255, 255}
renderer.draw_color = SDL::Color[255, 255, 255, 255]
renderer.clear

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

renderer.present
end
6 changes: 3 additions & 3 deletions samples/16_ttf.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ loop do
break
end

renderer.draw_color = {255, 255, 255, 255}
renderer.draw_color = SDL::Color[255, 255, 255, 255]
renderer.clear

color = {0, 0, 0, 255}
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
renderer.copy(surface, dstrect: {x, y, surface.width, surface.height})
renderer.copy(surface, dstrect: SDL::Rect[x, y, surface.width, surface.height])

renderer.present
end
48 changes: 28 additions & 20 deletions src/color.cr
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
module SDL
@[Extern]
struct Color
property r : Int32
property g : Int32
property b : Int32
property a : Int32
property r : UInt8
property g : UInt8
property b : UInt8
property a : UInt8

def initialize(@r, @g, @b, @a = 255)
# Creates a RGB(A) `Color`. For example `Color[0, 0, 0]` is opaque black, whereas
# `Color[255, 0, 0, 128]` is half transparent red.
macro [](r, g, b, a = 255)
SDL::Color.new({{r}}, {{g}}, {{b}}, {{a}})
end

def self.from(color : LibSDL::Color)
new(color.r, color.g, color.b, color.a)
# Creates a `Color` of a single intensity. For example `Color[255]` is white and
# equivalent to `Color[255, 255, 255]`.
macro [](rgb, a = 255)
SDL::Color.new({{rgb}}, {{rgb}}, {{rgb}}, {{a}})
end

def self.from(color : Tuple)
new(*color)
def initialize(r, g, b, a = 255_u8)
@r = r.to_u8
@g = g.to_u8
@b = b.to_u8
@a = a.to_u8
end

def self.from(color : NamedTuple)
new(color[:r], color[:g], color[:b], color[:a]? || 255)
def r
@r.to_i32
end

def self.from(color : Nil)
nil
def g
@g.to_i32
end

def to_unsafe
color = uninitialized LibSDL::Color
color.r = r
color.g = g
color.b = b
color.a = a
color
def b
@b.to_i32
end

def a
@a.to_i32
end
end
end
7 changes: 1 addition & 6 deletions src/lib_sdl/pixels.cr
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,7 @@ lib LibSDL
# YVYU = SDL_DEFINE_PIXELFOURCC('Y', 'V', 'Y', 'U')
#end

struct Color
r : UInt8
g : UInt8
b : UInt8
a : UInt8
end
alias Color = SDL::Color

struct Palette
ncolors : Int
Expand Down
13 changes: 2 additions & 11 deletions src/lib_sdl/rect.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
require "./pixels"

lib LibSDL
struct Point
x : Int
y : Int
end

struct Rect
x : Int
y : Int
w : Int
h : Int
end
alias Point = SDL::Point
alias Rect = SDL::Rect

fun has_intersection = SDL_HasIntersection(a : Rect*, b : Rect*) : Bool
fun intersect_rect = SDL_IntersectRect(a : Rect*, b : Rect*, result : Rect*) : Bool
Expand Down
72 changes: 14 additions & 58 deletions src/rect.cr
Original file line number Diff line number Diff line change
@@ -1,73 +1,29 @@
module SDL
@[Extern]
struct Point
property x, y
property x : Int32
property y : Int32

def initialize(@x : Int32, @y : Int32)
macro [](x, y)
SDL::Point.new({{x}}, {{y}})
end

def self.from(pt : Point)
pt
end

def self.from(pt : LibSDL::Point*)
Point.new(pt.value.x, pt.value.y)
end

def self.from(pt : Tuple)
Point.new(*pt)
end

def self.from(pt : NamedTuple)
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*)
pt.value.x = x
pt.value.y = y
pt
def initialize(@x, @y)
end
end

@[Extern]
struct Rect
property x, y, w, h

def initialize(@x : Int32, @y : Int32, @w : Int32, @h : Int32)
end

def self.from(rect : Rect)
rect
end

def self.from(rect : LibSDL::Rect*)
new(rect.value.x, rect.value.y, rect.value.w, rect.value.h)
end

def self.from(rect : Tuple)
new(*rect)
end

def self.from(rect : NamedTuple)
new(rect.x, rect.y, rect.w, rect.h)
end
property x : Int32
property y : Int32
property w : Int32
property h : Int32

def self.from(rect : Nil)
nil
macro [](x, y, w, h)
SDL::Rect.new({{x}}, {{y}}, {{w}}, {{h}})
end

# OPTIMIZE: avoid copy
def to_unsafe
rect = Pointer(LibSDL::Rect).malloc
rect.value.x = x
rect.value.y = y
rect.value.w = w
rect.value.h = h
rect
def initialize(@x, @y, @w, @h)
end
end
end
Loading

0 comments on commit 99e071c

Please sign in to comment.