zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Textures.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <zeni_graphics.h>
19 
20 #include <iostream>
21 #include <fstream>
22 
23 #if defined(_LINUX)
24 #include <dlfcn.h>
25 #endif
26 
27 #if defined(_DEBUG) && defined(_WINDOWS)
28 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
29 #define new DEBUG_NEW
30 #endif
31 
32 #include <Zeni/Database.hxx>
33 #include <Zeni/Singleton.hxx>
34 
35 namespace Zeni {
36 
37  template class Singleton<Textures>;
38  template class Database<Texture>;
39 
40  Textures * Textures::create() {
41  return new Textures;
42  }
43 
44  Textures::Lose Textures::g_lose;
45  Textures::Unlose Textures::g_unlose;
46 
47  Textures::Textures()
48  : Database<Texture>("config/textures.xml", "Textures")
49  {
50  Video &vr = get_Video();
51 
52  init();
53 
54  vr.lend_pre_uninit(&g_lose);
55  vr.lend_post_reinit(&g_unlose);
56  }
57 
58  Textures::~Textures() {
59  Video::remove_pre_uninit(&g_lose);
60 
62  }
63 
65  return Singleton<Textures>::get();
66  }
67 
69  (*this)[name].apply_Texture();
70  }
71 
72  void Textures::apply_Texture(const unsigned long &id) {
73  (*this)[id].apply_Texture();
74  }
75 
76  bool Textures::is_Sprite(const unsigned long &id) {
77  return dynamic_cast<const Sprite *>(&(*this)[id]) != 0;
78  }
79 
80  int Textures::get_num_frames(const unsigned long &id) {
81  const Sprite *sprite = dynamic_cast<Sprite *>(&(*this)[id]);
82  if(!sprite)
84 
85  return sprite->get_num_frames();
86  }
87 
88  int Textures::get_current_frame(const unsigned long &id) {
89  const Sprite *sprite = dynamic_cast<Sprite *>(&(*this)[id]);
90  if(!sprite)
92 
93  return sprite->get_current_frame();
94  }
95 
96  void Textures::set_current_frame(const unsigned long &id, const int &frame_number) {
97  Sprite *sprite = dynamic_cast<Sprite *>(&(*this)[id]);
98  if(!sprite)
100 
101  return sprite->set_current_frame(frame_number);
102  }
103 
104  void Textures::set_texturing_mode(const int &anisotropic_filtering_, const bool &bilinear_filtering_, const bool &mipmapping_) {
105  const int af = anisotropic_filtering_ == -1 ? get_Video().get_maximum_anisotropy() : anisotropic_filtering_;
106 
107  if(m_loaded && ((af != m_anisotropic_filtering) | (bilinear_filtering_ != m_bilinear_filtering) | (mipmapping_ != m_mipmapping))) {
108  Textures &tr = get_Textures();
109 
110  m_anisotropic_filtering = af;
111  m_bilinear_filtering = bilinear_filtering_;
112  m_mipmapping = mipmapping_;
113 
114  if(!tr.lost_resources())
115  tr.reload();
116  }
117  else {
118  m_anisotropic_filtering = af;
119  m_bilinear_filtering = bilinear_filtering_;
120  m_mipmapping = mipmapping_;
121  }
122  }
123 
124  void Textures::on_load() {
125  m_loaded = true;
126  }
127 
128  void Textures::on_clear() {
129  m_loaded = false;
130  }
131 
132  void Textures::on_lose() {
133  m_loaded = false;
134  }
135 
136  Texture * Textures::load(XML_Element_c &xml_element, const String &name, const String &filename) {
137  const XML_Element_c is_sprite_e = xml_element["is_sprite"];
138  const bool is_sprite = is_sprite_e.good() && is_sprite_e.to_bool();
139 
140  if(!is_sprite) {
141  const String filepath = xml_element["filepath"].to_string();
142  const bool tile = xml_element["tile"].to_bool();
143 
144  return get_Video().load_Texture(filepath, tile, m_lazy_loading);
145  }
146  else {
147  Sprite * s = new Sprite();
148 
149  size_t frame_number = 0;
150  for(XML_Element_c texture = xml_element.first(); texture.good(); texture = texture.next(), ++frame_number)
151  try {
152  if(texture.value() == "token") {
153  const String identifier = texture.to_string();
154  s->append_frame(identifier, get_id(identifier));
155  }
156  else if(texture.value() == "file") {
157  const String filepath = texture["filepath"].to_string();
158  const bool tile = texture["tile"].to_bool();
159  const String frame_name = name + '/' + ulltoa(frame_number);
160 
161  Texture * const texture = get_Video().load_Texture(filepath, tile, m_lazy_loading);
162 
163  const unsigned long id = give(frame_name, texture, false, filename);
164 
165  s->append_frame(frame_name, id);
166  }
167  else if(texture.value() == "is_sprite")
168  --frame_number;
169  else
170  throw Database_Load_Entry_Failed(name);
171  }
172  catch(...) {
173  delete s;
174  throw;
175  }
176 
177  return s;
178  }
179  }
180 
181  bool Textures::m_loaded = false;
182  bool Textures::m_bilinear_filtering = true;
183  bool Textures::m_mipmapping = true;
184  int Textures::m_anisotropic_filtering = 0;
185  bool Textures::m_lazy_loading = false;
186 
187 }
GLdouble s
Definition: glew.h:1376
unsigned long give(const String &name, Texture *const &type, const bool &keep, const String &filename="")
Add an entry (which it will later delete)
int get_current_frame(const unsigned long &id)
Get the currently selected frame number for a Sprite.
Definition: Textures.cpp:88
void set_current_frame(const unsigned long &id, const int &frame_number)
Set the frame number for a Sprite.
Definition: Textures.cpp:96
static void init(struct bs2b *bs2b)
Definition: bs2b.c:46
unsigned long get_id(const String &name) const
Get an id by name, possibly throwing an Error.
EGLImageKHR EGLint * name
Definition: eglext.h:284
void reload()
lose_resources + init
Definition: Database.hxx:308
int get_num_frames(const unsigned long &id)
Get the number of frames; returns 0 if it is not a Sprite.
Definition: Textures.cpp:80
GLuint id
Definition: gl2ext.h:1142
void set_current_frame(const int &frame_number)
Set this frame.
Definition: Texture.cpp:112
static void remove_pre_uninit(Event::Handler *const &handler)
A Texture Database Singleton.
Definition: Textures.h:52
static TYPE & get()
Definition: Singleton.hxx:31
Textures & get_Textures()
Get access to the singleton.
Definition: Textures.cpp:64
void apply_Texture(const String &name)
Apply a texture for upcoming polygons (Called by Video::apply_Texture)
Definition: Textures.cpp:68
GLenum GLenum GLuint texture
Definition: gl2ext.h:850
bool is_sprite(const String &sprite)
Definition: EZ2D.cpp:84
int get_current_frame() const
Get the currently selected frame number.
Definition: Texture.cpp:106
virtual Texture * load_Texture(const String &filename, const bool &repeat, const bool &lazy_loading=false)=0
Function for loading a Texture; used internally by Textures.
const bool & lost_resources()
Check to see if resources have been lost.
Definition: Database.hxx:381
int get_num_frames() const
Get the number of frames.
Definition: Texture.cpp:102
virtual int get_maximum_anisotropy() const =0
Get the current level of anisotrophy.
static void set_texturing_mode(const int &anisotropic_filtering_, const bool &bilinear_filtering_, const bool &mipmapping_)
Check to see if Textures is set to use lazy loading if possible.
Definition: Textures.cpp:104
bool is_Sprite(const unsigned long &id)
Does this id point to a Sprite.
Definition: Textures.cpp:76
Video & get_Video()
Get access to the singleton.
Definition: Video.cpp:149
String ulltoa(const unsigned long &number)