Image Atlas.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /******************************************************************************
  2. Use 'ImageAtlas' for storing multiple images in one texture.
  3. /******************************************************************************/
  4. struct ImageAtlas // Image Atlas, set of multiple images packed together into as few textures as possible
  5. {
  6. struct Source // source image used to create the atlas
  7. {
  8. ImagePtr image;
  9. Str name ;
  10. void set(C ImagePtr &image, C Str &name) {T.image=image; T.name=name;}
  11. };
  12. struct Part // single image stored in one of the textures
  13. {
  14. Byte image_index ; // index of the image in 'images' in which the part is located, or 0xFF if this part is completely transparent and is not stored in any image
  15. Bool rotated ; // if this part is stored in the image as rotated and needs vertical drawing
  16. Rect tex_rect ; // UV texture coordinates of the part (after trimming)
  17. Vec2 center_offset; // offset which you can apply for drawing assuming that the anchor point was located in the center of the original image, it's equal to "Vec2(trim_pos.x-original_size.x/2, -trim_pos.y+original_size.y/2)" (in pixels)
  18. VecI2 original_size, // size of the image (in pixels, without trimming)
  19. trimmed_size , // size of the image (in pixels, after trimming)
  20. trim_pos ; // position in the original image where the trim has started (in pixels)
  21. Str name ; // name of the part
  22. };
  23. Mems<Image> images;
  24. Mems<Part > parts;
  25. // get
  26. Part* findPart (C Str &name); // find 'name' part , null on fail
  27. Int findPartI(C Str &name); // find 'name' part index, -1 on fail
  28. Part* getPart (C Str &name); // get 'name' part , Exit on fail
  29. Int getPartI(C Str &name); // get 'name' part index, Exit on fail
  30. // manage
  31. void del ();
  32. Bool create(C MemPtr<Source> &images, IMAGE_TYPE image_type, Int mip_maps=0, Bool allow_rotate=true, Int border=4, Bool align_for_compression=false, Bool only_square=false, Int max_tex_size=2048, Bool trim_transparent=true, Bool transparent_to_neighbors=true); // create Image Atlas from array of 'images', 'image_type'=image type of the target image, 'mip_maps'=amount of mip maps the target image should have (0=autodetect), 'allow_rotate'=if allow images to be stored as rotated, 'border'=number of pixels to be used as empty space between images, 'align_for_compression'=if align rectangles on a 4-pixel boundary to allow for equal compression on all images (enabled 'allow_rotate' may prevent this option from best effect), 'only_square'=if allow textures to be created only in the form of a square (if false then rectangle is also allowed), 'max_tex_size'=maximum allowed texture size during creation of the atlas, 'trim_transparent'=if trim transparent pixels from source images and don't store them in the output, 'transparent_to_neighbors'=if convert transparent pixel RGB values to values taken from neighbors
  33. // draw
  34. void draw(Int part_index, C Vec2 &pos, Flt pixel_size)C; // draw specified part index at 'pos' screen position, with 'pixel_size'
  35. // io
  36. void operator=(C Str &name) ; // load, Exit on fail
  37. Bool save (C Str &name)C; // save, false on fail
  38. Bool load (C Str &name) ; // load, false on fail
  39. Bool save ( File &f )C; // save, false on fail
  40. Bool load ( File &f ) ; // load, false on fail
  41. };
  42. /******************************************************************************/
  43. DECLARE_CACHE(ImageAtlas, ImageAtlases, ImageAtlasPtr); // 'ImageAtlases' cache storing 'ImageAtlas' objects which can be accessed by 'ImageAtlasPtr' pointer
  44. /******************************************************************************/