ups, was missing the -f
[gl-cairo-simple.git] / main.c
blobb64123642fdc4fb8381319c354046abf9c9f3e32
1 /*******************************************************************************
2 **3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
3 ** 10 20 30 40 50 60 70 80
4 **
5 ** program:
6 ** gl-cairo-simple
7 **
8 ** author:
9 ** Mirco "MacSlow" Mueller <macslow@bangang.de>
11 ** copyright (C) Mirco Mueller, July 2006
13 ** notes:
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 *******************************************************************************/
23 #include <stdlib.h>
25 #include "SDL.h"
26 #include "SDL_opengl.h"
27 #include "geometry.h"
28 #include "opengl-rendering.h"
29 #include "cairo-rendering.h"
31 #define WIN_WIDTH 256
32 #define WIN_HEIGHT 256
34 void
35 update_animation_vars (Line* pLineOne,
36 Line* pLineTwo)
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;
48 Line lineOne;
49 Line lineTwo;
50 unsigned int uiTextureId;
51 SDL_Surface* pSurfaceWindow = NULL;
52 SDL_Event event;
53 int iKeepRunning = 1;
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;
105 /* init SDL */
106 if ((SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1))
108 printf ("Could not initialize SDL: %s.\n", SDL_GetError ());
109 exit (-1);
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,
122 iHeight,
124 SDL_OPENGL |
125 SDL_RESIZABLE);
127 /* did we get what we want? */
128 if (!pSurfaceWindow)
130 printf ("Couldn't open SDL-window: %s\n", SDL_GetError ());
131 exit (-2);
134 /* create cairo-surface/context to act as OpenGL-texture source */
135 pCairoContext = create_cairo_context (iWidth,
136 iHeight,
138 &pCairoSurface,
139 &pucSurfaceData);
141 /* setup "GL-context" */
142 init_gl ();
143 resize_func (iWidth, iHeight, &uiTextureId);
145 /* enter event-loop */
146 while (iKeepRunning)
148 /* get event from queue */
149 SDL_PollEvent (&event);
150 switch (event.type)
152 /* check for user hitting close-window widget */
153 case SDL_QUIT :
154 iKeepRunning = 0;
155 break;
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;
168 break;
170 /* check for the keys being pressed */
171 case SDL_KEYDOWN :
173 /* ESC-key, exit program */
174 if (event.key.keysym.sym == SDLK_ESCAPE)
175 iKeepRunning = 0;
177 /* q-key, exit program */
178 if (event.key.keysym.sym == SDLK_q)
179 iKeepRunning = 0;
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");
185 break;
187 /* user resized window */
188 case SDL_VIDEORESIZE :
189 iWidth = event.resize.w;
190 iHeight = event.resize.h;
191 pSurfaceWindow = SDL_SetVideoMode (iWidth,
192 iHeight,
194 SDL_OPENGL |
195 SDL_RESIZABLE);
196 resize_func (iWidth, iHeight, &uiTextureId);
197 free (pucSurfaceData);
198 cairo_destroy (pCairoContext);
199 pCairoContext = create_cairo_context (iWidth,
200 iHeight,
202 &pCairoSurface,
203 &pucSurfaceData);
204 break;
207 update_animation_vars (&lineOne, &lineTwo);
208 render_curve (pCairoContext,
209 iWidth,
210 iHeight,
211 &lineOne,
212 &lineTwo,
213 fLineWidth);
214 draw_func (iWidth, iHeight, pucSurfaceData, uiTextureId);
216 /* try to get a redraw-rate of 20 Hz */
217 SDL_Delay (50);
220 /* clear resources before exit */
221 glDeleteTextures (1, &uiTextureId);
222 free (pucSurfaceData);
223 cairo_destroy (pCairoContext);
224 SDL_Quit ();
226 return 0;