Posted By

spire8989 on 05/08/10


Tagged


Versions (?)

Stranded - Snippet 1


 / Published in: C++
 

  1. void InventoryGui::setGui()
  2. {
  3. if(inv)
  4. {
  5. // set item list
  6. {
  7. // here we have to hide the items which are assigned to slots
  8. // create a 'is' array which determines visibility of an element (1=visible, 0=hidden)
  9. Bool *is=Alloc<Bool>(inv->items.elms()); // allocate bytes for all items in container
  10. SetMem(is,1,inv->items.elms()); // set memory to '1' (make all items visible by default)
  11. REPA(inv->slot) // iterate through all slots
  12. {
  13. if(inv->slot[i].valid()) // if the slot is valid
  14. {
  15. Int index=inv->items.validIndex(&inv->slot[i]()); // get index of a slot item in items container
  16. if(InRange(index,inv->items)) // if its valid
  17. is[index]=0; // set visibility for item assigned to a slot to 0, to be hidden on the list
  18. }
  19. }
  20. list.setData(inv->items,is); // set list data from items container and visibility list
  21. Free(is); // free allocated memory
  22. }
  23.  
  24. // set slot images
  25. REP(SLOT_NUM) // for all slots
  26. if(i!=SLOT_TEMP) // skip temporary slot because it's not drawn using 'Image' class
  27. {
  28. GuiImage &img_back= slot_img[i][0], // background image, we use it for accessing its rectangle
  29. &img_item= slot_img[i][1]; // item image
  30. Reference<Item> & item=inv->slot [i] ; // item at slot
  31.  
  32. if(!item.valid())img_item.set(NULL);else // if there is no item then clear the item image slot
  33. {
  34. Image *icon=item().icon; // access item's icon
  35. img_item.set(icon); // set slot image as the item's icon
  36. if(icon) // set proper scaling
  37. {
  38. Vec2 size(icon->x(),icon->y()); size*=PIXEL_SIZE; // set default size
  39. if(size.x>img_back.rect.w())size*=img_back.rect.w()/size.x; // clamp item size to background slot width
  40. if(size.y>img_back.rect.h())size*=img_back.rect.h()/size.y; // clamp item size to background slot height
  41. Rect rect(img_back.rect.center()); rect.extend(size/2);
  42. img_item.setRect(rect);
  43. }
  44. }
  45. }
  46. }else
  47. {
  48. list.clear();
  49. REP(SLOT_NUM)slot_img[i][1].set(NULL);
  50. }
  51. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Sammo on August 4, 2010

Nice work!

You need to login to post a comment.