Permalink
Browse files

Update Transformable and added VertexArray

Transformable: renamed setCenter to setRotationCenter
Transformable: added setOrigin
Transformable: scaling is now a Vector2f, to add the possibility to
scale either in x or in y direction.
Matrix4: scale is now a Vector2f
  • Loading branch information...
Dgame committed May 3, 2015
1 parent 2c24113 commit 2984813fb1110b019322575ff851b8f0de9930ba
@@ -177,6 +177,6 @@ final:
h = _texRect.height;
}
return Rect(cast(int) _position.x, cast(int) _position.y, w, h);
return Rect(cast(int) super.x, cast(int) super.y, w, h);
}
}
@@ -41,22 +41,23 @@ public:
* Author: Randy Schuett (rswhite4@googlemail.com)
*/
class Transformable {
protected:
Vector2f _local_center;
private:
Vector2f _position;
Vector2f _rotationCenter;
Vector2f _origin;
Vector2f _scale = Vector2f(1, 1);
float _rotationAngle = 0;
float _scale = 1;
float _rotation = 0;
Matrix4 _matrix;
bool _was_transformed = true;
protected:
@nogc
final void _notifyTransform() pure nothrow {
_was_transformed = true;
}
private:
Matrix4 _matrix;
bool _was_transformed = true;
public:
final:
/**
@@ -66,16 +67,19 @@ final:
@nogc
ref const(Matrix4) getMatrix() pure nothrow {
if (_was_transformed) {
const Vector2f global_center = _position + _local_center;
_matrix.loadIdentity().rotate(_rotation, global_center).scale(_scale, global_center).translate(_position);
const Vector2f* center = _origin.isEmpty() ? &_rotationCenter : &_origin;
_matrix.loadIdentity()
.translate(_position - _origin)
.rotate(_rotationAngle, *center)
.scale(_scale, *center);
_was_transformed = false;
}
return _matrix;
}
/**
* Set the position
* Sets the position
*/
@nogc
void setPosition(float x, float y) pure nothrow {
@@ -85,7 +89,7 @@ final:
}
/**
* Set the position
* Sets the position
*/
@nogc
void setPosition()(auto ref const Vector2f pos) pure nothrow {
@@ -132,7 +136,7 @@ final:
}
/**
* Set a new x coordinate
* Sets a new x coordinate
*/
@nogc
@property
@@ -153,7 +157,7 @@ final:
}
/**
* Set a new y coordinate
* Sets a new y coordinate
*/
@nogc
@property
@@ -163,41 +167,100 @@ final:
}
/**
* Set the center position
* Sets the center position
*
* Note: if you use this function with setOrigin,
* the origin takes the place of the rotation center.
*/
@nogc
void setCenter(float x, float y) pure nothrow {
_local_center.x = x;
_local_center.y = y;
void setRotationCenter(float x, float y) pure nothrow {
_rotationCenter.x = x;
_rotationCenter.y = y;
_notifyTransform();
}
/**
* Set the center position
* Sets the center of the rotation
*
* Note: if you use this function with setOrigin,
* the origin takes the place of the rotation center.
*/
@nogc
void setCenter()(auto ref const Vector2f center) pure nothrow {
_local_center = center;
void setRotationCenter()(auto ref const Vector2f center) pure nothrow {
_rotationCenter = center;
_notifyTransform();
}
/**
* Returns the current center position
* Returns the current rotation center
*/
@nogc
ref const(Vector2f) getCenter() const pure nothrow {
return _local_center;
ref const(Vector2f) getRotationCenter() const pure nothrow {
return _rotationCenter;
}
/**
* Set the scaling (for both, x and y)
* Sets the origin. The origin is a offset which is added to the position
* and serves as center position for scaling and rotation.
*
* Note: If you use this with setRotationCenter
* the origin will suppress the rotation center and takes it's place.
*/
@nogc
void setScale(float scale) pure nothrow {
void setOrigin(float x, float y) pure nothrow {
_origin.x = x;
_origin.y = y;
_notifyTransform();
}
/**
* Sets the origin. The origin is a offset which is added to the position
* and serves as center position for scaling and rotation.
*
* Note: If you use this with setRotationCenter
* the origin will suppress the rotation center and takes it's place.
*/
@nogc
void setOrigin()(auto ref const Vector2f origin) pure nothrow {
_origin = origin;

This comment has been minimized.

@Dgame

Dgame May 4, 2015

Owner

Add _notifyTransform

}
/**
* Returns the current origin
*/
@nogc
ref const(Vector2f) getOrigin() const pure nothrow {
return _origin;
}
/**
* Sets the scaling (for both, x and y)
*/
@nogc
void setScale(float x, float y) pure nothrow {
_scale.x = x;
_scale.y = y;
_notifyTransform();
}
/**
* Sets the scaling (for both, x and y)
*/
@nogc
void setScale()(auto ref const Vector2f scale) pure nothrow {
_scale = scale;
_notifyTransform();
}
/**
* Inc-/Decrease the scaling
*/
@nogc
void scale()(auto ref const Vector2f offset) pure nothrow {
_scale += offset;
_notifyTransform();
}
/**
* Inc-/Decrease the scaling
*/
@@ -211,7 +274,7 @@ final:
* Returns the current scaling
*/
@nogc
float getScale() const pure nothrow {
ref const(Vector2f) getScale() const pure nothrow {
return _scale;
}
@@ -220,9 +283,9 @@ final:
*/
@nogc
void setRotation(float rotation) pure nothrow {
_rotation = rotation;
if (_rotation < 0 || _rotation > 360)
_rotation = 0;
_rotationAngle = rotation;
if (_rotationAngle < 0 || _rotationAngle > 360)
_rotationAngle = 0;
_notifyTransform();
}
@@ -232,9 +295,9 @@ final:
*/
@nogc
void rotate(float rotation) pure nothrow {
_rotation += rotation;
if (_rotation > 360 || _rotation < -360)
_rotation %= 360;
_rotationAngle += rotation;
if (_rotationAngle > 360 || _rotationAngle < -360)
_rotationAngle %= 360;
_notifyTransform();
}
@@ -243,6 +306,6 @@ final:
*/
@nogc
float getRotation() const pure nothrow {
return _rotation;
return _rotationAngle;
}
}
@@ -0,0 +1,105 @@
module Dgame.Graphic.VertexArray;
private:
import Dgame.Graphic.Drawable;
import Dgame.Graphic.Texture;
import Dgame.Math.Vertex;
import Dgame.Math.Geometry;
import Dgame.Window.Window;
public:
class VertexArray : Drawable {
private:
Vertex[] _vertices;
Texture* _texture;
protected:
@nogc
override void draw(ref const Window wnd) nothrow {
wnd.draw(Geometry.Triangle, _texture, _vertices);
}
public:
final:
/**
* CTor
*/
@nogc
this(ref Texture texture) pure nothrow {
this.setTexture(texture);
}
/**
* Set or reset a Texture
*/
@nogc
void setTexture(ref Texture texture) pure nothrow {
_texture = &texture;
}
/**
* Returns the current texture or null
*/
@nogc
inout(Texture)* getTexture() inout pure nothrow {
return _texture;
}
/**
* Clear all Vertices but preserve the storage and capacity
*/
void clear() nothrow {
_vertices.length = 0;
_vertices.assumeSafeAppend();
}
/**
* Appends four Vertices arounds the given position with the given texture coordinates
*/
void append()(auto ref const Vector2f position, auto ref const Rect texRect) pure nothrow {
immutable float tx = float(texRect.x) / _texture.width;
immutable float ty = float(texRect.y) / _texture.height;
immutable float tw = float(texRect.width) / _texture.width;
immutable float th = float(texRect.height) / _texture.height;
immutable float tx_tw = tx + tw;
immutable float ty_th = ty + th;
immutable float cx_tw = position.x + _texRect.width;
immutable float cy_th = position.y + _texRect.height;
_vertices.reserve(4);
_vertices ~= Vertex(position, Vector2f(tx, ty));
_vertices ~= Vertex(Vector2f(cx_tw, position.y), Vector2f(tx_tw, ty));
_vertices ~= Vertex(Vector2f(position.x, cy_th), Vector2f(tx, ty_th));
_vertices ~= Vertex(Vector2f(cx_tw, cy_th), Vector2f(tx_tw, ty_th));
}
/**
* Stores a Vertex
*/
void append()(auto ref const Vertex vertex) pure nothrow {
_vertices ~= vertex;
}
/**
* Returns all Vertices
*/
@nogc
ref inout(Vertex) opIndex(size_t index) inout pure nothrow {
assert(index < _vertices.length);
return _vertices[index];
}
/**
* Returns the current amount of stored Vertices
*/
@nogc
@property
size_t length() const pure nothrow {
return _vertices.length;
}
}
@@ -175,9 +175,9 @@ public:
* Scale the Matrix about factor scale
*/
@nogc
ref Matrix4 scale(float scale) pure nothrow {
const Matrix4 scaling = Matrix4(scale, 0, 0,
0, scale, 0,
ref Matrix4 scale()(auto ref const Vector2f scale) pure nothrow {
const Matrix4 scaling = Matrix4(scale.x, 0, 0,
0, scale.y, 0,
0, 0, 1);
return merge(this, scaling);
@@ -187,9 +187,9 @@ public:
* Scale the Matrix about factor scale about the given center position
*/
@nogc
ref Matrix4 scale()(float scale, auto ref const Vector2f center) pure nothrow {
const Matrix4 scaling = Matrix4(scale, 0, center.x * (1 - scale),
0, scale, center.y * (1 - scale),
ref Matrix4 scale()(auto ref const Vector2f scale, auto ref const Vector2f center) pure nothrow {
const Matrix4 scaling = Matrix4(scale.x, 0, center.x * (1 - scale.x),
0, scale.y, center.y * (1 - scale.y),
0, 0, 1);
return merge(this, scaling);
}
@@ -67,6 +67,15 @@ struct Vertex {
this(pos, coords, Color4f(col));
}
/**
* CTor
*/
@nogc
this()(auto ref const Vector2f pos, auto ref const Vector2f coords) pure nothrow {
this.position = pos;
this.texCoord = coords;
}
/**
* CTor
*/
Oops, something went wrong.

0 comments on commit 2984813

Please sign in to comment.