Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OOPolygonSprite(Private) Category Reference
+ Inheritance diagram for OOPolygonSprite(Private):
+ Collaboration diagram for OOPolygonSprite(Private):

Instance Methods

(BOOL) - loadPolygons:outlineWidth:
 
- Instance Methods inherited from <OOGraphicsResetClient>
(void) - resetGraphicsState
 

Detailed Description

Definition at line 58 of file OOPolygonSprite.m.

Method Documentation

◆ loadPolygons:outlineWidth:

- (BOOL) loadPolygons: (NSArray *) dataArray
outlineWidth: (float) outlineWidth 

Extends class OOPolygonSprite.

Definition at line 78 of file OOPolygonSprite.m.

257 :(NSArray *)dataArray outlineWidth:(float)outlineWidth
258{
259 NSParameterAssert(dataArray != nil);
260
261 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
262 GLUtesselator *tesselator = NULL;
263
264 TessPolygonData polygonData;
265 memset(&polygonData, 0, sizeof polygonData);
266 polygonData.OK = YES;
267#ifndef NDEBUG
268 polygonData.name = _name;
269 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"polygon-sprite-dump-svg"]) SVGDumpBegin(&polygonData);
270#endif
271
272 // For efficiency, grow to more than big enough for most cases to avoid regrowing.
273 if (!GrowTessPolygonData(&polygonData, 100))
274 {
275 polygonData.OK = NO;
276 goto END;
277 }
278
279 tesselator = gluNewTess();
280 if (tesselator == NULL)
281 {
282 polygonData.OK = NO;
283 goto END;
284 }
285
286 dataArray = DataArrayToPoints(&polygonData, dataArray);
287
288 /*** Tesselate polygon fill ***/
289 gluTessCallback(tesselator, GLU_TESS_BEGIN_DATA, (TessFuncPtr)TessBeginCallback);
290 gluTessCallback(tesselator, GLU_TESS_VERTEX_DATA, (TessFuncPtr)TessVertexCallback);
291 gluTessCallback(tesselator, GLU_TESS_END_DATA, (TessFuncPtr)TessEndCallback);
292 gluTessCallback(tesselator, GLU_TESS_ERROR_DATA, (TessFuncPtr)ErrorCallback);
293 gluTessCallback(tesselator, GLU_TESS_COMBINE_DATA, (TessFuncPtr)TessCombineCallback);
294
295 gluTessBeginPolygon(tesselator, &polygonData);
296 SVGDumpBeginGroup(&polygonData, @"Fill");
297
298 NSUInteger contourCount = [dataArray count], contourIndex;
299 for (contourIndex = 0; contourIndex < contourCount && polygonData.OK; contourIndex++)
300 {
301 NSArray *contour = [dataArray oo_arrayAtIndex:contourIndex];
302 if (contour == nil)
303 {
304 polygonData.OK = NO;
305 break;
306 }
307
308 SubmitVertices(tesselator, &polygonData, contour);
309 }
310
311 gluTessEndPolygon(tesselator);
312 SVGDumpEndGroup(&polygonData);
313
314 if (polygonData.OK)
315 {
316 _solidCount = polygonData.count;
317
318 if (_solidCount != 0)
319 {
320 _solidData = realloc(polygonData.data, polygonData.count * sizeof (GLfloat) * 2);
321 if (_solidData != NULL) polygonData.data = NULL; // realloc succeded.
322 else
323 {
324 // Unlikely, but legal: realloc failed to shrink buffer.
325 _solidData = polygonData.data;
326 if (_solidData == NULL) polygonData.OK = NO;
327 }
328 }
329 else
330 {
331 // Empty polygon.
332 _solidData = NULL;
333 }
334 }
335 if (!polygonData.OK) goto END;
336
337 /*** Tesselate polygon outline ***/
338 gluDeleteTess(tesselator);
339 tesselator = gluNewTess();
340 if (tesselator == NULL)
341 {
342 polygonData.OK = NO;
343 goto END;
344 }
345
346 polygonData.count = 0;
347 polygonData.capacity = 0;
348 if (!GrowTessPolygonData(&polygonData, 100))
349 {
350 polygonData.OK = NO;
351 goto END;
352 }
353#ifndef NDEBUG
354 polygonData.generatingOutline = YES;
355#endif
356
357 gluTessCallback(tesselator, GLU_TESS_BEGIN_DATA, (TessFuncPtr)TessBeginCallback);
358 gluTessCallback(tesselator, GLU_TESS_VERTEX_DATA, (TessFuncPtr)TessVertexCallback);
359 gluTessCallback(tesselator, GLU_TESS_END_DATA, (TessFuncPtr)TessEndCallback);
360 gluTessCallback(tesselator, GLU_TESS_ERROR_DATA, (TessFuncPtr)ErrorCallback);
361 gluTessCallback(tesselator, GLU_TESS_COMBINE_DATA, (TessFuncPtr)TessCombineCallback);
362 gluTessProperty(tesselator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);
363
364 gluTessBeginPolygon(tesselator, &polygonData);
365 SVGDumpBeginGroup(&polygonData, @"Outline");
366
367 outlineWidth *= 0.5f; // Half the width in, half the width out.
368 contourCount = [dataArray count];
369 for (contourIndex = 0; contourIndex < contourCount && polygonData.OK; contourIndex++)
370 {
371 NSArray *contour = [dataArray oo_arrayAtIndex:contourIndex];
372 if (contour == nil)
373 {
374 polygonData.OK = NO;
375 break;
376 }
377
378 SubmitVertices(tesselator, &polygonData, BuildOutlineContour(contour, outlineWidth, NO));
379 SubmitVertices(tesselator, &polygonData, BuildOutlineContour(contour, outlineWidth, YES));
380 }
381
382 gluTessEndPolygon(tesselator);
383 SVGDumpEndGroup(&polygonData);
384
385 if (polygonData.OK)
386 {
387 if (polygonData.count != 0)
388 {
389 _outlineCount = polygonData.count;
390 _outlineData = realloc(polygonData.data, polygonData.count * sizeof (GLfloat) * 2);
391 if (_outlineData != NULL) polygonData.data = NULL; // realloc succeded.
392 else
393 {
394 // Unlikely, but legal: realloc failed to shrink buffer.
395 _outlineData = polygonData.data;
396 if (_outlineData == NULL) polygonData.OK = NO;
397 }
398 }
399 else
400 {
401 // Empty polygon.
402 _outlineCount = 0;
403 _outlineData = NULL;
404 }
405 }
406
407END:
408 SVGDumpEnd(&polygonData);
409 free(polygonData.data);
410 gluDeleteTess(tesselator);
411 [pool release];
412#ifndef NDEBUG
413 DESTROY(polygonData.debugSVG);
414#endif
415 return polygonData.OK;
416}
#define DESTROY(x)
Definition OOCocoa.h:77
static void APIENTRY TessVertexCallback(void *vertexData, void *polygonData)
static void APIENTRY TessEndCallback(void *polygonData)
static void APIENTRY TessBeginCallback(GLenum type, void *polygonData)
static void SVGDumpBeginGroup(TessPolygonData *data, NSString *name)
static NSArray * DataArrayToPoints(TessPolygonData *data, NSArray *dataArray)
GLvoid(* TessFuncPtr)()
static void SubmitVertices(GLUtesselator *tesselator, TessPolygonData *polygonData, NSArray *contour)
static void APIENTRY ErrorCallback(GLenum error, void *polygonData)
static void APIENTRY TessCombineCallback(GLdouble coords[3], void *vertexData[4], GLfloat weight[4], void **outData, void *polygonData)
static NSArray * BuildOutlineContour(NSArray *dataArray, GLfloat width, BOOL inner)
static void SVGDumpBegin(TessPolygonData *data)
static void SVGDumpEndGroup(TessPolygonData *data)
static void SVGDumpEnd(TessPolygonData *data)
static BOOL GrowTessPolygonData(TessPolygonData *data, size_t capacityHint)
return nil
NSMutableString * debugSVG

The documentation for this category was generated from the following file: