diff --git a/KREngine/KREngine.xcodeproj/project.pbxproj b/KREngine/KREngine.xcodeproj/project.pbxproj index 817a5d5..2b5d3ed 100644 --- a/KREngine/KREngine.xcodeproj/project.pbxproj +++ b/KREngine/KREngine.xcodeproj/project.pbxproj @@ -26,14 +26,17 @@ E461A166152E56C000F2044A /* KRSpotLight.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E461A164152E56C000F2044A /* KRSpotLight.cpp */; }; E461A168152E570700F2044A /* KRSpotLight.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A167152E570500F2044A /* KRSpotLight.h */; }; E461A169152E570700F2044A /* KRSpotLight.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A167152E570500F2044A /* KRSpotLight.h */; settings = {ATTRIBUTES = (Public, ); }; }; - E461A16B152E575800F2044A /* KRLightManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A16A152E575700F2044A /* KRLightManager.h */; }; - E461A16C152E575800F2044A /* KRLightManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A16A152E575700F2044A /* KRLightManager.h */; }; - E461A16E152E576700F2044A /* KRLightManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E461A16D152E576700F2044A /* KRLightManager.cpp */; }; - E461A16F152E576700F2044A /* KRLightManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E461A16D152E576700F2044A /* KRLightManager.cpp */; }; E461A175152E5C4800F2044A /* KRLight.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E461A155152E54F700F2044A /* KRLight.cpp */; }; E461A176152E5C5600F2044A /* KRPointLight.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A157152E555400F2044A /* KRPointLight.h */; settings = {ATTRIBUTES = (Public, ); }; }; E461A177152E5C6600F2044A /* KRMat4.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017613C99BDC0098455B /* KRMat4.h */; settings = {ATTRIBUTES = (Public, ); }; }; E461A17A152E5C9100F2044A /* KRMat4.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017713C99BDC0098455B /* KRMat4.cpp */; }; + E46C214415364BC8009CABF3 /* tinyxml2_readme.txt in Resources */ = {isa = PBXBuildFile; fileRef = E46C214115364BC8009CABF3 /* tinyxml2_readme.txt */; }; + E46C214515364BC8009CABF3 /* tinyxml2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214215364BC8009CABF3 /* tinyxml2.cpp */; }; + E46C214615364BC8009CABF3 /* tinyxml2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214215364BC8009CABF3 /* tinyxml2.cpp */; }; + E46C214715364BC8009CABF3 /* tinyxml2.h in Headers */ = {isa = PBXBuildFile; fileRef = E46C214315364BC8009CABF3 /* tinyxml2.h */; }; + E46C214815364BC8009CABF3 /* tinyxml2.h in Headers */ = {isa = PBXBuildFile; fileRef = E46C214315364BC8009CABF3 /* tinyxml2.h */; }; + E46C214B15364DEC009CABF3 /* KRSceneManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214A15364DEC009CABF3 /* KRSceneManager.cpp */; }; + E46C214C15364DEC009CABF3 /* KRSceneManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E46C214A15364DEC009CABF3 /* KRSceneManager.cpp */; }; E46DBE7B1512ACE600D59F86 /* KRMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E49E79FE1512615F009CF99C /* KRMesh.cpp */; }; E46DBE7F1512AF0200D59F86 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E46DBE7D1512AD4900D59F86 /* OpenGL.framework */; }; E46DBE801512AF4600D59F86 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E491016413C99B9E0098455B /* Foundation.framework */; }; @@ -88,6 +91,28 @@ E4BBBBA71512A6DC00F43B5B /* KRVector3.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017F13C99BDC0098455B /* KRVector3.cpp */; }; E4D0683F1512A790005FFBEB /* KRVector3.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017E13C99BDC0098455B /* KRVector3.h */; settings = {ATTRIBUTES = (Public, ); }; }; E4F711A51512BB56007EE923 /* libfbxsdk-2012.2-static.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E4F711A41512BB56007EE923 /* libfbxsdk-2012.2-static.a */; }; + E4F975321536220900FD60B2 /* KRNode.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F975311536220900FD60B2 /* KRNode.h */; }; + E4F975331536220900FD60B2 /* KRNode.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F975311536220900FD60B2 /* KRNode.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F975361536221C00FD60B2 /* KRNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4F975351536221C00FD60B2 /* KRNode.cpp */; }; + E4F975371536221C00FD60B2 /* KRNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4F975351536221C00FD60B2 /* KRNode.cpp */; }; + E4F9754015362CD400FD60B2 /* KRScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAE81435585A00A668C4 /* KRScene.cpp */; }; + E4F9754115362CD900FD60B2 /* KRScene.h in Headers */ = {isa = PBXBuildFile; fileRef = E414BAE6143557D200A668C4 /* KRScene.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F9754215362D0D00FD60B2 /* KRInstance.h in Headers */ = {isa = PBXBuildFile; fileRef = E414BAE11435557300A668C4 /* KRInstance.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F9754315362D0F00FD60B2 /* KRInstance.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAE41435558800A668C4 /* KRInstance.cpp */; }; + E4F9754415362D2900FD60B2 /* KRBoundingVolume.h in Headers */ = {isa = PBXBuildFile; fileRef = E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F9754515362D2C00FD60B2 /* KRBoundingVolume.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */; }; + E4F975461536327C00FD60B2 /* KRModelManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E491018313C99BDC0098455B /* KRModelManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F975491536329E00FD60B2 /* KRTextureManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E491018513C99BDC0098455B /* KRTextureManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F9754A153632AA00FD60B2 /* KRModelManager.cpp in Headers */ = {isa = PBXBuildFile; fileRef = E491018213C99BDC0098455B /* KRModelManager.cpp */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F9754B153632D800FD60B2 /* KRModelManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491018213C99BDC0098455B /* KRModelManager.cpp */; }; + E4F9754C153632F000FD60B2 /* KRCamera.h in Headers */ = {isa = PBXBuildFile; fileRef = E48B3CBC14393DF5000C50E2 /* KRCamera.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F9754E1536331D00FD60B2 /* KRTextureManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491018013C99BDC0098455B /* KRTextureManager.cpp */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F9754F1536333200FD60B2 /* KRModel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017913C99BDC0098455B /* KRModel.cpp */; }; + E4F975501536333500FD60B2 /* KRModel.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017A13C99BDC0098455B /* KRModel.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F97551153633E200FD60B2 /* KRMaterialManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017B13C99BDC0098455B /* KRMaterialManager.cpp */; }; + E4F97552153633EF00FD60B2 /* KRMaterialManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E491018413C99BDC0098455B /* KRMaterialManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E4F975531536340000FD60B2 /* KRTexture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491018113C99BDC0098455B /* KRTexture.cpp */; }; + E4F975541536340400FD60B2 /* KRTexture.h in Headers */ = {isa = PBXBuildFile; fileRef = E491018613C99BDC0098455B /* KRTexture.h */; settings = {ATTRIBUTES = (Public, ); }; }; /* End PBXBuildFile section */ /* Begin PBXBuildRule section */ @@ -123,8 +148,11 @@ E461A15E152E565700F2044A /* KRDirectionalLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRDirectionalLight.cpp; path = Classes/KRDirectionalLight.cpp; sourceTree = ""; }; E461A164152E56C000F2044A /* KRSpotLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRSpotLight.cpp; path = Classes/KRSpotLight.cpp; sourceTree = ""; }; E461A167152E570500F2044A /* KRSpotLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRSpotLight.h; path = Classes/KRSpotLight.h; sourceTree = ""; }; - E461A16A152E575700F2044A /* KRLightManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRLightManager.h; path = Classes/KRLightManager.h; sourceTree = ""; }; - E461A16D152E576700F2044A /* KRLightManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRLightManager.cpp; path = Classes/KRLightManager.cpp; sourceTree = ""; }; + E46C214115364BC8009CABF3 /* tinyxml2_readme.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = tinyxml2_readme.txt; path = KREngine/Classes/tinyxml2_readme.txt; sourceTree = ""; }; + E46C214215364BC8009CABF3 /* tinyxml2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = tinyxml2.cpp; path = KREngine/Classes/tinyxml2.cpp; sourceTree = ""; }; + E46C214315364BC8009CABF3 /* tinyxml2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = tinyxml2.h; path = KREngine/Classes/tinyxml2.h; sourceTree = ""; }; + E46C214915364DDB009CABF3 /* KRSceneManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = KRSceneManager.h; path = Classes/KRSceneManager.h; sourceTree = ""; }; + E46C214A15364DEC009CABF3 /* KRSceneManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRSceneManager.cpp; path = Classes/KRSceneManager.cpp; sourceTree = ""; }; E46DBE7D1512AD4900D59F86 /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/OpenGL.framework; sourceTree = DEVELOPER_DIR; }; E46DBE841512B9E200D59F86 /* KREngine-common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "KREngine-common.h"; path = "Classes/KREngine-common.h"; sourceTree = ""; }; E47C25A113F4F65A00FF4370 /* KRShaderManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRShaderManager.h; path = Classes/KRShaderManager.h; sourceTree = ""; }; @@ -176,6 +204,8 @@ E4BBBB981512A47500F43B5B /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/CoreData.framework; sourceTree = DEVELOPER_DIR; }; E4BBBB9A1512A48200F43B5B /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; }; E4F711A41512BB56007EE923 /* libfbxsdk-2012.2-static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libfbxsdk-2012.2-static.a"; path = "../../../../../../../../Applications/Autodesk/FBXSDK20122/lib/gcc4/ub/libfbxsdk-2012.2-static.a"; sourceTree = ""; }; + E4F975311536220900FD60B2 /* KRNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRNode.h; path = Classes/KRNode.h; sourceTree = ""; }; + E4F975351536221C00FD60B2 /* KRNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRNode.cpp; path = Classes/KRNode.cpp; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -222,9 +252,9 @@ E461A170152E598200F2044A /* Resources */ = { isa = PBXGroup; children = ( + E4F9755615363AA500FD60B2 /* Scene */, E461A174152E5A1300F2044A /* Models */, E461A172152E59BC00F2044A /* Materials */, - E461A171152E599E00F2044A /* Lights */, E497B949151BCEE900D3DC67 /* KRResource.h */, E497B94C151BCF2500D3DC67 /* KRResource.cpp */, E497B94F151BD2CE00D3DC67 /* KRResource+obj.cpp */, @@ -281,6 +311,16 @@ name = Models; sourceTree = ""; }; + E46C214015364BB8009CABF3 /* tinyxml2 */ = { + isa = PBXGroup; + children = ( + E46C214115364BC8009CABF3 /* tinyxml2_readme.txt */, + E46C214215364BC8009CABF3 /* tinyxml2.cpp */, + E46C214315364BC8009CABF3 /* tinyxml2.h */, + ); + name = tinyxml2; + sourceTree = ""; + }; E491015613C99B9D0098455B = { isa = PBXGroup; children = ( @@ -331,6 +371,7 @@ E491016E13C99BAE0098455B /* Classes */ = { isa = PBXGroup; children = ( + E4F9753815362A5200FD60B2 /* 3rdparty */, E461A173152E59DF00F2044A /* Math */, E461A170152E598200F2044A /* Resources */, E491016F13C99BDC0098455B /* KREngine.mm */, @@ -349,15 +390,11 @@ E47C25A413F4F66F00FF4370 /* KRShader.h */, E47C25A613F4F6AB00FF4370 /* KRShaderManager.cpp */, E47C25A813F4F6DD00FF4370 /* KRShader.cpp */, - E414BAE11435557300A668C4 /* KRInstance.h */, - E414BAE41435558800A668C4 /* KRInstance.cpp */, - E414BAE6143557D200A668C4 /* KRScene.h */, - E414BAE81435585A00A668C4 /* KRScene.cpp */, E48B3CBC14393DF5000C50E2 /* KRCamera.h */, E48B3CBF14393E2F000C50E2 /* KRCamera.cpp */, E46DBE841512B9E200D59F86 /* KREngine-common.h */, - E461A16A152E575700F2044A /* KRLightManager.h */, - E461A16D152E576700F2044A /* KRLightManager.cpp */, + E46C214915364DDB009CABF3 /* KRSceneManager.h */, + E46C214A15364DEC009CABF3 /* KRSceneManager.cpp */, ); name = Classes; sourceTree = ""; @@ -397,6 +434,29 @@ name = "OSX Frameworks"; sourceTree = ""; }; + E4F9753815362A5200FD60B2 /* 3rdparty */ = { + isa = PBXGroup; + children = ( + E46C214015364BB8009CABF3 /* tinyxml2 */, + ); + name = 3rdparty; + path = ..; + sourceTree = ""; + }; + E4F9755615363AA500FD60B2 /* Scene */ = { + isa = PBXGroup; + children = ( + E414BAE11435557300A668C4 /* KRInstance.h */, + E414BAE41435558800A668C4 /* KRInstance.cpp */, + E461A171152E599E00F2044A /* Lights */, + E4F975311536220900FD60B2 /* KRNode.h */, + E4F975351536221C00FD60B2 /* KRNode.cpp */, + E414BAE6143557D200A668C4 /* KRScene.h */, + E414BAE81435585A00A668C4 /* KRScene.cpp */, + ); + name = Scene; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -423,7 +483,8 @@ E461A152152E54B500F2044A /* KRLight.h in Headers */, E461A15C152E563100F2044A /* KRDirectionalLight.h in Headers */, E461A168152E570700F2044A /* KRSpotLight.h in Headers */, - E461A16B152E575800F2044A /* KRLightManager.h in Headers */, + E4F975321536220900FD60B2 /* KRNode.h in Headers */, + E46C214715364BC8009CABF3 /* tinyxml2.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -434,15 +495,26 @@ E497B948151BB89D00D3DC67 /* KRVector2.h in Headers */, E4D0683F1512A790005FFBEB /* KRVector3.h in Headers */, E461A177152E5C6600F2044A /* KRMat4.h in Headers */, + E4F9754415362D2900FD60B2 /* KRBoundingVolume.h in Headers */, + E4F975461536327C00FD60B2 /* KRModelManager.h in Headers */, E497B94B151BCEE900D3DC67 /* KRResource.h in Headers */, + E4F975331536220900FD60B2 /* KRNode.h in Headers */, E4B2A4391523B027004CB0EC /* KRMaterial.h in Headers */, E46DBE811512AFE600D59F86 /* KRMesh.h in Headers */, + E4F9754115362CD900FD60B2 /* KRScene.h in Headers */, + E4F9754A153632AA00FD60B2 /* KRModelManager.cpp in Headers */, + E4F9754215362D0D00FD60B2 /* KRInstance.h in Headers */, + E4F975491536329E00FD60B2 /* KRTextureManager.h in Headers */, E46DBE851512B9FA00D59F86 /* KREngine-common.h in Headers */, + E4F9754C153632F000FD60B2 /* KRCamera.h in Headers */, + E4F975501536333500FD60B2 /* KRModel.h in Headers */, E461A153152E54B500F2044A /* KRLight.h in Headers */, + E4F97552153633EF00FD60B2 /* KRMaterialManager.h in Headers */, E461A176152E5C5600F2044A /* KRPointLight.h in Headers */, + E4F975541536340400FD60B2 /* KRTexture.h in Headers */, E461A15D152E563100F2044A /* KRDirectionalLight.h in Headers */, E461A169152E570700F2044A /* KRSpotLight.h in Headers */, - E461A16C152E575800F2044A /* KRLightManager.h in Headers */, + E46C214815364BC8009CABF3 /* tinyxml2.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -518,6 +590,7 @@ buildActionMask = 2147483647; files = ( E4BBBB8A1512A40300F43B5B /* InfoPlist.strings in Resources */, + E46C214415364BC8009CABF3 /* tinyxml2_readme.txt in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -551,7 +624,9 @@ E461A159152E557E00F2044A /* KRPointLight.cpp in Sources */, E461A15F152E565700F2044A /* KRDirectionalLight.cpp in Sources */, E461A165152E56C000F2044A /* KRSpotLight.cpp in Sources */, - E461A16E152E576700F2044A /* KRLightManager.cpp in Sources */, + E4F975361536221C00FD60B2 /* KRNode.cpp in Sources */, + E46C214515364BC8009CABF3 /* tinyxml2.cpp in Sources */, + E46C214B15364DEC009CABF3 /* KRSceneManager.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -569,10 +644,20 @@ E497B94E151BCF2500D3DC67 /* KRResource.cpp in Sources */, E497B951151BD2CE00D3DC67 /* KRResource+obj.cpp in Sources */, E497B954151BEDA600D3DC67 /* KRResource+fbx.cpp in Sources */, + E4F97551153633E200FD60B2 /* KRMaterialManager.cpp in Sources */, E461A15A152E557E00F2044A /* KRPointLight.cpp in Sources */, + E4F9754F1536333200FD60B2 /* KRModel.cpp in Sources */, + E4F9754B153632D800FD60B2 /* KRModelManager.cpp in Sources */, E461A160152E565700F2044A /* KRDirectionalLight.cpp in Sources */, + E4F9754515362D2C00FD60B2 /* KRBoundingVolume.cpp in Sources */, + E4F975531536340000FD60B2 /* KRTexture.cpp in Sources */, + E4F9754015362CD400FD60B2 /* KRScene.cpp in Sources */, E461A166152E56C000F2044A /* KRSpotLight.cpp in Sources */, - E461A16F152E576700F2044A /* KRLightManager.cpp in Sources */, + E4F9754315362D0F00FD60B2 /* KRInstance.cpp in Sources */, + E4F975371536221C00FD60B2 /* KRNode.cpp in Sources */, + E4F9754E1536331D00FD60B2 /* KRTextureManager.cpp in Sources */, + E46C214615364BC8009CABF3 /* tinyxml2.cpp in Sources */, + E46C214C15364DEC009CABF3 /* KRSceneManager.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/KREngine/KREngine/Classes/KRBoundingVolume.cpp b/KREngine/KREngine/Classes/KRBoundingVolume.cpp index e8b9b4f..52f7e80 100644 --- a/KREngine/KREngine/Classes/KRBoundingVolume.cpp +++ b/KREngine/KREngine/Classes/KRBoundingVolume.cpp @@ -193,8 +193,8 @@ bool KRBoundingVolume::test_intersect(const KRBoundingVolume &p) const { } -KRMat4 KRBoundingVolume::calcShadowProj(KRScene *pScene, GLfloat sun_yaw, GLfloat sun_pitch) const { - KRBoundingVolume sceneVolume = pScene->getExtents(); +KRMat4 KRBoundingVolume::calcShadowProj(KRScene *pScene, KRModelManager *pModelManager, GLfloat sun_yaw, GLfloat sun_pitch) const { + KRBoundingVolume sceneVolume = pScene->getExtents(pModelManager); KRMat4 shadowvp; shadowvp.rotate(sun_pitch, X_AXIS); diff --git a/KREngine/KREngine/Classes/KRBoundingVolume.h b/KREngine/KREngine/Classes/KRBoundingVolume.h index 40204a3..adf2899 100644 --- a/KREngine/KREngine/Classes/KRBoundingVolume.h +++ b/KREngine/KREngine/Classes/KRBoundingVolume.h @@ -38,6 +38,7 @@ #import "KRVector3.h" #import "KRMat4.h" #import "KRScene.h" +#import "KRModelManager.h" class KRScene; @@ -54,7 +55,7 @@ public: KRBoundingVolume get_union(const KRBoundingVolume &p) const; bool test_intersect(const KRBoundingVolume &p) const; - KRMat4 calcShadowProj(KRScene *pScene, GLfloat sun_yaw, GLfloat sun_pitch) const; + KRMat4 calcShadowProj(KRScene *pScene, KRModelManager *pModelManager, GLfloat sun_yaw, GLfloat sun_pitch) const; private: KRVector3 m_vertices[8]; }; diff --git a/KREngine/KREngine/Classes/KRDirectionalLight.cpp b/KREngine/KREngine/Classes/KRDirectionalLight.cpp index b878400..c19b423 100644 --- a/KREngine/KREngine/Classes/KRDirectionalLight.cpp +++ b/KREngine/KREngine/Classes/KRDirectionalLight.cpp @@ -20,7 +20,6 @@ KRDirectionalLight::~KRDirectionalLight() } -bool KRDirectionalLight::save(const std::string& path) -{ - return true; +std::string KRDirectionalLight::getElementName() { + return "directional_light"; } diff --git a/KREngine/KREngine/Classes/KRDirectionalLight.h b/KREngine/KREngine/Classes/KRDirectionalLight.h index effdafe..d8963b6 100644 --- a/KREngine/KREngine/Classes/KRDirectionalLight.h +++ b/KREngine/KREngine/Classes/KRDirectionalLight.h @@ -15,9 +15,9 @@ class KRDirectionalLight : public KRLight { public: KRDirectionalLight(std::string name); - ~KRDirectionalLight(); + virtual ~KRDirectionalLight(); - virtual bool save(const std::string& path); + virtual std::string getElementName(); }; diff --git a/KREngine/KREngine/Classes/KREngine.h b/KREngine/KREngine/Classes/KREngine.h index 6cc0bd9..de645f5 100644 --- a/KREngine/KREngine/Classes/KREngine.h +++ b/KREngine/KREngine/Classes/KREngine.h @@ -39,7 +39,7 @@ #import "KRMaterialManager.h" #import "KRShaderManager.h" #import "KRModelManager.h" -#import "KRLightManager.h" +#import "KRSceneManager.h" #import "KRCamera.h" #import "KREngine-common.h" @@ -81,11 +81,11 @@ typedef enum KREngineParameterType {KRENGINE_PARAMETER_INT, KRENGINE_PARAMETER_F GLuint m_postShaderProgram; GLuint m_shadowShaderProgram; + KRSceneManager *m_pSceneManager; KRTextureManager *m_pTextureManager; KRMaterialManager *m_pMaterialManager; KRShaderManager *m_pShaderManager; KRModelManager *m_pModelManager; - KRLightManager *m_pLightManager; int m_iFrame; @@ -103,6 +103,7 @@ typedef enum KREngineParameterType {KRENGINE_PARAMETER_INT, KRENGINE_PARAMETER_F - (void)renderShadowBufferNumber: (int)iShadow ForScene: (KRScene *)pScene; - (void)renderScene: (KRScene *)pScene WithViewMatrix: (KRMat4)viewMatrix LightDirection: (KRVector3)lightDirection CameraPosition: (KRVector3)cameraPosition; - (KRModelManager *)getModelManager; +- (KRSceneManager *)getSceneManager; - (void)invalidateShadowBuffers; - (void)allocateShadowBuffers; diff --git a/KREngine/KREngine/Classes/KREngine.mm b/KREngine/KREngine/Classes/KREngine.mm index 8f3a648..214454e 100644 --- a/KREngine/KREngine/Classes/KREngine.mm +++ b/KREngine/KREngine/Classes/KREngine.mm @@ -32,6 +32,7 @@ #import "KREngine.h" #import "KRVector3.h" #import "KRScene.h" +#import "KRSceneManager.h" #import #import @@ -82,7 +83,7 @@ double const PI = 3.141592653589793f; m_pTextureManager = new KRTextureManager(); m_pMaterialManager = new KRMaterialManager(m_pTextureManager, m_pShaderManager); m_pModelManager = new KRModelManager(m_pMaterialManager); - m_pLightManager = new KRLightManager(); + m_pSceneManager = new KRSceneManager(); if (![self createBuffers] || ![self loadShaders] /*|| ![self loadObjects]*/ ) { @@ -225,12 +226,12 @@ double const PI = 3.141592653589793f; KRMat4 newShadowMVP; if(shadowMaxDepths[m_cShadowBuffers - 1][iShadow] == 0.0) { - KRBoundingVolume ext = KRBoundingVolume(pScene->getExtents()); + KRBoundingVolume ext = KRBoundingVolume(pScene->getExtents(m_pModelManager)); - newShadowMVP = ext.calcShadowProj(pScene, sun_yaw, sun_pitch); + newShadowMVP = ext.calcShadowProj(pScene, m_pModelManager, sun_yaw, sun_pitch); } else { KRBoundingVolume frustrumSliceVolume = KRBoundingVolume(viewMatrix, m_camera.perspective_fov, m_camera.perspective_aspect, m_camera.perspective_nearz + (m_camera.perspective_farz - m_camera.perspective_nearz) * shadowMinDepths[m_cShadowBuffers - 1][iShadow], m_camera.perspective_nearz + (m_camera.perspective_farz - m_camera.perspective_nearz) * shadowMaxDepths[m_cShadowBuffers - 1][iShadow]); - newShadowMVP = frustrumSliceVolume.calcShadowProj(pScene, sun_yaw, sun_pitch); + newShadowMVP = frustrumSliceVolume.calcShadowProj(pScene, m_pModelManager, sun_yaw, sun_pitch); } if(!(shadowmvpmatrix[iShadow] == newShadowMVP)) { @@ -330,7 +331,7 @@ double const PI = 3.141592653589793f; KRVector3 cameraPosition; KRVector3 lightDirection; KRBoundingVolume shadowVolume = KRBoundingVolume(vertices); - pScene->render(&m_camera, shadowVolume, m_pMaterialManager, true, shadowmvpmatrix[iShadow], cameraPosition, lightDirection, shadowmvpmatrix, NULL, 0, m_pShaderManager, m_pTextureManager); + pScene->render(&m_camera, m_pModelManager, shadowVolume, m_pMaterialManager, true, shadowmvpmatrix[iShadow], cameraPosition, lightDirection, shadowmvpmatrix, NULL, 0, m_pShaderManager, m_pTextureManager); glViewport(0, 0, 768, 1024); } @@ -356,7 +357,7 @@ double const PI = 3.141592653589793f; glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); KRBoundingVolume frustrumVolume = KRBoundingVolume(viewMatrix, m_camera.perspective_fov, m_camera.perspective_aspect, m_camera.perspective_nearz, m_camera.perspective_farz); - pScene -> render(&m_camera, frustrumVolume, m_pMaterialManager, false, viewMatrix, cameraPosition, lightDirection, shadowmvpmatrix, shadowDepthTexture, m_cShadowBuffers, m_pShaderManager, m_pTextureManager); + pScene -> render(&m_camera, m_pModelManager, frustrumVolume, m_pMaterialManager, false, viewMatrix, cameraPosition, lightDirection, shadowmvpmatrix, shadowDepthTexture, m_cShadowBuffers, m_pShaderManager, m_pTextureManager); } - (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file withOptions: (NSString *)options @@ -543,21 +544,27 @@ double const PI = 3.141592653589793f; if([path hasSuffix: @".krobject"]) { NSLog(@"object: %@", path); m_pModelManager->loadModel([name UTF8String], [path UTF8String]); + } else if([path hasSuffix: @".krscene"]) { + NSLog(@"scene: %@", path); + m_pSceneManager->loadScene([name UTF8String], [path UTF8String]); } else if([path hasSuffix: @".pvr"]) { NSLog(@"texture: %@", path); m_pTextureManager->loadTexture([name UTF8String], [path UTF8String]); } else if([path hasSuffix: @".mtl"]) { NSLog(@"material: %@", path); m_pMaterialManager->loadFile([path UTF8String]); - } else if([path hasSuffix: @".krlight"]) { - m_pLightManager->loadFile([path UTF8String]); } return TRUE; } - (void)dealloc -{ +{ + if(m_pSceneManager) { + delete m_pSceneManager; + m_pSceneManager = NULL; + } + if(m_pModelManager) { delete m_pModelManager; m_pModelManager = NULL; @@ -578,11 +585,6 @@ double const PI = 3.141592653589793f; m_pShaderManager = NULL; } - if(m_pLightManager) { - delete m_pLightManager; - m_pLightManager = NULL; - } - [self invalidatePostShader]; [self destroyBuffers]; [super dealloc]; @@ -1114,6 +1116,10 @@ double const PI = 3.141592653589793f; return m_pModelManager; } +- (KRSceneManager *)getSceneManager { + return m_pSceneManager; +} + - (void)invalidateShadowBuffers { for(int i=0; i < m_cShadowBuffers; i++) { shadowValid[i] = false; diff --git a/KREngine/KREngine/Classes/KRInstance.cpp b/KREngine/KREngine/Classes/KRInstance.cpp index f70b05c..45e931a 100644 --- a/KREngine/KREngine/Classes/KRInstance.cpp +++ b/KREngine/KREngine/Classes/KRInstance.cpp @@ -31,57 +31,85 @@ #include #import "KRInstance.h" +#include -KRInstance::KRInstance(KRModel *pModel, const KRMat4 modelMatrix, std::string shadow_map) { - m_pModel = pModel; +KRInstance::KRInstance(std::string instance_name, std::string model_name, const KRMat4 modelMatrix, std::string shadow_map) : KRNode(instance_name) { m_modelMatrix = modelMatrix; m_shadowMap = shadow_map; m_pShadowMap = NULL; + m_pModel = NULL; + m_model_name = model_name; } KRInstance::~KRInstance() { } +std::string KRInstance::getElementName() { + return "mesh"; +} + +tinyxml2::XMLElement *KRInstance::saveXML( tinyxml2::XMLNode *parent) +{ + tinyxml2::XMLElement *e = KRNode::saveXML(parent); + e->SetAttribute("lightmap", m_shadowMap.c_str()); + return e; +} + + KRMat4 &KRInstance::getModelMatrix() { return m_modelMatrix; } -KRModel *KRInstance::getModel() { - return m_pModel; -} -void KRInstance::render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager) { +#if TARGET_OS_IPHONE + +void KRInstance::render(KRCamera *pCamera, KRModelManager *pModelManager, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager) { - if(m_pShadowMap == NULL && m_shadowMap.size()) { - m_pShadowMap = pTextureManager->getTexture(m_shadowMap.c_str()); + if(m_pModel == NULL) { + m_pModel = pModelManager->getModel(m_model_name.c_str()); } - if(cShadowBuffers == 0 && m_pShadowMap && pCamera->bEnableShadowMap && !bRenderShadowMap) { - int iTextureName = m_pShadowMap->getName(); - glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, iTextureName); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + if(m_pModel != NULL) { + + if(m_pShadowMap == NULL && m_shadowMap.size()) { + m_pShadowMap = pTextureManager->getTexture(m_shadowMap.c_str()); + } + + if(cShadowBuffers == 0 && m_pShadowMap && pCamera->bEnableShadowMap && !bRenderShadowMap) { + int iTextureName = m_pShadowMap->getName(); + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, iTextureName); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + + KRMat4 projectionMatrix; + if(!bRenderShadowMap) { + projectionMatrix = pCamera->getProjectionMatrix(); + } + KRMat4 mvpmatrix = m_modelMatrix * viewMatrix * projectionMatrix; + + // Transform location of camera to object space for calculation of specular halfVec + KRMat4 inverseModelMatrix = m_modelMatrix; + inverseModelMatrix.invert(); + KRVector3 cameraPosObject = inverseModelMatrix.dot(cameraPosition); + KRVector3 lightDirObject = inverseModelMatrix.dot(lightDirection); + + m_pModel->render(pCamera, pMaterialManager, bRenderShadowMap, mvpmatrix, cameraPosObject, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, pShaderManager, pTextureManager, m_pShadowMap); + } - KRMat4 projectionMatrix; - if(!bRenderShadowMap) { - projectionMatrix = pCamera->getProjectionMatrix(); - } - KRMat4 mvpmatrix = m_modelMatrix * viewMatrix * projectionMatrix; - - // Transform location of camera to object space for calculation of specular halfVec - KRMat4 inverseModelMatrix = m_modelMatrix; - inverseModelMatrix.invert(); - KRVector3 cameraPosObject = inverseModelMatrix.dot(cameraPosition); - KRVector3 lightDirObject = inverseModelMatrix.dot(lightDirection); - - m_pModel->render(pCamera, pMaterialManager, bRenderShadowMap, mvpmatrix, cameraPosObject, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, pShaderManager, pTextureManager, m_pShadowMap); - } -KRBoundingVolume KRInstance::getExtents() { +#endif + +KRBoundingVolume KRInstance::getExtents(KRModelManager *pModelManager) { + if(m_pModel == NULL) { + m_pModel = pModelManager->getModel(m_model_name.c_str()); + } + assert(m_pModel != NULL); + KRMesh *pMesh = m_pModel->getMesh(); return KRBoundingVolume(KRVector3(pMesh->getMinX(), pMesh->getMinY(), pMesh->getMinZ()), KRVector3(pMesh->getMaxX(), pMesh->getMaxY(), pMesh->getMaxZ()), m_modelMatrix); } \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRInstance.h b/KREngine/KREngine/Classes/KRInstance.h index a926dc3..1de1ce2 100644 --- a/KREngine/KREngine/Classes/KRInstance.h +++ b/KREngine/KREngine/Classes/KRInstance.h @@ -29,37 +29,49 @@ // or implied, of Kearwood Gilbert. // -#import "KRModel.h" -#import "KRMat4.h" -#import "KRVector3.h" -#import "KRBoundingVolume.h" -#import "KRInstance.h" -#import "KRCamera.h" + #import "KREngine-common.h" #ifndef KRINSTANCE_H #define KRINSTANCE_H +#import "KRModel.h" +#import "KRMat4.h" +#import "KRVector3.h" +#import "KRBoundingVolume.h" +#import "KRInstance.h" +#import "KRCamera.h" +#import "KRModelManager.h" +#import "KRNode.h" + class KRBoundingVolume; -class KRInstance { +class KRInstance : public KRNode { public: - KRInstance(KRModel *pModel, const KRMat4 modelMatrix, std::string shadow_map); - ~KRInstance(); - void render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager); + KRInstance(std::string instance_name, std::string model_name, const KRMat4 modelMatrix, std::string shadow_map); + virtual ~KRInstance(); - KRBoundingVolume getExtents(); + virtual std::string getElementName(); + virtual tinyxml2::XMLElement *saveXML( tinyxml2::XMLNode *parent); + +#if TARGET_OS_IPHONE + + void render(KRCamera *pCamera, KRModelManager *pModelManager, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager); + +#endif + + KRBoundingVolume getExtents(KRModelManager *pModelManager); KRMat4 &getModelMatrix(); - KRModel *getModel(); private: KRModel *m_pModel; KRMat4 m_modelMatrix; KRTexture *m_pShadowMap; std::string m_shadowMap; + std::string m_model_name; }; diff --git a/KREngine/KREngine/Classes/KRLight.cpp b/KREngine/KREngine/Classes/KRLight.cpp index a98043b..871aa4c 100644 --- a/KREngine/KREngine/Classes/KRLight.cpp +++ b/KREngine/KREngine/Classes/KRLight.cpp @@ -10,9 +10,9 @@ #import "KRLight.h" -KRLight::KRLight(std::string name) : KRResource(name) +KRLight::KRLight(std::string name) : KRNode(name) { - + m_intensity = 1.0f; } KRLight::~KRLight() @@ -20,7 +20,36 @@ KRLight::~KRLight() } -std::string KRLight::getExtension() +tinyxml2::XMLElement *KRLight::saveXML( tinyxml2::XMLNode *parent) { - return "krlight"; -} \ No newline at end of file + tinyxml2::XMLElement *e = KRNode::saveXML(parent); + e->SetAttribute("intensity", m_intensity); + e->SetAttribute("color_r", m_color.x); + e->SetAttribute("color_g", m_color.y); + e->SetAttribute("color_b", m_color.z); + e->SetAttribute("decay_start", m_decayStart); + return e; +} + +void KRLight::setIntensity(float intensity) { + m_intensity = intensity; +} +float KRLight::getIntensity() { + return m_intensity; +} + +const KRVector3 &KRLight::getColor() { + return m_color; +} + +void KRLight::setColor(const KRVector3 &color) { + m_color = color; +} + +void KRLight::setDecayStart(float decayStart) { + m_decayStart = decayStart; +} + +float KRLight::getDecayStart() { + return m_decayStart; +} diff --git a/KREngine/KREngine/Classes/KRLight.h b/KREngine/KREngine/Classes/KRLight.h index 3583ec8..e6edaef 100644 --- a/KREngine/KREngine/Classes/KRLight.h +++ b/KREngine/KREngine/Classes/KRLight.h @@ -10,15 +10,27 @@ #define KREngine_KRLight_h #import "KRResource.h" +#import "KRNode.h" -class KRLight : public KRResource { +class KRLight : public KRNode { public: - ~KRLight(); + virtual ~KRLight(); + virtual std::string getElementName() = 0; + virtual tinyxml2::XMLElement *saveXML( tinyxml2::XMLNode *parent); + + void setIntensity(float intensity); + float getIntensity(); + void setDecayStart(float decayStart); + float getDecayStart(); + const KRVector3 &getColor(); + void setColor(const KRVector3 &color); - virtual std::string getExtension(); - virtual bool save(const std::string& path) = 0; protected: KRLight(std::string name); + + float m_intensity; + float m_decayStart; + KRVector3 m_color; }; #endif diff --git a/KREngine/KREngine/Classes/KRLightManager.cpp b/KREngine/KREngine/Classes/KRLightManager.cpp deleted file mode 100644 index f97597f..0000000 --- a/KREngine/KREngine/Classes/KRLightManager.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// -// KRLightManager.cpp -// KREngine -// -// Created by Kearwood Gilbert on 12-04-05. -// Copyright (c) 2012 Kearwood Software. All rights reserved. -// - -#include - -#include "KRLightManager.h" - -KRLightManager::KRLightManager() -{ - -} - -KRLightManager::~KRLightManager() -{ - -} - -bool KRLightManager::loadFile(const char *szPath) -{ - return true; -} diff --git a/KREngine/KREngine/Classes/KRLightManager.h b/KREngine/KREngine/Classes/KRLightManager.h deleted file mode 100644 index c077df0..0000000 --- a/KREngine/KREngine/Classes/KRLightManager.h +++ /dev/null @@ -1,20 +0,0 @@ -// -// KRLightManager.h -// KREngine -// -// Created by Kearwood Gilbert on 12-04-05. -// Copyright (c) 2012 Kearwood Software. All rights reserved. -// - -#ifndef KREngine_KRLightManager_h -#define KREngine_KRLightManager_h - -class KRLightManager { -public: - KRLightManager(); - ~KRLightManager(); - - bool loadFile(const char *szPath); -}; - -#endif diff --git a/KREngine/KREngine/Classes/KRMaterial.h b/KREngine/KREngine/Classes/KRMaterial.h index 08a0869..f49d04e 100644 --- a/KREngine/KREngine/Classes/KRMaterial.h +++ b/KREngine/KREngine/Classes/KRMaterial.h @@ -37,9 +37,6 @@ using std::list; -#ifndef KRMATERIAL_H -#define KRMATRIAL_H - #import "KRTexture.h" #import "KRShaderManager.h" #import "KRShader.h" @@ -47,6 +44,11 @@ using std::list; #import "KRResource.h" #import "KRVector2.h" +#ifndef KRMATERIAL_H +#define KRMATERIAL_H + + + class KRTextureManager; class KRMaterial : public KRResource { diff --git a/KREngine/KREngine/Classes/KRModel.cpp b/KREngine/KREngine/Classes/KRModel.cpp index 9272d84..210b9e7 100644 --- a/KREngine/KREngine/Classes/KRModel.cpp +++ b/KREngine/KREngine/Classes/KRModel.cpp @@ -46,7 +46,8 @@ #import "KRShaderManager.h" -KRModel::KRModel(std::string path, KRMaterialManager *pMaterialManager) { +KRModel::KRModel(std::string name, std::string path, KRMaterialManager *pMaterialManager) { + m_name = name; loadPack(path, pMaterialManager); } @@ -63,9 +64,14 @@ void KRModel::loadPack(std::string path, KRMaterialManager *pMaterialManager) { } } +std::string KRModel::getName() { + return m_name; +} + KRModel::~KRModel() { } +#if TARGET_OS_IPHONE void KRModel::render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager, KRTexture *pShadowMap) { KRMaterial *pPrevBoundMaterial = NULL; @@ -102,6 +108,8 @@ void KRModel::render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, boo glBindBuffer(GL_ARRAY_BUFFER, 0); } +#endif + KRMesh *KRModel::getMesh() { return m_pMesh; } diff --git a/KREngine/KREngine/Classes/KRModel.h b/KREngine/KREngine/Classes/KRModel.h index 0af3713..2d3f114 100644 --- a/KREngine/KREngine/Classes/KRModel.h +++ b/KREngine/KREngine/Classes/KRModel.h @@ -49,12 +49,17 @@ using std::set; class KRModel { public: - KRModel(std::string path, KRMaterialManager *pMaterialManager); + KRModel(std::string name, std::string path, KRMaterialManager *pMaterialManager); ~KRModel(); +#if TARGET_OS_IPHONE + void render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager, KRTexture *pShadowMap); +#endif + KRMesh *getMesh(); + std::string getName(); private: void loadPack(std::string path, KRMaterialManager *pMaterialManager); @@ -62,6 +67,7 @@ private: vector m_materials; set m_uniqueMaterials; KRMesh *m_pMesh; + std::string m_name; }; diff --git a/KREngine/KREngine/Classes/KRModelManager.cpp b/KREngine/KREngine/Classes/KRModelManager.cpp index 9a19f39..8999f75 100644 --- a/KREngine/KREngine/Classes/KRModelManager.cpp +++ b/KREngine/KREngine/Classes/KRModelManager.cpp @@ -43,7 +43,7 @@ KRModelManager::~KRModelManager() { } KRModel *KRModelManager::loadModel(const char *szName, const char *szPath) { - KRModel *pModel = new KRModel(szPath, m_pMaterialManager); + KRModel *pModel = new KRModel(szName, szPath, m_pMaterialManager); m_models[szName] = pModel; return pModel; } diff --git a/KREngine/KREngine/Classes/KRNode.cpp b/KREngine/KREngine/Classes/KRNode.cpp new file mode 100644 index 0000000..43bcd73 --- /dev/null +++ b/KREngine/KREngine/Classes/KRNode.cpp @@ -0,0 +1,77 @@ +// +// KRNode.cpp +// KREngine +// +// Created by Kearwood Gilbert on 12-04-11. +// Copyright (c) 2012 Kearwood Software. All rights reserved. +// + +#include + +#import "KRNode.h" + + +KRNode::KRNode(std::string name) +{ + m_name = name; + m_localScale = KRVector3(1.0f, 1.0f, 1.0f); + m_localRotation = KRVector3(0.0f, 0.0f, 0.0f); + m_localTranslation = KRVector3(0.0f, 0.0f, 0.0f); +} + +KRNode::~KRNode() { + for(std::vector::iterator itr=m_childNodes.begin(); itr < m_childNodes.end(); ++itr) { + delete *itr; + } + m_childNodes.clear(); +} + +void KRNode::addChild(KRNode *child) { + m_childNodes.push_back(child); +} + +tinyxml2::XMLElement *KRNode::saveXML(tinyxml2::XMLNode *parent) { + tinyxml2::XMLDocument *doc = parent->GetDocument(); + tinyxml2::XMLElement *e = doc->NewElement(getElementName().c_str()); + tinyxml2::XMLNode *n = parent->InsertEndChild(e); + e->SetAttribute("name", m_name.c_str()); + e->SetAttribute("translate_x", m_localTranslation.x); + e->SetAttribute("translate_y", m_localTranslation.y); + e->SetAttribute("translate_z", m_localTranslation.z); + e->SetAttribute("scale_x", m_localScale.x); + e->SetAttribute("scale_y", m_localScale.y); + e->SetAttribute("scale_z", m_localScale.z); + e->SetAttribute("rotate_x", m_localRotation.x); + e->SetAttribute("rotate_y", m_localRotation.y); + e->SetAttribute("rotate_z", m_localRotation.z); + for(std::vector::iterator itr=m_childNodes.begin(); itr < m_childNodes.end(); ++itr) { + KRNode *child = (*itr); + child->saveXML(n); + } + return e; +} + +void KRNode::setLocalTranslation(const KRVector3 &v) { + m_localTranslation = v; +} +void KRNode::setLocalScale(const KRVector3 &v) { + m_localScale = v; +} +void KRNode::setLocalRotation(const KRVector3 &v) { + m_localRotation = v; +} + +const KRVector3 &KRNode::getLocalTranslation() { + return m_localTranslation; +} +const KRVector3 &KRNode::getLocalScale() { + return m_localScale; +} +const KRVector3 &KRNode::getLocalRotation() { + return m_localRotation; +} + +std::string KRNode::getElementName() { + return "node"; +} + diff --git a/KREngine/KREngine/Classes/KRNode.h b/KREngine/KREngine/Classes/KRNode.h new file mode 100644 index 0000000..2f48976 --- /dev/null +++ b/KREngine/KREngine/Classes/KRNode.h @@ -0,0 +1,46 @@ +// +// KRNode.h +// KREngine +// +// Created by Kearwood Gilbert on 12-04-11. +// Copyright (c) 2012 Kearwood Software. All rights reserved. +// + +#ifndef KREngine_KRNode_h +#define KREngine_KRNode_h + +#import "KRResource.h" +#import "KRVector3.h" +#import "tinyxml2.h" + +class KRNode +{ +public: + KRNode(std::string name); + virtual ~KRNode(); + + virtual tinyxml2::XMLElement *saveXML( tinyxml2::XMLNode *parent); + virtual std::string getElementName(); + + void addChild(KRNode *child); + + void setLocalTranslation(const KRVector3 &v); + void setLocalScale(const KRVector3 &v); + void setLocalRotation(const KRVector3 &v); + + const KRVector3 &getLocalTranslation(); + const KRVector3 &getLocalScale(); + const KRVector3 &getLocalRotation(); + +private: + KRVector3 m_localTranslation; + KRVector3 m_localScale; + KRVector3 m_localRotation; + + std::string m_name; + + std::vector m_childNodes; +}; + + +#endif diff --git a/KREngine/KREngine/Classes/KRPointLight.cpp b/KREngine/KREngine/Classes/KRPointLight.cpp index 7ffb8bb..8d5a5cd 100644 --- a/KREngine/KREngine/Classes/KRPointLight.cpp +++ b/KREngine/KREngine/Classes/KRPointLight.cpp @@ -20,7 +20,6 @@ KRPointLight::~KRPointLight() } -bool KRPointLight::save(const std::string& path) -{ - return true; -} \ No newline at end of file +std::string KRPointLight::getElementName() { + return "point_light"; +} diff --git a/KREngine/KREngine/Classes/KRPointLight.h b/KREngine/KREngine/Classes/KRPointLight.h index c6e04a4..2951398 100644 --- a/KREngine/KREngine/Classes/KRPointLight.h +++ b/KREngine/KREngine/Classes/KRPointLight.h @@ -15,9 +15,9 @@ class KRPointLight : public KRLight { public: KRPointLight(std::string name); - ~KRPointLight(); + virtual ~KRPointLight(); - virtual bool save(const std::string& path); + virtual std::string getElementName(); }; #endif diff --git a/KREngine/KREngine/Classes/KRResource+fbx.cpp b/KREngine/KREngine/Classes/KRResource+fbx.cpp index e677ea9..492c625 100644 --- a/KREngine/KREngine/Classes/KRResource+fbx.cpp +++ b/KREngine/KREngine/Classes/KRResource+fbx.cpp @@ -28,6 +28,8 @@ #include "KRPointLight.h" #include "KRDirectionalLight.h" #include "KRSpotLight.h" +#include "KRNode.h" +#include "KRScene.h" #ifdef IOS_REF #undef IOS_REF @@ -37,41 +39,44 @@ void InitializeSdkObjects(KFbxSdkManager*& pSdkManager, KFbxScene*& pScene); void DestroySdkObjects(KFbxSdkManager* pSdkManager); bool LoadScene(KFbxSdkManager* pSdkManager, KFbxDocument* pScene, const char* pFilename); -void LoadNode(std::vector &resources, KFbxGeometryConverter *pGeometryConverter, KFbxNode* pNode); -void LoadMesh(std::vector &resources, KFbxGeometryConverter *pGeometryConverter, KFbxNode* pNode); -void LoadLight(std::vector &resources, KFbxNode* pNode); +void LoadNode(KRNode *parent_node, std::vector &resources, KFbxGeometryConverter *pGeometryConverter, KFbxNode* pNode); +void LoadMesh(KRNode *parent_node, std::vector &resources, KFbxGeometryConverter *pGeometryConverter, KFbxNode* pNode); +void LoadLight(KRNode *parent_node, std::vector &resources, KFbxNode* pNode); std::vector KRResource::LoadFbx(const std::string& path) { std::vector resources; + KRScene *pScene = new KRScene(KRResource::GetFileBase(path)); + resources.push_back(pScene); + KFbxSdkManager* lSdkManager = NULL; - KFbxScene* pScene = NULL; + KFbxScene* pFbxScene = NULL; bool lResult; KFbxGeometryConverter *pGeometryConverter = NULL; // Prepare the FBX SDK. - InitializeSdkObjects(lSdkManager, pScene); + InitializeSdkObjects(lSdkManager, pFbxScene); // Initialize Geometry Converter pGeometryConverter = new KFbxGeometryConverter(lSdkManager); // Load the scene. - lResult = LoadScene(lSdkManager, pScene, path.c_str()); + lResult = LoadScene(lSdkManager, pFbxScene, path.c_str()); // ----====---- Walk Through Scene ----====---- int i; - KFbxNode* pNode = pScene->GetRootNode(); + KFbxNode* pNode = pFbxScene->GetRootNode(); if(pNode) { for(i = 0; i < pNode->GetChildCount(); i++) { - LoadNode(resources, pGeometryConverter, pNode->GetChild(i)); + LoadNode(pScene->getRootNode(), resources, pGeometryConverter, pNode->GetChild(i)); } } @@ -232,7 +237,7 @@ bool LoadScene(KFbxSdkManager* pSdkManager, KFbxDocument* pScene, const char* pF return lStatus; } -void LoadNode(std::vector &resources, KFbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) { +void LoadNode(KRNode *parent_node, std::vector &resources, KFbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) { KFbxVector4 lTmpVector; /* @@ -247,10 +252,10 @@ void LoadNode(std::vector &resources, KFbxGeometryConverter *pGeom KFbxNodeAttribute::EAttributeType attribute_type = (pNode->GetNodeAttribute()->GetAttributeType()); switch(attribute_type) { case KFbxNodeAttribute::eMESH: - LoadMesh(resources, pGeometryConverter, pNode); + LoadMesh(parent_node, resources, pGeometryConverter, pNode); break; case KFbxNodeAttribute::eLIGHT: - LoadLight(resources, pNode); + LoadLight(parent_node, resources, pNode); break; } @@ -258,11 +263,24 @@ void LoadNode(std::vector &resources, KFbxGeometryConverter *pGeom // Load child nodes for(int i = 0; i < pNode->GetChildCount(); i++) { - LoadNode(resources, pGeometryConverter, pNode->GetChild(i)); + LoadNode(parent_node, resources, pGeometryConverter, pNode->GetChild(i)); } } -void LoadMesh(std::vector &resources, KFbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) { +void LoadMesh(KRNode *parent_node, std::vector &resources, KFbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) { + std::string shadow_map = pNode->GetName(); + shadow_map.append("_lightmap"); + + KRInstance *new_instance = new KRInstance(pNode->GetName(), pNode->GetName(), KRMat4(), shadow_map); + fbxDouble3 local_rotation = pNode->GetGeometricRotation(KFbxNode::eSOURCE_SET); + fbxDouble3 local_translation = pNode->GetGeometricTranslation(KFbxNode::eSOURCE_SET); + fbxDouble3 local_scale = pNode->GetGeometricScaling(KFbxNode::eSOURCE_SET); + new_instance->setLocalRotation(KRVector3(local_rotation[0], local_rotation[1], local_rotation[2])); + new_instance->setLocalTranslation(KRVector3(local_translation[0], local_translation[1], local_translation[2])); + new_instance->setLocalScale(KRVector3(local_scale[0], local_scale[1], local_scale[2])); + parent_node->addChild(new_instance); + + printf("Mesh: %s\n", pNode->GetName()); KFbxMesh* pSourceMesh = (KFbxMesh*) pNode->GetNodeAttribute(); KFbxMesh* pMesh = pGeometryConverter->TriangulateMesh(pSourceMesh); @@ -558,7 +576,10 @@ void LoadMesh(std::vector &resources, KFbxGeometryConverter *pGeom } -void LoadLight(std::vector &resources, KFbxNode* pNode) { +void LoadLight(KRNode *parent_node, std::vector &resources, KFbxNode* pNode) { + const GLfloat PI = 3.14159265; + const GLfloat d2r = PI * 2 / 360; + KFbxLight* pLight = (KFbxLight*) pNode->GetNodeAttribute(); const char *szName = pNode->GetName(); @@ -575,19 +596,55 @@ void LoadLight(std::vector &resources, KFbxNode* pNode) { // KFbxLight::eQUADRATIC - attenuation of 1/d^2 // KFbxLight::eCUBIC - attenuation of + KFbxVector4 light_translation = pNode->GetGeometricTranslation(KFbxNode::eSOURCE_SET); + KFbxVector4 light_rotation = pNode->GetGeometricRotation(KFbxNode::eSOURCE_SET); + KFbxVector4 light_scaling = pNode->GetGeometricScaling(KFbxNode::eSOURCE_SET); + + + KRVector3 translation = KRVector3(light_translation[0], light_translation[1], light_translation[2]); + + KRLight *new_light = NULL; + switch(pLight->LightType.Get()) { case KFbxLight::ePOINT: - resources.push_back(new KRPointLight(szName)); + { + KRPointLight *l = new KRPointLight(szName); + new_light = l; + + } break; case KFbxLight::eDIRECTIONAL: - resources.push_back(new KRDirectionalLight(szName)); + { + KRDirectionalLight *l = new KRDirectionalLight(szName); + new_light = l; + } break; case KFbxLight::eSPOT: - resources.push_back(new KRSpotLight(szName)); + { + KRSpotLight *l = new KRSpotLight(szName); + l->setInnerAngle(light_hotspot * d2r); + l->setOuterAngle(light_coneangle * d2r); + new_light = l; + } break; case KFbxLight::eAREA: // Not supported yet break; } + + if(new_light) { + fbxDouble3 local_rotation = pNode->GetGeometricRotation(KFbxNode::eSOURCE_SET); + fbxDouble3 local_translation = pNode->GetGeometricTranslation(KFbxNode::eSOURCE_SET); + fbxDouble3 local_scale = pNode->GetGeometricScaling(KFbxNode::eSOURCE_SET); + new_light->setLocalRotation(KRVector3(local_rotation[0], local_rotation[1], local_rotation[2])); + new_light->setLocalTranslation(KRVector3(local_translation[0], local_translation[1], local_translation[2])); + new_light->setLocalScale(KRVector3(local_scale[0], local_scale[1], local_scale[2])); + new_light->setColor(KRVector3(light_color[0], light_color[1], light_color[2])); + new_light->setIntensity(light_intensity); + new_light->setDecayStart(light_decaystart); + + parent_node->addChild(new_light); + } + } diff --git a/KREngine/KREngine/Classes/KRResource.h b/KREngine/KREngine/Classes/KRResource.h index 46bff2a..49d3820 100644 --- a/KREngine/KREngine/Classes/KRResource.h +++ b/KREngine/KREngine/Classes/KRResource.h @@ -21,6 +21,7 @@ class KRResource public: std::string getName(); virtual std::string getExtension() = 0; + virtual bool save(const std::string& path) = 0; static std::string GetFileExtension(const std::string& name); static std::string GetFileBase(const std::string& name); @@ -29,7 +30,7 @@ public: static std::vector Load(const std::string& path); virtual ~KRResource(); - virtual bool save(const std::string& path) = 0; + protected: KRResource(std::string name); diff --git a/KREngine/KREngine/Classes/KRScene.cpp b/KREngine/KREngine/Classes/KRScene.cpp index d02d07e..3d3ed29 100644 --- a/KREngine/KREngine/Classes/KRScene.cpp +++ b/KREngine/KREngine/Classes/KRScene.cpp @@ -33,26 +33,35 @@ #import "KRVector3.h" #import "KRMat4.h" +#import "tinyxml2.h" #import "KRScene.h" -KRScene::KRScene() { +KRScene::KRScene(std::string name) : KRResource(name) { m_pExtents = NULL; + m_pRootNode = new KRNode("scene_root"); } KRScene::~KRScene() { for(vector::iterator itr = m_instances.begin(); itr != m_instances.end(); ++itr){ delete *itr; } m_instances.empty(); + + delete m_pRootNode; + m_pRootNode = NULL; + clearExtents(); } -KRInstance *KRScene::addInstance(KRModel *pModel, KRMat4 modelMatrix, std::string shadow_map) { +KRInstance *KRScene::addInstance(std::string instance_name, std::string model_name, KRMat4 modelMatrix, std::string shadow_map) { clearExtents(); - KRInstance *pInstance = new KRInstance(pModel, modelMatrix, shadow_map); + KRInstance *pInstance = new KRInstance(instance_name, model_name, modelMatrix, shadow_map); m_instances.push_back(pInstance); return pInstance; } -void KRScene::render(KRCamera *pCamera, KRBoundingVolume &frustrumVolume, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager) { + +#if TARGET_OS_IPHONE + +void KRScene::render(KRCamera *pCamera, KRModelManager *pModelManager, KRBoundingVolume &frustrumVolume, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager) { if(cShadowBuffers > 0 && !bRenderShadowMap) { glActiveTexture(GL_TEXTURE3); @@ -85,27 +94,29 @@ void KRScene::render(KRCamera *pCamera, KRBoundingVolume &frustrumVolume, KRMate for(vector::iterator itr = m_instances.begin(); itr != m_instances.end(); ++itr){ KRInstance *pInstance = *itr; - if(pInstance->getExtents().test_intersect(frustrumVolume) || bRenderShadowMap) { - pInstance->render(pCamera, pMaterialManager, bRenderShadowMap, viewMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, pShaderManager, pTextureManager); + if(pInstance->getExtents(pModelManager).test_intersect(frustrumVolume) || bRenderShadowMap) { + pInstance->render(pCamera, pModelManager, pMaterialManager, bRenderShadowMap, viewMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, pShaderManager, pTextureManager); } } } -void KRScene::calcExtents() { +#endif + +void KRScene::calcExtents(KRModelManager *pModelManager) { clearExtents(); for(vector::iterator itr = m_instances.begin(); itr != m_instances.end(); ++itr){ KRInstance *pInstance = *itr; if(m_pExtents) { - *m_pExtents = m_pExtents->get_union(pInstance->getExtents()); + *m_pExtents = m_pExtents->get_union(pInstance->getExtents(pModelManager)); } else { - m_pExtents = new KRBoundingVolume(pInstance->getExtents()); + m_pExtents = new KRBoundingVolume(pInstance->getExtents(pModelManager)); } } } -KRBoundingVolume KRScene::getExtents() { +KRBoundingVolume KRScene::getExtents(KRModelManager *pModelManager) { if(!m_pExtents) { - calcExtents(); + calcExtents(pModelManager); } return *m_pExtents; } @@ -116,3 +127,30 @@ void KRScene::clearExtents() { m_pExtents = NULL; } } + + +std::string KRScene::getExtension() { + return "krscene"; +} + +KRNode *KRScene::getRootNode() { + return m_pRootNode; +} + +bool KRScene::save(const std::string& path) { + tinyxml2::XMLDocument doc; + tinyxml2::XMLNode *scene_node = doc.InsertEndChild( doc.NewElement( "scene" )); + m_pRootNode->saveXML(scene_node); + doc.SaveFile(path.c_str()); + return true; +} + + +KRScene *KRScene::LoadXML(const std::string& path) +{ + tinyxml2::XMLDocument doc; + doc.LoadFile(path.c_str()); + KRScene *new_scene = new KRScene(KRResource::GetFileBase(path)); + + return new_scene; +} diff --git a/KREngine/KREngine/Classes/KRScene.h b/KREngine/KREngine/Classes/KRScene.h index 6220f7c..290dcfb 100644 --- a/KREngine/KREngine/Classes/KRScene.h +++ b/KREngine/KREngine/Classes/KRScene.h @@ -40,24 +40,41 @@ #import "KRMat4.h" #import "KRModel.h" #import "KRCamera.h" - +#import "KRModelManager.h" +#import "KRNode.h" class KRBoundingVolume; class KRInstance; using std::vector; -class KRScene { +class KRScene : public KRResource { public: - KRScene(); + KRScene(std::string name); ~KRScene(); - KRInstance *addInstance(KRModel *pModel, KRMat4 modelMatrix, std::string shadow_map); - void render(KRCamera *pCamera, KRBoundingVolume &frustrumVolume, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager); - KRBoundingVolume getExtents(); + + virtual std::string getExtension(); + virtual bool save(const std::string& path); + + static KRScene *LoadXML(const std::string& path); + + KRInstance *addInstance(std::string instance_name, std::string model_name, KRMat4 modelMatrix, std::string shadow_map); + KRNode *getRootNode(); + +#if TARGET_OS_IPHONE + + void render(KRCamera *pCamera, KRModelManager *pModelManager, KRBoundingVolume &frustrumVolume, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRShaderManager *pShaderManager, KRTextureManager *pTextureManager); + +#endif + + KRBoundingVolume getExtents(KRModelManager *pModelManager); private: vector m_instances; + KRNode *m_pRootNode; + + KRBoundingVolume *m_pExtents; - void calcExtents(); + void calcExtents(KRModelManager *pModelManager); void clearExtents(); }; diff --git a/KREngine/KREngine/Classes/KRSceneManager.cpp b/KREngine/KREngine/Classes/KRSceneManager.cpp new file mode 100644 index 0000000..e3d0842 --- /dev/null +++ b/KREngine/KREngine/Classes/KRSceneManager.cpp @@ -0,0 +1,62 @@ +// +// KRSceneManager.cpp +// KREngine +// +// Copyright 2012 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRSceneManager.h" + +KRSceneManager::KRSceneManager() { +} + +KRSceneManager::~KRSceneManager() { + for(map::iterator itr = m_scenes.begin(); itr != m_scenes.end(); ++itr){ + delete (*itr).second; + } + m_scenes.empty(); +} + +KRScene *KRSceneManager::loadScene(const char *szName, const char *szPath) { + KRScene *pScene = KRScene::LoadXML(szName); + m_scenes[szName] = pScene; + return pScene; +} + +KRScene *KRSceneManager::getScene(const char *szName) { + return m_scenes[szName]; +} + +KRScene *KRSceneManager::getFirstScene() { + static std::map::iterator Scene_itr = m_scenes.begin(); + return (*Scene_itr).second; +} + +std::map KRSceneManager::getScenes() { + return m_scenes; +} + diff --git a/KREngine/KREngine/Classes/KRSceneManager.h b/KREngine/KREngine/Classes/KRSceneManager.h new file mode 100644 index 0000000..9f41c66 --- /dev/null +++ b/KREngine/KREngine/Classes/KRSceneManager.h @@ -0,0 +1,63 @@ +// +// KRSceneManager.h +// KREngine +// +// Copyright 2012 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#ifndef KRENGINE_KRSCENEMANAGER_H +#define KRENGINE_KRSCENEMANAGER_H + +#import "KREngine-common.h" + +#include "KRScene.h" + +#include +#import +using std::map; + + +class KRSceneManager { +public: + KRSceneManager(); + ~KRSceneManager(); + + KRScene *loadScene(const char *szName, const char *szPath); + + KRScene *getScene(const char *szName); + KRScene *getFirstScene(); + + std::vector getSceneNames(); + std::map getScenes(); + + +private: + std::map m_scenes; + +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRSpotLight.cpp b/KREngine/KREngine/Classes/KRSpotLight.cpp index 182c1b5..a2da1a9 100644 --- a/KREngine/KREngine/Classes/KRSpotLight.cpp +++ b/KREngine/KREngine/Classes/KRSpotLight.cpp @@ -19,7 +19,28 @@ KRSpotLight::~KRSpotLight() } -bool KRSpotLight::save(const std::string& path) -{ - return true; +std::string KRSpotLight::getElementName() { + return "spot_light"; +} + +tinyxml2::XMLElement *KRSpotLight::saveXML( tinyxml2::XMLNode *parent) +{ + tinyxml2::XMLElement *e = KRLight::saveXML(parent); + e->SetAttribute("inner_angle", m_innerAngle); + e->SetAttribute("outer_angle", m_outerAngle); + return e; +} + + +float KRSpotLight::getInnerAngle() { + return m_innerAngle; +} +float KRSpotLight::getOuterAngle() { + return m_outerAngle; +} +void KRSpotLight::setInnerAngle(float innerAngle) { + m_innerAngle = innerAngle; +} +void KRSpotLight::setOuterAngle(float outerAngle) { + m_outerAngle = outerAngle; } diff --git a/KREngine/KREngine/Classes/KRSpotLight.h b/KREngine/KREngine/Classes/KRSpotLight.h index fe7ebcc..b61600a 100644 --- a/KREngine/KREngine/Classes/KRSpotLight.h +++ b/KREngine/KREngine/Classes/KRSpotLight.h @@ -14,9 +14,19 @@ class KRSpotLight : public KRLight { public: KRSpotLight(std::string name); - ~KRSpotLight(); + virtual ~KRSpotLight(); - virtual bool save(const std::string& path); + virtual std::string getElementName(); + virtual tinyxml2::XMLElement *saveXML( tinyxml2::XMLNode *parent); + + float getInnerAngle(); + float getOuterAngle(); + void setInnerAngle(float innerAngle); + void setOuterAngle(float outerAngle); + +private: + float m_innerAngle; // Inner angle of the cone, in radians. Inside this radius, the light will be at full brightness + float m_outerAngle; // Outer angle of the cone, in radians. Outside this radius, the light will be completely attenuated }; diff --git a/KREngine/KREngine/Classes/KRTexture.cpp b/KREngine/KREngine/Classes/KRTexture.cpp index 5d957b6..723abd2 100644 --- a/KREngine/KREngine/Classes/KRTexture.cpp +++ b/KREngine/KREngine/Classes/KRTexture.cpp @@ -87,6 +87,8 @@ KRTexture::~KRTexture() { } } +#if TARGET_OS_IPHONE + bool KRTexture::loadFromFile(const char *szFile) { struct stat statbuf; m_fdFile = open(szFile, O_RDONLY); @@ -179,6 +181,8 @@ bool KRTexture::loadFromFile(const char *szFile) { } } +#endif + bool KRTexture::createGLTexture() { int width = m_iWidth; int height = m_iHeight; diff --git a/KREngine/KREngine/Classes/KRTexture.h b/KREngine/KREngine/Classes/KRTexture.h index a08c6fb..e53a249 100644 --- a/KREngine/KREngine/Classes/KRTexture.h +++ b/KREngine/KREngine/Classes/KRTexture.h @@ -45,7 +45,11 @@ public: KRTexture(); ~KRTexture(); +#if TARGET_OS_IPHONE + bool loadFromFile(const char *szFile); + +#endif bool createGLTexture(); GLuint getName(); diff --git a/KREngine/KREngine/Classes/KRTextureManager.cpp b/KREngine/KREngine/Classes/KRTextureManager.cpp index 071ab45..82314a8 100644 --- a/KREngine/KREngine/Classes/KRTextureManager.cpp +++ b/KREngine/KREngine/Classes/KRTextureManager.cpp @@ -42,6 +42,8 @@ KRTextureManager::~KRTextureManager() { } } +#if TARGET_OS_IPHONE + KRTexture *KRTextureManager::loadTexture(const char *szName, const char *szPath) { KRTexture *pTexture = new KRTexture(); if(!pTexture->loadFromFile(szPath)) { @@ -64,6 +66,8 @@ KRTexture *KRTextureManager::loadTexture(const char *szName, const char *szPath) return pTexture; } +#endif + GLuint KRTextureManager::getTextureName(const char *szName) { KRTexture *pTexture = getTexture(szName); if(pTexture) { diff --git a/KREngine/KREngine/Classes/KRTextureManager.h b/KREngine/KREngine/Classes/KRTextureManager.h index 25df4ab..ced0a4d 100644 --- a/KREngine/KREngine/Classes/KRTextureManager.h +++ b/KREngine/KREngine/Classes/KRTextureManager.h @@ -44,7 +44,12 @@ public: KRTextureManager(); ~KRTextureManager(); +#if TARGET_OS_IPHONE + KRTexture *loadTexture(const char *szName, const char *szPath); + +#endif + GLuint getTextureName(const char *szName); KRTexture *getTexture(const char *szFile); diff --git a/KREngine/KREngine/Classes/tinyxml2.cpp b/KREngine/KREngine/Classes/tinyxml2.cpp new file mode 100644 index 0000000..f6998ea --- /dev/null +++ b/KREngine/KREngine/Classes/tinyxml2.cpp @@ -0,0 +1,1816 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#include "tinyxml2.h" + +#if 1 + #include + #include + #include +#else + #include + #include + #include + #include + #include + #include +#endif + +using namespace tinyxml2; + +static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF +static const char LF = LINE_FEED; +static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out +static const char CR = CARRIAGE_RETURN; +static const char SINGLE_QUOTE = '\''; +static const char DOUBLE_QUOTE = '\"'; + +// Bunch of unicode info at: +// http://www.unicode.org/faq/utf_bom.html +// ef bb bf (Microsoft "lead bytes") - designates UTF-8 + +static const unsigned char TIXML_UTF_LEAD_0 = 0xefU; +static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU; +static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; + + +#define DELETE_NODE( node ) { \ + if ( node ) { \ + MemPool* pool = node->memPool; \ + node->~XMLNode(); \ + pool->Free( node ); \ + } \ +} +#define DELETE_ATTRIBUTE( attrib ) { \ + if ( attrib ) { \ + MemPool* pool = attrib->memPool; \ + attrib->~XMLAttribute(); \ + pool->Free( attrib ); \ + } \ +} + +struct Entity { + const char* pattern; + int length; + char value; +}; + +static const int NUM_ENTITIES = 5; +static const Entity entities[NUM_ENTITIES] = +{ + { "quot", 4, DOUBLE_QUOTE }, + { "amp", 3, '&' }, + { "apos", 4, SINGLE_QUOTE }, + { "lt", 2, '<' }, + { "gt", 2, '>' } +}; + + +StrPair::~StrPair() +{ + Reset(); +} + + +void StrPair::Reset() +{ + if ( flags & NEEDS_DELETE ) { + delete [] start; + } + flags = 0; + start = 0; + end = 0; +} + + +void StrPair::SetStr( const char* str, int flags ) +{ + Reset(); + size_t len = strlen( str ); + start = new char[ len+1 ]; + memcpy( start, str, len+1 ); + end = start + len; + this->flags = flags | NEEDS_DELETE; +} + + +char* StrPair::ParseText( char* p, const char* endTag, int strFlags ) +{ + TIXMLASSERT( endTag && *endTag ); + + char* start = p; // fixme: hides a member + char endChar = *endTag; + int length = strlen( endTag ); + + // Inner loop of text parsing. + while ( *p ) { + if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) { + Set( start, p, strFlags ); + return p + length; + } + ++p; + } + return 0; +} + + +char* StrPair::ParseName( char* p ) +{ + char* start = p; + + if ( !start || !(*start) ) { + return 0; + } + + if ( !XMLUtil::IsAlpha( *p ) ) { + return 0; + } + + while( *p && ( + XMLUtil::IsAlphaNum( (unsigned char) *p ) + || *p == '_' + || *p == '-' + || *p == '.' + || *p == ':' )) + { + ++p; + } + + if ( p > start ) { + Set( start, p, 0 ); + return p; + } + return 0; +} + + + +const char* StrPair::GetStr() +{ + if ( flags & NEEDS_FLUSH ) { + *end = 0; + flags ^= NEEDS_FLUSH; + + if ( flags ) { + char* p = start; // the read pointer + char* q = start; // the write pointer + + while( p < end ) { + if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) { + // CR-LF pair becomes LF + // CR alone becomes LF + // LF-CR becomes LF + if ( *(p+1) == LF ) { + p += 2; + } + else { + ++p; + } + *q++ = LF; + } + else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) { + if ( *(p+1) == CR ) { + p += 2; + } + else { + ++p; + } + *q++ = LF; + } + else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) { + int i=0; + + // Entities handled by tinyXML2: + // - special entities in the entity table [in/out] + // - numeric character reference [in] + // 中 or 中 + + if ( *(p+1) == '#' ) { + char buf[10] = { 0 }; + int len; + p = const_cast( XMLUtil::GetCharacterRef( p, buf, &len ) ); + for( int i=0; i(p); + // Check for BOM: + if ( *(pu+0) == TIXML_UTF_LEAD_0 + && *(pu+1) == TIXML_UTF_LEAD_1 + && *(pu+2) == TIXML_UTF_LEAD_2 ) + { + *bom = true; + p += 3; + } + return p; +} + + +void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ) +{ + const unsigned long BYTE_MASK = 0xBF; + const unsigned long BYTE_MARK = 0x80; + const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; + + if (input < 0x80) + *length = 1; + else if ( input < 0x800 ) + *length = 2; + else if ( input < 0x10000 ) + *length = 3; + else if ( input < 0x200000 ) + *length = 4; + else + { *length = 0; return; } // This code won't covert this correctly anyway. + + output += *length; + + // Scary scary fall throughs. + switch (*length) + { + case 4: + --output; + *output = (char)((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + case 3: + --output; + *output = (char)((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + case 2: + --output; + *output = (char)((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + case 1: + --output; + *output = (char)(input | FIRST_BYTE_MARK[*length]); + } +} + + +const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length ) +{ + // Presume an entity, and pull it out. + *length = 0; + + if ( *(p+1) == '#' && *(p+2) ) + { + unsigned long ucs = 0; + int delta = 0; + unsigned mult = 1; + + if ( *(p+2) == 'x' ) + { + // Hexadecimal. + if ( !*(p+3) ) return 0; + + const char* q = p+3; + q = strchr( q, ';' ); + + if ( !q || !*q ) return 0; + + delta = (q-p); + --q; + + while ( *q != 'x' ) + { + if ( *q >= '0' && *q <= '9' ) + ucs += mult * (*q - '0'); + else if ( *q >= 'a' && *q <= 'f' ) + ucs += mult * (*q - 'a' + 10); + else if ( *q >= 'A' && *q <= 'F' ) + ucs += mult * (*q - 'A' + 10 ); + else + return 0; + mult *= 16; + --q; + } + } + else + { + // Decimal. + if ( !*(p+2) ) return 0; + + const char* q = p+2; + q = strchr( q, ';' ); + + if ( !q || !*q ) return 0; + + delta = q-p; + --q; + + while ( *q != '#' ) + { + if ( *q >= '0' && *q <= '9' ) + ucs += mult * (*q - '0'); + else + return 0; + mult *= 10; + --q; + } + } + // convert the UCS to UTF-8 + ConvertUTF32ToUTF8( ucs, value, length ); + return p + delta + 1; + } + return p+1; +} + + +char* XMLDocument::Identify( char* p, XMLNode** node ) +{ + XMLNode* returnNode = 0; + char* start = p; + p = XMLUtil::SkipWhiteSpace( p ); + if( !p || !*p ) + { + return p; + } + + // What is this thing? + // - Elements start with a letter or underscore, but xml is reserved. + // - Comments: + // + // With a special case: + // + // + // + // + // Where the closing element (/foo) *must* be the next thing after the opening + // element, and the names must match. BUT the tricky bit is that the closing + // element will be read by the child. + // + // 'endTag' is the end tag for this node, it is returned by a call to a child. + // 'parentEnd' is the end tag for the parent, which is filled in and returned. + + while( p && *p ) { + XMLNode* node = 0; + + p = document->Identify( p, &node ); + if ( p == 0 || node == 0 ) { + break; + } + + StrPair endTag; + p = node->ParseDeep( p, &endTag ); + if ( !p ) { + DELETE_NODE( node ); + node = 0; + if ( !document->Error() ) { + document->SetError( XML_ERROR_PARSING, 0, 0 ); + } + break; + } + + // We read the end tag. Return it to the parent. + if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) { + if ( parentEnd ) { + *parentEnd = ((XMLElement*)node)->value; + } + DELETE_NODE( node ); + return p; + } + + // Handle an end tag returned to this level. + // And handle a bunch of annoying errors. + XMLElement* ele = node->ToElement(); + if ( ele ) { + if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) { + document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); + p = 0; + } + else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) { + document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); + p = 0; + } + else if ( !endTag.Empty() ) { + if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) { + document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); + p = 0; + } + } + } + if ( p == 0 ) { + DELETE_NODE( node ); + node = 0; + } + if ( node ) { + this->InsertEndChild( node ); + } + } + return 0; +} + +// --------- XMLText ---------- // +char* XMLText::ParseDeep( char* p, StrPair* ) +{ + const char* start = p; + if ( this->CData() ) { + p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION ); + if ( !p ) { + document->SetError( XML_ERROR_PARSING_CDATA, start, 0 ); + } + return p; + } + else { + p = value.ParseText( p, "<", document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES ); + if ( !p ) { + document->SetError( XML_ERROR_PARSING_TEXT, start, 0 ); + } + if ( p && *p ) { + return p-1; + } + } + return 0; +} + + +XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = document; + } + XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern? + text->SetCData( this->CData() ); + return text; +} + + +bool XMLText::ShallowEqual( const XMLNode* compare ) const +{ + return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() )); +} + + +bool XMLText::Accept( XMLVisitor* visitor ) const +{ + return visitor->Visit( *this ); +} + + +// --------- XMLComment ---------- // + +XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLComment::~XMLComment() +{ + //printf( "~XMLComment\n" ); +} + + +char* XMLComment::ParseDeep( char* p, StrPair* ) +{ + // Comment parses as text. + const char* start = p; + p = value.ParseText( p, "-->", StrPair::COMMENT ); + if ( p == 0 ) { + document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 ); + } + return p; +} + + +XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = document; + } + XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern? + return comment; +} + + +bool XMLComment::ShallowEqual( const XMLNode* compare ) const +{ + return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() )); +} + + +bool XMLComment::Accept( XMLVisitor* visitor ) const +{ + return visitor->Visit( *this ); +} + + +// --------- XMLDeclaration ---------- // + +XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLDeclaration::~XMLDeclaration() +{ + //printf( "~XMLDeclaration\n" ); +} + + +char* XMLDeclaration::ParseDeep( char* p, StrPair* ) +{ + // Declaration parses as text. + const char* start = p; + p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION ); + if ( p == 0 ) { + document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 ); + } + return p; +} + + +XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = document; + } + XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern? + return dec; +} + + +bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const +{ + return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() )); +} + + + +bool XMLDeclaration::Accept( XMLVisitor* visitor ) const +{ + return visitor->Visit( *this ); +} + +// --------- XMLUnknown ---------- // + +XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLUnknown::~XMLUnknown() +{ +} + + +char* XMLUnknown::ParseDeep( char* p, StrPair* ) +{ + // Unknown parses as text. + const char* start = p; + + p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION ); + if ( !p ) { + document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 ); + } + return p; +} + + +XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = document; + } + XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern? + return text; +} + + +bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const +{ + return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() )); +} + + +bool XMLUnknown::Accept( XMLVisitor* visitor ) const +{ + return visitor->Visit( *this ); +} + +// --------- XMLAttribute ---------- // +char* XMLAttribute::ParseDeep( char* p, bool processEntities ) +{ + p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME ); + if ( !p || !*p ) return 0; + + char endTag[2] = { *p, 0 }; + ++p; + p = value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES ); + //if ( value.Empty() ) return 0; + return p; +} + + +void XMLAttribute::SetName( const char* n ) +{ + name.SetStr( n ); +} + + +int XMLAttribute::QueryIntValue( int* value ) const +{ + if ( TIXML_SSCANF( Value(), "%d", value ) == 1 ) + return XML_NO_ERROR; + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +int XMLAttribute::QueryUnsignedValue( unsigned int* value ) const +{ + if ( TIXML_SSCANF( Value(), "%u", value ) == 1 ) + return XML_NO_ERROR; + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +int XMLAttribute::QueryBoolValue( bool* value ) const +{ + int ival = -1; + QueryIntValue( &ival ); + + if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) { + *value = true; + return XML_NO_ERROR; + } + else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) { + *value = false; + return XML_NO_ERROR; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +int XMLAttribute::QueryDoubleValue( double* value ) const +{ + if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 ) + return XML_NO_ERROR; + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +int XMLAttribute::QueryFloatValue( float* value ) const +{ + if ( TIXML_SSCANF( Value(), "%f", value ) == 1 ) + return XML_NO_ERROR; + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +void XMLAttribute::SetAttribute( const char* v ) +{ + value.SetStr( v ); +} + + +void XMLAttribute::SetAttribute( int v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v ); + value.SetStr( buf ); +} + + +void XMLAttribute::SetAttribute( unsigned v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%u", v ); + value.SetStr( buf ); +} + + +void XMLAttribute::SetAttribute( bool v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v ? 1 : 0 ); + value.SetStr( buf ); +} + +void XMLAttribute::SetAttribute( double v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%f", v ); + value.SetStr( buf ); +} + +void XMLAttribute::SetAttribute( float v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%f", v ); + value.SetStr( buf ); +} + + +// --------- XMLElement ---------- // +XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), + closingType( 0 ), + rootAttribute( 0 ) +{ +} + + +XMLElement::~XMLElement() +{ + while( rootAttribute ) { + XMLAttribute* next = rootAttribute->next; + DELETE_ATTRIBUTE( rootAttribute ); + rootAttribute = next; + } +} + + +XMLAttribute* XMLElement::FindAttribute( const char* name ) +{ + XMLAttribute* a = 0; + for( a=rootAttribute; a; a = a->next ) { + if ( XMLUtil::StringEqual( a->Name(), name ) ) + return a; + } + return 0; +} + + +const XMLAttribute* XMLElement::FindAttribute( const char* name ) const +{ + XMLAttribute* a = 0; + for( a=rootAttribute; a; a = a->next ) { + if ( XMLUtil::StringEqual( a->Name(), name ) ) + return a; + } + return 0; +} + + +const char* XMLElement::Attribute( const char* name, const char* value ) const +{ + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) + return 0; + if ( !value || XMLUtil::StringEqual( a->Value(), value )) + return a->Value(); + return 0; +} + + +const char* XMLElement::GetText() const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + return FirstChild()->ToText()->Value(); + } + return 0; +} + + + +XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) +{ + XMLAttribute* attrib = FindAttribute( name ); + if ( !attrib ) { + attrib = new (document->attributePool.Alloc() ) XMLAttribute(); + attrib->memPool = &document->attributePool; + LinkAttribute( attrib ); + attrib->SetName( name ); + } + return attrib; +} + + +void XMLElement::LinkAttribute( XMLAttribute* attrib ) +{ + if ( rootAttribute ) { + XMLAttribute* end = rootAttribute; + while ( end->next ) + end = end->next; + end->next = attrib; + } + else { + rootAttribute = attrib; + } +} + + +void XMLElement::DeleteAttribute( const char* name ) +{ + XMLAttribute* prev = 0; + for( XMLAttribute* a=rootAttribute; a; a=a->next ) { + if ( XMLUtil::StringEqual( name, a->Name() ) ) { + if ( prev ) { + prev->next = a->next; + } + else { + rootAttribute = a->next; + } + DELETE_ATTRIBUTE( a ); + break; + } + prev = a; + } +} + + +char* XMLElement::ParseAttributes( char* p ) +{ + const char* start = p; + + // Read the attributes. + while( p ) { + p = XMLUtil::SkipWhiteSpace( p ); + if ( !p || !(*p) ) { + document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() ); + return 0; + } + + // attribute. + if ( XMLUtil::IsAlpha( *p ) ) { + XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute(); + attrib->memPool = &document->attributePool; + + p = attrib->ParseDeep( p, document->ProcessEntities() ); + if ( !p || Attribute( attrib->Name() ) ) { + DELETE_ATTRIBUTE( attrib ); + document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p ); + return 0; + } + LinkAttribute( attrib ); + } + // end of the tag + else if ( *p == '/' && *(p+1) == '>' ) { + closingType = CLOSED; + return p+2; // done; sealed element. + } + // end of the tag + else if ( *p == '>' ) { + ++p; + break; + } + else { + document->SetError( XML_ERROR_PARSING_ELEMENT, start, p ); + return 0; + } + } + return p; +} + + +// +// +// foobar +// +char* XMLElement::ParseDeep( char* p, StrPair* strPair ) +{ + // Read the element name. + p = XMLUtil::SkipWhiteSpace( p ); + if ( !p ) return 0; + + // The closing element is the form. It is + // parsed just like a regular element then deleted from + // the DOM. + if ( *p == '/' ) { + closingType = CLOSING; + ++p; + } + + p = value.ParseName( p ); + if ( value.Empty() ) return 0; + + p = ParseAttributes( p ); + if ( !p || !*p || closingType ) + return p; + + p = XMLNode::ParseDeep( p, strPair ); + return p; +} + + + +XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = document; + } + XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern? + for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) { + element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern? + } + return element; +} + + +bool XMLElement::ShallowEqual( const XMLNode* compare ) const +{ + const XMLElement* other = compare->ToElement(); + if ( other && XMLUtil::StringEqual( other->Value(), Value() )) { + + const XMLAttribute* a=FirstAttribute(); + const XMLAttribute* b=other->FirstAttribute(); + + while ( a && b ) { + if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) { + return false; + } + } + if ( a || b ) { + // different count + return false; + } + return true; + } + return false; +} + + +bool XMLElement::Accept( XMLVisitor* visitor ) const +{ + if ( visitor->VisitEnter( *this, rootAttribute ) ) + { + for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) + { + if ( !node->Accept( visitor ) ) + break; + } + } + return visitor->VisitExit( *this ); +} + + +// --------- XMLDocument ----------- // +XMLDocument::XMLDocument( bool _processEntities ) : + XMLNode( 0 ), + writeBOM( false ), + processEntities( _processEntities ), + errorID( 0 ), + errorStr1( 0 ), + errorStr2( 0 ), + charBuffer( 0 ) +{ + document = this; // avoid warning about 'this' in initializer list +} + + +XMLDocument::~XMLDocument() +{ + DeleteChildren(); + delete [] charBuffer; + +#if 0 + textPool.Trace( "text" ); + elementPool.Trace( "element" ); + commentPool.Trace( "comment" ); + attributePool.Trace( "attribute" ); +#endif + + TIXMLASSERT( textPool.CurrentAllocs() == 0 ); + TIXMLASSERT( elementPool.CurrentAllocs() == 0 ); + TIXMLASSERT( commentPool.CurrentAllocs() == 0 ); + TIXMLASSERT( attributePool.CurrentAllocs() == 0 ); +} + + +void XMLDocument::InitDocument() +{ + errorID = XML_NO_ERROR; + errorStr1 = 0; + errorStr2 = 0; + + delete [] charBuffer; + charBuffer = 0; + +} + + +XMLElement* XMLDocument::NewElement( const char* name ) +{ + XMLElement* ele = new (elementPool.Alloc()) XMLElement( this ); + ele->memPool = &elementPool; + ele->SetName( name ); + return ele; +} + + +XMLComment* XMLDocument::NewComment( const char* str ) +{ + XMLComment* comment = new (commentPool.Alloc()) XMLComment( this ); + comment->memPool = &commentPool; + comment->SetValue( str ); + return comment; +} + + +XMLText* XMLDocument::NewText( const char* str ) +{ + XMLText* text = new (textPool.Alloc()) XMLText( this ); + text->memPool = &textPool; + text->SetValue( str ); + return text; +} + + +XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) +{ + XMLDeclaration* dec = new (commentPool.Alloc()) XMLDeclaration( this ); + dec->memPool = &commentPool; + dec->SetValue( str ); + return dec; +} + + +XMLUnknown* XMLDocument::NewUnknown( const char* str ) +{ + XMLUnknown* unk = new (commentPool.Alloc()) XMLUnknown( this ); + unk->memPool = &commentPool; + unk->SetValue( str ); + return unk; +} + + +int XMLDocument::LoadFile( const char* filename ) +{ + DeleteChildren(); + InitDocument(); + +#if defined(_MSC_VER) +#pragma warning ( push ) +#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated. +#endif + FILE* fp = fopen( filename, "rb" ); +#if defined(_MSC_VER) +#pragma warning ( pop ) +#endif + if ( !fp ) { + SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 ); + return errorID; + } + LoadFile( fp ); + fclose( fp ); + return errorID; +} + + +int XMLDocument::LoadFile( FILE* fp ) +{ + DeleteChildren(); + InitDocument(); + + fseek( fp, 0, SEEK_END ); + unsigned size = ftell( fp ); + fseek( fp, 0, SEEK_SET ); + + if ( size == 0 ) { + return errorID; + } + + charBuffer = new char[size+1]; + fread( charBuffer, size, 1, fp ); + charBuffer[size] = 0; + + const char* p = charBuffer; + p = XMLUtil::SkipWhiteSpace( p ); + p = XMLUtil::ReadBOM( p, &writeBOM ); + if ( !p || !*p ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return errorID; + } + + ParseDeep( charBuffer + (p-charBuffer), 0 ); + return errorID; +} + + +void XMLDocument::SaveFile( const char* filename ) +{ +#if defined(_MSC_VER) +#pragma warning ( push ) +#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated. +#endif + FILE* fp = fopen( filename, "w" ); +#if defined(_MSC_VER) +#pragma warning ( pop ) +#endif + if ( fp ) { + XMLPrinter stream( fp ); + Print( &stream ); + fclose( fp ); + } + else { + SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 ); + } +} + + +int XMLDocument::Parse( const char* p ) +{ + DeleteChildren(); + InitDocument(); + + if ( !p || !*p ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return errorID; + } + p = XMLUtil::SkipWhiteSpace( p ); + p = XMLUtil::ReadBOM( p, &writeBOM ); + if ( !p || !*p ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return errorID; + } + + size_t len = strlen( p ); + charBuffer = new char[ len+1 ]; + memcpy( charBuffer, p, len+1 ); + + + ParseDeep( charBuffer, 0 ); + return errorID; +} + + +void XMLDocument::Print( XMLPrinter* streamer ) +{ + XMLPrinter stdStreamer( stdout ); + if ( !streamer ) + streamer = &stdStreamer; + Accept( streamer ); +} + + +void XMLDocument::SetError( int error, const char* str1, const char* str2 ) +{ + errorID = error; + errorStr1 = str1; + errorStr2 = str2; +} + + +void XMLDocument::PrintError() const +{ + if ( errorID ) { + static const int LEN = 20; + char buf1[LEN] = { 0 }; + char buf2[LEN] = { 0 }; + + if ( errorStr1 ) { + TIXML_SNPRINTF( buf1, LEN, "%s", errorStr1 ); + } + if ( errorStr2 ) { + TIXML_SNPRINTF( buf2, LEN, "%s", errorStr2 ); + } + + printf( "XMLDocument error id=%d str1=%s str2=%s\n", + errorID, buf1, buf2 ); + } +} + + +XMLPrinter::XMLPrinter( FILE* file ) : + elementJustOpened( false ), + firstElement( true ), + fp( file ), + depth( 0 ), + textDepth( -1 ), + processEntities( true ) +{ + for( int i=0; i'] = true; // not required, but consistency is nice + buffer.Push( 0 ); +} + + +void XMLPrinter::Print( const char* format, ... ) +{ + va_list va; + va_start( va, format ); + + if ( fp ) { + vfprintf( fp, format, va ); + } + else { + // This seems brutally complex. Haven't figured out a better + // way on windows. + #ifdef _MSC_VER + int len = -1; + int expand = 1000; + while ( len < 0 ) { + len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), _TRUNCATE, format, va ); + if ( len < 0 ) { + expand *= 3/2; + accumulator.PushArr( expand ); + } + } + char* p = buffer.PushArr( len ) - 1; + memcpy( p, accumulator.Mem(), len+1 ); + #else + int len = vsnprintf( 0, 0, format, va ); + // Close out and re-start the va-args + va_end( va ); + va_start( va, format ); + char* p = buffer.PushArr( len ) - 1; + vsnprintf( p, len+1, format, va ); + #endif + } + va_end( va ); +} + + +void XMLPrinter::PrintSpace( int depth ) +{ + for( int i=0; i 0 && *q < ENTITY_RANGE ) { + // Check for entities. If one is found, flush + // the stream up until the entity, write the + // entity, and keep looking. + if ( flag[(unsigned)(*q)] ) { + while ( p < q ) { + Print( "%c", *p ); + ++p; + } + for( int i=0; i 0) ) { + Print( "%s", p ); + } +} + + +void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) +{ + static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 }; + if ( writeBOM ) { + Print( "%s", bom ); + } + if ( writeDec ) { + PushDeclaration( "xml version=\"1.0\"" ); + } +} + + +void XMLPrinter::OpenElement( const char* name ) +{ + if ( elementJustOpened ) { + SealElement(); + } + stack.Push( name ); + + if ( textDepth < 0 && !firstElement ) { + Print( "\n" ); + PrintSpace( depth ); + } + + Print( "<%s", name ); + elementJustOpened = true; + firstElement = false; + ++depth; +} + + +void XMLPrinter::PushAttribute( const char* name, const char* value ) +{ + TIXMLASSERT( elementJustOpened ); + Print( " %s=\"", name ); + PrintString( value, false ); + Print( "\"" ); +} + + +void XMLPrinter::PushAttribute( const char* name, int v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, unsigned v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%u", v ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, bool v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v ? 1 : 0 ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, double v ) +{ + char buf[BUF_SIZE]; + TIXML_SNPRINTF( buf, BUF_SIZE, "%f", v ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::CloseElement() +{ + --depth; + const char* name = stack.Pop(); + + if ( elementJustOpened ) { + Print( "/>" ); + } + else { + if ( textDepth < 0 ) { + Print( "\n" ); + PrintSpace( depth ); + } + Print( "", name ); + } + + if ( textDepth == depth ) + textDepth = -1; + if ( depth == 0 ) + Print( "\n" ); + elementJustOpened = false; +} + + +void XMLPrinter::SealElement() +{ + elementJustOpened = false; + Print( ">" ); +} + + +void XMLPrinter::PushText( const char* text, bool cdata ) +{ + textDepth = depth-1; + + if ( elementJustOpened ) { + SealElement(); + } + if ( cdata ) { + Print( "" ); + } + else { + PrintString( text, true ); + } +} + + +void XMLPrinter::PushComment( const char* comment ) +{ + if ( elementJustOpened ) { + SealElement(); + } + if ( textDepth < 0 && !firstElement ) { + Print( "\n" ); + PrintSpace( depth ); + } + firstElement = false; + Print( "", comment ); +} + + +void XMLPrinter::PushDeclaration( const char* value ) +{ + if ( elementJustOpened ) { + SealElement(); + } + if ( textDepth < 0 && !firstElement) { + Print( "\n" ); + PrintSpace( depth ); + } + firstElement = false; + Print( "", value ); +} + + +void XMLPrinter::PushUnknown( const char* value ) +{ + if ( elementJustOpened ) { + SealElement(); + } + if ( textDepth < 0 && !firstElement ) { + Print( "\n" ); + PrintSpace( depth ); + } + firstElement = false; + Print( "", value ); +} + + +bool XMLPrinter::VisitEnter( const XMLDocument& doc ) +{ + processEntities = doc.ProcessEntities(); + if ( doc.HasBOM() ) { + PushHeader( true, false ); + } + return true; +} + + +bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) +{ + OpenElement( element.Name() ); + while ( attribute ) { + PushAttribute( attribute->Name(), attribute->Value() ); + attribute = attribute->Next(); + } + return true; +} + + +bool XMLPrinter::VisitExit( const XMLElement& ) +{ + CloseElement(); + return true; +} + + +bool XMLPrinter::Visit( const XMLText& text ) +{ + PushText( text.Value(), text.CData() ); + return true; +} + + +bool XMLPrinter::Visit( const XMLComment& comment ) +{ + PushComment( comment.Value() ); + return true; +} + +bool XMLPrinter::Visit( const XMLDeclaration& declaration ) +{ + PushDeclaration( declaration.Value() ); + return true; +} + + +bool XMLPrinter::Visit( const XMLUnknown& unknown ) +{ + PushUnknown( unknown.Value() ); + return true; +} diff --git a/KREngine/KREngine/Classes/tinyxml2.h b/KREngine/KREngine/Classes/tinyxml2.h new file mode 100644 index 0000000..feffd36 --- /dev/null +++ b/KREngine/KREngine/Classes/tinyxml2.h @@ -0,0 +1,1389 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef TINYXML2_INCLUDED +#define TINYXML2_INCLUDED + +#if 1 + #include + #include + #include + #include + #include +#else + // Not completely sure all the interesting systems + // can handle the new headers; can switch this if + // there is an include problem. + #include + #include + #include + #include // Needed by mac. +#endif + + +/* + TODO: add 'lastAttribute' for faster parsing. + TODO: intern strings instead of allocation. +*/ +/* + gcc: g++ -Wall tinyxml2.cpp xmltest.cpp -o gccxmltest.exe +*/ + +#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__) + #ifndef DEBUG + #define DEBUG + #endif +#endif + + +#if defined(DEBUG) + #if defined(_MSC_VER) + #define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak() + #elif defined (ANDROID_NDK) + #include + #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); } + #else + #include + #define TIXMLASSERT assert + #endif +#else + #define TIXMLASSERT( x ) {} +#endif + + +#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) + // Microsoft visual studio, version 2005 and higher. + /*int _snprintf_s( + char *buffer, + size_t sizeOfBuffer, + size_t count, + const char *format [, + argument] ... + );*/ + inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) { + va_list va; + va_start( va, format ); + int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); + va_end( va ); + return result; + } + #define TIXML_SSCANF sscanf_s +#else + // GCC version 3 and higher + //#warning( "Using sn* functions." ) + #define TIXML_SNPRINTF snprintf + #define TIXML_SSCANF sscanf +#endif + +static const int TIXML2_MAJOR_VERSION = 0; +static const int TIXML2_MINOR_VERSION = 9; +static const int TIXML2_PATCH_VERSION = 4; + +namespace tinyxml2 +{ +class XMLDocument; +class XMLElement; +class XMLAttribute; +class XMLComment; +class XMLNode; +class XMLText; +class XMLDeclaration; +class XMLUnknown; + +class XMLPrinter; + +/* + A class that wraps strings. Normally stores the start and end + pointers into the XML file itself, and will apply normalization + and entity translation if actually read. Can also store (and memory + manage) a traditional char[] +*/ +class StrPair +{ +public: + enum { + NEEDS_ENTITY_PROCESSING = 0x01, + NEEDS_NEWLINE_NORMALIZATION = 0x02, + + TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, + TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, + ATTRIBUTE_NAME = 0, + ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, + ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, + COMMENT = NEEDS_NEWLINE_NORMALIZATION + }; + + StrPair() : flags( 0 ), start( 0 ), end( 0 ) {} + ~StrPair(); + + void Set( char* _start, char* _end, int _flags ) { + Reset(); + this->start = _start; this->end = _end; this->flags = _flags | NEEDS_FLUSH; + } + const char* GetStr(); + bool Empty() const { return start == end; } + + void SetInternedStr( const char* str ) { Reset(); this->start = const_cast(str); } + void SetStr( const char* str, int flags=0 ); + + char* ParseText( char* in, const char* endTag, int strFlags ); + char* ParseName( char* in ); + + +private: + void Reset(); + + enum { + NEEDS_FLUSH = 0x100, + NEEDS_DELETE = 0x200 + }; + + // After parsing, if *end != 0, it can be set to zero. + int flags; + char* start; + char* end; +}; + + +/* + A dynamic array of Plain Old Data. Doesn't support constructors, etc. + Has a small initial memory pool, so that low or no usage will not + cause a call to new/delete +*/ +template +class DynArray +{ +public: + DynArray< T, INIT >() + { + mem = pool; + allocated = INIT; + size = 0; + } + ~DynArray() + { + if ( mem != pool ) { + delete mem; + } + } + void Push( T t ) + { + EnsureCapacity( size+1 ); + mem[size++] = t; + } + + T* PushArr( int count ) + { + EnsureCapacity( size+count ); + T* ret = &mem[size]; + size += count; + return ret; + } + T Pop() { + return mem[--size]; + } + void PopArr( int count ) + { + TIXMLASSERT( size >= count ); + size -= count; + } + + bool Empty() const { return size == 0; } + T& operator[](int i) { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; } + const T& operator[](int i) const { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; } + int Size() const { return size; } + int Capacity() const { return allocated; } + const T* Mem() const { return mem; } + T* Mem() { return mem; } + + +private: + void EnsureCapacity( int cap ) { + if ( cap > allocated ) { + int newAllocated = cap * 2; + T* newMem = new T[newAllocated]; + memcpy( newMem, mem, sizeof(T)*size ); // warning: not using constructors, only works for PODs + if ( mem != pool ) delete [] mem; + mem = newMem; + allocated = newAllocated; + } + } + + T* mem; + T pool[INIT]; + int allocated; // objects allocated + int size; // number objects in use +}; + + +/* + Parent virtual class a a pool for fast allocation + and deallocation of objects. +*/ +class MemPool +{ +public: + MemPool() {} + virtual ~MemPool() {} + + virtual int ItemSize() const = 0; + virtual void* Alloc() = 0; + virtual void Free( void* ) = 0; +}; + + +/* + Template child class to create pools of the correct type. +*/ +template< int SIZE > +class MemPoolT : public MemPool +{ +public: + MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {} + ~MemPoolT() { + // Delete the blocks. + for( int i=0; ichunk[i].next = &block->chunk[i+1]; + } + block->chunk[COUNT-1].next = 0; + root = block->chunk; + } + void* result = root; + root = root->next; + + ++currentAllocs; + if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs; + nAllocs++; + return result; + } + virtual void Free( void* mem ) { + if ( !mem ) return; + --currentAllocs; + Chunk* chunk = (Chunk*)mem; + memset( chunk, 0xfe, sizeof(Chunk) ); + chunk->next = root; + root = chunk; + } + void Trace( const char* name ) { + printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n", + name, maxAllocs, maxAllocs*SIZE/1024, currentAllocs, SIZE, nAllocs, blockPtrs.Size() ); + } + +private: + enum { COUNT = 1024/SIZE }; + union Chunk { + Chunk* next; + char mem[SIZE]; + }; + struct Block { + Chunk chunk[COUNT]; + }; + DynArray< Block*, 10 > blockPtrs; + Chunk* root; + + int currentAllocs; + int nAllocs; + int maxAllocs; +}; + + + +/** + Implements the interface to the "Visitor pattern" (see the Accept() method.) + If you call the Accept() method, it requires being passed a XMLVisitor + class to handle callbacks. For nodes that contain other nodes (Document, Element) + you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves + are simply called with Visit(). + + If you return 'true' from a Visit method, recursive parsing will continue. If you return + false, no children of this node or its sibilings will be Visited. + + All flavors of Visit methods have a default implementation that returns 'true' (continue + visiting). You need to only override methods that are interesting to you. + + Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting. + + You should never change the document from a callback. + + @sa XMLNode::Accept() +*/ +class XMLVisitor +{ +public: + virtual ~XMLVisitor() {} + + /// Visit a document. + virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; } + /// Visit a document. + virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; } + + /// Visit an element. + virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { return true; } + /// Visit an element. + virtual bool VisitExit( const XMLElement& /*element*/ ) { return true; } + + /// Visit a declaration + virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { return true; } + /// Visit a text node + virtual bool Visit( const XMLText& /*text*/ ) { return true; } + /// Visit a comment node + virtual bool Visit( const XMLComment& /*comment*/ ) { return true; } + /// Visit an unknown node + virtual bool Visit( const XMLUnknown& /*unknown*/ ) { return true; } +}; + + +/* + Utility functionality. +*/ +class XMLUtil +{ +public: + // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't + // correct, but simple, and usually works. + static const char* SkipWhiteSpace( const char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; } + static char* SkipWhiteSpace( char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; } + + inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) { + int n = 0; + if ( p == q ) { + return true; + } + while( *p && *q && *p == *q && n(const_cast(this)->FirstChildElement( _value )); } + + /// Get the last child node, or null if none exists. + const XMLNode* LastChild() const { return lastChild; } + XMLNode* LastChild() { return const_cast(const_cast(this)->LastChild() ); } + + /** Get the last child element or optionally the last child + element with the specified name. + */ + const XMLElement* LastChildElement( const char* value=0 ) const; + XMLElement* LastChildElement( const char* _value=0 ) { return const_cast(const_cast(this)->LastChildElement(_value) ); } + + /// Get the previous (left) sibling node of this node. + const XMLNode* PreviousSibling() const { return prev; } + XMLNode* PreviousSibling() { return prev; } + + /// Get the previous (left) sibling element of this node, with an opitionally supplied name. + const XMLElement* PreviousSiblingElement( const char* value=0 ) const ; + XMLElement* PreviousSiblingElement( const char* _value=0 ) { return const_cast(const_cast(this)->PreviousSiblingElement( _value ) ); } + + /// Get the next (right) sibling node of this node. + const XMLNode* NextSibling() const { return next; } + XMLNode* NextSibling() { return next; } + + /// Get the next (right) sibling element of this node, with an opitionally supplied name. + const XMLElement* NextSiblingElement( const char* value=0 ) const; + XMLElement* NextSiblingElement( const char* _value=0 ) { return const_cast(const_cast(this)->NextSiblingElement( _value ) ); } + + /** + Add a child node as the last (right) child. + */ + XMLNode* InsertEndChild( XMLNode* addThis ); + + XMLNode* LinkEndChild( XMLNode* addThis ) { return InsertEndChild( addThis ); } + /** + Add a child node as the first (left) child. + */ + XMLNode* InsertFirstChild( XMLNode* addThis ); + /** + Add a node after the specified child node. + */ + XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ); + + /** + Delete all the children of this node. + */ + void DeleteChildren(); + + /** + Delete a child of this node. + */ + void DeleteChild( XMLNode* node ); + + /** + Make a copy of this node, but not its children. + You may pass in a Document pointer that will be + the owner of the new Node. If the 'document' is + null, then the node returned will be allocated + from the current Document. (this->GetDocument()) + + Note: if called on a XMLDocument, this will return null. + */ + virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0; + + /** + Test if 2 nodes are the same, but don't test children. + The 2 nodes do not need to be in the same Document. + + Note: if called on a XMLDocument, this will return false. + */ + virtual bool ShallowEqual( const XMLNode* compare ) const = 0; + + /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the + XML tree will be conditionally visited and the host will be called back + via the TiXmlVisitor interface. + + This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse + the XML for the callbacks, so the performance of TinyXML is unchanged by using this + interface versus any other.) + + The interface has been based on ideas from: + + - http://www.saxproject.org/ + - http://c2.com/cgi/wiki?HierarchicalVisitorPattern + + Which are both good references for "visiting". + + An example of using Accept(): + @verbatim + TiXmlPrinter printer; + tinyxmlDoc.Accept( &printer ); + const char* xmlcstr = printer.CStr(); + @endverbatim + */ + virtual bool Accept( XMLVisitor* visitor ) const = 0; + + // internal + virtual char* ParseDeep( char*, StrPair* ); + +protected: + XMLNode( XMLDocument* ); + virtual ~XMLNode(); + XMLNode( const XMLNode& ); // not supported + void operator=( const XMLNode& ); // not supported + + XMLDocument* document; + XMLNode* parent; + mutable StrPair value; + + XMLNode* firstChild; + XMLNode* lastChild; + + XMLNode* prev; + XMLNode* next; + +private: + MemPool* memPool; + void Unlink( XMLNode* child ); +}; + + +/** XML text. + + Note that a text node can have child element nodes, for example: + @verbatim + This is bold + @endverbatim + + A text node can have 2 ways to output the next. "normal" output + and CDATA. It will default to the mode it was parsed from the XML file and + you generally want to leave it alone, but you can change the output mode with + SetCDATA() and query it with CDATA(). +*/ +class XMLText : public XMLNode +{ + friend class XMLBase; + friend class XMLDocument; +public: + virtual bool Accept( XMLVisitor* visitor ) const; + + virtual XMLText* ToText() { return this; } + virtual const XMLText* ToText() const { return this; } + + /// Declare whether this should be CDATA or standard text. + void SetCData( bool _isCData ) { this->isCData = _isCData; } + /// Returns true if this is a CDATA text element. + bool CData() const { return isCData; } + + char* ParseDeep( char*, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + + +protected: + XMLText( XMLDocument* doc ) : XMLNode( doc ), isCData( false ) {} + virtual ~XMLText() {} + XMLText( const XMLText& ); // not supported + void operator=( const XMLText& ); // not supported + +private: + bool isCData; +}; + + +/** An XML Comment. */ +class XMLComment : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLComment* ToComment() { return this; } + virtual const XMLComment* ToComment() const { return this; } + + virtual bool Accept( XMLVisitor* visitor ) const; + + char* ParseDeep( char*, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +protected: + XMLComment( XMLDocument* doc ); + virtual ~XMLComment(); + XMLComment( const XMLComment& ); // not supported + void operator=( const XMLComment& ); // not supported + +private: +}; + + +/** In correct XML the declaration is the first entry in the file. + @verbatim + + @endverbatim + + TinyXML2 will happily read or write files without a declaration, + however. + + The text of the declaration isn't interpreted. It is parsed + and written as a string. +*/ +class XMLDeclaration : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLDeclaration* ToDeclaration() { return this; } + virtual const XMLDeclaration* ToDeclaration() const { return this; } + + virtual bool Accept( XMLVisitor* visitor ) const; + + char* ParseDeep( char*, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +protected: + XMLDeclaration( XMLDocument* doc ); + virtual ~XMLDeclaration(); + XMLDeclaration( const XMLDeclaration& ); // not supported + void operator=( const XMLDeclaration& ); // not supported +}; + + +/** Any tag that tinyXml doesn't recognize is saved as an + unknown. It is a tag of text, but should not be modified. + It will be written back to the XML, unchanged, when the file + is saved. + + DTD tags get thrown into TiXmlUnknowns. +*/ +class XMLUnknown : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLUnknown* ToUnknown() { return this; } + virtual const XMLUnknown* ToUnknown() const { return this; } + + virtual bool Accept( XMLVisitor* visitor ) const; + + char* ParseDeep( char*, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +protected: + XMLUnknown( XMLDocument* doc ); + virtual ~XMLUnknown(); + XMLUnknown( const XMLUnknown& ); // not supported + void operator=( const XMLUnknown& ); // not supported +}; + + +enum { + XML_NO_ERROR = 0, + XML_SUCCESS = 0, + + XML_NO_ATTRIBUTE, + XML_WRONG_ATTRIBUTE_TYPE, + + XML_ERROR_FILE_NOT_FOUND, + XML_ERROR_FILE_COULD_NOT_BE_OPENED, + XML_ERROR_ELEMENT_MISMATCH, + XML_ERROR_PARSING_ELEMENT, + XML_ERROR_PARSING_ATTRIBUTE, + XML_ERROR_IDENTIFYING_TAG, + XML_ERROR_PARSING_TEXT, + XML_ERROR_PARSING_CDATA, + XML_ERROR_PARSING_COMMENT, + XML_ERROR_PARSING_DECLARATION, + XML_ERROR_PARSING_UNKNOWN, + XML_ERROR_EMPTY_DOCUMENT, + XML_ERROR_MISMATCHED_ELEMENT, + XML_ERROR_PARSING +}; + + +/** An attribute is a name-value pair. Elements have an arbitrary + number of attributes, each with a unique name. + + @note The attributes are not XMLNodes. You may only query the + Next() attribute in a list. +*/ +class XMLAttribute +{ + friend class XMLElement; +public: + const char* Name() const { return name.GetStr(); } ///< The name of the attribute. + const char* Value() const { return value.GetStr(); } ///< The value of the attribute. + const XMLAttribute* Next() const { return next; } ///< The next attribute in the list. + + /** IntAttribute interprets the attribute as an integer, and returns the value. + If the value isn't an integer, 0 will be returned. There is no error checking; + use QueryIntAttribute() if you need error checking. + */ + int IntValue() const { int i=0; QueryIntValue( &i ); return i; } + /// Query as an unsigned integer. See IntAttribute() + unsigned UnsignedValue() const { unsigned i=0; QueryUnsignedValue( &i ); return i; } + /// Query as a boolean. See IntAttribute() + bool BoolValue() const { bool b=false; QueryBoolValue( &b ); return b; } + /// Query as a double. See IntAttribute() + double DoubleValue() const { double d=0; QueryDoubleValue( &d ); return d; } + /// Query as a float. See IntAttribute() + float FloatValue() const { float f=0; QueryFloatValue( &f ); return f; } + + /** QueryIntAttribute interprets the attribute as an integer, and returns the value + in the provided paremeter. The function will return XML_NO_ERROR on success, + and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful. + */ + int QueryIntValue( int* value ) const; + /// See QueryIntAttribute + int QueryUnsignedValue( unsigned int* value ) const; + /// See QueryIntAttribute + int QueryBoolValue( bool* value ) const; + /// See QueryIntAttribute + int QueryDoubleValue( double* value ) const; + /// See QueryIntAttribute + int QueryFloatValue( float* value ) const; + + /// Set the attribute to a string value. + void SetAttribute( const char* value ); + /// Set the attribute to value. + void SetAttribute( int value ); + /// Set the attribute to value. + void SetAttribute( unsigned value ); + /// Set the attribute to value. + void SetAttribute( bool value ); + /// Set the attribute to value. + void SetAttribute( double value ); + /// Set the attribute to value. + void SetAttribute( float value ); + +private: + enum { BUF_SIZE = 200 }; + + XMLAttribute() : next( 0 ) {} + virtual ~XMLAttribute() {} + XMLAttribute( const XMLAttribute& ); // not supported + void operator=( const XMLAttribute& ); // not supported + void SetName( const char* name ); + + char* ParseDeep( char* p, bool processEntities ); + + mutable StrPair name; + mutable StrPair value; + XMLAttribute* next; + MemPool* memPool; +}; + + +/** The element is a container class. It has a value, the element name, + and can contain other elements, text, comments, and unknowns. + Elements also contain an arbitrary number of attributes. +*/ +class XMLElement : public XMLNode +{ + friend class XMLBase; + friend class XMLDocument; +public: + /// Get the name of an element (which is the Value() of the node.) + const char* Name() const { return Value(); } + /// Set the name of the element. + void SetName( const char* str, bool staticMem=false ) { SetValue( str, staticMem ); } + + virtual XMLElement* ToElement() { return this; } + virtual const XMLElement* ToElement() const { return this; } + virtual bool Accept( XMLVisitor* visitor ) const; + + /** Given an attribute name, Attribute() returns the value + for the attribute of that name, or null if none + exists. For example: + + @verbatim + const char* value = ele->Attribute( "foo" ); + @endverbatim + + The 'value' parameter is normally null. However, if specified, + the attribute will only be returned if the 'name' and 'value' + match. This allow you to write code: + + @verbatim + if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar(); + @endverbatim + + rather than: + @verbatim + if ( ele->Attribute( "foo" ) ) { + if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar(); + } + @endverbatim + */ + const char* Attribute( const char* name, const char* value=0 ) const; + + /** Given an attribute name, IntAttribute() returns the value + of the attribute interpreted as an integer. 0 will be + returned if there is an error. For a method with error + checking, see QueryIntAttribute() + */ + int IntAttribute( const char* name ) const { int i=0; QueryIntAttribute( name, &i ); return i; } + /// See IntAttribute() + unsigned UnsignedAttribute( const char* name ) const{ unsigned i=0; QueryUnsignedAttribute( name, &i ); return i; } + /// See IntAttribute() + bool BoolAttribute( const char* name ) const { bool b=false; QueryBoolAttribute( name, &b ); return b; } + /// See IntAttribute() + double DoubleAttribute( const char* name ) const { double d=0; QueryDoubleAttribute( name, &d ); return d; } + /// See IntAttribute() + float FloatAttribute( const char* name ) const { float f=0; QueryFloatAttribute( name, &f ); return f; } + + /** Given an attribute name, QueryIntAttribute() returns + XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion + can't be performed, or XML_NO_ATTRIBUTE if the attribute + doesn't exist. If successful, the result of the conversion + will be written to 'value'. If not successful, nothing will + be written to 'value'. This allows you to provide default + value: + + @verbatim + int value = 10; + QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10 + @endverbatim + */ + int QueryIntAttribute( const char* name, int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryIntValue( _value ); } + /// See QueryIntAttribute() + int QueryUnsignedAttribute( const char* name, unsigned int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryUnsignedValue( _value ); } + /// See QueryIntAttribute() + int QueryBoolAttribute( const char* name, bool* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryBoolValue( _value ); } + /// See QueryIntAttribute() + int QueryDoubleAttribute( const char* name, double* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryDoubleValue( _value ); } + /// See QueryIntAttribute() + int QueryFloatAttribute( const char* name, float* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryFloatValue( _value ); } + + /// Sets the named attribute to value. + void SetAttribute( const char* name, const char* _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } + /// Sets the named attribute to value. + void SetAttribute( const char* name, int _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } + /// Sets the named attribute to value. + void SetAttribute( const char* name, unsigned _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } + /// Sets the named attribute to value. + void SetAttribute( const char* name, bool _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } + /// Sets the named attribute to value. + void SetAttribute( const char* name, double _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); } + + /** + Delete an attribute. + */ + void DeleteAttribute( const char* name ); + + /// Return the first attribute in the list. + const XMLAttribute* FirstAttribute() const { return rootAttribute; } + /// Query a specific attribute in the list. + const XMLAttribute* FindAttribute( const char* name ) const; + + /** Convenience function for easy access to the text inside an element. Although easy + and concise, GetText() is limited compared to getting the TiXmlText child + and accessing it directly. + + If the first child of 'this' is a TiXmlText, the GetText() + returns the character string of the Text node, else null is returned. + + This is a convenient method for getting the text of simple contained text: + @verbatim + This is text + const char* str = fooElement->GetText(); + @endverbatim + + 'str' will be a pointer to "This is text". + + Note that this function can be misleading. If the element foo was created from + this XML: + @verbatim + This is text + @endverbatim + + then the value of str would be null. The first child node isn't a text node, it is + another element. From this XML: + @verbatim + This is text + @endverbatim + GetText() will return "This is ". + */ + const char* GetText() const; + + // internal: + enum { + OPEN, // + CLOSED, // + CLOSING // + }; + int ClosingType() const { return closingType; } + char* ParseDeep( char* p, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +private: + XMLElement( XMLDocument* doc ); + virtual ~XMLElement(); + XMLElement( const XMLElement& ); // not supported + void operator=( const XMLElement& ); // not supported + + XMLAttribute* FindAttribute( const char* name ); + XMLAttribute* FindOrCreateAttribute( const char* name ); + void LinkAttribute( XMLAttribute* attrib ); + char* ParseAttributes( char* p ); + + int closingType; + XMLAttribute* rootAttribute; +}; + + +/** A document binds together all the functionality. + It can be saved, loaded, and printed to the screen. + All Nodes are connected and allocated to a Document. + If the Document is deleted, all its Nodes are also deleted. +*/ +class XMLDocument : public XMLNode +{ + friend class XMLElement; +public: + /// constructor + XMLDocument( bool processEntities = true ); + ~XMLDocument(); + + virtual XMLDocument* ToDocument() { return this; } + virtual const XMLDocument* ToDocument() const { return this; } + + /** + Parse an XML file from a character string. + Returns XML_NO_ERROR (0) on success, or + an errorID. + */ + int Parse( const char* xml ); + /** + Load an XML file from disk. + Returns XML_NO_ERROR (0) on success, or + an errorID. + */ + int LoadFile( const char* filename ); + /** + Load an XML file from disk. You are responsible + for providing and closing the FILE*. + + Returns XML_NO_ERROR (0) on success, or + an errorID. + */ + int LoadFile( FILE* ); + /** + Save the XML file to disk. + */ + void SaveFile( const char* filename ); + + bool ProcessEntities() const { return processEntities; } + + /** + Returns true if this document has a leading Byte Order Mark of UTF8. + */ + bool HasBOM() const { return writeBOM; } + + /** Return the root element of DOM. Equivalent to FirstChildElement(). + To get the first node, use FirstChild(). + */ + XMLElement* RootElement() { return FirstChildElement(); } + const XMLElement* RootElement() const { return FirstChildElement(); } + + /** Print the Document. If the Printer is not provided, it will + print to stdout. If you provide Printer, this can print to a file: + @verbatim + XMLPrinter printer( fp ); + doc.Print( &printer ); + @endverbatim + + Or you can use a printer to print to memory: + @verbatim + XMLPrinter printer; + doc->Print( &printer ); + // printer.CStr() has a const char* to the XML + @endverbatim + */ + void Print( XMLPrinter* streamer=0 ); + virtual bool Accept( XMLVisitor* visitor ) const; + + /** + Create a new Element associated with + this Document. The memory for the Element + is managed by the Document. + */ + XMLElement* NewElement( const char* name ); + /** + Create a new Comment associated with + this Document. The memory for the Comment + is managed by the Document. + */ + XMLComment* NewComment( const char* comment ); + /** + Create a new Text associated with + this Document. The memory for the Text + is managed by the Document. + */ + XMLText* NewText( const char* text ); + /** + Create a new Declaration associated with + this Document. The memory for the object + is managed by the Document. + */ + XMLDeclaration* NewDeclaration( const char* text ); + /** + Create a new Unknown associated with + this Document. The memory for the object + is managed by the Document. + */ + XMLUnknown* NewUnknown( const char* text ); + + /** + Delete a node associated with this documented. + It will be unlinked from the DOM. + */ + void DeleteNode( XMLNode* node ) { node->parent->DeleteChild( node ); } + + void SetError( int error, const char* str1, const char* str2 ); + + /// Return true if there was an error parsing the document. + bool Error() const { return errorID != XML_NO_ERROR; } + /// Return the errorID. + int ErrorID() const { return errorID; } + /// Return a possibly helpful diagnostic location or string. + const char* GetErrorStr1() const { return errorStr1; } + /// Return possibly helpful secondary diagnostic location or string. + const char* GetErrorStr2() const { return errorStr2; } + /// If there is an error, print it to stdout + void PrintError() const; + + // internal + char* Identify( char* p, XMLNode** node ); + + virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const { return 0; } + virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const { return false; } + +private: + XMLDocument( const XMLDocument& ); // not supported + void operator=( const XMLDocument& ); // not supported + void InitDocument(); + + bool writeBOM; + bool processEntities; + int errorID; + const char* errorStr1; + const char* errorStr2; + char* charBuffer; + + MemPoolT< sizeof(XMLElement) > elementPool; + MemPoolT< sizeof(XMLAttribute) > attributePool; + MemPoolT< sizeof(XMLText) > textPool; + MemPoolT< sizeof(XMLComment) > commentPool; +}; + + +/** + A XMLHandle is a class that wraps a node pointer with null checks; this is + an incredibly useful thing. Note that XMLHandle is not part of the TinyXML + DOM structure. It is a separate utility class. + + Take an example: + @verbatim + + + + + + + @endverbatim + + Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very + easy to write a *lot* of code that looks like: + + @verbatim + XMLElement* root = document.FirstChildElement( "Document" ); + if ( root ) + { + XMLElement* element = root->FirstChildElement( "Element" ); + if ( element ) + { + XMLElement* child = element->FirstChildElement( "Child" ); + if ( child ) + { + XMLElement* child2 = child->NextSiblingElement( "Child" ); + if ( child2 ) + { + // Finally do something useful. + @endverbatim + + And that doesn't even cover "else" cases. XMLHandle addresses the verbosity + of such code. A XMLHandle checks for null pointers so it is perfectly safe + and correct to use: + + @verbatim + XMLHandle docHandle( &document ); + XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement(); + if ( child2 ) + { + // do something useful + @endverbatim + + Which is MUCH more concise and useful. + + It is also safe to copy handles - internally they are nothing more than node pointers. + @verbatim + XMLHandle handleCopy = handle; + @endverbatim + + See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects. +*/ +class XMLHandle +{ +public: + /// Create a handle from any node (at any depth of the tree.) This can be a null pointer. + XMLHandle( XMLNode* _node ) { node = _node; } + /// Create a handle from a node. + XMLHandle( XMLNode& _node ) { node = &_node; } + /// Copy constructor + XMLHandle( const XMLHandle& ref ) { node = ref.node; } + /// Assignment + XMLHandle operator=( const XMLHandle& ref ) { node = ref.node; return *this; } + + /// Get the first child of this handle. + XMLHandle FirstChild() { return XMLHandle( node ? node->FirstChild() : 0 ); } + /// Get the first child element of this handle. + XMLHandle FirstChildElement( const char* value=0 ) { return XMLHandle( node ? node->FirstChildElement( value ) : 0 ); } + /// Get the last child of this handle. + XMLHandle LastChild() { return XMLHandle( node ? node->LastChild() : 0 ); } + /// Get the last child element of this handle. + XMLHandle LastChildElement( const char* _value=0 ) { return XMLHandle( node ? node->LastChildElement( _value ) : 0 ); } + /// Get the previous sibling of this handle. + XMLHandle PreviousSibling() { return XMLHandle( node ? node->PreviousSibling() : 0 ); } + /// Get the previous sibling element of this handle. + XMLHandle PreviousSiblingElement( const char* _value=0 ) { return XMLHandle( node ? node->PreviousSiblingElement( _value ) : 0 ); } + /// Get the next sibling of this handle. + XMLHandle NextSibling() { return XMLHandle( node ? node->NextSibling() : 0 ); } + /// Get the next sibling element of this handle. + XMLHandle NextSiblingElement( const char* _value=0 ) { return XMLHandle( node ? node->NextSiblingElement( _value ) : 0 ); } + + /// Safe cast to XMLNode. This can return null. + XMLNode* ToNode() { return node; } + /// Safe cast to XMLElement. This can return null. + XMLElement* ToElement() { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } + /// Safe cast to XMLText. This can return null. + XMLText* ToText() { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } + /// Safe cast to XMLUnknown. This can return null. + XMLUnknown* ToUnknown() { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } + /// Safe cast to XMLDeclaration. This can return null. + XMLDeclaration* ToDeclaration() { return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 0 ); } + +private: + XMLNode* node; +}; + + +/** + A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the + same in all regards, except for the 'const' qualifiers. See XMLHandle for API. +*/ +class XMLConstHandle +{ +public: + XMLConstHandle( const XMLNode* _node ) { node = _node; } + XMLConstHandle( const XMLNode& _node ) { node = &_node; } + XMLConstHandle( const XMLConstHandle& ref ) { node = ref.node; } + + XMLConstHandle operator=( const XMLConstHandle& ref ) { node = ref.node; return *this; } + + const XMLConstHandle FirstChild() const { return XMLConstHandle( node ? node->FirstChild() : 0 ); } + const XMLConstHandle FirstChildElement( const char* value=0 ) const { return XMLConstHandle( node ? node->FirstChildElement( value ) : 0 ); } + const XMLConstHandle LastChild() const { return XMLConstHandle( node ? node->LastChild() : 0 ); } + const XMLConstHandle LastChildElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->LastChildElement( _value ) : 0 ); } + const XMLConstHandle PreviousSibling() const { return XMLConstHandle( node ? node->PreviousSibling() : 0 ); } + const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->PreviousSiblingElement( _value ) : 0 ); } + const XMLConstHandle NextSibling() const { return XMLConstHandle( node ? node->NextSibling() : 0 ); } + const XMLConstHandle NextSiblingElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->NextSiblingElement( _value ) : 0 ); } + + + const XMLNode* ToNode() const { return node; } + const XMLElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } + const XMLText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } + const XMLUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } + const XMLDeclaration* ToDeclaration() const { return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 0 ); } + +private: + const XMLNode* node; +}; + + +/** + Printing functionality. The XMLPrinter gives you more + options than the XMLDocument::Print() method. + + It can: + -# Print to memory. + -# Print to a file you provide + -# Print XML without a XMLDocument. + + Print to Memory + + @verbatim + XMLPrinter printer; + doc->Print( &printer ); + SomeFunctior( printer.CStr() ); + @endverbatim + + Print to a File + + You provide the file pointer. + @verbatim + XMLPrinter printer( fp ); + doc.Print( &printer ); + @endverbatim + + Print without a XMLDocument + + When loading, an XML parser is very useful. However, sometimes + when saving, it just gets in the way. The code is often set up + for streaming, and constructing the DOM is just overhead. + + The Printer supports the streaming case. The following code + prints out a trivially simple XML file without ever creating + an XML document. + + @verbatim + XMLPrinter printer( fp ); + printer.OpenElement( "foo" ); + printer.PushAttribute( "foo", "bar" ); + printer.CloseElement(); + @endverbatim +*/ +class XMLPrinter : public XMLVisitor +{ +public: + /** Construct the printer. If the FILE* is specified, + this will print to the FILE. Else it will print + to memory, and the result is available in CStr() + */ + XMLPrinter( FILE* file=0 ); + ~XMLPrinter() {} + + /** If streaming, write the BOM and declaration. */ + void PushHeader( bool writeBOM, bool writeDeclaration ); + /** If streaming, start writing an element. + The element must be closed with CloseElement() + */ + void OpenElement( const char* name ); + /// If streaming, add an attribute to an open element. + void PushAttribute( const char* name, const char* value ); + void PushAttribute( const char* name, int value ); + void PushAttribute( const char* name, unsigned value ); + void PushAttribute( const char* name, bool value ); + void PushAttribute( const char* name, double value ); + /// If streaming, close the Element. + void CloseElement(); + + /// Add a text node. + void PushText( const char* text, bool cdata=false ); + /// Add a comment + void PushComment( const char* comment ); + + void PushDeclaration( const char* value ); + void PushUnknown( const char* value ); + + virtual bool VisitEnter( const XMLDocument& /*doc*/ ); + virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; } + + virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute ); + virtual bool VisitExit( const XMLElement& element ); + + virtual bool Visit( const XMLText& text ); + virtual bool Visit( const XMLComment& comment ); + virtual bool Visit( const XMLDeclaration& declaration ); + virtual bool Visit( const XMLUnknown& unknown ); + + /** + If in print to memory mode, return a pointer to + the XML file in memory. + */ + const char* CStr() const { return buffer.Mem(); } + +private: + void SealElement(); + void PrintSpace( int depth ); + void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities. + void Print( const char* format, ... ); + + bool elementJustOpened; + bool firstElement; + FILE* fp; + int depth; + int textDepth; + bool processEntities; + + enum { + ENTITY_RANGE = 64, + BUF_SIZE = 200 + }; + bool entityFlag[ENTITY_RANGE]; + bool restrictedEntityFlag[ENTITY_RANGE]; + + DynArray< const char*, 10 > stack; + DynArray< char, 20 > buffer, accumulator; +}; + + +} // tinyxml2 + + +#endif // TINYXML2_INCLUDED diff --git a/KREngine/KREngine/Classes/tinyxml2_readme.txt b/KREngine/KREngine/Classes/tinyxml2_readme.txt new file mode 100755 index 0000000..29226f7 --- /dev/null +++ b/KREngine/KREngine/Classes/tinyxml2_readme.txt @@ -0,0 +1,289 @@ +/** @mainpage + +

TinyXML-2

+ +TinyXML is a simple, small, efficient, C++ XML parser that can be +easily integrated into other programs. + +The master is hosted on github: +github.com/leethomason/tinyxml2 + +The online HTML version of these docs: +http://grinninglizard.com/tinyxml2docs/index.html + +Examples are in the "related pages" tab of the HTML docs. + +

What it does.

+ +In brief, TinyXML parses an XML document, and builds from that a +Document Object Model (DOM) that can be read, modified, and saved. + +XML stands for "eXtensible Markup Language." It is a general purpose +human and machine readable markup language to describe arbitrary data. +All those random file formats created to store application data can +all be replaced with XML. One parser for everything. + +http://en.wikipedia.org/wiki/XML + +There are different ways to access and interact with XML data. +TinyXML-2 uses a Document Object Model (DOM), meaning the XML data is parsed +into a C++ objects that can be browsed and manipulated, and then +written to disk or another output stream. You can also construct an XML document +from scratch with C++ objects and write this to disk or another output +stream. You can even use TinyXML-2 to stream XML programmatically from +code without creating a document first. + +TinyXML-2 is designed to be easy and fast to learn. It is one header and +one cpp file. Simply add these to your project and off you go. +There is an example file - xmltest.cpp - to get you started. + +TinyXML-2 is released under the ZLib license, +so you can use it in open source or commercial code. The details +of the license are at the top of every source file. + +TinyXML-2 attempts to be a flexible parser, but with truly correct and +compliant XML output. TinyXML-2 should compile on any reasonably C++ +compliant system. It does not rely on exceptions, RTTI, or the STL. + +

What it doesn't do.

+ +TinyXML-2 doesn't parse or use DTDs (Document Type Definitions) or XSLs +(eXtensible Stylesheet Language.) There are other parsers out there +that are much more fully +featured. But they are also much bigger, take longer to set up in +your project, have a higher learning curve, and often have a more +restrictive license. If you are working with browsers or have more +complete XML needs, TinyXML-2 is not the parser for you. + +

TinyXML-1 vs. TinyXML-2

+ +Which should you use? TinyXML-2 uses a similar API to TinyXML-1 and the same +rich test cases. But the implementation of the parser is completely re-written +to make it more appropriate for use in a game. It uses less memory, is faster, +and uses far few memory allocations. + +TinyXML-2 has no requirement for STL, but has also dropped all STL support. All +strings are query and set as 'const char*'. This allows the use of internal +allocators, and keeps the code much simpler. + +Both parsers: +
    +
  1. Simple to use with similar APIs.
  2. +
  3. DOM based parser.
  4. +
  5. UTF-8 Unicode support. http://en.wikipedia.org/wiki/UTF-8
  6. +
+ +Advantages of TinyXML-2 +
    +
  1. The focus of all future dev.
  2. +
  3. Many fewer memory allocation (1/10th to 1/100th), uses less memory (about 40% of TinyXML-1), and faster.
  4. +
  5. No STL requirement.
  6. +
  7. More modern C++, including a proper namespace.
  8. +
  9. Proper and useful handling of whitespace
  10. +
+ +Advantages of TinyXML-1 +
    +
  1. Can report the location of parsing errors.
  2. +
  3. Support for some C++ STL conventions: streams and strings
  4. +
  5. Very mature and well debugged code base.
  6. +
+ +

Features

+ +

Memory Model

+ +An XMLDocument is a C++ object like any other, that can be on the stack, or +new'd and deleted on the heap. + +However, any sub-node of the Document, XMLElement, XMLText, etc, can only +be created by calling the appropriate XMLDocument::NewElement, NewText, etc. +method. Although you have pointers to these objects, they are still owned +by the Document. When the Document is deleted, so are all the nodes it contains. + +

White Space

+ +Microsoft has an excellent article on white space: http://msdn.microsoft.com/en-us/library/ms256097.aspx + +TinyXML-2 preserves white space in a (hopefully) sane way that is almost complient with the +spec.(TinyXML-1 used a completely outdated model.) + +As a first step, all newlines / carriage-returns / line-feeds are normalized to a +line-feed character, as required by the XML spec. + +White space in text is preserved. For example: + + Hello, World + +The leading space before the "Hello" and the double space after the comma are +preserved. Line-feeds are preserved, as in this example: + + Hello again, + World + +However, white space between elements is *not* preserved. Although not strictly +compliant, tracking and reporting inter-element space is awkward, and not normally +valuable. TinyXML-2 sees these as the same XML: + + + 1 + 2 + 3 + + + 123 + +

Entities

+TinyXML-2 recognizes the pre-defined "character entities", meaning special +characters. Namely: + + & & + < < + > > + " " + ' ' + +These are recognized when the XML document is read, and translated to there +UTF-8 equivalents. For instance, text with the XML of: + + Far & Away + +will have the Value() of "Far & Away" when queried from the XMLText object, +and will be written back to the XML stream/file as an ampersand. + +Additionally, any character can be specified by its Unicode code point: +The syntax " " or " " are both to the non-breaking space characher. +This is called a 'numeric character reference'. Any numeric character reference +that isn't one of the special entities above, will be read, but written as a +regular code point. The output is correct, but the entity syntax isn't preserved. + +

Printing

+ +

Print to file

+You can directly use the convenience function: + + XMLDocument doc; + ... + doc.Save( "foo.xml" ); + +Or the XMLPrinter class: + + XMLPrinter printer( fp ); + doc.Print( &printer ); + +

Print to memory

+Printing to memory is supported by the XMLPrinter. + + XMLPrinter printer; + doc->Print( &printer ); + // printer.CStr() has a const char* to the XML + +

Print without an XMLDocument

+ +When loading, an XML parser is very useful. However, sometimes +when saving, it just gets in the way. The code is often set up +for streaming, and constructing the DOM is just overhead. + +The Printer supports the streaming case. The following code +prints out a trivially simple XML file without ever creating +an XML document. + + XMLPrinter printer( fp ); + printer.OpenElement( "foo" ); + printer.PushAttribute( "foo", "bar" ); + printer.CloseElement(); + +

Examples

+ +

Load and parse an XML file.

+@verbatim + /* ------ Example 1: Load and parse an XML file. ---- */ + { + XMLDocument doc; + doc.LoadFile( "dream.xml" ); + } +@endverbatim + +

Lookup information.

+@verbatim + /* ------ Example 2: Lookup information. ---- */ + { + XMLDocument doc; + doc.LoadFile( "dream.xml" ); + + // Structure of the XML file: + // - Element "PLAY" the root Element, which is the + // FirstChildElement of the Document + // - - Element "TITLE" child of the root PLAY Element + // - - - Text child of the TITLE Element + + // Navigate to the title, using the convenience function, + // with a dangerous lack of error checking. + const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText(); + printf( "Name of play (1): %s\n", title ); + + // Text is just another Node to TinyXML-2. The more + // general way to get to the XMLText: + XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText(); + title = textNode->Value(); + printf( "Name of play (2): %s\n", title ); + } +@endverbatim + +

Using and Installing

+ +There are 2 files in TinyXML-2: +
    +
  1. tinyxml2.cpp
  2. +
  3. tinyxml2.h
  4. +
+And additionally a test file: +
    +
  1. xmltest.cpp
  2. +
+ +Simply compile and run. There is a visual studio 2010 project included, a simple Makefile, +an XCode project, and a cmake CMakeLists.txt included to help you. The top of tinyxml.h +even has a simple g++ command line if you are are *nix and don't want to use a build system. + +

Documentation

+ +The documentation is build with Doxygen, using the 'dox' +configuration file. + +

License

+ +TinyXML-2 is released under the zlib license: + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. + +

Contributors

+ +Thanks very much to everyone who sends suggestions, bugs, ideas, and +encouragement. It all helps, and makes this project fun. + +The original TinyXML-1 has many contributors, who all deserve thanks +in shaping what is a very successful library. Extra thanks to Yves +Berquin and Andrew Ellerton who were key contributors. + +TinyXML-2 grew from that effort. Lee Thomason is the original author +of TinyXML-2 (and TinyXML-1) but hopefully TinyXML-2 will be improved +by many contributors. +*/ diff --git a/objview/Classes/KRObjViewGLView.h b/objview/Classes/KRObjViewGLView.h index ae2bb1b..3f5180b 100644 --- a/objview/Classes/KRObjViewGLView.h +++ b/objview/Classes/KRObjViewGLView.h @@ -47,7 +47,7 @@ KREngine *renderEngine; - KRScene m_scene; + KRScene *m_pScene; } diff --git a/objview/Classes/KRObjViewGLView.mm b/objview/Classes/KRObjViewGLView.mm index 8415e62..5d05440 100644 --- a/objview/Classes/KRObjViewGLView.mm +++ b/objview/Classes/KRObjViewGLView.mm @@ -86,6 +86,10 @@ } - (void)dealloc { + if(m_pScene) { + delete m_pScene; + m_pScene = NULL; + } if(renderEngine) { [renderEngine release]; renderEngine = nil; @@ -99,9 +103,18 @@ - (BOOL)loadObjects { + m_pScene = new KRScene("default"); + NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSFileManager* fileManager = [NSFileManager defaultManager]; + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { + if([fileName hasSuffix: @".scene"]) { + NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; + [renderEngine loadResource: path]; + } + } + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { if([fileName hasSuffix: @".pvr"]) { NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; @@ -134,7 +147,7 @@ for(std::map::iterator itr=models.begin(); itr != models.end(); itr++) { std::string lightmap = (*itr).first; lightmap.append("_lightmap"); - m_scene.addInstance((*itr).second, KRMat4(), lightmap); + m_pScene->addInstance((*itr).second->getName(), (*itr).second->getName(), KRMat4(), lightmap); } @@ -242,7 +255,7 @@ - (KRScene *)getScene; { - return &m_scene; + return m_pScene; } #pragma mark - diff --git a/objview/Classes/KRObjViewViewController.mm b/objview/Classes/KRObjViewViewController.mm index 5462f6d..b1ee9ed 100644 --- a/objview/Classes/KRObjViewViewController.mm +++ b/objview/Classes/KRObjViewViewController.mm @@ -231,7 +231,7 @@ pos.rotate((double)rand() / (double)RAND_MAX * 6.282, Y_AXIS); pos.translate((double)rand() / (double)RAND_MAX * 10000 - 5000, 0.0, (double)rand() / (double)RAND_MAX * 10000 - 5000); - scene->addInstance(pModelManager->getModel(szName), pos, ""); + scene->addInstance(szName, szName, pos, ""); } }