[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/games/KGLEngineRefactoring
From:       Charles Huet <packadal () gmail ! com>
Date:       2008-11-30 21:18:09
Message-ID: 1228079889.100046.28632.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 891000 by chuet:

moved the textures management in another class, using an abstract class as interface \
for more versatility. Modified the pong in order to use this new system (not \
finished)

 M  +6 -1      CMakeLists.txt  
 M  +3 -2      examples/kglpong/ball.cpp  
 M  +2 -2      examples/kglpong/ball.h  
 M  +9 -1      examples/kglpong/pongengine.cpp  
 M  +3 -1      examples/kglpong/racket.cpp  
 M             examples/kglpong/sprites/ball.png  
 M  +2 -16     kglanimitem.cpp  
 M  +1 -1      kglanimitem.h  
 M  +5 -30     kglengine2d.cpp  
 M  +4 -8      kglengine2d.h  
 M  +8 -4      kglintro.cpp  
 M  +1 -1      kglitem.cpp  
 M  +1 -1      kglitem.h  
 M  +2 -7      kglparticlesitem.cpp  
 M  +2 -2      kglparticlesitem.h  
 M  +3 -11     kglspriteitem.cpp  
 M  +1 -1      kglspriteitem.h  


--- trunk/playground/games/KGLEngineRefactoring/CMakeLists.txt #890999:891000
@@ -34,6 +34,8 @@
     glpoint.cpp
     kgamekey.cpp
     kglgraphicsconfig.cpp
+    kglbasictexturemanager.cpp
+    kglenhancedtexturemanager.cpp
     kglintro.cpp
     )
 
@@ -54,7 +56,7 @@
 target_link_libraries(kglengine ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} \
${X11_Xrandr_LIB} ${X11_LIBRARY} )  
 add_subdirectory(tests)
-add_subdirectory(examples)
+#add_subdirectory(examples)
 ########### INSTALL FILES ###############
 
 install(TARGETS kglengine ${INSTALL_TARGETS_DEFAULT_ARGS} )
@@ -75,5 +77,8 @@
     kglphysicsitem.h
     kglspriteitem.h
     kglintro.h
+    kgltexturemanager.h
+    kglbasictexturemanager.h
+    kglenhancedtexturemanager.h
     #ui_kglgraphicsconfig.h
     DESTINATION ${INCLUDE_INSTALL_DIR} COMPONENT Devel)
--- trunk/playground/games/KGLEngineRefactoring/examples/kglpong/ball.cpp \
#890999:891000 @@ -25,7 +25,7 @@
 #include "kglparticlesitem.h"
 
 Ball::Ball()
-:KGLAnimItem(QString("sprites/ball.png"), Ball::staticMetaObject.className(), 4, \
1000) +:KGLSpriteItem(KGLEngine2d::getInstance()->textureManager()->getTexture("ball.svg"), \
Ball::staticMetaObject.className())  {
 //	KGLParticlesItem* particles = new KGLParticlesItem("sprites/partLine.png", 5);
 //	particles->setAngleSpan(360);
@@ -43,7 +43,6 @@
 //==============================================================================
 void Ball::collidingWith(const SET_COLLIDABLE_ITEMS &items)
 {
-
 	SET_COLLIDABLE_ITEMS::iterator it = items.begin();
 	while(it != items.end())
 	{
@@ -61,7 +60,9 @@
 			//particles->setDirection(m_position+m_speed);
 		}
 		if((*it)->typeName() == GOAL_TYPE)
+		{
 			emit(enterGoal(((*it)->position().x() > 0) ? 1 : 2));
+		}
 		++it;
 	}
 }
--- trunk/playground/games/KGLEngineRefactoring/examples/kglpong/ball.h \
#890999:891000 @@ -21,12 +21,12 @@
 #ifndef KGL_BALL_H
 #define KGL_BALL_H
 
-#include "kglanimitem.h"
+#include "kglspriteitem.h"
 
 const QString WALL_TYPE = "01Wall";
 const QString GOAL_TYPE = "04Goal";
 
-class Ball : public KGLAnimItem
+class Ball : public KGLSpriteItem
 {
 	Q_OBJECT
 public:
--- trunk/playground/games/KGLEngineRefactoring/examples/kglpong/pongengine.cpp \
#890999:891000 @@ -18,6 +18,7 @@
  * Boston, MA 02110-1301, USA.
  */
 
+#include "kglenhancedtexturemanager.h"
 #include "pongengine.h"
 #include "ball.h"
 #include "racket.h"
@@ -46,6 +47,8 @@
 //	intro->scale(0.5);
 //	addGLItem(intro);
 
+	m_textureManager = new \
KGLEnhancedTextureManager("/home/packadal/Skuld/workspace/kglengine/examples/kglpong/sprites/");
 +
 	setWidth(640);
 	setHeight(480);
 	const double wallThickness = 10;
@@ -66,7 +69,12 @@
 	player2Goal->setColor(Qt::black);
 	addGLItem(player2Goal);
 
-	KGLSpriteItem *background = new KGLSpriteItem("sprites/wall.png", "00background");
+	QImage i("sprites/wall.png");
+
+	KGLEngine2d::getInstance()->textureManager()->addTexture(i, "wall");
+	GLint wall = KGLEngine2d::getInstance()->textureManager()->getTexture("wall");
+
+	KGLSpriteItem *background = new KGLSpriteItem(wall, "00background");
 	background->setPosition(-background->center());
 	addGLItem(background);
 
--- trunk/playground/games/KGLEngineRefactoring/examples/kglpong/racket.cpp \
#890999:891000 @@ -20,12 +20,14 @@
 
 #include "racket.h"
 
+#include "kglengine2d.h"
+
 #include <QTimeLine>
 
 const QString WALL_TYPE = "01Wall";
 
 Racket::Racket()
-:KGLSpriteItem("sprites/bar.png", Racket::staticMetaObject.className())
+:KGLSpriteItem(KGLEngine2d::getInstance()->textureManager()->getTexture("bar.png"), \
Racket::staticMetaObject.className(), \
KGLEngine2d::getInstance()->textureManager()->getTextureDim("bar.png"))  {
 	m_timeLine = new QTimeLine(2000);
 	m_timeLine->setFrameRange(0, 255);
--- trunk/playground/games/KGLEngineRefactoring/kglanimitem.cpp #890999:891000
@@ -26,7 +26,7 @@
 #include <QDebug>
 #include <QTimeLine>
 
-KGLAnimItem::KGLAnimItem(const QString &texFile, const QString &typeName, const int \
&nbFrame, const int &duration, const QSizeF &dim) : +KGLAnimItem::KGLAnimItem(const \
GLint &texture, const QString &typeName, const int &nbFrame, const int &duration, \
const QSizeF &dim) :  KGLItem(typeName)
 {
 	m_timeLine = new QTimeLine(duration, this);
@@ -35,21 +35,7 @@
 	m_timeLine->setFrameRange(0, nbFrame);
 	m_timeLine->setDuration(duration);
 
-	if(dim.isNull())
-	{
-		const QImage q(texFile);
-		const RECT r(0, 0, q.width(), q.height() / nbFrame);
-		//		init(r, POINT(1, 1./nbFrame));
-		const float pal = 13 / nbFrame;
-		addPoint(new GLPoint(0, 0, Qt::white, QPointF(0, 0)));
-		addPoint(new GLPoint(0, q.height(), Qt::white, QPointF(0, pal)));
-		addPoint(new GLPoint(q.width(), q.height(), Qt::white, QPointF(1, pal)));
-		addPoint(new GLPoint(q.width(), 0, Qt::white, QPointF(1, 0)));
-	}
-	else
-		init(RECT(0, 0, dim.width(), dim.height()));
-
-	GLint texture = (texFile == "" ? -1 : \
KGLEngine2d::getInstance()->addTexture(texFile)); +	init(RECT(0, 0, dim.width(), \
dim.height()));  setTexture(texture);
 
 	connect(m_timeLine, SIGNAL(frameChanged(int)), this, SLOT(createFrame(int)));
--- trunk/playground/games/KGLEngineRefactoring/kglanimitem.h #890999:891000
@@ -31,7 +31,7 @@
 {
 	Q_OBJECT
 public:
-	explicit KGLAnimItem(const QString &texFile, const QString &typeName = \
KGLAnimItem::staticMetaObject.className(), const int &nbFrame = 2, const int \
&duration = 500, +	explicit KGLAnimItem(const GLint &texture, const QString &typeName \
= KGLAnimItem::staticMetaObject.className(), const int &nbFrame = 2, const int \
&duration = 500,  const QSizeF &dim = QSizeF(0.5, 0.5));
 
 	//	inline QTimeLine* timeLine() { return m_timeLine; }
--- trunk/playground/games/KGLEngineRefactoring/kglengine2d.cpp #890999:891000
@@ -22,6 +22,8 @@
 
 #include "kglengine2d.h"
 
+#include "kglbasictexturemanager.h"
+
 #include <stdlib.h>
 #include <algorithm>
 
@@ -54,6 +56,8 @@
 	m_physics = new KGLPhysicsEngine();
 	m_keyEvents = new KGameKey();
 
+	m_textureManager = new KGLBasicTextureManager();
+
 	setMouseTracking(true);
 	setFocusPolicy(Qt::StrongFocus);
 
@@ -77,9 +81,7 @@
 			delete (*it++);
 		mapIt++;
 	}
-	map<QString, GLuint>::iterator texIt = m_textureMap.begin();
-	while(texIt != m_textureMap.end())
-		deleteTexture((*texIt++).second);
+	delete m_textureManager;
 }
 
 //===========================================
@@ -297,33 +299,6 @@
 }
 //===============================================================
 
-GLint KGLEngine2d::addTexture(const QString &path)
-{
-	map<QString, GLuint>::iterator it = m_textureMap.find(path);
-	GLint res = 0;
-	//if the texture isn't already there, we add it
-	if(it == m_textureMap.end())
-	{
-		QFile file(path);
-		if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
-		{
-			cout << "cant find texture: " << qPrintable(path) << endl;
-			// 			KMessageBox::error(0,"cant find texture: "+path);
-			return res = 0;
-		}
-		else
-		{
-			QImage pic(path);
-			res = bindTexture(pic);
-			m_textureMap[path] = res;
-		}
-	}
-	//if it's here we simply return it
-	else
-		res = (*it).second;
-	return res;
-}
-
 void KGLEngine2d::addTriggers()
 {
 	//TODO update them according to the fact an item is not collidable by default, or \
                remove them compltetely
--- trunk/playground/games/KGLEngineRefactoring/kglengine2d.h #890999:891000
@@ -29,6 +29,7 @@
 #include <QtOpenGL/QGLWidget>
 #include <QtCore/QTimer>
 
+#include "kgltexturemanager.h"
 #include "kgltextitem.h"
 #include "kglspriteitem.h"
 #include "kgamekey.h"
@@ -117,7 +118,7 @@
 
 	inline void setWallpaper(const QString &file)
 	{
-		texWallpaper = bindTexture(QPixmap(file));
+		texWallpaper = bindTexture(QImage(file));
 	}
 	inline void setWallpaper(GLuint tex)
 	{
@@ -158,11 +159,7 @@
 		return m_keyEvents;
 	}
 
-	inline GLint texture(const QString &id)
-	{
-		return m_textureMap[id];
-	}
-	GLint addTexture(const QString &path);
+	KGLTextureManager* textureManager() { return m_textureManager; }
 
 	static KGLEngine2d* getInstance(int w = 640, int h = 480, int rateFrame = 10, \
QWidget* parent = 0)  {
@@ -246,6 +243,7 @@
 	std::map<QString, std::set<QString> > m_collisionRules;
 
 	KGLPhysicsEngine* m_physics;
+	KGLTextureManager* m_textureManager;
 	static KGLEngine2d* m_engine;
 	float m_width, m_height;
 	float m_hwidth, m_hheight;
@@ -265,8 +263,6 @@
 
 	bool hasRepere;
 	bool hasFps;
-
-	std::map<QString, GLuint> m_textureMap;
 };
 
 #endif //KGLENGINE2D_H
--- trunk/playground/games/KGLEngineRefactoring/kglintro.cpp #890999:891000
@@ -33,17 +33,21 @@
 	m_timer = new QTimer;
 	m_timer->start(10);
 
-	m_gear = new KGLSpriteItem(":gear.png", typeName, POINT(0.5, 0.5));
+	/*
+	QImage gear(":gear.png");
+	QImage title(":title.png");
+
+	m_gear = new KGLSpriteItem(gear, typeName, POINT(0.5, 0.5));
 	m_gear->scale(0.8);
 
-	m_gear2 = new KGLSpriteItem(":gear.png", typeName, POINT(0.5, 0.5));
+	m_gear2 = new KGLSpriteItem(gear, typeName, POINT(0.5, 0.5));
 	m_gear2->scale(0.6);
 	m_gear2->setPosition(0.28, 0.28);
 	m_gear2->rotate(90);
 
-	m_title = new KGLSpriteItem(":title.png", typeName, POINT(1.5, 0.25));
+	m_title = new KGLSpriteItem(title, typeName, POINT(1.5, 0.25));
 	m_title->setPosition(0.5, 0);
-
+*/
 	addChild(m_gear);
 	addChild(m_gear2);
 	addChild(m_title);
--- trunk/playground/games/KGLEngineRefactoring/kglitem.cpp #890999:891000
@@ -277,7 +277,7 @@
 	//	applyMatrix(item->matrix());
 }
 
-void KGLItem::addParticlesEffect(const QString& texture, int nbParticles)
+void KGLItem::addParticlesEffect(const GLint& texture, int nbParticles)
 {
 	m_particles = new KGLParticlesItem(texture, nbParticles);
 	KGLEngine2d::getInstance()->addGLItem(m_particles);
--- trunk/playground/games/KGLEngineRefactoring/kglitem.h #890999:891000
@@ -146,7 +146,7 @@
 	QPointF transform(QPointF p) const;
 	QRectF transform(QRectF r) const;
 
-	void addParticlesEffect(const QString&, int);
+	void addParticlesEffect(const GLint&, int);
 
 	Q_SIGNALS:
 	void colliding(PhysicsItem*);
--- trunk/playground/games/KGLEngineRefactoring/kglparticlesitem.cpp #890999:891000
@@ -27,14 +27,14 @@
 #include <cmath>
 
 
-KGLParticlesItem::KGLParticlesItem(const QString &texFile, int nbParticules) :
+KGLParticlesItem::KGLParticlesItem(const GLint &texture, int nbParticules) :
 	KGLItem(KGLParticlesItem::staticMetaObject.className())
 {
 	m_angleSpan = 360;
 	m_speed = 0.001;
 	m_alphaStep = 0.001;
 	m_pointSize = 20;
-	m_texture = KGLEngine2d::getInstance()->addTexture(texFile);
+	m_texture = texture;
 	m_modeRandom = true;
 	m_finish = false;
 	m_repeat = true;
@@ -126,8 +126,3 @@
 		glPopMatrix();
 	}
 }
-
-void KGLParticlesItem::setTexture(const QString& newText)
-{
-	m_texture = KGLEngine2d::getInstance()->addTexture(newText);
-}
--- trunk/playground/games/KGLEngineRefactoring/kglparticlesitem.h #890999:891000
@@ -32,7 +32,7 @@
 class KGLParticlesItem: public KGLItem
 {
 public:
-	explicit KGLParticlesItem(const QString &texFile, int nbParticules = 10);
+	explicit KGLParticlesItem(const GLint &texture, int nbParticules = 10);
 	virtual void draw();
 
 	inline void setNbParticles(uint nb)
@@ -84,7 +84,7 @@
 		m_finish = false;
 		m_count = 0;
 	}
-	void setTexture(const QString&);
+	inline void setTexture(const GLint &texture) { m_texture = texture; }
 
 	inline bool isFinished()
 	{
--- trunk/playground/games/KGLEngineRefactoring/kglspriteitem.cpp #890999:891000
@@ -25,19 +25,11 @@
 
 #include <Eigen/Core>
 
-KGLSpriteItem::KGLSpriteItem(const QString &texFile, const QString &typeName, const \
POINT &dim, const POINT &pos, const POINT &trepeat) : \
+KGLSpriteItem::KGLSpriteItem(const GLint &texture, const QString &typeName, const \
POINT &dim, const POINT &pos, const POINT &trepeat) :  KGLItem(typeName)
 {
-	if(dim.isNull())
-	{
-		QImage q(texFile);
-		init(RECT(pos.x(), pos.y(), q.width(), q.height()), trepeat);
-	}
-	else
-		init(RECT(pos.x(), pos.y(), dim.x(), dim.y()), trepeat);
-
-	GLint texture = (texFile == "" ? -1 : \
                KGLEngine2d::getInstance()->addTexture(texFile));
-	setTexture(texture);
+	init(RECT(pos.x(), pos.y(), dim.x(), dim.y()), trepeat);
+	m_texture = texture;
 }
 //======================================================================
 
--- trunk/playground/games/KGLEngineRefactoring/kglspriteitem.h #890999:891000
@@ -28,7 +28,7 @@
 class KGLSpriteItem: public KGLItem
 {
 public:
-	explicit KGLSpriteItem(const QString &texFile = "", const QString &typeName = \
KGLSpriteItem::staticMetaObject.className(), const POINT &dim = POINT(0, 0), \
+	explicit KGLSpriteItem(const GLint &texture = 0, const QString &typeName = \
                KGLSpriteItem::staticMetaObject.className(), const POINT &dim = \
                POINT(1, 1),
 	        const POINT &pos = POINT(0, 0), const POINT &trepeat = POINT(1, 1));
 };
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic