1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     lyt_TextureContainer.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 27719 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/lyt/lyt_Layout.h>
19 #include <nw/lyt/lyt_TextureContainer.h>
20 #include <nw/lyt/lyt_Util.h>
21 
22 namespace nw
23 {
24 namespace lyt
25 {
26 
TextureRefLink()27 TextureRefLink::TextureRefLink()
28 {
29     m_Name[0] = '\0';
30 }
31 
~TextureRefLink()32 TextureRefLink::~TextureRefLink()
33 {
34     GLuint texName = m_TexInfo.GetTextureObject();
35     if (texName != m_TexInfo.INVALID)
36     {
37         glDeleteTextures(1, &texName);
38     }
39 }
40 
41 void
Set(const char * name,const TextureInfo & texInfo)42 TextureRefLink::Set(
43     const char* name,
44     const TextureInfo& texInfo)
45 {
46     NW_NULL_ASSERT(name);
47 
48     // TODO: バイナリコンバータでファイル名の長さをチェックする。
49     ut::strcpy(m_Name, sizeof(m_Name), name);
50     m_TexInfo = texInfo;
51 }
52 
~TextureContainer()53 TextureContainer::~TextureContainer()
54 {
55     this->Finalize();
56 }
57 
58 void
Finalize()59 TextureContainer::Finalize()
60 {
61     while (!this->empty())
62     {
63         TextureRefLink* pLink = &this->front();
64         this->erase(pLink);
65         Layout::DeleteObj(pLink);
66     }
67 }
68 
69 const TextureInfo
FindTextureByName(const char * name)70 TextureContainer::FindTextureByName(const char* name)
71 {
72     for (Iterator it = this->GetBeginIter(); it != this->GetEndIter(); ++it)
73     {
74         if (0 == std::strcmp(name, it->GetResourceName()))
75         {
76             return it->GetTextureInfo();
77         }
78     }
79 
80     return TextureInfo();
81 }
82 
83 const TextureInfo
FindTextureByKey(TextureKey key)84 TextureContainer::FindTextureByKey(TextureKey key)
85 {
86     NW_NULL_ASSERT(key);
87     TextureRefLink* pLink = (TextureRefLink*)(key);
88 
89     for (Iterator it = this->GetBeginIter(); it != this->GetEndIter(); ++it)
90     {
91         if (&(*it) == pLink)
92         {
93             return it->GetTextureInfo();
94         }
95     }
96 
97     return TextureInfo();
98 }
99 
100 TextureKey
RegistTexture(const char * name,const TextureInfo & textureInfo)101 TextureContainer::RegistTexture(const char* name, const TextureInfo& textureInfo)
102 {
103     NW_ASSERT(name);
104 
105     TextureRefLink* pLink = Layout::NewObj<TextureRefLink>();
106     if (pLink == NULL)
107     {
108         return NULL;
109     }
110 
111     pLink->Set(name, textureInfo);
112 
113     this->push_back(pLink);
114 
115     return (TextureKey) pLink;
116 }
117 
118 void
UnregistTexture(TextureKey key)119 TextureContainer::UnregistTexture(TextureKey key)
120 {
121     NW_NULL_ASSERT(key);
122 
123     TextureRefLink* pLink = (TextureRefLink*)(key);
124     this->erase(pLink);
125     Layout::DeleteObj(pLink);
126 }
127 
128 } // namespace nw::lyt
129 } // namespace nw
130