Rev 1789 | Rev 7952 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1789 | Rev 5098 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | Ïðèìåð âçÿò èç íàáîðà ïðèìåðîâ ê êîìïèëÿòîðó XS Compiler |
2 | Ïðèìåð âçÿò èç íàáîðà ïðèìåðîâ ê êîìïèëÿòîðó XS Compiler |
3 | 3 | ||
4 | iadn |
4 | iadn |
5 | http://www.iadn.narod.ru |
5 | http://www.iadn.narod.ru |
6 | iadn@bk.ru |
6 | iadn@bk.ru |
7 | */ |
7 | */ |
8 | 8 | ||
9 | #include |
9 | #include |
10 | #include |
10 | #include |
11 | #include |
11 | #include |
12 | 12 | ||
13 | #include "SysCall.h" |
13 | #include "SysCall.h" |
14 | #include "ProcessTab.h" |
14 | #include "ProcessTab.h" |
15 | 15 | ||
16 | 16 | ||
17 | int Fps (long x, long y); |
17 | int Fps (long x, long y); |
18 | extern "C"{ |
18 | extern "C"{ |
19 | void app_main(void); |
19 | int main(void); |
20 | } |
20 | } |
21 | 21 | ||
22 | struct { |
22 | static struct { |
23 | int x,y; |
23 | int x,y; |
24 | int dx,dy; |
24 | int dx,dy; |
25 | } win; |
25 | } win; |
26 | 26 | ||
27 | #define CUBE_STEP 0.1 |
27 | #define CUBE_STEP 0.1 |
28 | 28 | ||
29 | #define KEY_ESC 1 |
29 | #define KEY_ESC 1 |
30 | #define KEY_F 33 |
30 | #define KEY_F 33 |
31 | 31 | ||
32 | char *title1 = "TinyGL in KolibriOS"; |
32 | static char title1[] = "TinyGL in KolibriOS"; |
33 | char *title2 = "F full screen"; |
33 | static char title2[] = "F full screen"; |
34 | char *title3 = "ESC - exit"; |
34 | static char title3[] = "ESC - exit"; |
35 | char *fps = "FPS:"; |
35 | static char fps[] = "FPS:"; |
36 | 36 | ||
37 | unsigned char FullScreen = 0; |
37 | static unsigned char FullScreen = 0; |
38 | unsigned char skin = 3; |
38 | static unsigned char skin = 3; |
39 | 39 | ||
40 | float angle; |
40 | static float angle; |
41 | process_table_entry_* pri; |
41 | static process_table_entry_* pri; |
42 | KOSGLContext cgl; |
42 | static KOSGLContext cgl; |
43 | 43 | ||
44 | void draw_cube() |
44 | static void draw_cube() |
45 | { |
45 | { |
46 | float x,y,z; |
46 | float x,y,z; |
47 | glBegin(GL_LINES); |
47 | glBegin(GL_LINES); |
48 | 48 | ||
49 | for(y=-0.5;y<=0.5;y+=CUBE_STEP) |
49 | for(y=-0.5;y<=0.5;y+=CUBE_STEP) |
50 | { |
50 | { |
51 | // the front |
51 | // the front |
52 | glColor3f(0,y+0.5,0); |
52 | glColor3f(0,y+0.5,0); |
53 | glVertex3f(-0.5,y,-0.5); |
53 | glVertex3f(-0.5,y,-0.5); |
54 | glColor3f(1,y+0.5,0); |
54 | glColor3f(1,y+0.5,0); |
55 | glVertex3f(0.5,y,-0.5); |
55 | glVertex3f(0.5,y,-0.5); |
56 | 56 | ||
57 | // the back |
57 | // the back |
58 | glColor3f(0,y+0.5,1); |
58 | glColor3f(0,y+0.5,1); |
59 | glVertex3f(-0.5,y,0.5); |
59 | glVertex3f(-0.5,y,0.5); |
60 | glColor3f(1,y+0.5,1); |
60 | glColor3f(1,y+0.5,1); |
61 | glVertex3f(0.5,y,0.5); |
61 | glVertex3f(0.5,y,0.5); |
62 | 62 | ||
63 | //right side |
63 | //right side |
64 | glColor3f(1,y+0.5,0); |
64 | glColor3f(1,y+0.5,0); |
65 | glVertex3f(0.5,y,-0.5); |
65 | glVertex3f(0.5,y,-0.5); |
66 | glColor3f(1,y+0.5,1); |
66 | glColor3f(1,y+0.5,1); |
67 | glVertex3f(0.5,y,0.5); |
67 | glVertex3f(0.5,y,0.5); |
68 | 68 | ||
69 | //left side |
69 | //left side |
70 | glColor3f(0,y+0.5,0); |
70 | glColor3f(0,y+0.5,0); |
71 | glVertex3f(-0.5,y,-0.5); |
71 | glVertex3f(-0.5,y,-0.5); |
72 | glColor3f(0,y+0.5,1); |
72 | glColor3f(0,y+0.5,1); |
73 | glVertex3f(-0.5,y,0.5); |
73 | glVertex3f(-0.5,y,0.5); |
74 | } |
74 | } |
75 | 75 | ||
76 | for(x=-0.5;x<=0.5;x+=CUBE_STEP) |
76 | for(x=-0.5;x<=0.5;x+=CUBE_STEP) |
77 | { |
77 | { |
78 | // the front |
78 | // the front |
79 | glColor3f(x+0.5,1,0); |
79 | glColor3f(x+0.5,1,0); |
80 | glVertex3f(x,0.5,-0.5); |
80 | glVertex3f(x,0.5,-0.5); |
81 | glColor3f(x+0.5,0,0); |
81 | glColor3f(x+0.5,0,0); |
82 | glVertex3f(x,-0.5,-0.5); |
82 | glVertex3f(x,-0.5,-0.5); |
83 | 83 | ||
84 | // the back |
84 | // the back |
85 | glColor3f(x+0.5,1,1); |
85 | glColor3f(x+0.5,1,1); |
86 | glVertex3f(x,0.5,0.5); |
86 | glVertex3f(x,0.5,0.5); |
87 | glColor3f(x+0.5,0,1); |
87 | glColor3f(x+0.5,0,1); |
88 | glVertex3f(x,-0.5,0.5); |
88 | glVertex3f(x,-0.5,0.5); |
89 | 89 | ||
90 | // the top |
90 | // the top |
91 | glColor3f(x+0.5,1,0); |
91 | glColor3f(x+0.5,1,0); |
92 | glVertex3f(x,0.5,-0.5); |
92 | glVertex3f(x,0.5,-0.5); |
93 | glColor3f(x+0.5,1,1); |
93 | glColor3f(x+0.5,1,1); |
94 | glVertex3f(x,0.5,0.5); |
94 | glVertex3f(x,0.5,0.5); |
95 | 95 | ||
96 | // the bottom |
96 | // the bottom |
97 | glColor3f(x+0.5,0,0); |
97 | glColor3f(x+0.5,0,0); |
98 | glVertex3f(x,-0.5,-0.5); |
98 | glVertex3f(x,-0.5,-0.5); |
99 | glColor3f(x+0.5,0,1); |
99 | glColor3f(x+0.5,0,1); |
100 | glVertex3f(x,-0.5,0.5); |
100 | glVertex3f(x,-0.5,0.5); |
101 | } |
101 | } |
102 | 102 | ||
103 | for(z=-0.5;z<=0.5;z+=CUBE_STEP) |
103 | for(z=-0.5;z<=0.5;z+=CUBE_STEP) |
104 | { |
104 | { |
105 | // the top |
105 | // the top |
106 | glColor3f(0,1,z+0.5); |
106 | glColor3f(0,1,z+0.5); |
107 | glVertex3f(-0.5,0.5,z); |
107 | glVertex3f(-0.5,0.5,z); |
108 | glColor3f(1,1,z+0.5); |
108 | glColor3f(1,1,z+0.5); |
109 | glVertex3f(0.5,0.5,z); |
109 | glVertex3f(0.5,0.5,z); |
110 | 110 | ||
111 | // the bottom |
111 | // the bottom |
112 | glColor3f(0,0,z+0.5); |
112 | glColor3f(0,0,z+0.5); |
113 | glVertex3f(-0.5,-0.5,z); |
113 | glVertex3f(-0.5,-0.5,z); |
114 | glColor3f(1,0,z+0.5); |
114 | glColor3f(1,0,z+0.5); |
115 | glVertex3f(0.5,-0.5,z); |
115 | glVertex3f(0.5,-0.5,z); |
116 | 116 | ||
117 | // right side |
117 | // right side |
118 | glColor3f(1,1,z+0.5); |
118 | glColor3f(1,1,z+0.5); |
119 | glVertex3f(0.5,0.5,z); |
119 | glVertex3f(0.5,0.5,z); |
120 | glColor3f(1,0,z+0.5); |
120 | glColor3f(1,0,z+0.5); |
121 | glVertex3f(0.5,-0.5,z); |
121 | glVertex3f(0.5,-0.5,z); |
122 | 122 | ||
123 | // left side |
123 | // left side |
124 | glColor3f(0,1,z+0.5); |
124 | glColor3f(0,1,z+0.5); |
125 | glVertex3f(-0.5,0.5,z); |
125 | glVertex3f(-0.5,0.5,z); |
126 | glColor3f(0,0,z+0.5); |
126 | glColor3f(0,0,z+0.5); |
127 | glVertex3f(-0.5,-0.5,z); |
127 | glVertex3f(-0.5,-0.5,z); |
128 | } |
128 | } |
129 | 129 | ||
130 | glEnd(); |
130 | glEnd(); |
131 | } |
131 | } |
132 | 132 | ||
133 | void DrawGL() |
133 | static void DrawGL() |
134 | { |
134 | { |
135 | glLoadIdentity(); // óñòàíàâëèâàåì åäåíè÷íóþ ìàòðèöó |
135 | glLoadIdentity(); // óñòàíàâëèâàåì åäåíè÷íóþ ìàòðèöó |
136 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
136 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
137 | 137 | ||
138 | glTranslatef(0.0, 0.0, -3.0); |
138 | glTranslatef(0.0, 0.0, -3.0); |
139 | glRotatef(angle, 1.0, 0.0, 0.0); |
139 | glRotatef(angle, 1.0, 0.0, 0.0); |
140 | glRotatef(2.0*angle, 0.0, 1.0, 0.0); |
140 | glRotatef(2.0*angle, 0.0, 1.0, 0.0); |
141 | glRotatef(3.0*angle, 0.0, 0.0, 1.0); |
141 | glRotatef(3.0*angle, 0.0, 0.0, 1.0); |
142 | 142 | ||
143 | draw_cube(); |
143 | draw_cube(); |
144 | 144 | ||
145 | kosglSwapBuffers(); |
145 | kosglSwapBuffers(); |
146 | } |
146 | } |
147 | 147 | ||
148 | void reshape() |
148 | static void reshape() |
149 | { |
149 | { |
150 | __menuet__get_process_table((process_table_entry*)pri,-1); |
150 | __menuet__get_process_table((process_table_entry*)pri,-1); |
151 | glViewport(0, 0, pri->winx_size, pri->winy_size-20); |
151 | glViewport(0, 0, pri->winx_size, pri->winy_size-20); |
152 | glMatrixMode(GL_PROJECTION); |
152 | glMatrixMode(GL_PROJECTION); |
153 | glLoadIdentity(); |
153 | glLoadIdentity(); |
154 | gluPerspective(45.0, (GLfloat)pri->winx_size/pri->winy_size, 1.0, 300.0); |
154 | gluPerspective(45.0, (GLfloat)pri->winx_size/pri->winy_size, 1.0, 300.0); |
155 | glMatrixMode(GL_MODELVIEW); |
155 | glMatrixMode(GL_MODELVIEW); |
156 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
156 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
157 | } |
157 | } |
158 | 158 | ||
159 | void disabletgl() |
159 | static void disabletgl() |
160 | { |
160 | { |
161 | kosglDestroyContext(cgl); |
161 | kosglDestroyContext(cgl); |
162 | delete pri; |
162 | delete pri; |
163 | } |
163 | } |
164 | 164 | ||
165 | void Title() |
165 | static void Title() |
166 | { |
166 | { |
167 | __menuet__write_text(300,8,0x10ffffff,fps,strlen(fps)); |
167 | __menuet__write_text(300,8,0x10ffffff,fps,strlen(fps)); |
168 | __menuet__write_text(8,8,0x10ffffff,title1,strlen(title1)); |
168 | __menuet__write_text(8,8,0x10ffffff,title1,strlen(title1)); |
169 | __menuet__write_text(180,8,0x00ffffff,title2,strlen(title2)); |
169 | __menuet__write_text(180,8,0x00ffffff,title2,strlen(title2)); |
170 | __menuet__write_text(600,8,0x00ffffff,title3,strlen(title3)); |
170 | __menuet__write_text(600,8,0x00ffffff,title3,strlen(title3)); |
171 | } |
171 | } |
172 | 172 | ||
173 | void draw_window(void) |
173 | static void draw_window(void) |
174 | { |
174 | { |
175 | // start redraw |
175 | // start redraw |
176 | __menuet__window_redraw(1); |
176 | __menuet__window_redraw(1); |
177 | // define&draw window |
177 | // define&draw window |
178 | __menuet__define_window(win.x,win.y,win.dx,win.dy,TYPEWIN(0,0,0,1,skin,0,0,0),0,0); |
178 | __menuet__define_window(win.x,win.y,win.dx,win.dy,TYPEWIN(0,0,0,1,skin,0,0,0),0,0); |
179 | // end redraw |
179 | // end redraw |
180 | __menuet__window_redraw(2); |
180 | __menuet__window_redraw(2); |
181 | // display string |
181 | // display string |
182 | Title(); |
182 | Title(); |
183 | } |
183 | } |
184 | 184 | ||
185 | void app_main(void) |
185 | int main(void) |
186 | { |
186 | { |
187 | 187 | ||
188 | win.x = 100; |
188 | win.x = 100; |
189 | win.y = 100; |
189 | win.y = 100; |
190 | win.dx = 400; |
190 | win.dx = 400; |
191 | win.dy = 400; |
191 | win.dy = 400; |
192 | 192 | ||
193 | draw_window(); |
193 | draw_window(); |
194 | 194 | ||
195 | cgl = kosglCreateContext( 0, 0); |
195 | cgl = kosglCreateContext( 0, 0); |
196 | kosglMakeCurrent( 0, 20, win.dx, win.dy-20, cgl); |
196 | kosglMakeCurrent( 0, 20, win.dx, win.dy-20, cgl); |
197 | 197 | ||
198 | glMatrixMode(GL_MODELVIEW); |
198 | glMatrixMode(GL_MODELVIEW); |
199 | glLoadIdentity(); |
199 | glLoadIdentity(); |
200 | 200 | ||
201 | glClearDepth(1.0); |
201 | glClearDepth(1.0); |
202 | glEnable( GL_CULL_FACE ); |
202 | glEnable( GL_CULL_FACE ); |
203 | glEnable(GL_DEPTH_TEST); |
203 | glEnable(GL_DEPTH_TEST); |
204 | 204 | ||
205 | pri=new process_table_entry_; |
205 | pri=new process_table_entry_; |
206 | SysCall(66,1,1); |
206 | SysCall(66,1,1); |
207 | 207 | ||
208 | reshape(); |
208 | reshape(); |
209 | 209 | ||
210 | do{ |
210 | do{ |
211 | 211 | ||
212 | if (angle < 360.0) angle += 0.001 + 0.1*Fps (330,8); |
212 | if (angle < 360.0) angle += 0.001 + 0.1*Fps (330,8); |
213 | else angle = 0.0; |
213 | else angle = 0.0; |
214 | 214 | ||
215 | DrawGL(); |
215 | DrawGL(); |
216 | 216 | ||
217 | switch(__menuet__check_for_event()) |
217 | switch(__menuet__check_for_event()) |
218 | { |
218 | { |
219 | case 1: draw_window(); |
219 | case 1: draw_window(); |
220 | reshape(); |
220 | reshape(); |
221 | break; |
221 | break; |
222 | 222 | ||
223 | case 2: |
223 | case 2: |
224 | switch(__menuet__getkey()){ |
224 | switch(__menuet__getkey()){ |
225 | 225 | ||
226 | case KEY_F: |
226 | case KEY_F: |
227 | if(!FullScreen){ |
227 | if(!FullScreen){ |
228 | skin=0; |
228 | skin=0; |
229 | SysCall(67,0,0,SysCall(14)>>16,SysCall(14)&0xffff); |
229 | SysCall(67,0,0,SysCall(14)>>16,SysCall(14)&0xffff); |
230 | draw_window(); |
230 | draw_window(); |
231 | reshape(); |
231 | reshape(); |
232 | FullScreen = 1; |
232 | FullScreen = 1; |
233 | } |
233 | } |
234 | else{ |
234 | else{ |
235 | skin=3; |
235 | skin=3; |
236 | draw_window(); |
236 | draw_window(); |
237 | SysCall(67,win.x,win.y,win.dx,win.dy); |
237 | SysCall(67,win.x,win.y,win.dx,win.dy); |
238 | reshape(); |
238 | reshape(); |
239 | FullScreen = 0; |
239 | FullScreen = 0; |
240 | }; |
240 | }; |
241 | break; |
241 | break; |
242 | 242 | ||
243 | case KEY_ESC: disabletgl(); |
243 | case KEY_ESC: disabletgl(); |
244 | return;} |
244 | return 0;} |
245 | break; |
245 | break; |
246 | 246 | ||
247 | case 3: disabletgl(); |
247 | case 3: disabletgl(); |
248 | return; |
248 | return 0; |
249 | } |
249 | } |
250 | }while(1); |
250 | }while(1); |
251 | }>=0.5;z+=CUBE_STEP) |
251 | }>=0.5;z+=CUBE_STEP) |
252 | >=0.5;x+=CUBE_STEP) |
252 | >=0.5;x+=CUBE_STEP) |
253 | >=0.5;y+=CUBE_STEP) |
253 | >=0.5;y+=CUBE_STEP) |
254 | > |
254 | > |