/graphics/view.d

http://github.com/wilkie/djehuty · D · 177 lines · 100 code · 54 blank · 23 comment · 5 complexity · 99c947758e57c3c8a6157f84b15daba2 MD5 · raw file

  1. module graphics.view;
  2. import graphics.graphics;
  3. import synch.semaphore;
  4. import gui.window;
  5. import graphics.brush;
  6. import graphics.pen;
  7. import graphics.font;
  8. import graphics.region;
  9. import platform.vars.view;
  10. import scaffold.view;
  11. // Section: Core
  12. // Description: This class implements and abstracts a view, which is a drawing canvas. With this class, one can create off-screen buffers.
  13. class View {
  14. // Description: This will instantiate an uninitialized view. It will need to be created with the create() function in order to fully use.
  15. this() {
  16. _mutex = new Semaphore;
  17. _inited = false;
  18. _mutex.init(1);
  19. _graphics = new Graphics();
  20. _graphics._view = this;
  21. _graphics._viewVars = &_pfvars;
  22. }
  23. ~this() {
  24. _destroy();
  25. }
  26. // Methods //
  27. // Description: Creates a new drawing canvas offscreen to the dimensions given.
  28. // width: The width of the new drawing context.
  29. // height: The height of the new drawing context.
  30. void create(int width, int height) {
  31. if (_inited) { destroy(); }
  32. _mutex.down();
  33. _width = width;
  34. _height = height;
  35. _platformCreate();
  36. _inited = true;
  37. _mutex.up();
  38. }
  39. // Description: Resizes a previously created drawing context. All content of the view WILL be lost.
  40. // width: The new width of the new drawing context.
  41. // height: The new height of the new drawing context.
  42. void resize(int width, int height) {
  43. _mutex.down();
  44. _width = width;
  45. _height = height;
  46. if (_inited) {
  47. ViewResize(this, &_pfvars);
  48. }
  49. _mutex.up();
  50. }
  51. // Description: Destroys and deallocates the drawing canvas created through the create() function.
  52. void destroy() {
  53. _mutex.down();
  54. if (!_inited) {
  55. _mutex.up();
  56. return;
  57. }
  58. _destroy();
  59. _mutex.up();
  60. }
  61. // Description: Will return the width of the drawing canvas.
  62. // Returns: The width of the canvas.
  63. int width() {
  64. return _width;
  65. }
  66. // Description: Will return the height of the drawing canvas.
  67. // Returns: The height of the canvas.
  68. int height() {
  69. return _height;
  70. }
  71. // Thread Interaction
  72. // Description: Will lock the canvas for drawing.
  73. // Returns: A Graphics object that will draw to the current view.
  74. Graphics lock() {
  75. _mutex.down();
  76. _locked = true;
  77. return _graphics;
  78. }
  79. // Description: Will unlock a locked canvas.
  80. void unlock() {
  81. _locked = false;
  82. if (_brush !is null) {
  83. // Unattach the Brush
  84. _brush._view = null;
  85. }
  86. _brush = null;
  87. if (_pen !is null) {
  88. // Unattach the Pen
  89. _pen._view = null;
  90. }
  91. _pen = null;
  92. _mutex.up();
  93. }
  94. uint rgbaTouint(uint r, uint g, uint b, uint a) {
  95. return ViewRGBAToInt32(_forcenopremultiply,&_pfvars,r,g,b,a);
  96. }
  97. uint rgbTouint(uint r, uint g, uint b) {
  98. return ViewRGBAToInt32(&_pfvars,r,g,b);
  99. }
  100. protected:
  101. package ViewPlatformVars _pfvars;
  102. bool _inited = false;
  103. package bool _locked = false;
  104. int _width = 0;
  105. int _height = 0;
  106. bool _hasAlpha = false;
  107. bool _forcenopremultiply = false;
  108. package Graphics _graphics = null;
  109. Semaphore _mutex;
  110. void _destroy() {
  111. ViewDestroy(this, &_pfvars);
  112. _inited = false;
  113. _width = 0;
  114. _height = 0;
  115. }
  116. void _platformCreate() {
  117. ViewCreate(this, &_pfvars);
  118. }
  119. // Retained Objects
  120. // (null : no object)
  121. package Brush _brush;
  122. package Pen _pen;
  123. }