Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. package com.sci.engine.graphics;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.DataBufferInt;
  5. import java.util.Arrays;
  6. import com.sci.engine.interfaces.Renderable;
  7.  
  8. /**
  9. * SciEngine
  10. *
  11. * @author sci4me
  12. * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
  13. */
  14.  
  15. public final class Renderer
  16. {
  17. private Font font;
  18. private int width;
  19. private int height;
  20. private int[] pixels;
  21. private int[] imagePixels;
  22. private BufferedImage image;
  23.  
  24. /**
  25. * Creates a new Renderer with the specified width and height
  26. *
  27. * @param width
  28. * @param height
  29. */
  30. public Renderer(int width, int height)
  31. {
  32. this.width = width;
  33. this.height = height;
  34. this.pixels = new int[this.width * this.height];
  35. this.image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_ARGB);
  36. this.imagePixels = ((DataBufferInt) this.image.getRaster().getDataBuffer()).getData();
  37. this.image.setAccelerationPriority(1f);
  38. }
  39.  
  40. /**
  41. * Clears the renderer to black
  42. */
  43. public void clear()
  44. {
  45. this.clear(Color.BLACK);
  46. }
  47.  
  48. /**
  49. * Clears the renderer to the specified color
  50. *
  51. * @param {@link Color}
  52. */
  53. public void clear(Color color)
  54. {
  55. Arrays.fill(this.pixels, color.getColor());
  56. }
  57.  
  58. /**
  59. * Fills a rectangle
  60. *
  61. * @param x
  62. * (in pixels)
  63. * @param y
  64. * (in pixels)
  65. * @param width
  66. * (in pixels)
  67. * @param height
  68. * (in pixels)
  69. * @param {@link Color}
  70. */
  71. public void fillRect(int x, int y, int width, int height, Color color)
  72. {
  73. for(int i = 0; i < width; i++)
  74. {
  75. for(int j = 0; j < height; j++)
  76. {
  77. this.setPixel(x + i, y + j, color);
  78. }
  79. }
  80. }
  81.  
  82. /**
  83. * Draws a string at the specified location using the {@link Renderer}'s
  84. * {@link Font}
  85. *
  86. * @param x
  87. * (in pixels)
  88. * @param y
  89. * (in pixels)
  90. * @param str
  91. */
  92. public void drawString(int x, int y, String str)
  93. {
  94. switch (this.font.getCharCase())
  95. {
  96. case UPPER:
  97. str = str.toUpperCase();
  98. break;
  99. case BOTH:
  100. break;
  101. case LOWER:
  102. str = str.toLowerCase();
  103. break;
  104. }
  105.  
  106. for(int i = 0; i < str.length(); i++)
  107. {
  108. char c = str.charAt(i);
  109. if(c < 0)
  110. continue;
  111. int xx = x + this.font.getCharacterWidth() * i;
  112. Glyph glyph = this.font.getGlyph(c);
  113. if(glyph != null)
  114. glyph.render(this, xx, y);
  115. }
  116. }
  117.  
  118. /**
  119. * Renders the renderable object at the specified coordinates
  120. *
  121. * @param x
  122. * (in pixels)
  123. * @param y
  124. * (in pixels)
  125. * @param {@link Renderable}
  126. */
  127. public void render(int x, int y, Renderable renderable)
  128. {
  129. renderable.render(this, x, y);
  130. }
  131.  
  132. /**
  133. * Renders the renderable object at the specified coordinates rotated around
  134. * the specified point at the specified angle
  135. *
  136. * @param x
  137. * (in pixels)
  138. * @param y
  139. * (in pixels)
  140. * @param rotX
  141. * (in pixels)
  142. * @param rotY
  143. * (in pixels)
  144. * @param {@link Renderable}
  145. * @param angle
  146. * (in degrees)
  147. */
  148. public void renderRotated(int x, int y, int rotX, int rotY, Renderable renderable, int angle)
  149. {
  150. renderable.renderRotated(this, x, y, rotX, rotY, angle);
  151. }
  152.  
  153. /**
  154. * Sets the pixels at the specified location
  155. *
  156. * @param x
  157. * @param y
  158. * @param width
  159. * @param height
  160. * @param pixels
  161. */
  162. public void setPixels(int x, int y, int width, int height, int[] pixels)
  163. {
  164. for(int i = 0; i < width; i++)
  165. {
  166. for(int j = 0; j < height; j++)
  167. {
  168. int xx = x + i;
  169. int yy = y + j;
  170. this.setPixel(xx, yy, new Color(pixels[i + j * width]));
  171. }
  172. }
  173. }
  174.  
  175. /**
  176. * Sets a pixel to the specified color
  177. *
  178. * @param the
  179. * x ordinate of the pixel
  180. * @param the
  181. * y ordinate of the pixel
  182. * @param the
  183. * {@link Color} to set the pixel to
  184. */
  185. public void setPixel(int x, int y, Color color)
  186. {
  187. if(x < 0 || x >= this.width)
  188. return;
  189. if(y < 0 || y >= this.height)
  190. return;
  191.  
  192. int n = color.getColor();
  193. int nAlpha = (n & 0xff000000) >> 24;
  194. int o = this.pixels[x + y * this.width];
  195.  
  196. if(nAlpha < 255)
  197. n = n + o * (1 - nAlpha);
  198.  
  199. this.pixels[x + y * this.width] = n;
  200. }
  201.  
  202. /**
  203. * Copies the renderer pixels to the renderer image pixels
  204. */
  205. public void copy()
  206. {
  207. System.arraycopy(this.pixels, 0, this.imagePixels, 0, this.pixels.length);
  208. }
  209.  
  210. /**
  211. * Gets the width of the renderer
  212. *
  213. * @return the width (in pixels)
  214. */
  215. public int getWidth()
  216. {
  217. return this.width;
  218. }
  219.  
  220. /**
  221. * Gets the width of the renderer
  222. *
  223. * @return the height (in pixels)
  224. */
  225. public int getHeight()
  226. {
  227. return this.height;
  228. }
  229.  
  230. /**
  231. * Gets the rendered image
  232. *
  233. * @return {@link BufferedImage}
  234. */
  235. public BufferedImage getImage()
  236. {
  237. return this.image;
  238. }
  239.  
  240. /**
  241. * Sets this {@link Renderer}'s {@link Font}
  242. *
  243. * @param {@link Font}
  244. */
  245. public void setFont(Font font)
  246. {
  247. this.font = font;
  248. }
  249.  
  250. /**
  251. * Gets this {@link Renderer}'s {@link Font}
  252. *
  253. * @return {@link Font}
  254. */
  255. public Font getFont()
  256. {
  257. return this.font;
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement