tex_mgmt.h

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software 
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00010 /*!****************************************************************************
00011  * \file    tex_mgmt.h
00012  * \author  Raj Sharma, roos@allacrost.org
00013  * \brief   Header file for texture management code
00014  *
00015  * We use texture management so that at runtime, we can load many small
00016  * images (e.g. tiles) and stick them together into larger textures called
00017  * "texture sheets". This improves performance, because then we don't have to
00018  * constantly switch textures while rendering.
00019  *
00020  * This file contains several classes:
00021  *
00022  * TexSheet: physically represents an OpenGL texture in memory, plus a pointer
00023  *           to a texture management class which keeps track of which images
00024  *           are in that texture sheet.
00025  *
00026  * TexMemMgr: abstract base class, used by TexSheet. The job of a texture
00027  *            memory manager is to allocate sub-rectangles within the sheet
00028  *            so we can stuff images into it.
00029  *
00030  * FixedTexMemMgr: texture memory manager for fixed-size images, i.e. 32x32.
00031  *                 This class can do all its operations in constant (O(1)) time
00032  *                 because it knows in advance that all textures are the same size
00033  *
00034  * FixedImageNode: this represents a single allocation within the fixed-size
00035  *                 texture memory manager
00036  *
00037  * VariableTexMemMgr: this manages memory for any size images. This works fairly
00038  *                    well in practice, but it generally has a fair amount of wasted
00039  *                    space, because the images don't always "fit together" perfectly
00040  *                    on the sheet.
00041  *
00042  * VariableImageNode: a single alloaction within the variable-size texture memory
00043  *                    manager
00044  *****************************************************************************/ 
00045 
00046 
00047 #ifndef __TEX_MGMT_HEADER__
00048 #define __TEX_MGMT_HEADER__
00049 
00050 #include "defs.h"
00051 #include "utils.h"
00052 
00053 // OpenGL includes
00054 #ifdef __APPLE__
00055   #include <OpenGL/gl.h>
00056   #include <OpenGL/glu.h>
00057 #else
00058   #include <GL/gl.h>
00059   #include <GL/glu.h>
00060 #endif
00061 
00062 namespace hoa_video
00063 {
00064 
00065 namespace private_video
00066 {
00067 
00068 
00069 
00071 enum TexSheetType
00072 {
00073   VIDEO_TEXSHEET_INVALID = -1,
00074   
00075   VIDEO_TEXSHEET_32x32 = 0,
00076   VIDEO_TEXSHEET_32x64 = 1,
00077   VIDEO_TEXSHEET_64x64 = 2,
00078   VIDEO_TEXSHEET_ANY = 3,
00079   
00080   VIDEO_TEXSHEET_TOTAL = 4
00081 };
00082 
00083 
00084 
00085 
00087 
00090 class TexMemMgr
00091 {
00092 public:
00093   
00094   virtual ~TexMemMgr() {}
00095 
00097 
00100   virtual bool Insert  (Image *img)=0;
00101   
00103 
00106   virtual bool Remove  (Image *img)=0;
00107   
00109 
00112   virtual bool Free    (Image *img)=0;
00113   
00115 
00118   virtual bool Restore (Image *img)=0;
00119 };
00120 
00121 
00122 
00123 
00125 
00131 class TexSheet
00132 {
00133 public:
00134 
00135   TexSheet(int32 w, int32 h, GLuint texID_, TexSheetType type_, bool is_static_);
00136   ~TexSheet();
00137 
00139 
00143   bool AddImage
00144   (
00145     Image *img,
00146     ImageLoadInfo & load_info
00147   );
00148   
00150 
00155   bool CopyRect(int32 x, int32 y, private_video::ImageLoadInfo & load_info);
00156 
00158 
00163   bool CopyScreenRect(int32 x, int32 y, const ScreenRect &screen_rect);
00164   
00166 
00169   bool RemoveImage (Image *img);
00170   
00172 
00175   bool FreeImage   (Image *img);
00176   
00178 
00181   bool RestoreImage (Image *img);
00182   
00184 
00186   bool Unload();
00187   
00189 
00191   bool Reload();
00192 
00194   int32 width;
00195   
00197   int32 height;
00198 
00200   bool is_static;
00201   
00203   TexSheetType type;
00204 
00206   TexMemMgr *tex_mem_manager;
00207 
00209   GLuint tex_ID;
00210   
00212   bool loaded;    
00213 };
00214 
00215 
00216 
00217 
00219 
00222 class FixedImageNode
00223 {
00224 public:
00225 
00227   Image          *image;
00228   
00230   FixedImageNode *next;
00231   
00233   FixedImageNode *prev;
00234   
00236   int32 block_index;
00237 };
00238 
00239 
00240 
00241 
00243 
00249 class FixedTexMemMgr : public TexMemMgr
00250 {
00251 public:
00252   FixedTexMemMgr(TexSheet *texSheet, int32 imgW, int32 imgH);
00253   ~FixedTexMemMgr();
00254   
00256 
00259   bool Insert  (Image *img);
00260   
00262 
00265   bool Remove  (Image *img);
00266   
00268 
00271   bool Free    (Image *img);
00272   
00274 
00277   bool Restore (Image *img);
00278 
00279 private:
00280 
00282 
00285   int32 _CalculateBlockIndex(Image *img);
00286   
00288 
00290   void _DeleteNode(int32 block_index);
00291 
00293   int32 _sheet_width;
00294 
00296   int32 _sheet_height;
00297 
00299   int32 _image_width;
00300 
00302   int32 _image_height;
00303   
00304   TexSheet *_tex_sheet;
00305   
00307 
00313   FixedImageNode *_open_list_head;
00314 
00316 
00323   FixedImageNode *_open_list_tail;
00324   
00326 
00329   FixedImageNode *_blocks;
00330 };
00331 
00332 
00333 
00334 
00336 class VariableImageNode
00337 {
00338 public:
00339   VariableImageNode()
00340   {
00341     image = NULL;
00342     free  = true;
00343   }
00344 
00346   Image *image;
00347   
00349   bool   free;
00350 };
00351 
00352 
00353 
00354 
00356 
00362 class VariableTexMemMgr : public TexMemMgr
00363 {
00364 public:
00365   
00366   VariableTexMemMgr(TexSheet *sheet);
00367   ~VariableTexMemMgr();
00368 
00370 
00373   bool Insert  (Image *img);
00374   
00376 
00379   bool Remove  (Image *img);
00380   
00382 
00385   bool Free    (Image *img);
00386   
00388 
00391   bool Restore (Image *img);
00392 
00393 private:
00394 
00396 
00403   bool SetBlockProperties
00404   (
00405     Image *img, 
00406     bool change_free, 
00407     bool change_image, 
00408     bool free, 
00409     Image *new_image
00410   );
00411 
00413   TexSheet *_tex_sheet;
00414   
00416   VariableImageNode *_blocks;
00417     
00419   int32 _sheet_width;
00420 
00422   int32 _sheet_height;
00423 };
00424 
00425 
00426 
00427 }  // namespace private_video
00428 
00429 }  // namespace hoa_video
00430 
00431 
00432 
00433 #endif   // !__TEX_MGMT_HEADER__

Generated on Fri Jul 6 23:11:24 2007 for Hero of Allacrost by  doxygen 1.5.1