Guest User

Untitled

a guest
May 26th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.56 KB | None | 0 0
  1. /* renderer.c - graphics renderer
  2.  * SF2DS by whiplash
  3.  */
  4.  
  5. #include <stdio.h>      // c standard library
  6.  
  7. #include <nds.h>        // ds-specific stuff
  8.  
  9. #include "main9.h"      // system variables
  10. #include "enemy.h"      // simple enemy test
  11. #include "renderer.h"
  12.  
  13. renderer_type_t sf2ds_renderer; // outside of this source file, READ ONLY FFS
  14.  
  15. // render the specified mesh (positions are f32s, rotations are s16s [-32767..32768])
  16. void renderer_drawmesh( s32 x, s32 y, s32 z, s16 rx, s16 ry, s16 rz, u8* mesh ) {
  17.     glPushMatrix();
  18.    
  19.     glTranslate3f32( x, y, z );
  20.    
  21.     glRotateXi( rx );
  22.     glRotateYi( ry );
  23.     glRotateZi( rz );
  24.    
  25.     glCallList( (u32*) mesh );
  26.    
  27.     glPopMatrix( 1 );
  28. }
  29.  
  30. // render a square to the framebuffer with the specified location, size and color
  31. void renderer_rendersquare ( u8 x, u8 y, u8 size, u16 color ) {
  32.     int iy, ix;
  33.     for ( iy = y; iy < ( y + size ); iy++ ) {
  34.         for ( ix = x; ix < ( x + size ); ix++ ) {
  35.             VRAM_A[ ( SCREEN_WIDTH * iy ) + ix ] = color;
  36.         }
  37.     }
  38.     iprintf( "RENDERSQ x=%3u y=%3u size=%u\n", x, y, size );
  39. }
  40.  
  41. // clear the framebuffer with this clear color
  42. void renderer_clearfb ( u16 color ) {
  43.     // use DMA to efficiently clear the screen
  44.     dmaFillHalfWords ( color, VRAM_A, SCREEN_WIDTH * SCREEN_HEIGHT * 2 );
  45.     iprintf( "RENDERCLEARFB\n" );
  46. }
  47.  
  48. // initialize the renderer in the specified mode
  49. // returns 0 if sucessful, returns anything else if something goes wrong
  50. int renderer_init ( renderer_type_t type ) {
  51.     if ( type == RENDERER_2D_FB ) {
  52.         powerOn( POWER_ALL );      
  53.         videoSetMode( MODE_FB0 );  
  54.         vramSetBankA( VRAM_A_LCD );
  55.        
  56.         sf2ds_renderer = type;
  57.         return 0;
  58.     }
  59.    
  60.     if ( type == RENDERER_3D ) {
  61.         videoSetMode( MODE_0_3D );
  62.        
  63.         glInit();                       // initialize gl
  64.        
  65.         glEnable( GL_ANTIALIAS );       // enable antialiasing
  66.        
  67.                                         // setup the rear plane
  68.         glClearColor( 0, 0, 0, 31 );    // BG must be opaque for AA to work
  69.         glClearPolyID( 63 );            // BG must have a unique polygon ID for AA to work
  70.         glClearDepth( GL_MAX_DEPTH );
  71.        
  72.         glViewport( 0, 0, 255, 191 );   // this should work the same as the normal gl call
  73.        
  74.        
  75.         gluPerspective( 35, 256.0 / 192.0, 0.1, 80 );
  76.        
  77.         gluLookAt(  0.0, 0.0, 1.0,      // camera possition
  78.                     0.0, 0.0, 0.0,      // look at
  79.                     0.0, 1.0, 0.0 );    // up  
  80.                    
  81.         glLight( 0, RGB15( 7, 7, 15 ), 0, floattov10( -1.0 ), 0 );      //hard-coded lights for now
  82.         glLight( 1, RGB15( 7, 7, 7 ), 0, floattov10( 1 ) - 1, 0 );
  83.         glLight( 2, RGB15( 7, 7, 7 ), floattov10( -1.0 ), 0, 0 );
  84.         glLight( 3, RGB15( 7, 7, 7 ), floattov10( 1.0 ) - 1, 0, 0);
  85.        
  86.         glMaterialf( GL_AMBIENT, RGB15( 8, 8, 8 ) );
  87.         glMaterialf( GL_DIFFUSE, RGB15( 16, 16, 16 ) );
  88.         glMaterialf( GL_SPECULAR, BIT( 15 ) | RGB15( 8, 8, 8 ) );
  89.         glMaterialf( GL_EMISSION, RGB15( 5, 5, 5 ) );
  90.  
  91.         glMaterialShinyness();
  92.        
  93.         glMatrixMode( GL_TEXTURE );
  94.         glLoadIdentity();
  95.        
  96.         glPolyFmt( POLY_ALPHA( 31 ) | POLY_CULL_BACK | POLY_FORMAT_LIGHT0 | POLY_FORMAT_LIGHT1 | POLY_FORMAT_LIGHT2 | POLY_FORMAT_LIGHT3 );
  97.        
  98.         glMatrixMode( GL_PROJECTION );  // any floating point gl call is being converted to fixed prior to being implemented
  99.         glLoadIdentity();
  100.                
  101.         glMatrixMode( GL_MODELVIEW );
  102.         glLoadIdentity();
  103.        
  104.         return 0;
  105.     }
  106.     else return 1; // someone made an oops 
  107. }
  108.  
  109. // to be called every frame, renders all game objects
  110. void sf2ds_render ( void ) {
  111.     iprintf( "RENDER BEGIN\n" );
  112.    
  113.     if ( sf2ds_renderer == RENDERER_2D_FB ) {
  114.         renderer_clearfb( RGB15( 31, 31, 31 ) );
  115.     }
  116.    
  117.     enemy_render( &enemies[0] );
  118.     //enemy_render( &enemies[1] );
  119.    
  120.     if ( sf2ds_renderer == RENDERER_3D ) {
  121.         glFlush( 0 );
  122.     }
  123.    
  124.     iprintf( "RENDER END\n" );
  125.    
  126. }
Add Comment
Please, Sign In to add comment