00001 /****************************************************************************** 00002 * This file is part of the Gluon Development Platform 00003 * Copyright (c) 2010 Arjen Hiemstra <ahiemstra@heimr.nl> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 #include "spriterenderercomponent.h" 00021 00022 #include <core/debughelper.h> 00023 #include <graphics/item.h> 00024 #include <graphics/engine.h> 00025 #include <graphics/material.h> 00026 #include <graphics/mesh.h> 00027 #include <graphics/materialinstance.h> 00028 #include <engine/gameobject.h> 00029 #include <engine/asset.h> 00030 00031 #include <QtCore/QMimeData> 00032 #include <QtCore/QVariant> 00033 #include <QtGui/QMatrix4x4> 00034 #include <QtGui/QColor> 00035 #include <texture.h> 00036 #include <game.h> 00037 00038 REGISTER_OBJECTTYPE( GluonEngine, SpriteRendererComponent ) 00039 00040 using namespace GluonEngine; 00041 00042 class SpriteRendererComponent::SpriteRendererComponentPrivate 00043 { 00044 public: 00045 SpriteRendererComponentPrivate() 00046 { 00047 item = 0; 00048 texture = 0; 00049 material = 0; 00050 size = QSizeF( 1.0f, 1.0f ); 00051 color.setRgb( 255, 255, 255 ); 00052 } 00053 00054 GluonGraphics::Item* item; 00055 GluonEngine::Asset* texture; 00056 GluonGraphics::MaterialInstance* material; 00057 00058 QColor color; 00059 QSizeF size; 00060 }; 00061 00062 SpriteRendererComponent::SpriteRendererComponent( QObject* parent ) 00063 : Component( parent ) 00064 , d( new SpriteRendererComponentPrivate ) 00065 { 00066 00067 } 00068 00069 SpriteRendererComponent::SpriteRendererComponent( const SpriteRendererComponent& other ) 00070 : Component( other ) 00071 , d( other.d ) 00072 { 00073 } 00074 00075 SpriteRendererComponent::~SpriteRendererComponent() 00076 { 00077 delete d; 00078 } 00079 00080 QString SpriteRendererComponent::category() const 00081 { 00082 return QString( "Graphics Rendering" ); 00083 } 00084 00085 void SpriteRendererComponent::initialize() 00086 { 00087 if( !d->item ) 00088 { 00089 d->item = GluonGraphics::Engine::instance()->createItem( "default" ); 00090 } 00091 00092 if( d->material ) 00093 { 00094 Asset* materialAsset = qobject_cast<Asset*>(d->material->parent()); 00095 if(materialAsset) 00096 materialAsset->load(); 00097 00098 Asset* texture = gameProject()->findChild<Asset*>( d->material->property( "texture0" ).toString() ); 00099 if( texture ) 00100 texture->load(); 00101 d->item->setMaterialInstance( d->material ); 00102 } 00103 } 00104 00105 void SpriteRendererComponent::start() 00106 { 00107 } 00108 00109 void SpriteRendererComponent::draw( int timeLapse ) 00110 { 00111 Q_UNUSED( timeLapse ) 00112 00113 if( d->item ) 00114 { 00115 QMatrix4x4 transform = gameObject()->transform(); 00116 transform.scale( d->size.width() / 2, d->size.height() / 2 ); 00117 d->item->setTransform( transform ); 00118 } 00119 } 00120 00121 void SpriteRendererComponent::cleanup() 00122 { 00123 if( d->item ) 00124 { 00125 GluonGraphics::Engine::instance()->destroyItem( d->item ); 00126 d->item = 0; 00127 } 00128 } 00129 00130 void SpriteRendererComponent::setSize( const QSizeF& size ) 00131 { 00132 d->size = size; 00133 } 00134 00135 QSizeF SpriteRendererComponent::size() 00136 { 00137 return d->size; 00138 } 00139 00140 GluonGraphics::MaterialInstance* 00141 SpriteRendererComponent::material() 00142 { 00143 return d->material; 00144 } 00145 00146 void SpriteRendererComponent::setMaterial( GluonGraphics::MaterialInstance* material ) 00147 { 00148 if( !material ) 00149 return; 00150 00151 d->material = material; 00152 00153 if( d->item ) 00154 d->item->setMaterialInstance( material ); 00155 } 00156 00157 void SpriteRendererComponent::setMaterial( const QString& path ) 00158 { 00159 setMaterial( qobject_cast<GluonGraphics::MaterialInstance*>( Game::instance()->gameProject()->findItemByName( path ) ) ); 00160 } 00161 00162 Q_EXPORT_PLUGIN2( gluon_component_spriterenderer, GluonEngine::SpriteRendererComponent ); 00163 00164 #include "spriterenderercomponent.moc"