File: VTexture.h

package info (click to toggle)
xmoto 0.5.11+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 80,908 kB
  • sloc: cpp: 96,757; ansic: 22,196; sh: 4,940; makefile: 1,073; yacc: 289; sed: 16
file content (144 lines) | stat: -rw-r--r-- 4,157 bytes parent folder | download | duplicates (2)
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
/*=============================================================================
XMOTO

This file is part of XMOTO.

XMOTO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

XMOTO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with XMOTO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=============================================================================*/

#ifndef __VTEXTURE_H__
#define __VTEXTURE_H__

#include "VCommon.h"
#include "helpers/VExcept.h"
#include "include/xm_SDL.h"
#include <vector>
#include "include/xm_hashmap.h"

class Sprite;

enum FilterMode {
  FM_NEAREST,
  FM_LINEAR,
  FM_MIPMAP
};

// Our friendly texture exception friend
class TextureError : public Exception {
public:
  TextureError(std::string &iMsg)
    : Exception(iMsg) {}
  TextureError(const char *pc)
    : Exception(std::string(pc)) {}
};

//keesj:todo. I experimented with converting this struct to a
//class and extending it so that the opengl version would only
//contain nID and the SDL_based one the Surface pointer. I think it is
//a lot of work and because the game currently depends on SDL
//and nID is not a openGL specific structure
//it's

enum RegistrationStageMode { RSM_PERSISTENT, RSM_NORMAL };

#define PERSISTENT 0
class Texture {
public:
  Texture() {
    nWidth               = 0;
    nHeight              = 0;
    nID                  = 0;
    surface              = NULL;
    nSize                = 0;
    isAlpha              = false;
    curRegistrationStageMode = RSM_PERSISTENT;
  }

  std::string    Name;
  int            nWidth;
  int            nHeight;
  // OpenGL name
  unsigned int   nID;
  SDL_Surface*   surface;
  // size in bytes
  int            nSize;
  bool           isAlpha;
  unsigned char* pcData;

  // zero for persistent textures
  RegistrationStageMode curRegistrationStageMode;
  std::vector<unsigned int> curRegistrationStage;

  // when the texture is removed, keep the sprites informed so that
  // it invalides its pointer on the texture
  std::vector<Sprite*> associatedSprites;

  void addAssociatedSprite(Sprite* sprite);
  void invalidateSpritesTexture();
  void removeAssociatedSprites();
};



class TextureManager {
public:
  TextureManager() {
    m_nTexSpaceUsage=0;
  }

  ~TextureManager();

  Texture* createTexture(const std::string& Name, unsigned char* pcData, int nWidth, int nHeight, bool bAlpha=false, bool bClamp=false, FilterMode eFilterMode = FM_MIPMAP);
  void destroyTexture(Texture* pTexture);
  Texture* loadTexture(const std::string& Path, bool bSmall=false, bool bClamp=false, FilterMode eFilterMode = FM_MIPMAP, bool persistent=false, Sprite* associatedSprite=NULL);
  int getTextureSize(const std::string& p_fileName);
  Texture* getTexture(const std::string& Name);
  void removeAssociatedSpritesFromTextures();
  void unloadTextures(void);

  std::vector<Texture*>& getTextures(void) {
    return m_Textures;
  }
  int getTextureUsage(void) {
    return m_nTexSpaceUsage;
  }

  // texture registration
  static bool registering();
  static unsigned int currentRegistrationStage();
  unsigned int beginTexturesRegistration();
  void endTexturesRegistration();
  void registerTexture(Texture* i_texture);
  bool isRegisteredTexture(Texture* i_texture);
  void unregister(unsigned int i_registerValue);

private:
  std::vector<Texture*> m_Textures;

  int m_nTexSpaceUsage;

  // texture registration
  static unsigned int m_curRegistrationStage;
  static bool m_registering;

  void cleanUnregistredTextures();

  HashNamespace::unordered_map<std::string, int*> m_textureSizeCache;
  std::vector<std::string> m_textureSizeCacheKeys;
  std::vector<int*> m_textureSizeCacheValues;

};

#endif