MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AssetManager.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  AssetManager.h SDL_Surface management system
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
28 #ifndef ASSETMANAGER_H
29 #define ASSETMANAGER_H
30 
31 #include "SDL_image.h"
32 #include "SDL.h"
33 #include "SDL_ttf.h"
34 #include "Logger.h"
35 #include "Utils.h"
36 
37 namespace MFM
38 {
39  enum Asset
40  {
41  ASSET_SELECTOR_ICON = 0,
42  ASSET_ATOM_SELECTOR_ICON,
43  ASSET_PENCIL_ICON,
44  ASSET_ERASER_ICON,
45  ASSET_BRUSH_ICON,
46  ASSET_BUCKET_ICON,
47  ASSET_XRAY_ICON,
48  ASSET_CLONE_ICON,
49  ASSET_AIRBRUSH_ICON,
50  ASSET_CHECKBOX_ICON_ON,
51  ASSET_CHECKBOX_ICON_OFF,
52  ASSET_SLIDER_HANDLE,
53  ASSET_SELECTOR_ICON_BIG,
54  ASSET_ATOM_SELECTOR_ICON_BIG,
55  ASSET_PENCIL_ICON_BIG,
56  ASSET_ERASER_ICON_BIG,
57  ASSET_BRUSH_ICON_BIG,
58  ASSET_BUCKET_ICON_BIG,
59  ASSET_XRAY_ICON_BIG,
60  ASSET_CLONE_ICON_BIG,
61  ASSET_AIRBRUSH_ICON_BIG,
62  ASSET_COUNT
63  };
64 
65  enum FontAsset
66  {
67  FONT_ASSET_ELEMENT = 0,
68  FONT_ASSET_ELEMENT_BIG,
69  FONT_ASSET_HELPPANEL_BIG,
70  FONT_ASSET_HELPPANEL_SMALL,
71  FONT_ASSET_LOGGER,
72  FONT_ASSET_COUNT
73  };
74 
76  {
77  private:
78 
79  static SDL_Surface* surfaces[ASSET_COUNT];
80 
81  static TTF_Font* fonts[FONT_ASSET_COUNT];
82 
83  static bool initialized;
84 
85  static SDL_Surface* LoadImage(const char* relativeFilename)
86  {
87  const u32 BUFFER_SIZE = 1024;
88  char filename[BUFFER_SIZE];
89  SDL_Surface* loaded = NULL;
90  SDL_Surface* opped = NULL;
91 
92  if(Utils::GetReadableResourceFile(relativeFilename, filename, BUFFER_SIZE))
93  {
94  loaded = IMG_Load(filename);
95 
96  if(loaded)
97  {
98  opped = SDL_DisplayFormatAlpha(loaded);
99 
100  SDL_FreeSurface(loaded);
101 
102  LOG.Debug("Surface %s loaded: %p, opped: %p", filename, loaded, opped);
103  }
104  else
105  {
106  LOG.Error("Image %s not loaded : %s",
107  filename,
108  IMG_GetError());
109  }
110  }
111  else
112  {
113  LOG.Error("Cannot compute relative path to: %s", relativeFilename);
114  }
115 
116  return opped;
117  }
118 
119  static TTF_Font* LoadFont(const char* relativePath, u32 size)
120  {
121  const u32 BUFFER_SIZE = 1024;
122  TTF_Font* font = NULL;
123  char path[BUFFER_SIZE];
124 
125  if(Utils::GetReadableResourceFile(relativePath, path, BUFFER_SIZE))
126  {
127  font = TTF_OpenFont(path, size);
128 
129  if(!font)
130  {
131  LOG.Error("Font %s not loaded: %s",
132  path,
133  TTF_GetError());
134  }
135 
136  LOG.Debug("Font %s loaded: %p", relativePath, font);
137  }
138  else
139  {
140  LOG.Error("Cannot compute relative path to: %s", relativePath);
141  }
142 
143  return font;
144  }
145 
146  public:
147 
152  static void Initialize()
153  {
154  if(!initialized)
155  {
156  surfaces[ASSET_SELECTOR_ICON] = LoadImage("images/selector_icon.png");
157  surfaces[ASSET_ATOM_SELECTOR_ICON] = LoadImage("images/atom_selector_icon.png");
158  surfaces[ASSET_PENCIL_ICON] = LoadImage("images/pencil_icon.png");
159  surfaces[ASSET_ERASER_ICON] = LoadImage("images/eraser_icon.png");
160  surfaces[ASSET_BRUSH_ICON] = LoadImage("images/brush_icon.png");
161  surfaces[ASSET_BUCKET_ICON] = LoadImage("images/bucket_icon.png");
162  surfaces[ASSET_XRAY_ICON] = LoadImage("images/xray_icon.png");
163  surfaces[ASSET_CLONE_ICON] = LoadImage("images/clone_icon.png");
164  surfaces[ASSET_AIRBRUSH_ICON] = LoadImage("images/airbrush_icon.png");
165  surfaces[ASSET_CHECKBOX_ICON_ON] = LoadImage("images/checkbox_on.png");
166  surfaces[ASSET_CHECKBOX_ICON_OFF] = LoadImage("images/checkbox_off.png");
167  surfaces[ASSET_SLIDER_HANDLE] = LoadImage("images/slider_handle.png");
168  surfaces[ASSET_SELECTOR_ICON_BIG] = LoadImage("images/selector_icon_big.png");
169  surfaces[ASSET_ATOM_SELECTOR_ICON_BIG] = LoadImage("images/atom_selector_icon_big.png");
170  surfaces[ASSET_PENCIL_ICON_BIG] = LoadImage("images/pencil_icon_big.png");
171  surfaces[ASSET_ERASER_ICON_BIG] = LoadImage("images/eraser_icon_big.png");
172  surfaces[ASSET_BRUSH_ICON_BIG] = LoadImage("images/brush_icon_big.png");
173  surfaces[ASSET_BUCKET_ICON_BIG] = LoadImage("images/bucket_icon_big.png");
174  surfaces[ASSET_XRAY_ICON_BIG] = LoadImage("images/xray_icon_big.png");
175  surfaces[ASSET_CLONE_ICON_BIG] = LoadImage("images/clone_icon_big.png");
176  surfaces[ASSET_AIRBRUSH_ICON_BIG] = LoadImage("images/airbrush_icon_big.png");
177 
178  fonts[FONT_ASSET_ELEMENT] = LoadFont("fonts/tiny.ttf", 24);
179  fonts[FONT_ASSET_ELEMENT_BIG] = LoadFont("fonts/tiny.ttf", 40);
180  fonts[FONT_ASSET_HELPPANEL_BIG] = LoadFont("fonts/tiny.ttf", 26);
181  fonts[FONT_ASSET_HELPPANEL_SMALL] = LoadFont("fonts/tiny.ttf", 16);
182  fonts[FONT_ASSET_LOGGER] = LoadFont("fonts/tiny.ttf", 15);
183 
184  initialized = true;
185  }
186  }
187 
191  static void Destroy()
192  {
193  if(initialized)
194  {
195  for(Asset i = ASSET_SELECTOR_ICON; i < ASSET_COUNT; i = (Asset)(i + 1))
196  {
197  SDL_FreeSurface(surfaces[i]);
198  surfaces[i] = NULL;
199  }
200 
201  for(FontAsset i = FONT_ASSET_ELEMENT; i < FONT_ASSET_COUNT; i = (FontAsset)(i + 1))
202  {
203  TTF_CloseFont(fonts[i]);
204  fonts[i] = NULL;
205  }
206 
207  initialized = false;
208  }
209  }
210 
211  static SDL_Surface* Get(Asset a)
212  {
213  return surfaces[a];
214  }
215 
216  static TTF_Font* Get(FontAsset a)
217  {
218  return fonts[a];
219  }
220  };
221 }
222 
223 #endif /* ASSETMANAGER_H */
void Error(const char *format,...)
Definition: Logger.h:259
Definition: AssetManager.h:75
void Debug(const char *format,...)
Definition: Logger.h:301
static void Destroy()
Definition: AssetManager.h:191
static void Initialize()
Definition: AssetManager.h:152