1 /* 2 Copyright (c) 2008 Seneca College 3 Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/) 4 */ 5 6 7 /** 8 @private 9 @class TextureManager is a class designed to prevent the same 10 texture from being loaded more than once in the OpenGL context. 11 Users of the library don't actually have to bother with using it, 12 they just have to create their Textures. 13 */ 14 c3dl.TextureManager = function(gl) 15 { 16 this.currentID = 1; 17 this.keys = []; 18 this.values = []; 19 20 this.glCanvas3D = gl; 21 22 /** 23 @private 24 Add a texture to be used in the script. 25 26 @param {String} relativePath The relative path of the Texture from 27 the index.html file. 28 */ 29 this.addTexture = function(relativePath) 30 { 31 // if its already present, don't readd it 32 if( this.getID(relativePath) == -1 ) 33 { 34 var texture = new c3dl.Texture(); 35 if( texture.setup(this.glCanvas3D, relativePath) ) 36 { 37 this.keys.push(texture.getTextureID()); 38 this.values.push(texture); 39 this.currentID++; 40 } 41 } 42 } 43 44 /** 45 @private 46 */ 47 this.addTextureFromCanvas2D = function(sourceCanvas) 48 { 49 if( this.getID(sourceCanvas) == -1 ) 50 { 51 var texture = new Texture(); 52 if( texture.setup(this.glCanvas3D, 'deleteme', sourceCanvas) ) 53 { 54 this.keys.push(texture.getTextureID()); 55 this.values.push(texture); 56 this.currentID++; 57 } 58 } 59 } 60 61 /** 62 @private 63 Has the Texture already been added? 64 65 @param {String} relativePath The relative path of the Texture from 66 the index.html file. 67 68 @returns {boolean} True if the texture has already been added, false 69 otherwise. 70 */ 71 this.hasTexture = function(relativePath) 72 { 73 // -1 indicates an invalid texture id 74 return this.getID(relativePath) == -1 ? false : true; 75 } 76 77 /** 78 @private 79 Remove a texture. Currently not implemented. 80 81 @param {String} relativePath 82 */ 83 this.removeTexture = function(relativePath) 84 { 85 // check if it exists 86 if( this.getID(relativePath) != -1) 87 { 88 // remove it from this list? 89 90 // remove it from opengl 91 } 92 } 93 94 /** 95 @private 96 97 Get the ID of a Texture, referenced by 'relativePath'. 98 99 @param {String} relativePath The relative path of the Texture from 100 the index.html file. 101 102 @returns {int} The ID of the Texture, reutrns -1 if the Texture 103 hasn't been added. 104 */ 105 this.getID = function(relativePath) 106 { 107 var id = -1; 108 109 for (var i = 0; i < this.values.length; i++) 110 { 111 if( this.values[i].getRelativePath() == relativePath) 112 { 113 id = this.keys[i]; 114 break; 115 } 116 } 117 return id; 118 } 119 120 /** 121 @private 122 Get a string representation of this class. Will display all the OpenGLES 123 texture IDs along with the associated texture path. 124 125 @param {null|String} delimiter A string which will separate values. Typically will be 126 "," , "\n" or "<br />". If none is specified, "," will be used. 127 128 @returns {String} A string representation of this object. 129 */ 130 this.toString = function(delimiter) 131 { 132 // make sure user passed up a string if they actually decided 133 // to specify a delimiter. 134 if(!delimiter || typeof(delimiter) != "string") 135 { 136 delimiter = ","; 137 } 138 139 // start with en empty string 140 var str = ""; 141 142 for (var i = 0; i < this.values.length; i++) 143 { 144 str += "ID = " + this.keys[i] + delimiter + 145 "Path = " + this.values[i].getRelativePath(); 146 147 // only add the comma, if this isn't the last path, we don't 148 // want a trailing comma. 149 if(i+1 < this.values.length) 150 { 151 str += delimiter; 152 } 153 } 154 155 return str; 156 } 157 } 158