ups, was missing the -f
[gl-cairo-simple.git] / cairo-rendering.c
blobfd6f0a574a4ddfb79ed60a3ea9c7c49fe21ec4ff
1 /*******************************************************************************
2 **3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
3 ** 10 20 30 40 50 60 70 80
4 **
5 ** file:
6 ** cairo-rendering.c
7 **
8 ** author:
9 ** Mirco "MacSlow" Mueller <macslow@bangang.de>
11 ** copyright (C) Mirco Mueller, July 2006, placed under the terms of the LGPL
13 *******************************************************************************/
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <math.h>
19 #include "geometry.h"
20 #include "cairo-rendering.h"
22 cairo_t*
23 create_cairo_context (int iWidth,
24 int iHeight,
25 int iChannels,
26 cairo_surface_t** pSurface,
27 unsigned char** pucBuffer)
29 cairo_t* pCairoContext = NULL;
31 /* create cairo-surface/context to act as OpenGL-texture source */
32 *pucBuffer = calloc (iChannels * iWidth * iHeight, sizeof (unsigned char));
33 if (!*pucBuffer)
35 printf ("create_cairo_context() - Couldn't allocate surface-buffer\n");
36 return NULL;
39 *pSurface = cairo_image_surface_create_for_data (*pucBuffer,
40 CAIRO_FORMAT_ARGB32,
41 iWidth,
42 iHeight,
43 iChannels * iWidth);
44 if (cairo_surface_status (*pSurface) != CAIRO_STATUS_SUCCESS)
46 free (*pucBuffer);
47 printf ("create_cairo_context() - Couldn't create surface\n");
48 return NULL;
51 pCairoContext = cairo_create (*pSurface);
52 if (cairo_status (pCairoContext) != CAIRO_STATUS_SUCCESS)
54 free (*pucBuffer);
55 printf ("create_cairo_context() - Couldn't create context\n");
56 return NULL;
59 return pCairoContext;
62 void
63 render_curve (cairo_t* pCairoContext,
64 int iWidth,
65 int iHeight,
66 Line* pLineOne,
67 Line* pLineTwo,
68 double fLineWidth)
70 if (pLineOne == NULL || pLineTwo == NULL)
71 return;
73 cairo_save (pCairoContext);
74 cairo_set_operator (pCairoContext, CAIRO_OPERATOR_OVER);
75 cairo_scale (pCairoContext, (double) iWidth / 1.0f, (double) iHeight / 1.0f);
76 cairo_set_source_rgba (pCairoContext, 0.0f, 1.0f, 0.0f, 1.0f);
77 cairo_paint (pCairoContext);
79 cairo_set_source_rgba (pCairoContext, 0.0f, 0.0f, 0.0f, 1.0f);
80 cairo_set_line_cap (pCairoContext, CAIRO_LINE_CAP_ROUND);
81 cairo_set_line_width (pCairoContext, fLineWidth);
82 cairo_move_to (pCairoContext, pLineOne->start.fX, pLineOne->start.fY);
83 cairo_curve_to (pCairoContext,
84 pLineOne->end.fX,
85 pLineOne->end.fY,
86 pLineTwo->start.fX,
87 pLineTwo->start.fY,
88 pLineTwo->end.fX,
89 pLineTwo->end.fY);
90 cairo_stroke (pCairoContext);
91 cairo_restore (pCairoContext);