Partial buffer slicing.
[SquirrelJME.git] / nanocoat / lib / scritchui / scritchPencilRasterShapes.c
blob88db03656a5230d0fe3dafb8a8539c957cad54cc
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "sjme/util.h"
13 #include "lib/scritchui/scritchui.h"
14 #include "lib/scritchui/scritchuiPencil.h"
15 #include "lib/scritchui/scritchuiTypes.h"
16 #include "lib/scritchui/core/coreRaster.h"
17 #include "sjme/debug.h"
18 #include "sjme/fixed.h"
20 sjme_errorCode sjme_scritchpen_core_drawRect(
21 sjme_attrInNotNull sjme_scritchui_pencil g,
22 sjme_attrInValue sjme_jint x,
23 sjme_attrInValue sjme_jint y,
24 sjme_attrInPositive sjme_jint w,
25 sjme_attrInPositive sjme_jint h)
27 sjme_errorCode error;
28 sjme_jint xw, yh;
30 if (g == NULL)
31 return SJME_ERROR_NULL_ARGUMENTS;
33 /* Nothing to draw? */
34 if (w <= 0 || h <= 0)
35 return SJME_ERROR_NONE;
37 /* Lock. */
38 if (sjme_error_is(error = sjme_scritchpen_core_lock(g)))
39 return sjme_error_default(error);
41 /* Transform. */
42 sjme_scritchpen_coreUtil_applyTranslate(g, &x, &y);
44 /* Pre-calculate coordinates. */
45 xw = x + w;
46 yh = y + h;
48 /* Clear error state. */
49 error = SJME_ERROR_NONE;
51 /* Draw horizontal spans first. */
52 error |= g->prim.drawHoriz(g, x, y, w);
53 error |= g->prim.drawHoriz(g, x, yh, w);
55 /* Draw vertical spans. */
56 error |= g->prim.drawLine(g, x, y, x, yh);
57 error |= g->prim.drawLine(g, xw, y, xw, yh);
59 /* Failed? */
60 if (sjme_error_is(error))
61 goto fail_any;
63 /* Release lock. */
64 if (sjme_error_is(error = sjme_scritchpen_core_lockRelease(g)))
65 return sjme_error_default(error);
67 /* Success! */
68 return SJME_ERROR_NONE;
70 fail_any:
71 /* Need to release the lock? */
72 if (sjme_error_is(sjme_scritchpen_core_lockRelease(g)))
73 return sjme_error_default(error);
75 return sjme_error_default(error);
78 sjme_errorCode sjme_scritchpen_core_drawTriangle(
79 sjme_attrInNotNull sjme_scritchui_pencil g,
80 sjme_attrInValue sjme_jint x1,
81 sjme_attrInValue sjme_jint y1,
82 sjme_attrInValue sjme_jint x2,
83 sjme_attrInValue sjme_jint y2,
84 sjme_attrInValue sjme_jint x3,
85 sjme_attrInValue sjme_jint y3)
87 sjme_errorCode error;
89 if (g == NULL)
90 return SJME_ERROR_NULL_ARGUMENTS;
92 /* Lock. */
93 if (sjme_error_is(error = sjme_scritchpen_core_lock(g)))
94 return sjme_error_default(error);
96 /* Transform. */
97 sjme_scritchpen_coreUtil_applyTranslate(g, &x1, &y1);
98 sjme_scritchpen_coreUtil_applyTranslate(g, &x2, &y2);
99 sjme_scritchpen_coreUtil_applyTranslate(g, &x3, &y3);
101 /* Clear error state. */
102 error = SJME_ERROR_NONE;
104 /* Draw lines via primitives. */
105 error |= g->prim.drawLine(g, x1, y1, x2, y2);
106 error |= g->prim.drawLine(g, x2, y2, x3, y3);
107 error |= g->prim.drawLine(g, x3, y3, x1, y1);
109 /* Failed? */
110 if (sjme_error_is(error))
111 goto fail_any;
113 /* Release lock. */
114 if (sjme_error_is(error = sjme_scritchpen_core_lockRelease(g)))
115 return sjme_error_default(error);
117 /* Success! */
118 return SJME_ERROR_NONE;
120 fail_any:
121 /* Need to release the lock? */
122 if (sjme_error_is(sjme_scritchpen_core_lockRelease(g)))
123 return sjme_error_default(error);
125 return sjme_error_default(error);
128 sjme_errorCode sjme_scritchpen_core_fillRect(
129 sjme_attrInNotNull sjme_scritchui_pencil g,
130 sjme_attrInValue sjme_jint x,
131 sjme_attrInValue sjme_jint y,
132 sjme_attrInPositive sjme_jint w,
133 sjme_attrInPositive sjme_jint h)
135 sjme_errorCode error;
136 sjme_scritchui_pencilDrawHorizFunc drawHoriz;
137 sjme_jint yz, yze;
139 if (g == NULL)
140 return SJME_ERROR_NULL_ARGUMENTS;
142 /* Lock. */
143 if (sjme_error_is(error = sjme_scritchpen_core_lock(g)))
144 return sjme_error_default(error);
146 /* Transform. */
147 sjme_scritchpen_coreUtil_applyTranslate(g, &x, &y);
149 /* Cap width and height to 1 always. */
150 if (w <= 0)
151 w = 1;
152 if (h <= 0)
153 h = 1;
155 /* Use primitives otherwise. */
156 error = SJME_ERROR_NONE;
157 drawHoriz = g->prim.drawHoriz;
158 for (yz = y, yze = y + h; yz < yze; yz++)
159 if (sjme_error_is(error = drawHoriz(g, x, yz, w)))
160 break;
162 /* Failed? */
163 if (sjme_error_is(error))
164 goto fail_any;
166 /* Release lock. */
167 if (sjme_error_is(error = sjme_scritchpen_core_lockRelease(g)))
168 return sjme_error_default(error);
170 /* Success? */
171 return error;
173 fail_any:
174 /* Need to release the lock? */
175 if (sjme_error_is(sjme_scritchpen_core_lockRelease(g)))
176 return sjme_error_default(error);
178 return sjme_error_default(error);
181 sjme_errorCode sjme_scritchpen_core_fillTriangle(
182 sjme_attrInNotNull sjme_scritchui_pencil g,
183 sjme_attrInValue sjme_jint x1,
184 sjme_attrInValue sjme_jint y1,
185 sjme_attrInValue sjme_jint x2,
186 sjme_attrInValue sjme_jint y2,
187 sjme_attrInValue sjme_jint x3,
188 sjme_attrInValue sjme_jint y3)
190 sjme_errorCode error;
192 if (g == NULL)
193 return SJME_ERROR_NULL_ARGUMENTS;
195 /* Lock. */
196 if (sjme_error_is(error = sjme_scritchpen_core_lock(g)))
197 return sjme_error_default(error);
199 sjme_todo("Impl?");
200 return sjme_error_notImplemented(0);
202 /* Release lock. */
203 if (sjme_error_is(error = sjme_scritchpen_core_lockRelease(g)))
204 return sjme_error_default(error);
206 return sjme_error_notImplemented(0);
208 fail_any:
209 /* Need to release the lock? */
210 if (sjme_error_is(sjme_scritchpen_core_lockRelease(g)))
211 return sjme_error_default(error);
213 return sjme_error_default(error);