Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.24 KB | None | 0 0
  1. #pragma target fte
  2. // ========================================================
  3. //  GRAPHICS **********************************************
  4. // ========================================================
  5. // This is the base graphic class. It includes sources and
  6. // subpic definitions used by both sprites and tiles.
  7. // --------------------------------------------------------
  8.  
  9. // --------------------------------------------------------
  10. // Graphics - Struct - SourcePic
  11. // --------------------------------------------------------
  12. typedef struct
  13. {
  14.     string alias;
  15.     string path;
  16.     vector size;
  17. }SourcePic;
  18. // --------------------------------------------------------
  19. // Graphics - Struct - Graphic (subpic)
  20. // --------------------------------------------------------
  21. typedef struct
  22. {
  23.     string alias;
  24.     string source;
  25.     vector origin;
  26.     vector size;
  27. }Graphic;
  28. // --------------------------------------------------------
  29. // Graphics - Pointers for storing sources and graphics
  30. // --------------------------------------------------------
  31. SourcePic *sources;
  32. Graphic *graphics;
  33. // --------------------------------------------------------
  34. // Graphics - Counters for pointer lengths
  35. // --------------------------------------------------------
  36. int sourcecnt;
  37. int graphiccnt;
  38. // --------------------------------------------------------
  39. // Graphics - Hashtables for quick lookup
  40. // --------------------------------------------------------
  41. hashtable srctab;
  42. hashtable gfxtab;
  43. // --------------------------------------------------------
  44. // Graphics - Accessor for sourcepic
  45. // --------------------------------------------------------
  46. accessor src:SourcePic*
  47. {
  48.     get SourcePic* [string key] =
  49.     {
  50.         int idx = ftoi(hash_get(srctab, key));
  51.         return sources + idx;
  52.     }
  53. }SourcePics;
  54. // --------------------------------------------------------
  55. // Graphics - Accessor for graphic
  56. // --------------------------------------------------------
  57. accessor gfx:Graphic*
  58. {
  59.     get SourcePic* [string key] =
  60.     {
  61.         int idx = ftoi(hash_get(gfxtab, key));
  62.         return graphics + idx;
  63.     }
  64. }Graphics;
  65. // --------------------------------------------------------
  66. // Graphics - Add new source
  67. // --------------------------------------------------------
  68. void(string alias, string path) SourcePic_AddNew =
  69. {
  70.     if(sourcecnt <= 0) srctab = hash_createtab(0, EV_FLOAT);
  71.     sourcecnt++;
  72.     SourcePic *temp = memalloc(sizeof(SourcePic) * sourcecnt);
  73.     memcpy(temp, sources, sizeof(SourcePic) * (sourcecnt - 1));
  74.     memfree(sources);
  75.     sources = temp;
  76.     hash_add(srctab, alias, itof(sourcecnt - 1));
  77.     SourcePics[alias]->path = path;
  78.     SourcePics[alias]->alias = alias;
  79.     SourcePics[alias]->size = drawgetimagesize(path);
  80. };
  81. // --------------------------------------------------------
  82. // Graphics - Source Index
  83. // --------------------------------------------------------
  84. int(SourcePic *source) SourcePic_IndexOf =
  85. {
  86.     return ftoi(hash_get(srctab, source->alias));
  87. };
  88. // --------------------------------------------------------
  89. // Graphics - Remove a source
  90. // --------------------------------------------------------
  91. void(SourcePic *source) SourcePic_Remove =
  92. {
  93.     int index = SourcePic_IndexOf(source);
  94.     memcpy(&sources[index], &sources[index + 1], sizeof(SourcePic) * (sourcecnt - (index + 1)));
  95. };
  96. // --------------------------------------------------------
  97. // Graphics - Draw source resized
  98. // --------------------------------------------------------
  99. void(SourcePic *source, vector location, vector size, vector rgb, float alpha) SourcePic_DrawScaled =
  100. {
  101.     drawpic(location, source->path, size, rgb, alpha);
  102. };
  103. // --------------------------------------------------------
  104. // Graphics - Draw source at base size
  105. // --------------------------------------------------------
  106. void(SourcePic *source, vector location, vector rgb, float alpha) SourcePic_Draw =
  107. {
  108.     SourcePic_DrawScaled(source, location, source->size, rgb, alpha);
  109. };
  110. // --------------------------------------------------------
  111. // Graphics - Purge all source data
  112. // --------------------------------------------------------
  113. void() SourcePic_Clear =
  114. {
  115.     memfree(sources);
  116.     hash_destroytab(srctab);
  117.     sourcecnt = 0;
  118. };
  119. // --------------------------------------------------------
  120. // Graphics - Add new graphic
  121. // --------------------------------------------------------
  122. void(string alias, vector origin, vector size, SourcePic *source) Graphic_AddNew =
  123. {
  124.     if(graphiccnt <= 0) gfxtab = hash_createtab(0, EV_FLOAT);
  125.     graphiccnt++;
  126.     Graphic *temp = memalloc(sizeof(Graphic) * graphiccnt);
  127.     memcpy(temp, graphics, sizeof(Graphic) * (graphiccnt - 1));
  128.     hash_add(gfxtab, alias, itof(graphiccnt - 1));
  129.     Graphics[alias]->source = source->alias;
  130.     Graphics[alias]->origin = origin;
  131.     Graphics[alias]->size = size;
  132. };
  133.  
  134. int(Graphic *gf) Graphic_IndexOf =
  135. {
  136.     return ftoi(hash_get(gfxtab, gf->alias));
  137. };
  138.  
  139. void(Graphic *gf) Graphic_Remove =
  140. {
  141.     int index = Graphic_IndexOf(gf);
  142.     memcpy(&graphics[index], &graphics[index + 1], sizeof(Graphic) * (graphiccnt - (index + 1)));
  143. };
  144.  
  145. void(Graphic *gf, vector location, vector anchor, float angle, vector rgb, float alpha) Graphic_DrawRotated =
  146. {
  147.     // four corners of the rectangle.
  148.     vector ul = location;
  149.     vector ur = [location_x + gf->size[0], location_y];
  150.     vector lr = [location_x + gf->size[0], location_y + gf->size[1]];
  151.     vector ll = [location_x, location_y + gf->size[1]];
  152.     // Hotspot
  153.     vector hotspot = anchor + location;
  154.     // Distances
  155.     float uld = GetDistance(hotspot, ul);
  156.     float urd = GetDistance(hotspot, ur);
  157.     float lrd = GetDistance(hotspot, lr);
  158.     float lld = GetDistance(hotspot, ll);
  159.     // Rotate with trig
  160.     ul = OrbitPoint(hotspot, uld, angle);
  161.     rl = OrbitPoint(hotspot, urd, angle);
  162.     lr = OrbitPoint(hotspot, lrd, angle);
  163.     ll = OrbitPoint(hotspot, lld, angle);
  164.     // Draw
  165.     R_BeginPolygon(SourcePics[gf->source]->path, 0, 1);
  166.     R_PolygonVertex(ul, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0], gf->origin[1]]), rgb, alpha);
  167.     R_PolygonVertex(ur, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0] + gf->size[0], gf->origin[1]]), rgb, alpha);
  168.     R_PolygonVertex(lr, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0] + gf->size[0], gf->origin[1] + gf->size[1]]), rgb, alpha);
  169.     R_PolygonVertex(ll, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0], gf->origin[1] + gf->size[1]]), rgb, alpha);
  170.     R_EndPolygon();
  171. };
  172.  
  173. void(Graphic *gf, vector location, vector rgb, float alpha) Graphic_Draw =
  174. {
  175.     // four corners of the rectangle.
  176.     vector ul = location;
  177.     vector ur = [location_x + gf->size[0], location_y];
  178.     vector lr = [location_x + gf->size[0], location_y + gf->size[1]];
  179.     vector ll = [location_x, location_y + gf->size[1]];
  180.     // Draw
  181.     R_BeginPolygon(SourcePics[gf->source]->path, 0, 1);
  182.     R_PolygonVertex(ul, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0], gf->origin[1]]), rgb, alpha);
  183.     R_PolygonVertex(ur, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0] + gf->size[0], gf->origin[1]]), rgb, alpha);
  184.     R_PolygonVertex(lr, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0] + gf->size[0], gf->origin[1] + gf->size[1]]), rgb, alpha);
  185.     R_PolygonVertex(ll, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0], gf->origin[1] + gf->size[1]]), rgb, alpha);
  186.     R_EndPolygon();
  187. };
  188.  
  189. void() Graphic_Clear =
  190. {
  191.     memfree(graphics);
  192.     graphiccnt = 0;
  193.     hash_destroytab(gfxtab);
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement