aboutsummaryrefslogtreecommitdiffstats
path: root/src/Texture.cpp
blob: 1487dcd6a895cf06986d5b0bcaa0631f321092ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <Texture.h>
#include <string.h>

struct texture_t {
	char *name;
	GLuint tex;
	dim2 dim;
} __attribute__ ((packed));

struct index_t{
	Color color;
	int indexx;
	int indexy;
};

struct texture_t *LoadedTexture[256];
unsigned int LoadedTextureCounter = 0;

namespace Texture{
	Color pixels[8][4];
	GLuint loadTexture(const char *fileName){
		SDL_Surface *image;
		GLuint object = 0;

		for(unsigned int i=0;i<LoadedTextureCounter;i++){
			if(!strcmp(LoadedTexture[i]->name,fileName)){
#ifdef DEBUG
				DEBUG_printf("Reusing loaded texture for %s\n",fileName);
#endif // DEBUG
				return LoadedTexture[i]->tex;
			}
		}

		if(!fileName)
			return 0;

		if(!(image = IMG_Load(fileName)))
			return 0;
#ifdef DEBUG
		DEBUG_printf("Loaded image file: %s\n", fileName);
#endif // DEBUG
		
		glGenTextures(1,&object);				// Turns "object" into a texture
		glBindTexture(GL_TEXTURE_2D,object);	// Binds "object" to the top of the stack
		glPixelStoref(GL_UNPACK_ALIGNMENT,1);

		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Sets the "min" filter
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// The the "max" filter of the stack

		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Wrap the texture to the matrix
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //

		glTexImage2D(GL_TEXTURE_2D,  // Sets the texture to the image file loaded above
					 0, 
					 GL_RGBA,
					 image->w,
					 image->h, 
					 0, 
					 GL_RGBA, 
					 GL_UNSIGNED_BYTE, 
					 image->pixels
					 );
		
		LoadedTexture[LoadedTextureCounter]		  = new struct texture_t;		//(struct texture_t *)malloc(sizeof(struct texture_t));
		LoadedTexture[LoadedTextureCounter]->name = new char[strlen(fileName)+1];	//(char *)malloc(safe_strlen(fileName));
		LoadedTexture[LoadedTextureCounter]->tex  = object;
		strcpy(LoadedTexture[LoadedTextureCounter]->name,fileName);
		LoadedTexture[LoadedTextureCounter]->dim.x = image->w;
		LoadedTexture[LoadedTextureCounter]->dim.y = image->h;
		LoadedTextureCounter++;
		
		SDL_FreeSurface(image);	// Free the surface

		return object;
	}

	dim2 imageDim(const char *fileName){
		for(unsigned int i=0;i<LoadedTextureCounter;i++){
			if(!strcmp(LoadedTexture[i]->name,fileName)){
				return LoadedTexture[i]->dim;
			}
		}
		return {0,0};
	}
	
	void freeTextures(void){
		for(unsigned int i=0;i<LoadedTextureCounter;i++){
			glDeleteTextures(1,&LoadedTexture[i]->tex);
			delete[] LoadedTexture[i]->name;
			delete LoadedTexture[i];
		}
	}

	#define CINDEX_WIDTH (8*4*3)
	void initColorIndex(){
		unsigned int i;
		GLubyte *buffer;
		GLfloat *bufferf;
		
		buffer  = new GLubyte[CINDEX_WIDTH];
		bufferf = new GLfloat[CINDEX_WIDTH];
		
		colorIndex = loadTexture("assets/colorIndex.png");
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, colorIndex);
		glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
		
		for(i = 0; i < CINDEX_WIDTH; i++)
			bufferf[i] = (float)buffer[i] / 255.0f;
		
		i = 0;
		for(unsigned int y = 0; y < 8; y++){
			for(unsigned int x = 0; x < 4; x++){
					if(i >= CINDEX_WIDTH){
						delete[] buffer;
						delete[] bufferf;
						return;
					}
					pixels[y][x].red = buffer[i++];
					pixels[y][x].green = buffer[i++];
					pixels[y][x].blue = buffer[i++];
			}
		}
		delete[] buffer;
		delete[] bufferf;
	}

	//sqrt((255-145)^2+(90-145)^2+(0-0)^2);
	std::vector<index_t>ind;
	vec2 getIndex(Color c){
		for(auto &i : ind){
			if(c.red == i.color.red && c.green == i.color.green && c.blue == i.color.blue){
				//std::cout << float(i.indexy) << "," << float(i.indexx) << std::endl;
				return {float(i.indexx), float(i.indexy)};
			}
		}
		uint buf[2];
		float buff = 999;
		float shit = 999;
		for(uint y = 0; y < 8; y++){
			for(uint x = 0; x < 4; x++){
				//std::cout << y << "," << x << ":" << pixels[y][x].red << "," << pixels[y][x].green << "," << pixels[y][x].blue << std::endl;
				buff = sqrt(pow((pixels[y][x].red-	c.red),  2)+
							pow((pixels[y][x].green-c.green),2)+
							pow((pixels[y][x].blue-	c.blue), 2));
				//std::cout << buff << std::endl;
				if(buff < shit){
					shit = buff;
					buf[0] = y;
					buf[1] = x;
				}
				//
				//std::cout << shit << std::endl;
			}
		}
		ind.push_back({c, (int)buf[1], (int)buf[0]});
		//std::cout << float(buf[1]) << ", " << float(buf[0]) << std::endl;
		return {float(buf[1]),float(buf[0])};
	}
}

Texturec::Texturec(uint amt, ...){
	va_list fNames;
	texState = 0;
	image = new GLuint[amt];
	va_start(fNames, amt);
	for(unsigned int i = 0; i < amt; i++){
		image[i] = Texture::loadTexture(va_arg(fNames, char *));
	}
	va_end(fNames);
}

Texturec::Texturec(std::vector<std::string>v){
	texState = 0;
	image = new GLuint[v.size()];
	for(unsigned int i = 0; i < v.size(); i++){
		image[i] = Texture::loadTexture(v[i].c_str());
	}
}

Texturec::Texturec(uint amt,const char **paths){
	texState = 0;
	image = new GLuint[amt];
	for(unsigned int i = 0; i < amt; i++){
		image[i] = Texture::loadTexture(paths[i]);
	}
}

Texturec::~Texturec(){
	delete[] image;
}

void Texturec::bind(unsigned int bn){
	texState = bn;
	glBindTexture(GL_TEXTURE_2D,image[(int)texState]);
}

void Texturec::bindNext(){
	bind(++texState);
}

void Texturec::bindPrev(){
	bind(--texState);
}