2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #include <cstdio> // FILE
13 #include <cstring> // strncmp
15 #include <boost/algorithm/string.hpp>
16 #include <boost/bind.hpp>
18 #include "Dispatch.hh"
32 * The texture implementation just contains all the information about the
33 * image which is worth having in memory. The image data itself is not
34 * worth keeping in memory if the texture has been loaded to GL, but the
35 * name of the resource is retained so that it can be reloaded if
36 * necessary. The implementation is a manager so that multiple texture
37 * objects can share the same internal objects and avoid having duplicate
38 * textures loaded to GL.
41 class Texture::Impl
: public Manager
<Impl
>
45 * Delete the texture (if it is loaded) from GL.
52 if (mObject
== gObject
)
57 glDeleteTextures(1, &mObject
);
63 * If the GL context was recreated, we need to reload the texture.
64 * This may involve reading it from disk again, but hopefully the OS
65 * was smart enough to cache it if the client has plenty of RAM.
68 void contextRecreated()
70 mObject
= gObject
= 0;
75 * This is a helper method used by some of the texture loading code.
76 * It returns the first power of two which is greater than the input
80 static int powerOfTwo(int input
)
92 static void bindScriptConstants(Mf::Script
& script
)
94 script
.push(GL_CLAMP
); script
.set("CLAMP");
95 script
.push(GL_REPEAT
); script
.set("REPEAT");
96 script
.push(GL_LINEAR
); script
.set("LINEAR");
97 script
.push(GL_NEAREST
); script
.set("NEAREST");
98 script
.push(GL_LINEAR_MIPMAP_LINEAR
);
99 script
.set("LINEAR_MIPMAP_LINEAR");
100 script
.push(GL_LINEAR_MIPMAP_NEAREST
);
101 script
.set("LINEAR_MIPMAP_NEAREST");
102 script
.push(GL_NEAREST_MIPMAP_LINEAR
);
103 script
.set("NEAREST_MIPMAP_LINEAR");
104 script
.push(GL_NEAREST_MIPMAP_NEAREST
);
105 script
.set("NEAREST_MIPMAP_NEAREST");
111 * Construction is initialization.
115 mMinFilter(GL_NEAREST
),
116 mMagFilter(GL_NEAREST
),
123 // make sure we have a video context
125 "cannot load textures without a current video context");
127 // we want to know when the GL context is recreated
128 mDispatchHandler
= core
.addHandler("video.newcontext",
129 boost::bind(&Impl::contextRecreated
, this));
139 * Adapted from some public domain code. This stuff is common enough
140 * that it really should be included in SDL_image... We need this
141 * because images loaded with SDL_image aren't exactly GL-ready right
142 * out of the box. This method makes them ready.
146 static SDL_Surface* prepareImageForGL(SDL_Surface* surface)
148 int w = powerOfTwo(surface->w);
149 int h = powerOfTwo(surface->h);
151 // 2. OpenGL textures make more sense within the coordinate system
152 // when they are "upside down," so let's flip it.
154 flipSurface(surface);
156 // 1. OpenGL images must (generally) have dimensions of a
157 // power-of-two. If this one doesn't, we can at least be more
158 // friendly by expanding the dimensions so that they are, though
159 // there will be some empty space within the range of normal
160 // texture coordinates. It's better if textures are the right size
163 SDL_Surface* image = SDL_CreateRGBSurface
168 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
186 Uint32 savedFlags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
187 Uint8 savedAlpha = surface->format->alpha;
188 if (savedFlags & SDL_SRCALPHA)
190 SDL_SetAlpha(surface, 0, 0);
193 SDL_Rect srcArea, destArea;
194 srcArea.x = 0; destArea.x = 0;
195 srcArea.y = 0; destArea.y = h - surface->h;
196 srcArea.w = surface->w;
197 srcArea.h = surface->h;
198 SDL_BlitSurface(surface, &srcArea, image, &destArea);
200 if (savedFlags & SDL_SRCALPHA)
202 SDL_SetAlpha(surface, savedFlags, savedAlpha);
210 * Use SDL_image to load images from file. A surface with the image
212 * @return Image data.
215 void init(const std::string
& name
)
217 std::string path
= Texture::getPath(name
);
219 mImage
= Image::alloc(path
);
220 if (!mImage
->isValid())
222 logWarning
<< "texture not found: " << path
<< std::endl
;
223 Error(Error::RESOURCE_NOT_FOUND
, path
).raise();
230 importLogFunctions(script
);
231 bindScriptConstants(script
);
233 if (script
.doString(mImage
->getComment()) != Mf::Script::SUCCESS
)
241 Mf::logInfo
<< "loading tiles from texture " << path
244 Mf::Script::Slot globals
= script
.getGlobalTable();
245 Mf::Script::Slot top
= script
[-1];
247 globals
.pushField("tiles_s");
250 globals
.pushField("tiles_t");
253 globals
.pushField("min_filter");
256 globals
.pushField("mag_filter");
259 globals
.pushField("wrap_s");
262 globals
.pushField("wrap_t");
269 * Upload the image to GL so that it will be accessible by a much more
270 * manageable handle and hopefully reside in video memory.
281 glGenTextures(1, &mObject
);
282 glBindTexture(GL_TEXTURE_2D
, mObject
);
304 * Sets some texture properties such as the filters and external
305 * coordinate behavior.
310 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, mMinFilter
);
311 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mMagFilter
);
312 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, mWrapS
);
313 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, mWrapT
);
316 inline void setMinFilter(GLuint filter
)
320 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, mMinFilter
);
323 inline void setMagFilter(GLuint filter
)
327 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mMagFilter
);
330 inline void setWrapS(GLuint wrap
)
334 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, mWrapS
);
337 inline void setWrapT(GLuint wrap
)
341 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, mWrapT
);
351 if (mObject
!= gObject
)
353 glBindTexture(GL_TEXTURE_2D
, mObject
);
359 bool getTileCoords(Texture::TileIndex index
, Scalar coords
[8]) const
361 // make sure the index represents a real tile
362 if (index
>= mTilesS
* mTilesT
) return false;
364 Scalar w
= 1.0 / Scalar(mTilesS
);
365 Scalar h
= 1.0 / Scalar(mTilesT
);
367 coords
[0] = Scalar(index
% mTilesS
) * w
;
368 coords
[1] = (Scalar(mTilesT
- 1) - Scalar(index
/ mTilesS
)) * h
;
369 coords
[2] = coords
[0] + w
;
370 coords
[3] = coords
[1];
371 coords
[4] = coords
[2];
372 coords
[5] = coords
[1] + h
;
373 coords
[6] = coords
[0];
374 coords
[7] = coords
[5];
381 GLuint mMinFilter
; ///< Minification filter.
382 GLuint mMagFilter
; ///< Magnification filter.
383 GLuint mWrapS
; ///< Wrapping behavior horizontally.
384 GLuint mWrapT
; ///< Wrapping behavior vertically.
388 GLuint mObject
; ///< GL texture handle.
389 static GLuint gObject
; ///< Global GL texture handle.
391 Dispatch::Handler mDispatchHandler
;
394 GLuint
Texture::Impl::gObject
= 0;
397 Texture::Texture(const std::string
& name
) :
398 Image(Texture::getPath(name
)),
400 mImpl(Texture::Impl::getInstance(name
)) {}
404 * Bind the GL texture for mapping, etc.
407 void Texture::bind() const
415 * Get the texture object, for the curious.
418 GLuint
Texture::getObject() const
421 return mImpl
->mObject
;
425 void Texture::resetBind()
427 glBindTexture(GL_TEXTURE_2D
, 0);
432 void Texture::setMinFilter(GLuint filter
)
435 mImpl
->setMinFilter(filter
);
438 void Texture::setMagFilter(GLuint filter
)
441 mImpl
->setMagFilter(filter
);
444 void Texture::setWrapS(GLuint wrap
)
447 mImpl
->setWrapS(wrap
);
450 void Texture::setWrapT(GLuint wrap
)
453 mImpl
->setWrapT(wrap
);
457 bool Texture::getTileCoords(TileIndex index
, Scalar coords
[8]) const
460 return mImpl
->getTileCoords(index
, coords
);
463 bool Texture::getTileCoords(TileIndex index
, Scalar coords
[8],
464 Orientation orientation
) const
466 if (getTileCoords(index
, coords
))
468 if (orientation
& FLIP
)
470 // this looks kinda weird, but it's just swapping in a way that
471 // doesn't require an intermediate variable
472 coords
[1] = coords
[5];
473 coords
[5] = coords
[3];
474 coords
[3] = coords
[7];
475 coords
[7] = coords
[5];
477 if (orientation
& REVERSE
)
479 coords
[0] = coords
[2];
480 coords
[2] = coords
[6];
481 coords
[4] = coords
[6];
482 coords
[6] = coords
[0];
492 std::string
Texture::getPath(const std::string
& name
)
494 if (boost::find_last(name
, ".png"))
496 return Resource::getPath(name
);
500 std::string
path("textures/");
503 return Resource::getPath(path
);