opengl device (just for fun)
[swftools.git] / lib / devices / opengl.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "../gfxdevice.h"
6 #include "../gfxtools.h"
7
8 #include <time.h>
9 #include <GL/gl.h>
10 #include <GL/glut.h>
11
12 #include <setjmp.h>
13
14 typedef struct _fontlist {
15     gfxfont_t*font;
16     char*id;
17     struct _fontlist*next;
18 } fontlist_t;
19
20 typedef struct _internal {
21     gfxfont_t*font;
22     char*fontid;
23     fontlist_t* fontlist;
24 } internal_t;
25
26 int opengl_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
27 {
28     return 0;
29 }
30
31 jmp_buf env;
32 //jmp_buf env2;
33
34 void display()
35 {
36     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
37     /*if(!setjmp(env2))
38         longjmp(env, 1);*/
39     longjmp(env, 1);
40 }
41
42 void opengl_startpage(struct _gfxdevice*dev, int width, int height)
43 {
44     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
45     glutInitWindowSize(width,height);
46     glutInitWindowPosition(100,100);
47     glutCreateWindow("gfxdevice_opengl");
48     glutDisplayFunc(display);
49
50     glClearColor(1.0,0.0,0.0,0.0);
51     glShadeModel(GL_FLAT);
52     glEnable(GL_DEPTH_TEST);
53     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
54     
55     glViewport(0,0, width, height);
56
57     glMatrixMode(GL_PROJECTION);
58     glLoadIdentity();
59     //gluPerspective(/*angle*/60.0, (GLfloat)width/(GLfloat)height, /*nearclip*/1.0, /*farclip*/30.0);
60
61     glMatrixMode(GL_MODELVIEW);
62     glLoadIdentity();
63     glScalef(1.0/width,1.0/height,1.0);
64     
65     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
66    
67     int ret=0;
68     ret = setjmp(env);
69     if(!ret) {
70         glutMainLoop();
71     }
72 }
73
74 void opengl_startclip(struct _gfxdevice*dev, gfxline_t*line)
75 {
76     int t;
77     for(t=1;t<100;t++) {
78         glColor3f(1.0,1.0,1.0);
79         glBegin(GL_POLYGON);
80             glVertex3f(-t*10,-t*10,0);
81             glVertex3f(-t*10,t*10,0);
82             glVertex3f(t*10,t*10,0);
83             glVertex3f(t*10,-t*10,0);
84         glEnd();
85     }
86     //glRectf(-50,-50,0.0,0.0);
87 }
88
89 void opengl_endclip(struct _gfxdevice*dev)
90 {
91 }
92
93 void opengl_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
94 {
95 }
96
97 void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
98 {
99 }
100
101 void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
102 {
103 }
104
105 void opengl_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
106 {
107 }
108
109 void opengl_addfont(struct _gfxdevice*dev, char*fontid, gfxfont_t*font)
110 {
111 }
112
113 void opengl_drawchar(struct _gfxdevice*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
114 {
115 }
116
117 void opengl_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
118 {
119 }
120
121 void opengl_endpage(struct _gfxdevice*dev)
122 {
123     glFlush();
124     //longjmp(env2, 1);
125     glutMainLoop();
126 }
127
128
129 int opengl_result_save(struct _gfxresult*gfx, char*filename)
130 {
131     return 0;
132 }
133 void* opengl_result_get(struct _gfxresult*gfx, char*name)
134 {
135     return 0;
136 }
137 void opengl_result_destroy(struct _gfxresult*gfx)
138 {
139     free(gfx);
140 }
141
142 gfxresult_t*opengl_finish(struct _gfxdevice*dev)
143 {
144     internal_t*i = (internal_t*)dev->internal;
145     gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
146     memset(result, 0, sizeof(gfxresult_t));
147     result->save = opengl_result_save;
148     result->get = opengl_result_get;
149     result->destroy = opengl_result_destroy;
150     return result;
151 }
152
153 void gfxdevice_opengl_init(gfxdevice_t*dev)
154 {
155     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
156     int argc=1;
157     char*argv[]={"gfxdevice_opengl", 0};
158     glutInit(&argc, argv);
159     
160     memset(dev, 0, sizeof(gfxdevice_t));
161
162     dev->internal = i;
163     
164     dev->setparameter = opengl_setparameter;
165     dev->startpage = opengl_startpage;
166     dev->startclip = opengl_startclip;
167     dev->endclip = opengl_endclip;
168     dev->stroke = opengl_stroke;
169     dev->fill = opengl_fill;
170     dev->fillbitmap = opengl_fillbitmap;
171     dev->fillgradient = opengl_fillgradient;
172     dev->addfont = opengl_addfont;
173     dev->drawchar = opengl_drawchar;
174     dev->drawlink = opengl_drawlink;
175     dev->endpage = opengl_endpage;
176     dev->finish = opengl_finish;
177 }