home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser-CD 2001 January / LCD_01_2001.iso / develop / tinylib041 / tiny / atari / demo / testiny.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-18  |  11.2 KB  |  290 lines

  1. /* example of use of the Library Dynamic Mesa_gl.ldg in C
  2. compiled with success for PureC and GCC 
  3. Olivier LANDEMARRE le 18/6/2000 
  4. It can work on all atari GEM system the video restriction is the restriction
  5. for screen.ldg (to B&W -> 32bits TC) if direct memory acces is avaible (the slower is for
  6. 256 colors!)*/
  7.  
  8. /* WARNING LDG use the stack of the program witch call them and Mesa_gl need a good stack!!
  9. near 32Ko don't forget to change your stack!!! (more if you use lists!!!) */
  10.  
  11.  
  12. #include <stdio.h>
  13. #ifdef __PUREC__
  14. #include <tos.h> 
  15. #include <vdi.h>
  16. #else 
  17. #include <osbind.h>
  18. #include <vdibind.h>
  19. #endif
  20. #include <ldg.h>  /* To load LDGs */
  21. #include "mesa_ldg.h"   /* Définitions pour OpenGL */
  22. #include "scrnldg.h"   /* for screen.ldg     */
  23.  
  24. #ifndef __CDECL
  25. #define __CDECL cdecl
  26. #endif
  27.  
  28. /* base functions of screen.ldg */
  29. long __CDECL (*screen_detect)(long vdihandle, char *answer);
  30. long __CDECL (*screen_init)(long vdihandle, videoinf *display, char *type_ecran, sortievideo *more_infos, long flag);
  31. static void __CDECL (*fix_palette)(sortievideo *more_infos, long mode, long vdihandle)=NULL;
  32. void __CDECL (*screen_display)(long vdihandle, videoinf *display);
  33. void __CDECL (*screen_display_texture)(long vdihandle, videoinf *display,long startx, long starty);
  34.  
  35.                   /* pour déclarer toutes les fonctions à utiliser 
  36.                   dans Mesa */
  37. #undef APIENTRY
  38. #define APIENTRY __CDECL
  39.                       /* toutes les fonctions ont un passage standard 
  40.                       C de type _CDECL c'est la condition obligatoire 
  41.                       */
  42.                       /* fonctions standard OpenGL */
  43. #include "initiny.c"
  44. unsigned char *texture=NULL;
  45. int app_id, vdihandle;
  46. LDG *mesaldg,*screenldg;
  47.  
  48. void  APIENTRY onerror(long error)
  49. {
  50.     if(error==1L) form_alert("[1][Memory allocatio error in OpenGL!][End]");
  51.     else form_alert("[1][Error in OpenGL!][End]");
  52.  
  53.     ldg_term(app_id,mesaldg);  /* clean close of MESA_GL.LDG */
  54.       ldg_term(app_id,screenldg);  /* clean close of SCREEN.LDG */
  55.   
  56.       v_clsvwk(vdihandle);  
  57.       appl_exit();     
  58.       exit(0);
  59. }
  60.  
  61.  
  62. int main()
  63. { int work_in[]= {1,1,1,1,1,1,1,1,1,1,2}, work_out[57],i;
  64.     char retourneecc[50];  
  65.  
  66.   long mem1, mem2,mem3;
  67.   videoinf mydisplay;
  68.   sortievideo myinfos;
  69.   char *image_buffer;   /* buffer de l'image générée */
  70.   long width=300, height=300;  /* taille de l'image à générer */
  71.   
  72.   app_id = appl_init ();            /* AES init need for LDG*/
  73.   /*                   ----------------------------------------                  */
  74.   
  75.                                     /* Load of screen.ldg */
  76.   if((screenldg = ldg_exec ( app_id, "screen.ldg")) == NULL)  /* Load of screen.ldg */
  77.   { 
  78.       if((screenldg = ldg_exec ( app_id, "screen.ldg")) == NULL) /* Retry */
  79.       {
  80.           
  81.           form_alert ( 1, "[1][Can't load |SCREEN.LDG!][OK]");
  82.         appl_exit();
  83.         return(0);
  84.     }
  85.   }
  86.   /*                       ---------------------------------                  */
  87.                      /* link of screen.ldg functions */
  88.   screen_detect=ldg_find("screen_detect", screenldg);
  89.   screen_init=ldg_find("screen_init", screenldg);
  90.   fix_palette=ldg_find("fix_palette", screenldg);
  91.   screen_display=ldg_find("screen_display", screenldg);
  92.   screen_display_texture=ldg_find("screen_display_texture", screenldg);
  93.  
  94.   /*                       ---------------------------------                  */
  95.   
  96.   (void)v_opnvwk(work_in,&vdihandle,work_out); /* VDI workstation needed */
  97.  
  98.   { char *video,*oldscreen;  int mwork_out2[57],nb_plans;
  99.       while(!wind_update(BEG_UPDATE));   /* attente qu'il n'y ai plus de dessin à l'écran!! */
  100.     vq_extnd((int)vdihandle,1,mwork_out2);
  101.         nb_plans=mwork_out2[4]; 
  102.       video=(char *)ldg_Malloc((long)((work_out[0]+33))*5L*(long)(nb_plans)/8L);    /* 5 lignes suffisent */
  103.       oldscreen=(char *)Logbase();
  104.       if(video!=NULL) Setscreen(video,-1L,-1);   /* nouveau logbase pour screen_detect() */
  105.   /*                       ---------------------------------                  */
  106.  
  107.                         /* Use of screen.ldg */
  108.       if(screen_detect((long)vdihandle,retourneecc)<0)    /* detection of memory screen */
  109.       {
  110.           Setscreen(oldscreen,-1L,-1);  /* on remet l'ancien Logbase */
  111.           ldg_Free(video);
  112.           wind_update(END_UPDATE);   /* on relaisse l'écran libre */
  113.           ldg_term(app_id,mesaldg);  /* clean close of MESA_GL.LDG */
  114.           ldg_term(app_id,screenldg);  /* clean close of SCREEN.LDG */
  115.           form_alert ( 1, "[1][Detection error in SCREEN!][OK]");
  116.         appl_exit();
  117.         return(0);
  118.       }
  119.       
  120.       Setscreen(oldscreen,-1L,-1);    /* on remet l'ancien Logbase */
  121.       ldg_Free(video); 
  122.       wind_update(END_UPDATE);   /* on relaisse l'écran libre */
  123.   }
  124.  
  125.                             /* emulators like Stemulator where Palette don't work well!!! use gray can be recognize by SP ex 8CSP or 8ESP */
  126.   if((retourneecc[1]=='C')&&(retourneecc[2]=='S')&&(retourneecc[3]=='P'))retourneecc[1]='G';
  127.   if((retourneecc[2]=='C')&&(retourneecc[3]=='S')&&(retourneecc[4]=='P'))retourneecc[1]='G';
  128.  
  129.  
  130.   if(screen_init((long)vdihandle, &mydisplay,retourneecc,&myinfos,0L)<=0)  /* is it supported ??? */
  131.   {            /* No !!! */
  132.       ldg_term(app_id,screenldg);  /* clean close of SCREEN.LDG */
  133.       form_alert ( 1, "[1][Your display isn't supported by SCREEN|, sorry (must be >=256 colors)][OK]");
  134.     appl_exit();
  135.     return(0);
  136.   }
  137.           
  138.   { int nb_plans;
  139.       sscanf(retourneecc,"%d",&nb_plans);     /* take number of color planes */
  140.       
  141.       if((nb_plans<=8)&&(nb_plans>1)) 
  142.       {
  143.           fix_palette(&myinfos, 1L, (long) vdihandle); /* fix palette */
  144.       }
  145.   }
  146.               /* SRC image description  */
  147.   mydisplay.largeur_RGB=(unsigned WORD)width;
  148.   mydisplay.hauteur_RGB=(unsigned WORD)height;
  149.             
  150.               /* Position of draw on screen */
  151.   mydisplay.position_X=20; 
  152.   mydisplay.position_Y=20;
  153.                 /* position in SRC image to draw*/
  154.   mydisplay.X_voulu=0;
  155.   mydisplay.Y_voulu=0;
  156.                 /* size to draw */
  157.   mydisplay.largeur_voulue=(unsigned WORD)width;
  158.   mydisplay.hauteur_voulue=(unsigned WORD)height;
  159.             
  160.   /*                       ---------------------------------                  */
  161.  
  162.       /* load of mesa_gl.ldg */
  163.   if( (mesaldg = ldg_exec ( app_id, "tiny_gl.ldg")) == NULL)  /* Load of mesa_gl.ldg*/
  164.   { /* échec */
  165.       /* vous pouvez relancer éventuellement ldg_exec() des fois que 
  166.       le système soit lambin !!! (Mint + Multitos) ca passe souvent 
  167.       mesa est très grosse */
  168.       if( (mesaldg = ldg_exec ( app_id, "tiny_gl.ldg")) == NULL)   /* retry */ 
  169.       {
  170.           ldg_term(app_id,screenldg);  /* clean close of MESA_GL.LDG */
  171.            form_alert ( 1, "[1][Can't load |TINY_GL.LDG!][OK]");
  172.            appl_exit();
  173.            return(0);
  174.     }
  175.   }
  176.   init_tiny(mesaldg);   /* dynamic link of functions of mesa */
  177.     if(exception_error!=NULL) exception_error(onerror); /* function for exception error */
  178.   /*                       ---------------------------------                  */
  179.   
  180.   /* Use of Mesa - OpenGL */
  181.   
  182.   /* initialisation (specific to Mesa_gl.ldg ) */
  183.   texture=ldg_Malloc(256L*256L*3L);
  184.   if(texture)
  185.   { int i,j;  unsigned char *ptex=texture;
  186.       for(j=0;j<255;j++)
  187.       {
  188.           for(i=0;i<255;i++)
  189.           {
  190.               *ptex++=i;
  191.               *ptex++=0;
  192.               *ptex++=255-j;
  193.           }
  194.       }
  195.       
  196.   }
  197.   else printf("ERREUR Alloc\015\012");
  198.   mem1=Malloc(-1L);
  199.   image_buffer = OSMesaCreateLDG(VDI_RGB, (long)GL_UNSIGNED_BYTE, width, height );
  200.       /* create the context, this function return the address of picture
  201.       First parameter : Type of picture to do (see gl.h) we choice VDI_RGB witch is
  202.       the natural format for screen.ldg (24R8V8B8 format)
  203.       création du contexte la fonction retourne le pointeur sur 
  204.       2eme parameter : Always  GL_UNSIGNED_BYTE
  205.       3eme et 4eme parameter width and height of picture to calculate
  206.       The maximal size can know with use of functions max_width() and max_height()
  207.       
  208.       */
  209.       
  210.   /*                       ---------------------------------                  */                             
  211.   if(image_buffer)  /* initialisation OK */
  212.   { int status; long t1,t2;
  213.   float x=0.5,y=0.2,z=0.5; 
  214.       
  215.       
  216.     mydisplay.add_RGB=(long)image_buffer;    /* for screen to know where is the picture to draw */
  217.     
  218.     /* OpenGL only */
  219.                              /* use of OpenGL see documentation on OpenGl*/
  220.     glMatrixMode((long)GL_PROJECTION);
  221.     glLoadIdentity();  /* Identity matrix */
  222.     glOrtho(-1.0,       1.0,   -1.0,    1.0,  -1.0,    1.0);  /* the space of calculation*/
  223.          /*
  224.                 Y | 
  225.                   |
  226.                   |-------X
  227.                  /
  228.                Z/ 
  229.                       */
  230.     glMatrixMode((long)GL_MODELVIEW);     /* projection mode */
  231.     glClearColor(0.0,0.0,0.1,0.0);  /* clear color RGBA (dark blue) */
  232.     glClear( (long)(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) );   /* to clear */
  233.     glEnable(GL_DEPTH_TEST);    /* test of position of course */
  234.  /*   glEnable(GL_TEXTURE_2D); 
  235.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  236.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  237.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  238.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  239.     glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,texture);
  240.     glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);*/
  241.     t1=Gettime();
  242.     glPolygonMode(GL_BACK,GL_LINE); 
  243.     for(i=0;i<100;i++)    /* for rotation */
  244.     
  245.         {
  246.         /* vq_key_s(vdihandle,&status);*/  /* get keyboard */
  247.         /* if((status&0x7)!=0) i=1020; */ /* stop if ctrl or shift */
  248.         glRotatef(8., .5, 1.0, 0.3); /* rotation of the objects after */
  249.         glClear( (long)(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) );   /* clear */
  250.      glBegin(GL_TRIANGLES);  /* to use triangle 3point for each */
  251.                               
  252.        glColor3f(0.0, 0.0, 1.0);   /* each point can have there own color (blue)*/
  253.        glVertex3f(-0.5, 0.0, 0.0);  /* first point */
  254.          glColor3f(1.0, 1.0, 1.0);   /* white */
  255.          glVertex3f(0.0, 0.5, 0.2);  /* second point */ 
  256.          glColor3f(1.0, 0.0, 0.0);   /* red */
  257.          glVertex3f(0.5, 0.0, 0.0);  /* third point */
  258.         
  259.          glColor3f(0.0, 1.0, 0.0);     /* a triangle all green */     
  260.          glVertex3f(-x, y, -z);  /* first point */
  261.          glVertex3f(x, y, -z);   /* second point */             
  262.          glVertex3f(0.0, -0.1, z);   /* third point */
  263.          
  264.      glEnd();   /* end for triangles */
  265.      glBegin(GL_LINES);
  266.      glVertex3f(0.0, 0.0, 0.0); 
  267.      glVertex3f(0.0, 0.5, 0.0);
  268.      glEnd();
  269.         /* end of call of OpenGL */
  270.   /*  swapbuffer(NULL); */
  271.     screen_display(vdihandle, &mydisplay); /* display picture directly on screen */
  272.     
  273.     }
  274.     t2=Gettime();
  275.     mem3=Malloc(-1L);
  276.     OSMesaDestroyLDG();                      /* destroy context */
  277.   /*  Save_TGA("image.tga", image_buffer, width, height);  / * save picture */
  278.     Mfree( image_buffer );                   /* Don't forget to free the picture */
  279.     form_dial(3,20,20,(int)width,(int)height,20,20,(int)width,(int)height);  /* redraw the desktop */
  280.     mem2=Malloc(-1L);
  281.   }
  282.   else form_alert ( 1, "[1][Memory allocation failed!][OK]");
  283.   ldg_term(app_id,mesaldg);  /* clean close of MESA_GL.LDG */
  284.   ldg_term(app_id,screenldg);  /* clean close of SCREEN.LDG */
  285.   
  286.   v_clsvwk(vdihandle);  
  287.   appl_exit();
  288.   ldg_Free(texture);
  289.   return(1);
  290. }