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