Advertisement
Guest User

Untitled

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