Subversion Repositories Kolibri OS

Rev

Rev 1789 | Rev 7951 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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