Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 3c81627a924b32e6cfcd3e815e01338e25aca758 @elliotwoods committed Jul 10, 2011
Showing with 294 additions and 0 deletions.
  1. +4 −0 README
  2. +136 −0 src/ofxFontAsset.cpp
  3. +31 −0 src/ofxFontAsset.h
  4. +4 −0 src/ofxGraphicAsssets.h
  5. +92 −0 src/ofxImageAsset.cpp
  6. +27 −0 src/ofxImageAsset.h
4 README
@@ -0,0 +1,4 @@
+ofxGraphicAssets, copyright Kimchi and Chips 2011
+
+Creates global assets for images and fonts with c++ Map style access
+(some of this functionality is being introduced to ofImage for Android)
@@ -0,0 +1,136 @@
+//
+// ofxFontAsset.cpp
+// ofxImageAsset
+//
+// Created by Elliot Woods on 12/02/2011.
+// Copyright 2011 Kimchi and Chips. All rights reserved.
+//
+
+#include "ofxFontAsset.h"
+
+bool ofxFontAsset::initialised = false;
+
+map<string, ofTrueTypeFont> ofxFontAsset::dictionary = map<string, ofTrueTypeFont>();
+ofTrueTypeFont ofxFontAsset::blank = ofTrueTypeFont();
+
+ofxFontAsset::ofxFontAsset()
+{
+ initialise();
+}
+
+ofTrueTypeFont& ofxFontAsset::getFont(string assetName)
+{
+ initialise();
+
+ if (dictionary.count(assetName) != 0)
+ return dictionary[assetName];
+ else
+ {
+ if (assetName=="default")
+ return blank;
+ else
+ return dictionary["defualt"];
+ }
+}
+
+void ofxFontAsset::initialise()
+{
+ if (initialised)
+ return;
+
+ initialised = true;
+
+ ofxDirList dir;
+
+ dir.allowExt("ttf");
+ dir.setVerbose(false);
+
+ int nFiles = dir.listDir("fonts/");
+
+ string filename;
+ string sizesFilePath;
+ string name;
+
+ vector<int> sizes;
+ int currentSize;
+
+ for (int i=0; i < nFiles; i++)
+ {
+ //get filename/name without 3 char extension
+ filename = dir.getName(i);
+ name = filename.substr(0, filename.length()-4);
+
+ //get absolute filename of sizes file
+ sizesFilePath = dir.getPath(i);
+ sizesFilePath = sizesFilePath.substr(0, sizesFilePath.length()-3) + "txt";
+
+ ////////////////////////
+ //try to read sizes file
+ sizes.clear();
+ try {
+ ifstream file;
+ file.open(sizesFilePath.c_str());
+
+ while (file >> currentSize)
+ sizes.push_back(currentSize);
+
+ } catch (...) {
+
+ }
+
+ if (sizes.size() == 0)
+ sizes.push_back(10);
+ ////////////////////////
+
+ //we didn't throw, so let's
+ //load font/sizes
+
+ //if name is 'default' then we have special
+ //case of supporting only 1 text size
+ if (name == "default")
+ {
+ dictionary["default"] = ofTrueTypeFont();
+ dictionary["default"].loadFont("fonts/" + filename, sizes[0]);
+ }
+
+ //otherwise we add all text sizes
+ //as fonts name with size, e.g.:
+ // "Helvetica_10"
+
+ vector<int>::iterator it;
+ string compoundName;
+ //
+ for (it = sizes.begin(); it != sizes.end(); it++)
+ {
+ compoundName = name + "_" + ofToString(*it);
+
+ dictionary[compoundName] = ofTrueTypeFont();
+ dictionary[compoundName].loadFont("fonts/" + filename, *it);
+ }
+
+ }
+
+ //let's output what assets we have
+ cout << "////////////////////////\n";
+ cout << "//FONTS LOADED\n";
+ cout << "////////////////////////\n";
+ cout << "//\n";
+
+ map<string, ofTrueTypeFont>::iterator it;
+ for (it = dictionary.begin(); it != dictionary.end(); it++)
+ cout << it->first << "\n";
+
+ cout << "////////////////////////\n";
+
+}
+
+vector<string> ofxFontAsset::getFontList()
+{
+ vector<string> retval;
+
+ map<string, ofTrueTypeFont>::iterator it;
+ for (it = dictionary.begin(); it != dictionary.end(); it++)
+ retval.push_back(it->first);
+
+ return retval;
+}
@@ -0,0 +1,31 @@
+#pragma once
+//
+// ofxFontAsset.h
+// MapTools
+//
+// Created by Elliot Woods on 12/02/2011.
+// Copyright 2011 Kimchi and Chips. All rights reserved.
+//
+
+#include "ofxDirList.h"
+#include <fstream>
+
+#include "ofTrueTypeFont.h"
+#include <map>
+#include <algorithm>
+
+class ofxFontAsset
+{
+public:
+ ofxFontAsset();
+ static ofTrueTypeFont& getFont(string assetName);
+ static vector<string> getFontList();
+
+private:
+ static void initialise();
+
+ static map<string, ofTrueTypeFont> dictionary;
+ static bool initialised;
+
+ static ofTrueTypeFont blank;
+};
@@ -0,0 +1,4 @@
+#pragma once
+
+#include "ofxFontAsset.h"
+#include "ofxImageAsset.h"
@@ -0,0 +1,92 @@
+//
+// ofxImageAsset.cpp
+// MapTools
+//
+// Created by Elliot Woods on 11/02/2011.
+// Copyright 2011 Kimchi and Chips. All rights reserved.
+//
+
+#include "ofxImageAsset.h"
+
+bool ofxImageAsset::initialised = false;
+
+map<string, ofImage> ofxImageAsset::dictionary = map<string, ofImage>();
+ofImage ofxImageAsset::blank = ofImage();
+
+ofxImageAsset::ofxImageAsset()
+{
+ initialise();
+}
+
+ofImage& ofxImageAsset::getImage(string assetName)
+{
+ initialise();
+
+ if (dictionary.count(assetName) != 0)
+ return dictionary[assetName];
+ else
+ {
+ ofLog(OF_LOG_ERROR, "ofxImageAsset: Asset " + assetName + " not found, returning blank image");
+ return blank;
+ }
+}
+
+void ofxImageAsset::initialise()
+{
+ if (initialised)
+ return;
+
+ initialised = true;
+
+ ofxDirList dir;
+
+ dir.allowExt("png");
+ dir.setVerbose(false);
+
+ int nFiles = dir.listDir("images/");
+
+ string filename;
+ string name;
+ for (int i=0; i < nFiles; i++)
+ {
+ //get filename/name without 3 char extension
+ filename = dir.getName(i);
+ name = filename.substr(0, filename.length()-4);
+
+ //insert into map
+ dictionary [name] = ofImage();
+
+ //load image
+ dictionary [name].loadImage("images/" + filename);
+ }
+
+ //setup blank image
+ unsigned char blankpix[32*32];
+ memset(blankpix, 100, 32*32);
+ //
+ blank.setFromPixels(blankpix, 32,32, OF_IMAGE_GRAYSCALE);
+ //
+
+ //let's output what assets we have
+ cout << "////////////////////////\n";
+ cout << "//IMAGES LOADED\n";
+ cout << "////////////////////////\n";
+ cout << "//\n";
+
+ map<string, ofImage>::iterator it;
+ for (it = dictionary.begin(); it != dictionary.end(); it++)
+ cout << it->first << "\n";
+
+ cout << "////////////////////////\n";
+}
+
+vector<string> ofxImageAsset::getImageList()
+{
+ vector<string> retval;
+
+ map<string, ofImage>::iterator it;
+ for (it = dictionary.begin(); it != dictionary.end(); it++)
+ retval.push_back(it->first);
+
+ return retval;
+}
@@ -0,0 +1,27 @@
+#pragma once
+//
+// ofxImageAsset.h
+// MapTools
+//
+// Created by Elliot Woods on 11/02/2011.
+// Copyright 2011 Kimchi and Chips. All rights reserved.
+//
+
+#include "ofImage.h"
+#include "ofxDirList.h"
+
+class ofxImageAsset
+{
+ public:
+ ofxImageAsset();
+ ofImage& getImage(string assetName);
+ vector<string> getImageList();
+
+ private:
+ void initialise();
+
+ static map<string, ofImage> dictionary;
+ static bool initialised;
+
+ static ofImage blank;
+};

0 comments on commit 3c81627

Please sign in to comment.