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: 13839 $
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 TextureKey
RegistTexture(const char * name,const TextureInfo & textureInfo)84 TextureContainer::RegistTexture(const char* name, const TextureInfo& textureInfo)
85 {
86     NW_ASSERT(name);
87 
88     TextureRefLink* pLink = Layout::NewObj<TextureRefLink>();
89     if (pLink == NULL)
90     {
91         return NULL;
92     }
93 
94     pLink->Set(name, textureInfo);
95 
96     this->push_back(pLink);
97 
98     return (TextureKey) pLink;
99 }
100 
101 void
UnregistTexture(TextureKey key)102 TextureContainer::UnregistTexture(TextureKey key)
103 {
104     NW_NULL_ASSERT(key);
105 
106     TextureRefLink* pLink = (TextureRefLink*)(key);
107     this->erase(pLink);
108     Layout::DeleteObj(pLink);
109 }
110 
111 } // namespace nw::lyt
112 } // namespace nw
113