D:/Programmation/Cpp/SFML/src/SFML/AdvancedGraphics/TiledBackground.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/AdvancedGraphics/TiledBackground.hpp>
00029 #include <SFML/Graphics/Image.hpp>
00030 #include <SFML/Graphics/OpenGL.hpp>
00031 #include <SFML/Graphics/RenderWindow.hpp>
00032 #include <SFML/Utilities/Rect.hpp>
00033 
00034 
00038 sfTiledBackground::sfTiledBackground() :
00039 myLeft      (0.f),
00040 myTop       (0.f),
00041 myTileset   (NULL),
00042 myTileSize  (32),
00043 myScale     (1.f),
00044 myNbTilesX  (1),
00045 myNbTilesY  (1),
00046 myTiles     (1),
00047 myIsCompiled(false),
00048 myGLList    (0)
00049 {
00050 
00051 }
00052 
00053 
00057 sfTiledBackground::sfTiledBackground(const sfImage& Img, unsigned int TileSize, unsigned int NbTilesX, unsigned int NbTilesY) :
00058 myLeft      (0.f),
00059 myTop       (0.f),
00060 myTileset   (&Img),
00061 myTileSize  (TileSize),
00062 myScale     (1.f),
00063 myNbTilesX  (NbTilesX),
00064 myNbTilesY  (NbTilesY),
00065 myTiles     (NbTilesX * NbTilesY),
00066 myIsCompiled(false),
00067 myGLList    (0)
00068 {
00069     Initialize(Img, TileSize, NbTilesX, NbTilesY);
00070 }
00071 
00072 
00076 sfTiledBackground::~sfTiledBackground()
00077 {
00078     DestroyVideoResources();
00079 }
00080 
00081 
00085 void sfTiledBackground::Render(sfRenderWindow& Window)
00086 {
00087     // Check if rendering commands are compiled before rendering the background
00088     if (!myIsCompiled)
00089         Compile();
00090 
00091     // Just call compiled list
00092     sfGLCheck(glCallList(myGLList));
00093 }
00094 
00095 
00099 void sfTiledBackground::Initialize(const sfImage& Img, unsigned int TileSize, unsigned int NbTilesX, unsigned int NbTilesY)
00100 {
00101     myTileset    = &Img;
00102     myTileSize   = TileSize;
00103     myScale      = 1.f;
00104     myNbTilesX   = NbTilesX;
00105     myNbTilesY   = NbTilesY;
00106     myIsCompiled = false;
00107     myTiles.resize(NbTilesX * NbTilesY);
00108 }
00109 
00110 
00114 void sfTiledBackground::SetTile(unsigned int PosX, unsigned int PosY, unsigned int IndexX, unsigned int IndexY)
00115 {
00116     if ((PosX < myNbTilesX) && (PosY < myNbTilesY))
00117     {
00118         myTiles[PosX + PosY * myNbTilesX].X = IndexX;
00119         myTiles[PosX + PosY * myNbTilesX].Y = IndexY;
00120         myIsCompiled = false;
00121     }
00122     else
00123     {
00124         std::cerr << "Trying to access an invalid position in tiled background "
00125                   << "(position = (" << PosX << ", " << PosY << ", size = (" << myNbTilesX << ", " << myNbTilesY << ")"
00126                   << std::endl;
00127     }
00128 }
00129 
00130 
00134 void sfTiledBackground::SetPosition(float Left, float Top)
00135 {
00136     myLeft       = Left;
00137     myTop        = Top;
00138     myIsCompiled = false;
00139 }
00140 
00141 
00145 void sfTiledBackground::SetScale(float Scale)
00146 {
00147     if (Scale > 0.f)
00148     {
00149         myScale      = Scale;
00150         myIsCompiled = false;
00151     }
00152     else
00153     {
00154         std::cerr << "Trying to assign an invalid scale to tiled background (value = " << Scale << ")" << std::endl;
00155     }
00156 }
00157 
00158 
00162 void sfTiledBackground::Compile()
00163 {
00164     // If background is already compiled, we don't do anything
00165     if (myIsCompiled)
00166         return;
00167 
00168     // Check tileset is valid
00169     if (myTileset == NULL)
00170         return;
00171 
00172     // If list is not created, create it
00173     if (!myGLList)
00174         myGLList = glGenLists(1);
00175 
00176     // Start capturing OpenGL commands
00177     sfGLCheck(glNewList(myGLList, GL_COMPILE));
00178 
00179     // Disable alpha-blending
00180     sfGLCheck(glDisable(GL_BLEND));
00181 
00182     // Disable color
00183     sfGLCheck(glColor4ub(255, 255, 255, 255));
00184 
00185     // Bind tileset texture
00186     myTileset->Bind();
00187 
00188     // Calculate actual tile size
00189     const float Size = myTileSize * myScale;
00190 
00191     // Render tiles
00192     glBegin(GL_QUADS);
00193     for (unsigned int i = 0; i < myNbTilesX; ++i)
00194         for (unsigned int j = 0; j < myNbTilesY; ++j)
00195         {
00196             // Calculate texture coordinates
00197             int X = myTiles[i + j * myNbTilesX].X * myTileSize;
00198             int Y = myTiles[i + j * myNbTilesX].Y * myTileSize;
00199             sfFloatRect TexCoords = myTileset->GetTexCoords(sfIntRect(X, Y, X + myTileSize, Y + myTileSize));
00200 
00201             // Create quad corresponding to current tile
00202             glTexCoord2f(TexCoords.Left,  TexCoords.Top);    glVertex2f((i + 0) * Size + myLeft, (j + 0) * Size + myTop);
00203             glTexCoord2f(TexCoords.Left,  TexCoords.Bottom); glVertex2f((i + 0) * Size + myLeft, (j + 1) * Size + myTop);
00204             glTexCoord2f(TexCoords.Right, TexCoords.Bottom); glVertex2f((i + 1) * Size + myLeft, (j + 1) * Size + myTop);
00205             glTexCoord2f(TexCoords.Right, TexCoords.Top);    glVertex2f((i + 1) * Size + myLeft, (j + 0) * Size + myTop);
00206         }
00207     glEnd();
00208 
00209     // Enable alpha-blending
00210     sfGLCheck(glEnable(GL_BLEND));
00211 
00212     // End capturing OpenGL commands
00213     sfGLCheck(glEndList());
00214 
00215     // Set the "compiled" flag
00216     myIsCompiled = true;
00217 }
00218 
00219 
00223 void sfTiledBackground::InitVideoResources()
00224 {
00225     // We don't recompile display list now, because texture may not be reloaded yet
00226     // So we just wait for the first call to Render :)
00227 }
00228 
00229 
00233 void sfTiledBackground::DestroyVideoResources()
00234 {
00235     // Destroy display list
00236     if (myGLList)
00237     {
00238         sfGLCheck(glDeleteLists(myGLList, 1));
00239         myGLList = 0;
00240     }
00241 
00242     // Reset "compiled" flag
00243     myIsCompiled = false;
00244 }

Generated for SFML by  doxygen 1.5.2