Parcourir la source

Made label size methods static, added Vector2 shader uniforms, animation end callback in ScreenSprite, shape testing methods for 2D Physics

Ivan Safrin il y a 13 ans
Parent
commit
0a4775b3ca

+ 2 - 0
Core/Contents/Include/PolyCocoaCore.h

@@ -117,6 +117,8 @@ namespace Polycode {
 		
 		void setCursor(int cursorType);
 		
+		void openURL(String url);
+		
 		void copyStringToClipboard(const String& str);
 		String getClipboardString();		
 		

+ 6 - 0
Core/Contents/Include/PolyCore.h

@@ -250,6 +250,12 @@ namespace Polycode {
 		
 		void doSleep();
 		
+		/**
+		* Launches the default browser and directs it to specified URL
+		* @param url URL to launch.
+		*/
+		virtual void openURL(String url) = 0;
+		
 		/**
 		* Returns the time elapsed since last frame.
 		* @return Time elapsed since last frame in floating point microseconds.

+ 3 - 2
Core/Contents/Include/PolyGLSLProgram.h

@@ -83,8 +83,9 @@ class _PolyExport GLSLProgramParam {
 	
 	static const int PARAM_UNKNOWN = 0;	
 	static const int PARAM_Number = 1;
-	static const int PARAM_Number3 = 2;
-	static const int PARAM_Number4 = 3;
+	static const int PARAM_Number2 = 2;		
+	static const int PARAM_Number3 = 3;
+	static const int PARAM_Number4 = 4;
 	
 	};
 

+ 3 - 2
Core/Contents/Include/PolyLabel.h

@@ -38,8 +38,9 @@ namespace Polycode {
 			~Label();
 			void setText(const String& text);
 			const String& getText() const;
-			int getTextWidth(Font *font, const String& text, int size) const;
-			int getTextHeight(Font *font, const String& text, int size) const;
+			
+			static int getTextWidth(Font *font, const String& text, int size);
+			static int getTextHeight(Font *font, const String& text, int size);
 					
 			Number getTextWidth() const;
 			Number getTextHeight() const;

+ 7 - 0
Core/Contents/Include/PolyObject.h

@@ -204,6 +204,13 @@ namespace Polycode {
 		* @return Returns true is succesful, false if otherwise.
 		*/		
 		bool loadFromXML(const String& fileName);
+
+		/**
+		* Loads data from XML string into the object. 
+		* @param xmlString XML data in a string.
+		* @return Returns true is succesful, false if otherwise.
+		*/				
+		bool loadFromXMLString(const String &xmlString);
 		
 		/**
 		* Saves the object to an XML file.

+ 3 - 2
Core/Contents/Include/PolyString.h

@@ -71,6 +71,8 @@ namespace Polycode {
 			* Initializes the string from an STL wstring.
 			*/															
 			String(const std::wstring& str);
+			
+			String(const wchar_t wchar);
 		
 			~String();
 		
@@ -190,8 +192,7 @@ namespace Polycode {
 			* @return The size the data would take up if returned with this encoding.
 			* @see getDataWithEncoding()
 			*/																																				
-			size_t getDataSizeWithEncoding(int encoding) const;
-					
+			size_t getDataSizeWithEncoding(int encoding) const;					
 			
 			/**
 			* Sets the data for the string using specified encoding.

+ 4 - 0
Core/Contents/Source/PolyCocoaCore.mm

@@ -374,6 +374,10 @@ void CocoaCore::checkEvents() {
 	unlockMutex(eventMutex);		
 }
 
+void CocoaCore::openURL(String url) {
+	[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithUTF8String: url.c_str()]]];
+}
+
 void CocoaCore::createFolder(const String& folderPath) {
 	[[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithUTF8String: folderPath.c_str()] withIntermediateDirectories:YES attributes:nil error:nil];
 }

+ 13 - 1
Core/Contents/Source/PolyGLSLProgram.cpp

@@ -23,6 +23,7 @@ THE SOFTWARE.
 
 #include "PolyGLSLProgram.h"
 #include "PolyVector3.h"
+#include "PolyVector2.h"
 #include "PolyLogger.h"
 
 #ifdef _WINDOWS
@@ -84,7 +85,18 @@ void *GLSLProgramParam::createParamData(int *retType, const String& type, const
 			Number *val = new Number();
 			*val = atof(value.c_str());
 			defaultData = (void*)val;
-			return defaultData;			
+			return defaultData;		
+		} else if(type == "Number2") {
+			*retType = GLSLProgramParam::PARAM_Number2;
+			Vector2 *val = new Vector2();
+			defaultData = (void*)val;
+			vector<String> values = value.split(" ");
+			if(values.size() == 2) {
+				val->set(atof(values[0].c_str()), atof(values[1].c_str()));
+			} else {
+				Logger::log("Error: A Number2 must have 2 values (%d provided)!\n", values.size());
+			}
+			return defaultData;				
 		} else if(type == "Number3") {
 			*retType = GLSLProgramParam::PARAM_Number3;
 			Vector3 *val = new Vector3();

+ 6 - 1
Core/Contents/Source/PolyGLSLShaderModule.cpp

@@ -381,10 +381,15 @@ void GLSLShaderModule::updateGLSLParam(Renderer *renderer, GLSLShader *glslShade
 				glUniform1f(paramLocation, *fval);
 				break;
 			}
+			case GLSLProgramParam::PARAM_Number2:
+			{
+				Vector2 *fval2 = (Vector2*)paramData;
+				int paramLocation = glGetUniformLocation(glslShader->shader_id, param.name.c_str());
+				glUniform2f(paramLocation, fval2->x, fval2->y);				break;				
+			}			
 			case GLSLProgramParam::PARAM_Number3:
 			{
 				Vector3 *fval3 = (Vector3*)paramData;
-				fval = (Number*)paramData;
 				int paramLocation = glGetUniformLocation(glslShader->shader_id, param.name.c_str());
 				glUniform3f(paramLocation, fval3->x,fval3->y,fval3->z);
 				break;				

+ 6 - 20
Core/Contents/Source/PolyLabel.cpp

@@ -43,7 +43,7 @@ Label::~Label() {
 
 }
 
-int Label::getTextWidth(Font *font, const String& text, int size) const {
+int Label::getTextWidth(Font *font, const String& text, int size) {
 	FT_Vector delta;
 	FT_UInt previous = 0;
 	FT_UInt glyph_index;
@@ -70,15 +70,9 @@ int Label::getTextWidth(Font *font, const String& text, int size) const {
 				width += delta.x >> 6;
 			}
 			FT_Load_Glyph( font->getFace(), glyph_index, NORMAL_FT_FLAGS );
-		
-			switch(antiAliasMode) {
-				case ANTIALIAS_FULL:
-					FT_Render_Glyph(slot, FT_RENDER_MODE_LIGHT );			
-					break;
-				case ANTIALIAS_NONE:
-					FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
-					break;
-			}
+			
+			FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
+
 		width += slot->advance.x >> 6;
 		}
 	}
@@ -87,7 +81,7 @@ int Label::getTextWidth(Font *font, const String& text, int size) const {
 	return width+5;
 }
 
-int Label::getTextHeight(Font *font, const String& text, int size) const {
+int Label::getTextHeight(Font *font, const String& text, int size) {
 	
 	String actualString = text; //StringUtil::replace(text, "\t", TAB_REPLACE);
 	
@@ -101,15 +95,7 @@ int Label::getTextHeight(Font *font, const String& text, int size) const {
 	{
 		glyph_index = FT_Get_Char_Index( font->getFace(), actualString[i] );
 		FT_Load_Glyph(font->getFace(), glyph_index, NORMAL_FT_FLAGS );
-		switch(antiAliasMode) {
-			case ANTIALIAS_FULL:
-				FT_Render_Glyph(slot, FT_RENDER_MODE_LIGHT );			
-			break;
-			case ANTIALIAS_NONE:
-				FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
-			break;
-		}
-
+		FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
 		
 		if(slot->bitmap_top > height)
 			height = slot->bitmap_top;

+ 15 - 0
Core/Contents/Source/PolyObject.cpp

@@ -93,6 +93,21 @@ TiXmlElement *Object::createElementFromObjectEntry(ObjectEntry *entry) {
 	return newElement;
 }
 
+bool Object::loadFromXMLString(const String &xmlString) {
+
+	TiXmlDocument doc;
+	doc.Parse((const char*)xmlString.c_str(), 0, TIXML_ENCODING_UTF8);
+
+	if(doc.Error()) {
+		Logger::log("Error loading xml string: %s\n", doc.ErrorDesc());
+		return false;
+	}
+	
+	TiXmlElement *rootElement = doc.RootElement();
+	createFromXMLElement(rootElement, &root);
+	return true;	
+}
+		
 bool Object::loadFromXML(const String& fileName) {
 
 	TiXmlDocument doc(fileName.c_str());

+ 5 - 3
Core/Contents/Source/PolyScreenSprite.cpp

@@ -107,10 +107,12 @@ void ScreenSprite::Update() {
 	if(elapsed > currentAnimation->speed) {
 	currentFrame++;
 	if(currentFrame >= currentAnimation->numFrames) {
-		if(playingOnce)
-			return;
-		else
+		if(playingOnce) {
+			dispatchEvent(new Event(), Event::COMPLETE_EVENT);
+			return;			
+		} else {
 			currentFrame = 0;
+		}
 	}
 	
 	Number xOffset = currentAnimation->framesOffsets[currentFrame].x;

+ 9 - 0
Core/Contents/Source/PolyString.cpp

@@ -51,6 +51,15 @@ String::String(const wstring& str) {
 	wstrToUtf8(contents, str);
 }
 
+String::String(const wchar_t wchar) {
+	std::wstring srcstr;
+	srcstr = srcstr + wchar;
+	std::string newstr;
+	wstrToUtf8(newstr, srcstr);
+	contents = contents + newstr;
+}
+
+
 String::~String() {
 	
 }

+ 5 - 0
Modules/Contents/2DPhysics/Include/PolyPhysicsScreen.h

@@ -344,6 +344,11 @@ public:
 	* @return If there specified entity overlaps the specified position, this returns true.
 	*/ 														
 	bool testEntityAtPosition(ScreenEntity *ent, Number x, Number y);
+
+	/**
+	* Tests collision between two entities
+	*/
+	bool testEntityCollision(ScreenEntity *ent1, ScreenEntity *ent2);
 	
 	void Shutdown();
 	

+ 30 - 5
Modules/Contents/2DPhysics/Source/PolyPhysicsScreen.cpp

@@ -46,9 +46,9 @@ ScreenEntity *PhysicsScreenEvent::getSecondEntity() {
 
 void PhysicsScreen::BeginContact (b2Contact *contact) {
 
-	if(!contact->GetFixtureA()->IsSensor() && !contact->GetFixtureB()->IsSensor()) {
-		return;
-	}
+//	if(!contact->GetFixtureA()->IsSensor() && !contact->GetFixtureB()->IsSensor()) {
+//		return;
+//	}
 	
 	PhysicsScreenEvent *newEvent = new PhysicsScreenEvent();
 	newEvent->entity1 = getPhysicsEntityByFixture(contact->GetFixtureA())->getScreenEntity();
@@ -78,6 +78,8 @@ void PhysicsScreen::BeginContact (b2Contact *contact) {
 	newEvent->frictionStrength = 0;
 
 	dispatchEvent(newEvent, PhysicsScreenEvent::EVENT_NEW_SHAPE_COLLISION);
+	
+	contacts.push_back(contact);
 }
 
 void PhysicsScreen::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
@@ -122,11 +124,34 @@ void PhysicsScreen::PostSolve(b2Contact* contact, const b2ContactImpulse* impuls
 void PhysicsScreen::EndContact (b2Contact *contact) {
 	PhysicsScreenEvent *newEvent = new PhysicsScreenEvent();
 	newEvent->entity1 = getPhysicsEntityByFixture(contact->GetFixtureA())->getScreenEntity();
-	newEvent->entity2 = getPhysicsEntityByFixture(contact->GetFixtureB())->getScreenEntity();		
-		
+	newEvent->entity2 = getPhysicsEntityByFixture(contact->GetFixtureB())->getScreenEntity();
+	
+	for(int i=0; i < contacts.size(); i++) {
+		if(contacts[i] == contact) {
+			contacts.erase(contacts.begin()+i);
+			break;
+		}
+	}
+	
 	dispatchEvent(newEvent, PhysicsScreenEvent::EVENT_END_SHAPE_COLLISION);
 }
 
+bool PhysicsScreen::testEntityCollision(ScreenEntity *ent1, ScreenEntity *ent2) {
+	PhysicsScreenEntity *pEnt1 = getPhysicsByScreenEntity(ent1);
+	PhysicsScreenEntity *pEnt2 = getPhysicsByScreenEntity(ent2);	
+	if(pEnt1 == NULL || pEnt2 == NULL)
+		return false;
+	
+	for(int i=0; i < contacts.size(); i++) {
+		ScreenEntity *cEnt1 = getPhysicsEntityByFixture(contacts[i]->GetFixtureA())->getScreenEntity();
+		ScreenEntity *cEnt2 = getPhysicsEntityByFixture(contacts[i]->GetFixtureB())->getScreenEntity();
+		
+		if((cEnt1 == ent1 && cEnt2 == ent2) || (cEnt1 == ent2 && cEnt2 == ent1)) {
+			return true;
+		}
+	}
+	return false;
+}
 
 PhysicsScreen::PhysicsScreen() : Screen() {
 	init(10.0f, 1.0f/60.0f,10,Vector2(0.0f, 10.0f));

+ 4 - 2
Modules/Contents/2DPhysics/Source/PolyPhysicsScreenEntity.cpp

@@ -35,13 +35,15 @@ PhysicsScreenEntity::PhysicsScreenEntity(ScreenEntity *entity, b2World *world, N
 	
 	this->worldScale = worldScale;
 	
+	Vector3 entityScale = entity->getScale();
+	
 	screenEntity = entity;
 	
 	bodyDef = new b2BodyDef();
 	bodyDef->position.Set(screenEntity->getPosition().x/worldScale, screenEntity->getPosition().y/worldScale);
 	bodyDef->angle = screenEntity->getRotation()*(PI/180.0f);	
 	bodyDef->bullet = isSensor;	
-	bodyDef->fixedRotation = fixedRotation;
+	bodyDef->fixedRotation = fixedRotation;	
 	
 	if(isStatic) {
 		bodyDef->type = b2_staticBody;		
@@ -87,7 +89,7 @@ PhysicsScreenEntity::PhysicsScreenEntity(ScreenEntity *entity, b2World *world, N
 		case ENTITY_RECT: 
 		{
 			b2PolygonShape *b2shape = new b2PolygonShape;			
-			b2shape->SetAsBox(screenEntity->getWidth()/(worldScale*2.0f), screenEntity->getHeight()/(worldScale*2.0f));
+			b2shape->SetAsBox(screenEntity->getWidth()/(worldScale*2.0f) * entityScale.x, screenEntity->getHeight()/(worldScale*2.0f) * entityScale.y);
 			fDef.shape = b2shape;						
 			shape = b2shape;
 		}