From af02f4a8681e00b969ddc5477d6e7bcfe9193e58 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 18 Sep 2015 17:33:50 +0200 Subject: [PATCH] Add the ObjRef wrapper. --- CMakeLists.txt | 1 + src/collisionline.d | 11 ++++++++--- src/objref.d | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/objref.d diff --git a/CMakeLists.txt b/CMakeLists.txt index 357e7b2..1af5676 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ add_executable(${PROJECT_NAME} src/collisionline.d src/rect.d src/collidingentity.d + src/objref.d lib/DerelictSDL2/source/derelict/sdl2/functions.d lib/DerelictSDL2/source/derelict/sdl2/image.d diff --git a/src/collisionline.d b/src/collisionline.d index 8c46911..3954073 100644 --- a/src/collisionline.d +++ b/src/collisionline.d @@ -18,11 +18,12 @@ module collisionline; public import vector : Vector; import rect : Rect; +import objref; alias vec2 = Vector!(float, 2); struct CollisionLine { - this (in vec2 parA, in vec2 parB) { + this (vec2 parA, vec2 parB) { m_from = parA; m_to = parB; assert(m_from != m_to); @@ -55,11 +56,15 @@ struct CollisionQuad { alias m_lines this; } -bool collides (CollisionLine parLine, CollisionLine parLine1, vec2 parOffs, out vec2 parPoint) { +alias CollisionQuadRef = TObjRef!(const(CollisionQuad)).ObjRef; +alias CollisionLineRef = TObjRef!(const(CollisionLine)).ObjRef; +alias vec2ref = TObjRef!(const(vec2)).ObjRef; + +bool collides (CollisionLineRef parLine, CollisionLineRef parLine1, vec2ref parOffs, out vec2 parPoint) { parPoint = vec2(0.0f); return false; } -bool collides (CollisionQuad parQuad, CollisionQuad parQuad1, vec2 parOffs, out vec2 parPoint) { +bool collides (CollisionQuadRef parQuad, CollisionQuadRef parQuad1, vec2ref parOffs, out vec2 parPoint) { return false; } diff --git a/src/objref.d b/src/objref.d new file mode 100644 index 0000000..7e86dd8 --- /dev/null +++ b/src/objref.d @@ -0,0 +1,36 @@ +/* Copyright 2015, Michele Santullo + * This file is part of "Jumping in D". + * + * "Jumping in D" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "Jumping in D" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "Jumping in D". If not, see . + */ + +module objref; + +template TObjRef(T) { + static if (T.sizeof > size_t.sizeof) { + struct ObjRef { + this(ref T parMainObj) { + m_ref = &parMainObj; + } + + ref inout(T) get_ref() inout { return *m_ref; } + alias get_ref this; + + private T* m_ref; + } + } + else { + alias ObjRef = T; + } +}