1 /*******************************************************************************
2 **3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
3 ** 10 20 30 40 50 60 70 80
9 ** Mirco "MacSlow" Mueller <macslow@bangang.de>
11 ** copyright (C) Mirco Mueller, July 2006
14 ** Simple example demonstrating how one could use cairo for generating
15 ** dynamic texture-mapping with OpenGL. I put this program under the terms of
16 ** the "GNU Lesser General Public License". If you don't know what that means
17 ** take a look a here...
19 ** http://www.gnu.org/licenses/licenses.html#TOCLGPL
21 *******************************************************************************/
26 #include "SDL_opengl.h"
28 #include "opengl-rendering.h"
29 #include "cairo-rendering.h"
32 #define WIN_HEIGHT 256
35 update_animation_vars (Line
* pLineOne
,
38 advance (&pLineOne
->start
);
39 advance (&pLineOne
->end
);
40 advance (&pLineTwo
->start
);
41 advance (&pLineTwo
->end
);
44 int main (int argc
, char** argv
)
46 int iWidth
= WIN_WIDTH
;
47 int iHeight
= WIN_HEIGHT
;
50 unsigned int uiTextureId
;
51 SDL_Surface
* pSurfaceWindow
= NULL
;
54 cairo_surface_t
* pCairoSurface
= NULL
;
55 cairo_t
* pCairoContext
;
56 unsigned char* pucSurfaceData
;
57 double fLineWidth
= 0.05f
;
59 /* initialize the four control-points of the two lines to draw
60 ** the bezier-curve between */
61 lineOne
.start
.fX
= 0.1f
;
62 lineOne
.start
.fY
= 0.2f
;
63 lineOne
.start
.iGrowX
= 1;
64 lineOne
.start
.iGrowY
= 1;
65 lineOne
.start
.fStepX
= 0.025f
;
66 lineOne
.start
.fStepY
= 0.02f
;
67 lineOne
.start
.fLowerLimitX
= 0.1f
;
68 lineOne
.start
.fUpperLimitX
= 0.9f
;
69 lineOne
.start
.fLowerLimitY
= 0.1f
;
70 lineOne
.start
.fUpperLimitY
= 0.9f
;
72 lineOne
.end
.fX
= 0.5f
;
73 lineOne
.end
.fY
= 0.7f
;
74 lineOne
.end
.iGrowX
= 1;
75 lineOne
.end
.iGrowY
= 0;
76 lineOne
.end
.fStepX
= 0.025f
;
77 lineOne
.end
.fStepY
= 0.01f
;
78 lineOne
.end
.fLowerLimitX
= 0.1f
;
79 lineOne
.end
.fUpperLimitX
= 0.9f
;
80 lineOne
.end
.fLowerLimitY
= 0.1f
;
81 lineOne
.end
.fUpperLimitY
= 0.9f
;
83 lineTwo
.start
.fX
= 0.75f
;
84 lineTwo
.start
.fY
= 0.1f
;
85 lineTwo
.start
.iGrowX
= 0;
86 lineTwo
.start
.iGrowY
= 1;
87 lineTwo
.start
.fStepX
= 0.01f
;
88 lineTwo
.start
.fStepY
= 0.025f
;
89 lineTwo
.start
.fLowerLimitX
= 0.1f
;
90 lineTwo
.start
.fUpperLimitX
= 0.9f
;
91 lineTwo
.start
.fLowerLimitY
= 0.1f
;
92 lineTwo
.start
.fUpperLimitY
= 0.9f
;
94 lineTwo
.end
.fX
= 0.8f
;
95 lineTwo
.end
.fY
= 0.8f
;
96 lineTwo
.end
.iGrowX
= 0;
97 lineTwo
.end
.iGrowY
= 0;
98 lineTwo
.end
.fStepX
= 0.01f
;
99 lineTwo
.end
.fStepY
= 0.01f
;
100 lineTwo
.end
.fLowerLimitX
= 0.1f
;
101 lineTwo
.end
.fUpperLimitX
= 0.9f
;
102 lineTwo
.end
.fLowerLimitY
= 0.1f
;
103 lineTwo
.end
.fUpperLimitY
= 0.9f
;
106 if ((SDL_Init (SDL_INIT_VIDEO
| SDL_INIT_TIMER
) == -1))
108 printf ("Could not initialize SDL: %s.\n", SDL_GetError ());
112 /* set window title */
113 SDL_WM_SetCaption ("gl-cairo-simple", NULL
);
115 /* create "GL-context" */
116 SDL_GL_SetAttribute (SDL_GL_RED_SIZE
, 5);
117 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE
, 6);
118 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE
, 5);
119 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE
, 16);
120 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER
, 1);
121 pSurfaceWindow
= SDL_SetVideoMode (iWidth
,
127 /* did we get what we want? */
130 printf ("Couldn't open SDL-window: %s\n", SDL_GetError ());
134 /* create cairo-surface/context to act as OpenGL-texture source */
135 pCairoContext
= create_cairo_context (iWidth
,
141 /* setup "GL-context" */
143 resize_func (iWidth
, iHeight
, &uiTextureId
);
145 /* enter event-loop */
148 /* get event from queue */
149 SDL_PollEvent (&event
);
152 /* check for user hitting close-window widget */
157 /* check for mouse-buttons */
158 case SDL_MOUSEBUTTONDOWN
:
160 if (event
.button
.button
== SDL_BUTTON_WHEELUP
)
161 if (fLineWidth
< 0.2f
)
162 fLineWidth
+= 0.005f
;
164 if (event
.button
.button
== SDL_BUTTON_WHEELDOWN
)
165 if (fLineWidth
> 0.01f
)
166 fLineWidth
-= 0.005f
;
170 /* check for the keys being pressed */
173 /* ESC-key, exit program */
174 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
177 /* q-key, exit program */
178 if (event
.key
.keysym
.sym
== SDLK_q
)
181 /* d-key, dump last cairo-rendered frame in a PNG-file */
182 if (event
.key
.keysym
.sym
== SDLK_d
)
183 cairo_surface_write_to_png (pCairoSurface
, "frame.png");
187 /* user resized window */
188 case SDL_VIDEORESIZE
:
189 iWidth
= event
.resize
.w
;
190 iHeight
= event
.resize
.h
;
191 pSurfaceWindow
= SDL_SetVideoMode (iWidth
,
196 resize_func (iWidth
, iHeight
, &uiTextureId
);
197 free (pucSurfaceData
);
198 cairo_destroy (pCairoContext
);
199 pCairoContext
= create_cairo_context (iWidth
,
207 update_animation_vars (&lineOne
, &lineTwo
);
208 render_curve (pCairoContext
,
214 draw_func (iWidth
, iHeight
, pucSurfaceData
, uiTextureId
);
216 /* try to get a redraw-rate of 20 Hz */
220 /* clear resources before exit */
221 glDeleteTextures (1, &uiTextureId
);
222 free (pucSurfaceData
);
223 cairo_destroy (pCairoContext
);