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(Script
& script
)
94 Script::Slot g
= script
.globals();
96 g
.setField("CLAMP", GL_CLAMP
);
97 g
.setField("REPEAT", GL_REPEAT
);
98 g
.setField("LINEAR", GL_LINEAR
);
99 g
.setField("NEAREST", GL_NEAREST
);
100 g
.setField("LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR
);
101 g
.setField("LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST
);
102 g
.setField("NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR
);
103 g
.setField("NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST
);
109 * Construction is initialization.
113 mMinFilter(GL_NEAREST
),
114 mMagFilter(GL_NEAREST
),
121 // make sure we have a video context
122 Video
* video
= Video::current();
123 ASSERT(video
&& "should have a video context set");
125 // we want to know when the GL context is recreated
126 Dispatch
& dispatch
= Dispatch::global();
127 mNewContextDispatch
= dispatch
.addTarget("video.newcontext",
128 boost::bind(&Impl::contextRecreated
, this));
138 * Adapted from some public domain code. This stuff is common enough
139 * that it really should be included in SDL_image... We need this
140 * because images loaded with SDL_image aren't exactly GL-ready right
141 * out of the box. This method makes them ready.
145 static SDL_Surface* prepareImageForGL(SDL_Surface* surface)
147 int w = powerOfTwo(surface->w);
148 int h = powerOfTwo(surface->h);
150 // 2. OpenGL textures make more sense within the coordinate system
151 // when they are "upside down," so let's flip it.
153 flipSurface(surface);
155 // 1. OpenGL images must (generally) have dimensions of a
156 // power-of-two. If this one doesn't, we can at least be more
157 // friendly by expanding the dimensions so that they are, though
158 // there will be some empty space within the range of normal
159 // texture coordinates. It's better if textures are the right size
162 SDL_Surface* image = SDL_CreateRGBSurface
167 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
185 Uint32 savedFlags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
186 Uint8 savedAlpha = surface->format->alpha;
187 if (savedFlags & SDL_SRCALPHA)
189 SDL_SetAlpha(surface, 0, 0);
192 SDL_Rect srcArea, destArea;
193 srcArea.x = 0; destArea.x = 0;
194 srcArea.y = 0; destArea.y = h - surface->h;
195 srcArea.w = surface->w;
196 srcArea.h = surface->h;
197 SDL_BlitSurface(surface, &srcArea, image, &destArea);
199 if (savedFlags & SDL_SRCALPHA)
201 SDL_SetAlpha(surface, savedFlags, savedAlpha);
209 * Use SDL_image to load images from file. A surface with the image
211 * @return Image data.
214 void init(const std::string
& name
)
216 std::string
path(name
);
218 Texture::getPath(path
);
220 mImage
= Image::alloc(path
);
221 if (!mImage
->isValid())
223 logWarning
<< "texture not found: " << path
<< std::endl
;
224 Error(Error::RESOURCE_NOT_FOUND
, path
).raise();
231 importLogFunctions(script
);
232 bindScriptConstants(script
);
234 if (script
.doString(mImage
->getComment()) != Script::SUCCESS
)
242 logInfo
<< "loading tiles from texture " << path
245 Script::Slot globals
= script
.globals();
246 globals
.get(mTilesS
, "tiles_s");
247 globals
.get(mTilesT
, "tiles_t");
248 globals
.get(mMinFilter
, "min_filter");
249 globals
.get(mMagFilter
, "mag_filter");
250 globals
.get(mWrapS
, "wrap_s");
251 globals
.get(mWrapT
, "wrap_t");
257 * Upload the image to GL so that it will be accessible by a much more
258 * manageable handle and hopefully reside in video memory.
269 glGenTextures(1, &mObject
);
270 glBindTexture(GL_TEXTURE_2D
, mObject
);
292 * Sets some texture properties such as the filters and external
293 * coordinate behavior.
298 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, mMinFilter
);
299 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mMagFilter
);
300 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, mWrapS
);
301 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, mWrapT
);
304 inline void setMinFilter(GLuint filter
)
308 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, mMinFilter
);
311 inline void setMagFilter(GLuint filter
)
315 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mMagFilter
);
318 inline void setWrapS(GLuint wrap
)
322 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, mWrapS
);
325 inline void setWrapT(GLuint wrap
)
329 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, mWrapT
);
339 if (mObject
!= gObject
)
341 glBindTexture(GL_TEXTURE_2D
, mObject
);
347 bool getTileCoords(Texture::TileIndex index
, Scalar coords
[8]) const
349 // make sure the index represents a real tile
350 if (index
>= mTilesS
* mTilesT
) return false;
352 Scalar w
= 1.0 / Scalar(mTilesS
);
353 Scalar h
= 1.0 / Scalar(mTilesT
);
355 coords
[0] = Scalar(index
% mTilesS
) * w
;
356 coords
[1] = (Scalar(mTilesT
- 1) - Scalar(index
/ mTilesS
)) * h
;
357 coords
[2] = coords
[0] + w
;
358 coords
[3] = coords
[1];
359 coords
[4] = coords
[2];
360 coords
[5] = coords
[1] + h
;
361 coords
[6] = coords
[0];
362 coords
[7] = coords
[5];
369 GLuint mMinFilter
; ///< Minification filter.
370 GLuint mMagFilter
; ///< Magnification filter.
371 GLuint mWrapS
; ///< Wrapping behavior horizontally.
372 GLuint mWrapT
; ///< Wrapping behavior vertically.
376 GLuint mObject
; ///< GL texture handle.
377 static GLuint gObject
; ///< Global GL texture handle.
379 Dispatch::Handle mNewContextDispatch
;
382 GLuint
Texture::Impl::gObject
= 0;
385 Texture::Texture(const std::string
& name
) : // TODO hmm..
388 mImpl(Texture::Impl::getInstance(name
)) {}
392 * Bind the GL texture for mapping, etc.
395 void Texture::bind() const
403 * Get the texture object, for the curious.
406 GLuint
Texture::getObject() const
409 return mImpl
->mObject
;
413 void Texture::resetBind()
415 glBindTexture(GL_TEXTURE_2D
, 0);
420 void Texture::setMinFilter(GLuint filter
)
423 mImpl
->setMinFilter(filter
);
426 void Texture::setMagFilter(GLuint filter
)
429 mImpl
->setMagFilter(filter
);
432 void Texture::setWrapS(GLuint wrap
)
435 mImpl
->setWrapS(wrap
);
438 void Texture::setWrapT(GLuint wrap
)
441 mImpl
->setWrapT(wrap
);
445 bool Texture::getTileCoords(TileIndex index
, Scalar coords
[8]) const
448 return mImpl
->getTileCoords(index
, coords
);
451 bool Texture::getTileCoords(TileIndex index
, Scalar coords
[8],
452 Orientation orientation
) const
454 if (getTileCoords(index
, coords
))
456 if (orientation
& FLIP
)
458 // this looks kinda weird, but it's just swapping in a way that
459 // doesn't require an intermediate variable
460 coords
[1] = coords
[5];
461 coords
[5] = coords
[3];
462 coords
[3] = coords
[7];
463 coords
[7] = coords
[5];
465 if (orientation
& REVERSE
)
467 coords
[0] = coords
[2];
468 coords
[2] = coords
[6];
469 coords
[4] = coords
[6];
470 coords
[6] = coords
[0];
480 bool Texture::getPath(std::string
& name
)
482 return Resource::getPath(name
, "textures/", "png");