JavaRenderer.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package me.hammerle.supersnuvi.javafx;
  2. import javafx.animation.AnimationTimer;
  3. import javafx.application.Application;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.canvas.Canvas;
  7. import javafx.scene.canvas.GraphicsContext;
  8. import javafx.scene.paint.Color;
  9. import javafx.stage.Stage;
  10. import me.hammerle.text.SnuviTextPainter;
  11. public class JavaRenderer extends Application implements IJavaRenderer
  12. {
  13. // -------------------------------------------------------------------------
  14. // start up
  15. // -------------------------------------------------------------------------
  16. public static void main(String[] args)
  17. {
  18. Application.launch(args);
  19. }
  20. // -------------------------------------------------------------------------
  21. // javafx stuff
  22. // -------------------------------------------------------------------------
  23. private Group root;
  24. private Scene scene;
  25. private Canvas canvas;
  26. private GraphicsContext context;
  27. @Override
  28. public void init() throws Exception
  29. {
  30. root = new Group();
  31. }
  32. @Override
  33. public void start(Stage stage) throws Exception
  34. {
  35. // setup
  36. stage.setTitle("Super Snuvi");
  37. scene = new Scene(root);
  38. stage.setScene(scene);
  39. canvas = new Canvas(800, 600);
  40. root.getChildren().add(canvas);
  41. context = canvas.getGraphicsContext2D();
  42. textPainter = new SnuviTextPainter(canvas);
  43. // scene rescaling event
  44. scene.widthProperty().addListener((observable, oldValue, newValue) ->
  45. {
  46. if(oldValue.intValue() != 0)
  47. {
  48. canvas.setWidth(scene.getWidth());
  49. }
  50. });
  51. scene.heightProperty().addListener((observable, oldValue, newValue) ->
  52. {
  53. if(oldValue.intValue() != 0)
  54. {
  55. canvas.setHeight(scene.getHeight());
  56. }
  57. });
  58. // init key listener / register key events
  59. initKeyListener();
  60. // start game loop
  61. startLoop();
  62. // stage is shown after first game loop, the first frame can be seen
  63. stage.show();
  64. }
  65. @Override
  66. public void stop() throws Exception
  67. {
  68. if(timer != null)
  69. {
  70. timer.stop();
  71. }
  72. }
  73. // -------------------------------------------------------------------------
  74. // game tick
  75. // -------------------------------------------------------------------------
  76. private AnimationTimer timer = null;
  77. private void startLoop()
  78. {
  79. if(timer == null)
  80. {
  81. timer = new AnimationTimer()
  82. {
  83. private long last = System.nanoTime();
  84. private long lag = 0;
  85. @Override
  86. public void handle(long now)
  87. {
  88. lag += now - last;
  89. last = now;
  90. while(lag >= 12_500_000)
  91. {
  92. lag -= 12_500_000;
  93. }
  94. }
  95. };
  96. timer.start();
  97. }
  98. }
  99. // -------------------------------------------------------------------------
  100. // key input
  101. // -------------------------------------------------------------------------
  102. private void initKeyListener()
  103. {
  104. }
  105. // -------------------------------------------------------------------------
  106. // image rendering
  107. // -------------------------------------------------------------------------
  108. @Override
  109. public void drawImage(Image image, double x, double y)
  110. {
  111. context.drawImage(image.getImageSlot(), x, y);
  112. }
  113. @Override
  114. public void drawImage(Image image, double x, double y, double w, double h)
  115. {
  116. context.drawImage(image.getImageSlot(), x, y, w, h);
  117. }
  118. @Override
  119. public void drawImage(Image image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)
  120. {
  121. context.drawImage(image.getImageSlot(), sx, sy, sw, sh, dx, dy, dw, dh);
  122. }
  123. // -------------------------------------------------------------------------
  124. // primitive rendering
  125. // -------------------------------------------------------------------------
  126. @Override
  127. public double getWidth()
  128. {
  129. return canvas.getWidth();
  130. }
  131. @Override
  132. public double getHeight()
  133. {
  134. return canvas.getHeight();
  135. }
  136. @Override
  137. public void prepareRendering()
  138. {
  139. context.setFill(Color.BLACK);
  140. context.fillRect(0, 0, getWidth(), getHeight());
  141. }
  142. @Override
  143. public void save()
  144. {
  145. context.save();
  146. }
  147. @Override
  148. public void restore()
  149. {
  150. context.restore();
  151. }
  152. @Override
  153. public void setFillColor(int r, int g, int b, double alpha)
  154. {
  155. context.setFill(Color.rgb(r, g, b, alpha));
  156. }
  157. @Override
  158. public void setStrokeColor(int r, int g, int b, double alpha)
  159. {
  160. context.setStroke(Color.rgb(r, g, b, alpha));
  161. }
  162. @Override
  163. public void fillRectangle(double x, double y, double w, double h)
  164. {
  165. context.fillRect(x, y, w, h);
  166. }
  167. @Override
  168. public void setGlobalAlpha(double alpha)
  169. {
  170. context.setGlobalAlpha(alpha);
  171. }
  172. @Override
  173. public void setScale(double scale)
  174. {
  175. context.scale(scale, scale);
  176. }
  177. // -------------------------------------------------------------------------
  178. // text rendering
  179. // -------------------------------------------------------------------------
  180. public static final int CHARS_PER_LINE = 50;
  181. private SnuviTextPainter textPainter;
  182. @Override
  183. public void prepareTextDrawing(int r, int g, int b, double alpha, int width)
  184. {
  185. textPainter.setScale(1);
  186. double scale = getWidth() / textPainter.getWidth(width);
  187. if(scale >= 1)
  188. {
  189. scale = (int) scale;
  190. }
  191. textPainter.prepareStringDrawing(Color.rgb(r, g, b, alpha), scale);
  192. }
  193. @Override
  194. public void stopTextDrawing()
  195. {
  196. textPainter.stopStringDrawing();
  197. }
  198. @Override
  199. public void drawText(double x, double y, char[] text, int max)
  200. {
  201. textPainter.paintString(text, x, y, max);
  202. }
  203. @Override
  204. public double getTextWidth(int chars)
  205. {
  206. return textPainter.getWidth(chars);
  207. }
  208. @Override
  209. public double getTextHeight(int lines)
  210. {
  211. return textPainter.getHeight(lines);
  212. }
  213. }