Skip to content

Commit

Permalink
images: Allow finding an image from its name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyp committed Jan 19, 2013
1 parent 0d4e4fa commit 1e50a3a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/ivis_opengl/bitimage.cpp
Expand Up @@ -147,6 +147,7 @@ IMAGEFILE *iV_LoadImageFile(const char *fileName)
}
IMAGEFILE *imageFile = new IMAGEFILE;
imageFile->imageDefs.resize(numImages);
imageFile->imageNames.resize(numImages);
ImageMerge pageLayout;
pageLayout.images.resize(numImages);
ptr = pFileData;
Expand All @@ -163,6 +164,8 @@ IMAGEFILE *iV_LoadImageFile(const char *fileName)
debug(LOG_ERROR, "Bad line in \"%s\".", fileName);
return NULL;
}
imageFile->imageNames[numImages].first = tmpName;
imageFile->imageNames[numImages].second = numImages;
std::string spriteName = imageDir + tmpName;

ImageMergeRectangle *imageRect = &pageLayout.images[numImages];
Expand All @@ -180,6 +183,8 @@ IMAGEFILE *iV_LoadImageFile(const char *fileName)
}
free(pFileData);

std::sort(imageFile->imageNames.begin(), imageFile->imageNames.end());

pageLayout.arrange(); // Arrange all the images onto texture pages (attempt to do so with as few pages as possible).
imageFile->pages.resize(pageLayout.pages.size());

Expand Down Expand Up @@ -259,3 +264,17 @@ void iV_FreeImageFile(IMAGEFILE *imageFile)
{
delete imageFile;
}

Image IMAGEFILE::find(std::string const &name)
{
Image img;
img.images = this;
img.id = 0;
std::pair<std::string, int> val(name, 0);
std::vector<std::pair<std::string, int> >::const_iterator i = std::lower_bound(imageNames.begin(), imageNames.end(), val);
if (i != imageNames.end() && i->first == name)
{
img.id = i->second;
}
return img;
}
13 changes: 13 additions & 0 deletions lib/ivis_opengl/ivisdef.h
Expand Up @@ -35,6 +35,8 @@

#include <vector>
#include <QtCore/QVector>
#include <string>


//*************************************************************************
//
Expand Down Expand Up @@ -139,6 +141,8 @@ struct ImageDef
int YOffset; /**< Y offset into source position */
};

struct Image;

struct IMAGEFILE
{
struct Page
Expand All @@ -147,8 +151,17 @@ struct IMAGEFILE
int size; /// Size of texture in pixels. (Should be square.)
};

Image find(std::string const &name); // Defined in bitimage.cpp.

std::vector<Page> pages; /// Texture pages.
std::vector<ImageDef> imageDefs; /// Stored images.
std::vector<std::pair<std::string, int> > imageNames; ///< Names of images, sorted by name. Can lookup indices from name.
};

struct Image
{
IMAGEFILE *images;
unsigned id;
};

#endif // _ivisdef_h

0 comments on commit 1e50a3a

Please sign in to comment.