home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / next / generic.m < prev    next >
Encoding:
Text File  |  1998-01-31  |  2.6 KB  |  122 lines

  1. /* generic.m*/
  2.  
  3. /* Demo of NEXTSTEP Mesa rendering */
  4.  
  5. /*
  6.  * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
  7.  *
  8.  * If you want to render BIG images you'll probably have to increase
  9.  * MAX_WIDTH and MAX_HEIGHT in src/config.h.
  10.  *
  11.  * This program is in the public domain.
  12.  *
  13.  * Brian Paul
  14.  *
  15.  * NEXTSTEP output provided by Pascal Thibaudeau
  16.  * pthibaud@cribx1.u-bordeaux.fr
  17.  * OpenStep conversion by Pete French
  18.  * pete@ohm.york.ac.uk
  19.  */
  20.  
  21. #import <appkit/Application.h>
  22. #import <appkit/Window.h>
  23. #import <appkit/Menu.h>
  24. #import <appkit/View.h>
  25. #import <appkit/color.h>
  26. #import <appkit/NXBitmapImageRep.h>
  27. #import <stdio.h>
  28. #import <stdlib.h>
  29. #import "GL/osmesa.h"
  30.  
  31. extern int gl_width,gl_height;
  32. extern void render_image(void);
  33.  
  34. int main( int argc, char *argv[] )
  35. {
  36.     OSMesaContext ctx;
  37.     unsigned char *buffer;
  38.     id myWindow;
  39.     id myView;
  40.     id myMenu;
  41.     char name[50];
  42.     unsigned long start, end;
  43.     NXBitmapImageRep *bitmap;
  44.     NXRect GR;
  45.  
  46.     NXApp=[Application new];
  47.  
  48.     /* Create an RGBA-mode context */
  49.     ctx = OSMesaCreateContext( GL_RGBA, NULL );
  50.  
  51.     /* Allocate the image buffer */
  52.     buffer = malloc( gl_width * gl_height * 4 );
  53.  
  54.     /* Bind the buffer to the context and make it current */
  55.     OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, gl_width, gl_height );
  56.     OSMesaPixelStore( OSMESA_Y_UP, 0 );
  57.  
  58.     /* Fill the bitmap with the buffer */
  59.     bitmap = [[ NXBitmapImageRep alloc]
  60.                  initData: buffer
  61.                  pixelsWide:gl_width
  62.                  pixelsHigh:gl_height
  63.                  bitsPerSample:8
  64.                  samplesPerPixel:4
  65.                  hasAlpha:YES
  66.                  isPlanar:NO
  67.                  colorSpace: 2
  68.                  bytesPerRow:gl_width*4
  69.                  bitsPerPixel: 32];
  70.  
  71.     NXSetRect(&GR,100, 100, gl_width, gl_height);
  72.  
  73.     myWindow = [[ Window alloc]
  74.                    initContent: &GR
  75.                    style:NX_TITLEDSTYLE
  76.                    backing: NX_BUFFERED
  77.                    buttonMask: NX_MINIATURIZEBUTTONMASK
  78.                    defer: NO];
  79.  
  80.     sprintf(name, "Mesa demo: `%s'", argv[0]);
  81.  
  82.     myView = [[ View alloc] initFrame:&GR];
  83.  
  84.     myMenu = [[ Menu alloc] initTitle: "NeXTStep Mesa"];
  85.     [myMenu addItem: "Quit"
  86.             action:@selector(terminate:)
  87.             keyEquivalent: 'q'];
  88.     [myMenu sizeToFit];
  89.  
  90.     [myWindow setTitle: name];
  91.     [myWindow display];
  92.     [myWindow setContentView:myView];
  93.     [myWindow makeKeyAndOrderFront:nil];
  94.  
  95.     [NXApp setMainMenu:myMenu];
  96.  
  97.     [myView lockFocus];
  98.  
  99.     /* here is the Mesa call */
  100.     start=time(0);
  101.     render_image();
  102.     end=time(0);
  103.     printf("Rendering took %ld seconds\n",end-start);
  104.     fflush(stdout);
  105.  
  106.     /* draw the bitmap */
  107.     [bitmap draw];
  108.     /* free the bitmap */
  109.     [bitmap free];
  110.     [myWindow flushWindow];
  111.     [myView unlockFocus];
  112.     free( buffer );
  113.  
  114.    /* destroy the context */
  115.     OSMesaDestroyContext( ctx );
  116.  
  117.     [NXApp run];
  118.     [NXApp free];
  119.  
  120.     return 0;
  121. }
  122.