From 7577d224a6652af447c48121f9b269858782e329 Mon Sep 17 00:00:00 2001 From: olsner Date: Mon, 20 Jun 2005 15:14:36 +0000 Subject: [PATCH] Terrain XML loading, plus mouse/JS portability and some misc stuff to mix it up This was SVN commit r2412. --- source/graphics/MapReader.cpp | 1 + source/graphics/MapWriter.cpp | 6 +- source/graphics/ObjectEntry.h | 1 + source/graphics/TextureEntry.cpp | 177 ++++++++++++++++++++--- source/graphics/TextureEntry.h | 57 +++++--- source/graphics/TextureManager.cpp | 218 ++++++++++++++++++----------- source/graphics/TextureManager.h | 68 ++++++--- source/gui/GUIutil.cpp | 32 +---- source/lib/mmgr.cpp | 12 +- source/lib/ogl.cpp | 1 + source/lib/sdl.h | 29 ++++ source/lib/sysdep/unix/udbg.cpp | 2 +- source/lib/sysdep/win/wsdl.h | 14 +- source/main.cpp | 10 ++ source/ps/Interact.cpp | 25 ++-- source/ps/Network/Client.cpp | 11 +- source/ps/Network/Client.h | 5 +- source/ps/Network/Serialization.h | 18 +-- source/ps/Network/Server.cpp | 7 +- source/ps/Overlay.cpp | 38 +++++ source/ps/Overlay.h | 3 + source/simulation/EntitySupport.h | 4 +- 22 files changed, 536 insertions(+), 203 deletions(-) diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index 531d32f453..b86d0b859d 100755 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -14,6 +14,7 @@ #include "Model.h" #include "Terrain.h" #include "TextureManager.h" +#include "TextureEntry.h" #include "timer.h" #include "Loader.h" diff --git a/source/graphics/MapWriter.cpp b/source/graphics/MapWriter.cpp index 402c46b65b..921431cdee 100755 --- a/source/graphics/MapWriter.cpp +++ b/source/graphics/MapWriter.cpp @@ -1,6 +1,8 @@ #include "precompiled.h" #include "lib/types.h" +#include "lib/res/vfs.h" + #include "MapWriter.h" #include "MapReader.h" #include "UnitManager.h" @@ -11,11 +13,11 @@ #include "Terrain.h" #include "LightEnv.h" #include "TextureManager.h" +#include "TextureEntry.h" #include "VFSUtil.h" #include "Loader.h" #include "ps/XMLWriter.h" -#include "lib/res/vfs.h" #include "simulation/Entity.h" #include "simulation/BaseEntity.h" #include "simulation/BaseEntityCollection.h" @@ -101,7 +103,7 @@ void CMapWriter::EnumTerrainTextures(CTerrain *pTerrain, // uh-oh, this shouldn't happen; set texturename to empty string texturename=""; } else { - texturename=texentry->GetName(); + texturename=texentry->GetTag(); } textures.push_back(texturename); } diff --git a/source/graphics/ObjectEntry.h b/source/graphics/ObjectEntry.h index 0bbbf8b853..a183f8b709 100755 --- a/source/graphics/ObjectEntry.h +++ b/source/graphics/ObjectEntry.h @@ -5,6 +5,7 @@ class CModel; class CSkeletonAnim; struct SPropPoint; +#include #include #include #include "CStr.h" diff --git a/source/graphics/TextureEntry.cpp b/source/graphics/TextureEntry.cpp index 46bcef0a92..2c0a7850ab 100755 --- a/source/graphics/TextureEntry.cpp +++ b/source/graphics/TextureEntry.cpp @@ -1,19 +1,35 @@ #include "precompiled.h" + +#include + #include "ogl.h" #include "res/ogl_tex.h" + +#include "CLogger.h" + #include "TextureEntry.h" #include "TextureManager.h" #include "Texture.h" -#include "CLogger.h" #include "Renderer.h" +#include "Overlay.h" +#include "Parser.h" +#include "XeroXMB.h" +#include "Xeromyces.h" + +using namespace std; + +#define LOG_CATEGORY "graphics" ///////////////////////////////////////////////////////////////////////////////////// // CTextureEntry constructor -CTextureEntry::CTextureEntry(const char* name,int type) - : m_Name(name), m_Bitmap(0), m_Handle(-1), m_BaseColor(0), m_Type(type), m_BaseColorValid(false) -{ -} +CTextureEntry::CTextureEntry(): + m_pParent(NULL), + m_Bitmap(NULL), + m_Handle(-1), + m_BaseColor(0), + m_BaseColorValid(false) +{} ///////////////////////////////////////////////////////////////////////////////////// // CTextureEntry destructor @@ -21,23 +37,136 @@ CTextureEntry::~CTextureEntry() { if (m_Handle > 0) tex_free(m_Handle); + + GroupVector::iterator it=m_Groups.begin(); + for (;it!=m_Groups.end();++it) + (*it)->RemoveTerrain(this); +} + +CTextureEntry *CTextureEntry::FromXML(XMBElement node, CXeromyces *pFile) +{ + CTextureEntry *thiz=new CTextureEntry(); + + #define ELMT(x) int elmt_##x = pFile->getElementID(#x) + #define ATTR(x) int attr_##x = pFile->getAttributeID(#x) + ELMT(doodad); + ELMT(passable); + ELMT(event); + // Terrain Attribs + ATTR(tag); + ATTR(skin); + ATTR(parent); + ATTR(mmap); + ATTR(groups); + ATTR(properties); + // Passable Attribs + ATTR(type); + ATTR(speed); + ATTR(effect); + // Doodad Attribs + ATTR(name); + ATTR(max); + // Event attribs + ATTR(on); + #undef ELMT + #undef ATTR + + XMBAttributeList attribs = node.getAttributes(); + for (int i=0;im_Tag = (CStr)attr.Value; + if (attr.Name == attr_parent) + thiz->m_ParentName = (CStr)attr.Value; + else if (attr.Name == attr_skin) + { + thiz->m_TexturePath = (CStr)attr.Value; + } + else if (attr.Name == attr_groups) + { + // Parse a comma-separated list of groups, add the new entry to + // each of them + CParser parser; + CParserLine parserLine; + parser.InputTaskType("GroupList", "<_$value_,>_$value_"); + + if (!parserLine.ParseString(parser, (CStr)attr.Value)) + continue; + for (size_t i=0;im_Groups.push_back(pType); + pType->AddTerrain(thiz); + } + } + else if (attr.Name == attr_mmap) + { + CColor col; + if (!col.ParseString((CStr)attr.Value, 255)) + continue; + + // m_BaseColor is RGBA + u8 (&baseColor)[4] = *(u8 (*)[4])&thiz->m_BaseColor; + baseColor[0] = (u8)(col.r*255); + baseColor[1] = (u8)(col.g*255); + baseColor[2] = (u8)(col.b*255); + baseColor[3] = (u8)(col.a*255); + thiz->m_BaseColorValid = true; + } + else if (attr.Name == attr_properties) + { + // TODO Parse a list of properties and store them somewhere + } + } + + // TODO Parse information in child nodes and store them somewhere + + LOG(NORMAL, LOG_CATEGORY, "CTextureEntry::FromXML(): Terrain type %s (texture %s) loaded", thiz->m_Tag.c_str(), thiz->m_TexturePath.c_str()); + + return thiz; +} + +void CTextureEntry::LoadParent() +{ + // Don't do anything at all if we already have a parent, or if we are a root + // entry + if (m_pParent || !m_ParentName.size()) + return; + + m_pParent = g_TexMan.FindTexture(m_ParentName); + if (!m_pParent) + { + LOG(ERROR, LOG_CATEGORY, "CTextureEntry::LoadParent(): %s: Could not find parent terrain type %s", m_Tag.c_str(), m_ParentName.c_str()); + return; + } + m_pParent->LoadParent(); // Make sure we're loaded all the way to our root } ///////////////////////////////////////////////////////////////////////////////////// // LoadTexture: actually load the texture resource from file void CTextureEntry::LoadTexture() { - CStr pathname("art/textures/terrain/types/"); - pathname+=g_TexMan.m_TerrainTextures[m_Type].m_Name; - pathname+='/'; - pathname+=m_Name; - - CTexture texture; - texture.SetName(pathname); - if (g_Renderer.LoadTexture(&texture,GL_REPEAT)) { - m_Handle=texture.GetHandle(); - } else { - m_Handle=0; + if (m_TexturePath.size() == 0) + { + assert(m_pParent); + m_pParent->LoadTexture(); + m_Handle = m_pParent->m_Handle; + return; + } + else + { + CTexture texture; + texture.SetName(m_TexturePath); + if (g_Renderer.LoadTexture(&texture,GL_REPEAT)) { + m_Handle=texture.GetHandle(); + } else { + m_Handle=0; + } } } @@ -46,6 +175,22 @@ void CTextureEntry::LoadTexture() // in m_BaseColor member void CTextureEntry::BuildBaseColor() { + // a) We have an XML override: m_BaseColorValid=true, so we never get here + // b) No XML override, overriden texture: use own texture, calc. BaseColor + + // c) No XML override, parent texture: use parent BaseColor + // d) No XML override, but parent has: use parent BaseColor + + // cases c&d: We don't have our own texture, use parent base color instead + if (m_TexturePath.size() == 0) + { + assert(m_pParent); + m_BaseColor=m_pParent->GetBaseColor(); + m_BaseColorValid=true; + return; + } + + // case b: Calculate base color by mipmapping our texture Handle handle=GetHandle(); g_Renderer.BindTexture(0,tex_id(handle)); diff --git a/source/graphics/TextureEntry.h b/source/graphics/TextureEntry.h index c740a9c081..2e15740488 100755 --- a/source/graphics/TextureEntry.h +++ b/source/graphics/TextureEntry.h @@ -4,17 +4,26 @@ #include "res/handle.h" #include "CStr.h" +#include "TextureManager.h" + +class XMBElement; +class CXeromyces; + ////////////////////////////////////////////////////////////////////////////////////////////////////////// // CTextureEntry: class wrapping a terrain texture object; contains various other required // elements - color of minimap, terrain "group" it belongs to, etc class CTextureEntry { public: - CTextureEntry(const char* name,int type); + typedef std::vector GroupVector; + ~CTextureEntry(); - // accessor - return texture name - const char* GetName() const { return (const char*) m_Name; } + CStr GetTag() const + { return m_Tag; } + + CStr GetTexturePath() const + { return m_TexturePath; } // accessor - get UI bitmap object void* GetBitmap() const { return m_Bitmap; } @@ -31,30 +40,44 @@ public: if (!m_BaseColorValid) BuildBaseColor(); return m_BaseColor; } - // accessor - return texture type - int GetType() const { return m_Type; } - + + const GroupVector &GetGroups() const + { return m_Groups; } + // returns whether this texture-entry has loaded any data yet bool IsLoaded() { return (m_Handle!=-1); } + + static CTextureEntry *FromXML(XMBElement el, CXeromyces *pFile); + + // Load all properties from the parent (run on all terrains after loading + // all the xml's or when changes has been made on the parent) + // This will only actually work once per instance (noop on subsequent calls) + void LoadParent(); private: + CTextureEntry(); + // load texture from file void LoadTexture(); // calculate the root color of the texture, used for coloring minimap void BuildBaseColor(); - // filename - CStr m_Name; - // UI bitmap object - void* m_Bitmap; - // handle to GL texture data - Handle m_Handle; - // BGRA color of topmost mipmap level, for coloring minimap + CStr m_Tag; + CStr m_ParentName; + CTextureEntry *m_pParent; + + CStr m_TexturePath; + void* m_Bitmap; // UI bitmap object (user data for ScEd) + Handle m_Handle; // handle to GL texture data + + // BGRA color of topmost mipmap level, for coloring minimap, or a color + // manually specified in the Terrain XML (or by any parent) + // ..Valid is true if the base color is a cached value or an XML override u32 m_BaseColor; - // above color valid? bool m_BaseColorValid; - // "type" of texture - index into TextureManager texturetypes array - int m_Type; + + // All terrain type groups we're a member of + GroupVector m_Groups; }; -#endif +#endif diff --git a/source/graphics/TextureManager.cpp b/source/graphics/TextureManager.cpp index 53f844b676..28bec09f3c 100755 --- a/source/graphics/TextureManager.cpp +++ b/source/graphics/TextureManager.cpp @@ -1,53 +1,48 @@ #include "precompiled.h" #include +#include #include "TextureManager.h" +#include "TextureEntry.h" + #include "res/res.h" -#include "ogl.h" #include "res/ogl_tex.h" -#include "CLogger.h" +#include "ogl.h" #include "timer.h" +#include "CLogger.h" +#include "Xeromyces.h" +#include "XeroXMB.h" + #define LOG_CATEGORY "graphics" -// filter for vfs_next_dirent -static const char* SupportedTextureFormats[] = { "*.png", "*.dds", "*.tga", "*.bmp" }; - - +using namespace std; CTextureManager::CTextureManager() { - m_TerrainTextures.reserve(32); } CTextureManager::~CTextureManager() { - for (size_t i=0;isecond; + ++it; } } -void CTextureManager::AddTextureType(const char* name) +CTextureEntry* CTextureManager::FindTexture(CStr tag) { - m_TerrainTextures.resize(m_TerrainTextures.size()+1); - STextureType& ttype=m_TerrainTextures.back(); - ttype.m_Name=name; - ttype.m_Index=(int)m_TerrainTextures.size()-1; -} - -CTextureEntry* CTextureManager::FindTexture(const char* filename) -{ - // check if file already loaded - for (uint k=0;kGetName(),filename)==0) { - return ttype.m_Textures[i]; - } - } + for (uint i=0;iGetTag() == tag) + return m_TextureEntries[i]; } return 0; @@ -55,23 +50,77 @@ CTextureEntry* CTextureManager::FindTexture(const char* filename) CTextureEntry* CTextureManager::FindTexture(Handle handle) { - for (uint k=0;kIsLoaded() && handle==ttype.m_Textures[i]->GetHandle()) { - return ttype.m_Textures[i]; - } + for (uint i=0;iIsLoaded() + && handle==m_TextureEntries[i]->GetHandle()) + { + return m_TextureEntries[i]; } } return 0; } -CTextureEntry* CTextureManager::AddTexture(const char* filename,int type) +void CTextureManager::LoadTerrainsFromXML(const char *filename) +{ + CXeromyces XeroFile; + if (XeroFile.Load(filename) != PSRETURN_OK) + return; + + XMBElement root = XeroFile.getRoot(); + CStr rootName = XeroFile.getElementString(root.getNodeName()); + + // Check that we've got the right kind of xml document + if (rootName != "terrains") + { + LOG(ERROR, + LOG_CATEGORY, + "TextureManager: Loading %s: Root node is not terrains (found \"%s\")", + filename, + rootName.c_str()); + return; + } + + #define ELMT(x) int el_##x = XeroFile.getElementID(#x) + #define ATTR(x) int at_##x = XeroFile.getAttributeID(#x) + ELMT(terrain); + + // Load terrains + + // Iterate main children + // they should all be or