forked from ysbaddaden/sdl.cr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework: @[Extern] Color and Rect structs
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
1 parent
50beac9
commit 99e071c
Showing
19 changed files
with
123 additions
and
158 deletions.
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
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
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
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -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 |
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
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
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 |
---|---|---|
@@ -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 |
Oops, something went wrong.