-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.c
executable file
·410 lines (378 loc) · 13.5 KB
/
context.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "grlib.h"
//*****************************************************************************
//
//! \addtogroup context_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
//! Initializes a drawing context.
//!
//! \param context is a pointer to the drawing context to initialize.
//! \param display is a pointer to the Graphics_Display Info structure that
//! describes the display driver to use.
//!
//! This function initializes a drawing context, preparing it for use. The
//! provided display driver will be used for all subsequent graphics
//! operations, and the default clipping region will be set to the extent of
//! the screen.
//!
//! \return None.
//
//*****************************************************************************
void Graphics_initContext(Graphics_Context *context,
const Graphics_Display *display)
{
//
// Check the arguments.
//
assert(context);
assert(display);
//
// Set the size of the context.
//
context->size = sizeof(Graphics_Context);
//
// Save the pointer to the display.
//
context->display = display;
//
// Initialize the extent of the clipping region to the extents of the
// screen.
//
context->clipRegion.xMin = 0;
context->clipRegion.yMin = 0;
context->clipRegion.xMax = display->width - 1;
context->clipRegion.yMax = display->heigth - 1;
//
// Provide a default color and font.
//
context->foreground = 0;
context->background = 0;
context->font = 0;
}
//*****************************************************************************
//
//! Sets the extents of the clipping region.
//!
//! \param context is a pointer to the drawing context to use.
//! \param rect is a pointer to the structure containing the extents of the
//! clipping region.
//!
//! This function sets the extents of the clipping region. The clipping region
//! is not allowed to exceed the extents of the screen, but may be a portion of
//! the screen.
//!
//! The supplied coordinate are inclusive; \e xMin of 1 and \e xMax of 1 will
//! define a clipping region that will display only the pixels in the X = 1
//! column. A consequence of this is that the clipping region must contain
//! at least one row and one column.
//!
//! \return None.
//
//*****************************************************************************
void Graphics_setClipRegion(Graphics_Context *context, Graphics_Rectangle *rect)
{
uint32_t w, h;
//
// Check the arguments.
//
assert(context);
assert(rect);
//
// Get the width and height of the display.
//
w = Graphics_getWidthOfDisplay(context->display);
h = Graphics_getHeightOfDisplay(context->display);
//
// Set the extents of the clipping region, forcing them to reside within
// the extents of the screen.
//
context->clipRegion.xMin = ((rect->xMin < 0) ? 0 :
((rect->xMin >= w) ? (w - 1) :
rect->xMin));
context->clipRegion.yMin = ((rect->yMin < 0) ? 0 :
((rect->yMin >= h) ? (h - 1) :
rect->yMin));
context->clipRegion.xMax = ((rect->xMax < 0) ? 0 :
((rect->xMax >= w) ? (w - 1) :
rect->xMax));
context->clipRegion.yMax = ((rect->yMax < 0) ? 0 :
((rect->yMax >= h) ? (h - 1) :
rect->yMax));
}
//*****************************************************************************
//
//! Sets the background color to be used.
//!
//! \param context is a pointer to the drawing context to modify.
//! \param value is the 24-bit RGB color to be used.
//!
//! This function sets the background color to be used for drawing operations
//! in the specified drawing context.
//!
//! \return None.
//
//*****************************************************************************
void Graphics_setBackgroundColor(Graphics_Context *context, int32_t value)
{
Graphics_Context *pC = context;
pC->background = Graphics_translateColorOnDisplay(pC->display, value);
}
//*****************************************************************************
//
//! Sets the background color to be used.
//!
//! \param context is a pointer to the drawing context to modify.
//! \param value is the display driver-specific color to be used.
//!
//! This function sets the background color to be used for drawing operations
//! in the specified drawing context, using a color that has been previously
//! translated to a driver-specific color (for example, via
//! Graphics_translateColorDisplay()).
//!
//! \return None.
//
//*****************************************************************************
void Graphics_setBackgroundColorTranslated(Graphics_Context *context,
int32_t value)
{
Graphics_Context *pC = context;
pC->background = value;
}
//*****************************************************************************
//
//! Sets the font to be used.
//!
//! \param context is a pointer to the drawing context to modify.
//! \param font is a pointer to the font to be used.
//!
//! This function sets the font to be used for string drawing operations in the
//! specified drawing context. If a tFontEx type font is to be used, cast its
//! pointer to a font pointer before passing it as the font parameter.
//!
//! \return None.
//
//*****************************************************************************
void Graphics_setFont(Graphics_Context *context, const Graphics_Font *font)
{
Graphics_Context *pC = context;
const Graphics_Font *pF = font;
pC->font = pF;
}
//*****************************************************************************
//
//! Gets the baseline of a font.
//!
//! \param font is a pointer to the font to query.
//!
//! This function determines the baseline position of a font. The baseline is
//! the offset between the top of the font and the bottom of the capital
//! letters. The only font data that exists below the baseline are the
//! descenders on some lower-case letters (such as ``y'').
//!
//! \return Returns the baseline of the font, in pixels.
//
//*****************************************************************************
uint8_t Graphics_getFontBaseline(const Graphics_Font *font)
{
return font->baseline;
}
//*****************************************************************************
//
//! Sets the foreground color to be used.
//!
//! \param context is a pointer to the drawing context to modify.
//! \param value is the 24-bit RGB color to be used.
//!
//! This function sets the color to be used for drawing operations in the
//! specified drawing context.
//!
//! \return None.
//
//*****************************************************************************
void Graphics_setForegroundColor(Graphics_Context *context, int32_t value)
{
context->foreground = Graphics_translateColorOnDisplay(context->display,value);
}
//*****************************************************************************
//
//! Sets the foreground color to be used.
//!
//! \param context is a pointer to the drawing context to modify.
//! \param value is the display driver-specific color to be used.
//!
//! This function sets the foreground color to be used for drawing operations
//! in the specified drawing context, using a color that has been previously
//! translated to a driver-specific color (for example, via
//! Graphics_translateColorDisplay()).
//!
//! \return None.
//
//*****************************************************************************
void Graphics_setForegroundColorTranslated(Graphics_Context *context,
int32_t value)
{
context->foreground = value;
}
//*****************************************************************************
//
//! Gets the height of a font.
//!
//! \param font is a pointer to the font to query.
//!
//! This function determines the height of a font. The height is the offset
//! between the top of the font and the bottom of the font, including any
//! ascenders and descenders.
//!
//! \return Returns the height of the font, in pixels.
//
//*****************************************************************************
uint8_t Graphics_getFontHeight(const Graphics_Font *font)
{
return font->height;
}
//*****************************************************************************
//
//! Gets the maximum width of a font.
//!
//! \param font is a pointer to the font to query.
//!
//! This function determines the maximum width of a font. The maximum width is
//! the width of the widest individual character in the font.
//!
//! \return Returns the maximum width of the font, in pixels.
//
//*****************************************************************************
uint8_t Graphics_getFontMaxWidth(const Graphics_Font *font)
{
return font->maxWidth;
}
//*****************************************************************************
//
//! Gets the width of the display being used by this drawing context.
//!
//! \param context is a pointer to the drawing context to query.
//!
//! This function returns the width of the display that is being used by this
//! drawing context.
//!
//! \return Returns the width of the display in pixels.
//
//*****************************************************************************
uint16_t Graphics_getDisplayWidth(Graphics_Context *context)
{
return context->display->width;
}
//*****************************************************************************
//
//! Gets the height of the display being used by this drawing context.
//!
//! \param context is a pointer to the drawing context to query.
//!
//! This function returns the height of the display that is being used by this
//! drawing context.
//!
//! \return Returns the height of the display in pixels.
//
//*****************************************************************************
uint16_t Graphics_getDisplayHeight(Graphics_Context *context)
{
return context->display->heigth;
}
//*****************************************************************************
//
//! Gets the height of the display.
//!
//! \param display is a pointer to the display driver structure for the
//! display to query.
//!
//! This function determines the height of the display.
//!
//! \return Returns the height of the display in pixels.
//
//*****************************************************************************
uint16_t Graphics_getHeightOfDisplay(const Graphics_Display *display)
{
return display->heigth;
}
//*****************************************************************************
//
//! Flushes any cached drawing operations.
//!
//! \param context is a pointer to the drawing context to use.
//!
//! This function flushes any cached drawing operations. For display drivers
//! that draw into a local frame buffer before writing to the actual display,
//! calling this function will cause the display to be updated to match the
//! contents of the local frame buffer.
//!
//! \return None.
//
//*****************************************************************************
void Graphics_flushBuffer(const Graphics_Context *context)
{
Graphics_flushOnDisplay(context->display);
}
//*****************************************************************************
//
//! Forces a clear screen. Contents of Display buffer unmodified
//!
//! \param context is a pointer to the drawing context to use.
//!
//! This function forces a clear screen.
//!
//! \return None.
//
//*****************************************************************************
void Graphics_clearDisplay(const Graphics_Context *context)
{
Graphics_clearDisplayOnDisplay(context->display,context->background );
}
//*****************************************************************************
//
//! Draws a pixel.
//!
//! \param context is a pointer to the drawing context to use.
//! \param x is the X coordinate of the pixel.
//! \param y is the Y coordinate of the pixel.
//!
//! This function draws a pixel if it resides within the clipping region.
//!
//! \return None.
//
//*****************************************************************************
void Graphics_drawPixel(const Graphics_Context *context, uint16_t x, uint16_t y)
{
if((x >= context->clipRegion.xMin) &&
(x <= context->clipRegion.xMax) &&
(y >= context->clipRegion.yMin) &&
(y <= context->clipRegion.yMax))
{
Graphics_drawPixelOnDisplay(context->display, x, y,
context->foreground);
}
}
//*****************************************************************************
//
//! Gets the width of the display.
//!
//! \param display is a pointer to the display driver structure for the
//! display to query.
//!
//! This function determines the width of the display.
//!
//! \return Returns the width of the display in pixels.
//
//*****************************************************************************
uint16_t Graphics_getWidthOfDisplay(const Graphics_Display *display)
{
return display->width;
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************