RectangleDrawable WIP.

Drawables now can keep a position.
Some code clean up.

--HG--
branch : dev
This commit is contained in:
Martí­n Lucas Golini
2017-03-29 03:02:59 -03:00
parent 0735ded34e
commit 12c74347ac
22 changed files with 307 additions and 94 deletions

View File

@@ -27,6 +27,10 @@ Sizef ArcDrawable::getSize() {
return Sizef( mRadius * 2, mRadius * 2 );
}
void ArcDrawable::draw() {
draw( mPosition );
}
void ArcDrawable::draw(const Vector2f & position) {
draw( position, getSize() );
}

View File

@@ -72,10 +72,24 @@ EE_DRAWABLE_TYPE Drawable::getDrawableType() const {
return mDrawableType;
}
const Vector2f& Drawable::getPosition() const {
return mPosition;
}
void Drawable::setPosition( const Vector2f& position ) {
if ( position != mPosition ) {
mPosition = position;
onPositionChange();
}
}
void Drawable::onAlphaChange() {
}
void Drawable::onColorFilterChange() {
}
void Drawable::onPositionChange() {
}
}}

View File

@@ -75,6 +75,10 @@ void PrimitiveDrawable::onColorFilterChange() {
mNeedsUpdate = true;
}
void PrimitiveDrawable::onPositionChange() {
mNeedsUpdate = true;
}
void PrimitiveDrawable::prepareVertexBuffer( const EE_DRAW_MODE& drawableType ) {
if ( mRecreateVertexBuffer ) {
eeSAFE_DELETE( mVertexBuffer );

View File

@@ -1,27 +1,191 @@
#include <eepp/graphics/rectangledrawable.hpp>
#include <eepp/graphics/globalbatchrenderer.hpp>
#include <eepp/graphics/vertexbuffer.hpp>
namespace EE { namespace Graphics {
RectangleDrawable::RectangleDrawable() :
PrimitiveDrawable( DRAWABLE_RECTANGLE )
PrimitiveDrawable( DRAWABLE_RECTANGLE ),
mRotation( 0 ),
mScale( 1, 1 ),
mCorners( 0 ),
mUsingRectColors( false )
{
}
RectangleDrawable::~RectangleDrawable() {
RectangleDrawable::RectangleDrawable(const Vector2f & position, const Sizef & size) :
PrimitiveDrawable( DRAWABLE_RECTANGLE ),
mSize( size ),
mRotation( 0 ),
mScale( 1, 1 ),
mCorners( 0 ),
mUsingRectColors( false )
{
mPosition = position;
}
Sizef RectangleDrawable::getSize() {
return Sizef();
return mSize;
}
void RectangleDrawable::draw() {
draw( mPosition );
}
void RectangleDrawable::draw(const Vector2f & position) {
draw( mPosition, mSize );
}
void RectangleDrawable::draw(const Vector2f & position, const Sizef & size) {
if ( size != mSize ) {
mSize = size;
mNeedsUpdate = true;
}
if ( mCorners == 0 ) {
if ( mUsingRectColors ) {
drawRectangle( Rectf( mPosition, mSize ), mRectColors.TopLeft, mRectColors.BottomLeft, mRectColors.BottomRight, mRectColors.TopRight, mRotation, mScale );
} else {
drawRectangle( Rectf( mPosition, mSize ), mColor, mColor, mColor, mColor, mRotation, mScale );
}
} else {
PrimitiveDrawable::draw( position, size );
}
}
Float RectangleDrawable::getRotation() const {
return mRotation;
}
void RectangleDrawable::setRotation(const Float & rotation) {
mRotation = rotation;
mNeedsUpdate = true;
}
Vector2f RectangleDrawable::getScale() const {
return mScale;
}
void RectangleDrawable::setScale(const Vector2f & scale) {
mScale = scale;
mNeedsUpdate = true;
}
void RectangleDrawable::setSize(const Sizef & size) {
mSize = size;
mNeedsUpdate = true;
}
Uint32 RectangleDrawable::getCorners() const {
return mCorners;
}
void RectangleDrawable::setCorners(const Uint32 & corners) {
mCorners = corners;
mNeedsUpdate = true;
mRecreateVertexBuffer = true;
}
RectColors RectangleDrawable::getRectColors() const {
return mRectColors;
}
void RectangleDrawable::setRectColors(const RectColors & rectColors) {
mRectColors = rectColors;
mUsingRectColors = true;
}
void RectangleDrawable::drawRectangle( const Rectf& R, const ColorA& TopLeft, const ColorA& BottomLeft, const ColorA& BottomRight, const ColorA& TopRight, const Float& Angle, const Vector2f& Scale ) {
BatchRenderer * sBR = GlobalBatchRenderer::instance();
sBR->setTexture( NULL );
sBR->setBlendMode( mBlendMode );
switch( mFillMode ) {
case DRAW_FILL:
{
sBR->quadsBegin();
sBR->quadsSetColorFree( TopLeft, BottomLeft, BottomRight, TopRight );
Sizef size = const_cast<Rectf*>(&R)->getSize();
sBR->batchQuadEx( R.Left, R.Top, size.getWidth(), size.getHeight(), Angle, Scale );
break;
}
case DRAW_LINE:
{
sBR->setLineWidth( mLineWidth );
sBR->lineLoopBegin();
sBR->lineLoopSetColorFree( TopLeft, BottomLeft );
if ( Scale != 1.0f || Angle != 0.0f ) {
Quad2f Q( R );
Sizef size = const_cast<Rectf*>(&R)->getSize();
Q.scale( Scale );
Q.rotate( Angle, Vector2f( R.Left + size.getWidth() * 0.5f, R.Top + size.getHeight() * 0.5f ) );
sBR->batchLineLoop( Q[0].x, Q[0].y, Q[1].x, Q[1].y );
sBR->lineLoopSetColorFree( BottomRight, TopRight );
sBR->batchLineLoop( Q[2].x, Q[2].y, Q[3].x, Q[3].y );
} else {
sBR->batchLineLoop( R.Left, R.Top, R.Left, R.Bottom );
sBR->lineLoopSetColorFree( BottomRight, TopRight );
sBR->batchLineLoop( R.Right, R.Bottom, R.Right, R.Top );
}
break;
}
}
sBR->draw();
}
void RectangleDrawable::updateVertex() {
if ( mCorners == 0 )
return;
prepareVertexBuffer( mFillMode == DRAW_LINE ? DM_LINE_LOOP : DM_POLYGON );
unsigned int i;
Sizef size = mSize;
Float xscalediff = size.getWidth() * mScale.x - size.getWidth();
Float yscalediff = size.getHeight() * mScale.y - size.getHeight();
Vector2f Center( mPosition.x + size.getWidth() * 0.5f + xscalediff, mPosition.y + size.getHeight() * 0.5f + yscalediff );
Polygon2f Poly = Polygon2f::createRoundedRectangle( mPosition.x - xscalediff, mPosition.y - yscalediff, size.getWidth() + xscalediff, size.getHeight() + yscalediff, mCorners );
Vector2f poly;
Poly.rotate( mRotation, Center );
if ( !mUsingRectColors ) {
for ( i = 0; i < Poly.getSize(); i++ ) {
mVertexBuffer->addVertex( Poly[i] );
mVertexBuffer->addColor( mColor );
}
} else {
for ( i = 0; i < Poly.getSize(); i++ ) {
poly = Poly[i];
if ( poly.x <= Center.x && poly.y <= Center.y )
mVertexBuffer->addColor( mRectColors.TopLeft );
else if ( poly.x <= Center.x && poly.y >= Center.y )
mVertexBuffer->addColor( mRectColors.BottomLeft );
else if ( poly.x > Center.x && poly.y > Center.y )
mVertexBuffer->addColor( mRectColors.BottomRight );
else if ( poly.x > Center.x && poly.y < Center.y )
mVertexBuffer->addColor( mRectColors.TopRight );
else
mVertexBuffer->addColor( mRectColors.TopLeft );
mVertexBuffer->addVertex( poly );
}
}
mNeedsUpdate = false;
}
void RectangleDrawable::onColorFilterChange() {
PrimitiveDrawable::onColorFilterChange();
mUsingRectColors = false;
}
}}

View File

@@ -13,7 +13,6 @@ namespace EE { namespace Graphics {
Sprite::Sprite() :
Drawable( DRAWABLE_SPRITE ),
mFlags( SPRITE_FLAG_AUTO_ANIM | SPRITE_FLAG_EVENTS_ENABLED ),
mPos(),
mRotation( 0.f ),
mScale( 1.f, 1.f ),
mAnimSpeed( 16.f ),
@@ -34,7 +33,6 @@ Sprite::Sprite() :
Sprite::Sprite( const std::string& name, const std::string& extension, TextureAtlas * SearchInTextureAtlas ) :
Drawable( DRAWABLE_SPRITE ),
mFlags( SPRITE_FLAG_AUTO_ANIM | SPRITE_FLAG_EVENTS_ENABLED ),
mPos(),
mRotation( 0.f ),
mScale( 1.f, 1.f ),
mAnimSpeed( 16.f ),
@@ -56,7 +54,6 @@ Sprite::Sprite( const std::string& name, const std::string& extension, TextureAt
Sprite::Sprite( SubTexture * SubTexture ) :
Drawable( DRAWABLE_SPRITE ),
mFlags( SPRITE_FLAG_AUTO_ANIM | SPRITE_FLAG_EVENTS_ENABLED ),
mPos(),
mRotation( 0.f ),
mScale( 1.f, 1.f ),
mAnimSpeed( 16.f ),
@@ -78,7 +75,6 @@ Sprite::Sprite( SubTexture * SubTexture ) :
Sprite::Sprite( const Uint32& TexId, const Sizef &DestSize, const Vector2i &Offset, const Recti& TexSector ) :
Drawable( DRAWABLE_SPRITE ),
mFlags( SPRITE_FLAG_AUTO_ANIM | SPRITE_FLAG_EVENTS_ENABLED ),
mPos(),
mRotation( 0.f ),
mScale( 1.f, 1.f ),
mAnimSpeed( 16.f ),
@@ -102,14 +98,16 @@ Sprite::~Sprite() {
}
Sprite& Sprite::operator =( const Sprite& Other ) {
mDrawableType = Other.mDrawableType;
mFrames = Other.mFrames;
mFlags = Other.mFlags;
mPos = Other.mPos;
mRotation = Other.mRotation;
mColor = Other.mColor;
mPosition = Other.mPosition;
mRotation = Other.mRotation;
mScale = Other.mScale;
mAnimSpeed = Other.mAnimSpeed;
mColor = Other.mColor;
mRepetitions = Other.mRepetitions;
mRepetitions = Other.mRepetitions;
mBlend = Other.mBlend;
mEffect = Other.mEffect;
mCurrentFrame = Other.mCurrentFrame;
@@ -135,9 +133,11 @@ Sprite& Sprite::operator =( const Sprite& Other ) {
Sprite Sprite::clone() {
Sprite Spr;
Spr.mDrawableType = mDrawableType;
Spr.mColor = mColor;
Spr.mFrames = mFrames;
Spr.mFlags = mFlags;
Spr.mPos = mPos;
Spr.mPosition = mPosition;
Spr.mRotation = mRotation;
Spr.mScale = mScale;
Spr.mAnimSpeed = mAnimSpeed;
@@ -224,10 +224,10 @@ Quad2f Sprite::getQuad() {
SubTexture * S;
if ( mFrames.size() && ( S = getCurrentSubTexture() ) ) {
Rectf TmpR( mPos.x,
mPos.y,
mPos.x + S->getDestSize().x,
mPos.y + S->getDestSize().y
Rectf TmpR( mPosition.x,
mPosition.y,
mPosition.x + S->getDestSize().x,
mPosition.y + S->getDestSize().y
);
Quad2f Q = Quad2f( Vector2f( TmpR.Left, TmpR.Top ),
@@ -241,9 +241,9 @@ Quad2f Sprite::getQuad() {
if ( mOrigin.OriginType == OriginPoint::OriginCenter ) {
Center = TmpR.getCenter();
} else if ( mOrigin.OriginType == OriginPoint::OriginTopLeft ) {
Center = mPos;
Center = mPosition;
} else {
Center += mPos;
Center += mPosition;
}
switch ( mEffect ) {
@@ -285,16 +285,16 @@ eeAABB Sprite::getAABB() {
if ( mRotation != 0 || mEffect >= 4 ) {
return getQuad().toAABB();
} else { // The method used if mAngle != 0 works for mAngle = 0, but i prefer to use the faster way
TmpR = Rectf( mPos.x, mPos.y, mPos.x + S->getDestSize().x, mPos.y + S->getDestSize().y );
TmpR = Rectf( mPosition.x, mPosition.y, mPosition.x + S->getDestSize().x, mPosition.y + S->getDestSize().y );
Vector2f Center;
if ( mOrigin.OriginType == OriginPoint::OriginCenter ) {
Center = TmpR.getCenter();
} else if ( mOrigin.OriginType == OriginPoint::OriginTopLeft ) {
Center = mPos;
Center = mPosition;
} else {
Center = mPos + mOrigin;
Center = mPosition + mOrigin;
}
TmpR.scale( mScale, Center );
@@ -304,19 +304,6 @@ eeAABB Sprite::getAABB() {
return TmpR;
}
const Vector2f Sprite::getPosition() const {
return mPos;
}
void Sprite::setPosition(const Float& x, const Float& y) {
mPos.x = x;
mPos.y = y;
}
void Sprite::setPosition( const Vector2f& NewPos ) {
mPos = NewPos;
}
void Sprite::updateVertexColors( const ColorA& Color0, const ColorA& Color1, const ColorA& Color2, const ColorA& Color3 ) {
if ( NULL == mVertexColors )
mVertexColors = eeNewArray( ColorA, 4 );
@@ -589,9 +576,9 @@ void Sprite::draw( const EE_BLEND_MODE& Blend, const EE_RENDER_MODE& Effect ) {
return;
if ( NULL == mVertexColors )
S->draw( mPos.x, mPos.y, mColor, mRotation, mScale, Blend, Effect, mOrigin );
S->draw( mPosition.x, mPosition.y, mColor, mRotation, mScale, Blend, Effect, mOrigin );
else
S->draw( mPos.x, mPos.y, mRotation, mScale, mVertexColors[0], mVertexColors[1], mVertexColors[2], mVertexColors[3], Blend, Effect, mOrigin );
S->draw( mPosition.x, mPosition.y, mRotation, mScale, mVertexColors[0], mVertexColors[1], mVertexColors[2], mVertexColors[3], Blend, Effect, mOrigin );
}
void Sprite::draw() {
@@ -722,22 +709,6 @@ SubTexture * Sprite::getSubTexture( const unsigned int& frame, const unsigned in
return NULL;
}
void Sprite::setX( const Float& X ) {
mPos.x = X;
}
Float Sprite::getX() const {
return mPos.x;
}
void Sprite::setY( const Float& Y ) {
mPos.y = Y;
}
Float Sprite::getY() const {
return mPos.y;
}
void Sprite::setRotation( const Float& rotation ) {
mRotation = rotation;
}

View File

@@ -173,6 +173,10 @@ void SubTexture::draw( const Quad2f Q, const Vector2f& Offset, const Float& Angl
mTexture->drawQuadEx( Q, Offset, Angle, Scale, Color0, Color1, Color2, Color3, Blend, mSrcRect );
}
void SubTexture::draw() {
draw( mPosition );
}
void SubTexture::draw( const Vector2f& position ) {
draw( position.x, position.y, getColor() );
}

View File

@@ -738,6 +738,10 @@ Sizei EE::Graphics::Texture::getPixelSize() {
return Sizei( mImgWidth, mImgHeight );
}
void EE::Graphics::Texture::draw() {
drawFast( mPosition.x, mPosition.y );
}
void EE::Graphics::Texture::draw( const Vector2f & position ) {
drawFast( position.x, position.y );
}

View File

@@ -26,7 +26,7 @@ ShapeCircleSprite::~ShapeCircleSprite() {
void ShapeCircleSprite::draw( Space * space ) {
cVect Pos = getBody()->getPos();
mSprite->setPosition( Pos.x, Pos.y );
mSprite->setPosition( Vector2f( Pos.x, Pos.y ) );
mSprite->setRotation( getBody()->getAngleDeg() );
mSprite->draw();
}

View File

@@ -40,7 +40,7 @@ void ShapePolySprite::draw( Space * space ) {
cVect Pos = getBody()->getPos();
mSprite->setOffset( mOffset );
mSprite->setPosition( Pos.x, Pos.y );
mSprite->setPosition( Vector2f( Pos.x, Pos.y ) );
mSprite->setRotation( getBody()->getAngleDeg() );
mSprite->draw();
}

View File

@@ -52,7 +52,7 @@ void UISprite::draw() {
if ( NULL != mSprite && 0.f != mAlpha ) {
checkSubTextureUpdate();
mSprite->setPosition( (Float)( mScreenPos.x + mAlignOffset.x ), (Float)( mScreenPos.y + mAlignOffset.y ) );
mSprite->setPosition( Vector2f( (Float)( mScreenPos.x + mAlignOffset.x ), (Float)( mScreenPos.y + mAlignOffset.y ) ) );
SubTexture * subTexture = mSprite->getCurrentSubTexture();

View File

@@ -10,6 +10,7 @@ EE::Window::Window * win = NULL;
float circ = 0, circ2 = 0;
int op = 1;
ArcDrawable arcDrawable( 100, 64 );
RectangleDrawable rectDrawable( Vector2f(0,0), Sizef(250,250) );
void mainLoop()
{
@@ -84,6 +85,8 @@ void mainLoop()
arcDrawable.setArcStartAngle( circ2 );
arcDrawable.draw( winCenter );
rectDrawable.draw();
// Draw frame
win->display();
}
@@ -102,6 +105,10 @@ EE_MAIN_FUNC int main (int argc, char * argv [])
arcDrawable.setColorFilter( ColorA( 0, 255, 0, 150 ) );
arcDrawable.setFillMode( DRAW_FILL );
rectDrawable.setColorFilter( ColorA( 255, 0, 0, 150 ) );
rectDrawable.setFillMode( DRAW_FILL );
rectDrawable.setCorners( 64 );
// Set the MainLoop function and run it
// This is the application loop, it will loop until the window is closed.
// This is only a requirement if you want to support Emscripten builds ( WebGL + Canvas ).

View File

@@ -139,11 +139,14 @@ EE_MAIN_FUNC int main (int argc, char * argv [])
// Set the sprites position to the screen center
Vector2i ScreenCenter( Engine::instance()->getCurrentWindow()->getWidth() / 2, Engine::instance()->getCurrentWindow()->getHeight() / 2 );
Planet.setPosition( ScreenCenter.x - Planet.getAABB().getSize().getWidth() / 2, ScreenCenter.y - Planet.getAABB().getSize().getHeight() / 2 );
Planet.setPosition( Vector2f( ScreenCenter.x - Planet.getAABB().getSize().getWidth() / 2,
ScreenCenter.y - Planet.getAABB().getSize().getHeight() / 2 ) );
Rock.setPosition( ScreenCenter.x - Rock.getAABB().getSize().getWidth() / 2, ScreenCenter.y - Rock.getAABB().getSize().getHeight() / 2 );
Rock.setPosition( Vector2f( ScreenCenter.x - Rock.getAABB().getSize().getWidth() / 2,
ScreenCenter.y - Rock.getAABB().getSize().getHeight() / 2 ) );
Blindy.setPosition( ScreenCenter.x - Blindy.getAABB().getSize().getWidth() / 2, ScreenCenter.y - Blindy.getAABB().getSize().getHeight() / 2 );
Blindy.setPosition( Vector2f( ScreenCenter.x - Blindy.getAABB().getSize().getWidth() / 2,
ScreenCenter.y - Blindy.getAABB().getSize().getHeight() / 2 ) );
// Set the planet angle interpolation
PlanetAngle.addWaypoint( 0 );

View File

@@ -1104,7 +1104,7 @@ void EETest::loadTextures() {
CurMan->set( EE_CURSOR_ARROW );
CL1.addFrame( TN[2] );
CL1.setPosition( 500, 400 );
CL1.setPosition( Vector2f( 500, 400 ) );
CL1.setScale( 0.5f );
CL2.addFrame(TN[0], Sizef(96, 96) );
@@ -1113,7 +1113,7 @@ void EETest::loadTextures() {
mTGL = eeNew( TextureAtlasLoader, ( MyPath + "atlases/bnb" + EE_TEXTURE_ATLAS_EXTENSION ) );
mBlindy.addFramesByPattern( "rn" );
mBlindy.setPosition( 320.f, 0.f );
mBlindy.setPosition( Vector2f( 320.f, 0.f ) );
mBoxSprite = eeNew( Sprite, ( GlobalTextureAtlas::instance()->add( eeNew( SubTexture, ( TN[3], "ilmare" ) ) ) ) );
mCircleSprite = eeNew( Sprite, ( GlobalTextureAtlas::instance()->add( eeNew( SubTexture, ( TN[1], "thecircle" ) ) ) ) );
@@ -1242,7 +1242,7 @@ void EETest::screen2() {
ColorA Col(255,255,255,(int)alpha);
TNP[1]->drawEx( (Float)mWindow->getWidth() - 128.f, (Float)mWindow->getHeight() - 128.f, 128.f, 128.f, ang, Vector2f::One, Col, Col, Col, Col, ALPHA_BLENDONE, RN_FLIPMIRROR);
SP.setPosition( alpha, alpha );
SP.setPosition( Vector2f( alpha, alpha ) );
SP.draw();
#ifndef EE_GLES
@@ -1262,7 +1262,7 @@ void EETest::screen2() {
CL1.setRotation(ang);
CL1.setScale(scale * 0.5f);
CL2.setPosition( (Float)Mousef.x - 64.f, (Float)Mousef.y + 128.f );
CL2.setPosition( Vector2f( (Float)Mousef.x - 64.f, (Float)Mousef.y + 128.f ) );
CL2.setRotation(-ang);
CL1.draw();
@@ -1348,7 +1348,7 @@ void EETest::screen4() {
}
if ( NULL != mVBO ) {
mBlindy.setPosition( 128-16, 128-16 );
mBlindy.setPosition( Vector2f( 128-16, 128-16 ) );
mBlindy.draw();
mVBO->bind();