PageRenderTime 35ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/graphics/view.d

http://github.com/wilkie/djehuty
D | 177 lines | 100 code | 54 blank | 23 comment | 5 complexity | 99c947758e57c3c8a6157f84b15daba2 MD5 | raw file
  1module graphics.view;
  2
  3import graphics.graphics;
  4
  5import synch.semaphore;
  6
  7import gui.window;
  8
  9import graphics.brush;
 10import graphics.pen;
 11import graphics.font;
 12import graphics.region;
 13
 14import platform.vars.view;
 15
 16import scaffold.view;
 17
 18// Section: Core
 19
 20// Description: This class implements and abstracts a view, which is a drawing canvas.  With this class, one can create off-screen buffers.
 21class View {
 22	// Description: This will instantiate an uninitialized view.  It will need to be created with the create() function in order to fully use.
 23	this() {
 24		_mutex = new Semaphore;
 25
 26		_inited = false;
 27		_mutex.init(1);
 28
 29		_graphics = new Graphics();
 30
 31		_graphics._view = this;
 32		_graphics._viewVars = &_pfvars;
 33	}
 34
 35	~this() {
 36		_destroy();
 37	}
 38
 39	// Methods //
 40
 41
 42	// Description: Creates a new drawing canvas offscreen to the dimensions given.
 43	// width: The width of the new drawing context.
 44	// height: The height of the new drawing context.
 45	void create(int width, int height) {
 46		if (_inited) { destroy(); }
 47
 48		_mutex.down();
 49
 50		_width = width;
 51		_height = height;
 52
 53		_platformCreate();
 54
 55		_inited = true;
 56
 57		_mutex.up();
 58	}
 59
 60	// Description: Resizes a previously created drawing context.  All content of the view WILL be lost.
 61	// width: The new width of the new drawing context.
 62	// height: The new height of the new drawing context.
 63	void resize(int width, int height) {
 64		_mutex.down();
 65
 66		_width = width;
 67		_height = height;
 68
 69		if (_inited) {
 70			ViewResize(this, &_pfvars);
 71		}
 72
 73		_mutex.up();
 74	}
 75
 76	// Description: Destroys and deallocates the drawing canvas created through the create() function.
 77	void destroy() {
 78		_mutex.down();
 79
 80		if (!_inited) {
 81			_mutex.up();
 82			return;
 83		}
 84
 85		_destroy();
 86
 87		_mutex.up();
 88	}
 89
 90	// Description: Will return the width of the drawing canvas.
 91	// Returns: The width of the canvas.
 92	int width() {
 93		return _width;
 94	}
 95
 96	// Description: Will return the height of the drawing canvas.
 97	// Returns: The height of the canvas.
 98	int height() {
 99		return _height;
100	}
101
102	// Thread Interaction
103
104	// Description: Will lock the canvas for drawing.
105	// Returns: A Graphics object that will draw to the current view.
106	Graphics lock() {
107		_mutex.down();
108
109		_locked = true;
110		return _graphics;
111	}
112
113	// Description: Will unlock a locked canvas.
114	void unlock() {
115		_locked = false;
116
117		if (_brush !is null) {
118			// Unattach the Brush
119			_brush._view = null;
120		}
121		_brush = null;
122
123		if (_pen !is null) {
124			// Unattach the Pen
125			_pen._view = null;
126		}
127		_pen = null;
128		_mutex.up();
129	}
130
131	uint rgbaTouint(uint r, uint g, uint b, uint a) {
132		return ViewRGBAToInt32(_forcenopremultiply,&_pfvars,r,g,b,a);
133	}
134
135	uint rgbTouint(uint r, uint g, uint b) {
136		return ViewRGBAToInt32(&_pfvars,r,g,b);
137	}
138
139protected:
140
141	package ViewPlatformVars _pfvars;
142
143	bool _inited = false;
144
145	package bool _locked = false;
146
147	int _width = 0;
148	int _height = 0;
149
150	bool _hasAlpha = false;
151
152	bool _forcenopremultiply = false;
153
154	package Graphics _graphics = null;
155
156	Semaphore _mutex;
157
158	void _destroy() {
159		ViewDestroy(this, &_pfvars);
160
161		_inited = false;
162
163		_width = 0;
164		_height = 0;
165	}
166
167	void _platformCreate() {
168		ViewCreate(this, &_pfvars);
169	}
170
171	// Retained Objects
172
173	// (null : no object)
174
175	package Brush _brush;
176	package Pen _pen;
177}