Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* renderer.c - graphics renderer
- * SF2DS by whiplash
- */
- #include <stdio.h> // c standard library
- #include <nds.h> // ds-specific stuff
- #include "main9.h" // system variables
- #include "enemy.h" // simple enemy test
- #include "renderer.h"
- renderer_type_t sf2ds_renderer; // outside of this source file, READ ONLY FFS
- // render the specified mesh (positions are f32s, rotations are s16s [-32767..32768])
- void renderer_drawmesh( s32 x, s32 y, s32 z, s16 rx, s16 ry, s16 rz, u8* mesh ) {
- glPushMatrix();
- glTranslate3f32( x, y, z );
- glRotateXi( rx );
- glRotateYi( ry );
- glRotateZi( rz );
- glCallList( (u32*) mesh );
- glPopMatrix( 1 );
- }
- // render a square to the framebuffer with the specified location, size and color
- void renderer_rendersquare ( u8 x, u8 y, u8 size, u16 color ) {
- int iy, ix;
- for ( iy = y; iy < ( y + size ); iy++ ) {
- for ( ix = x; ix < ( x + size ); ix++ ) {
- VRAM_A[ ( SCREEN_WIDTH * iy ) + ix ] = color;
- }
- }
- iprintf( "RENDERSQ x=%3u y=%3u size=%u\n", x, y, size );
- }
- // clear the framebuffer with this clear color
- void renderer_clearfb ( u16 color ) {
- // use DMA to efficiently clear the screen
- dmaFillHalfWords ( color, VRAM_A, SCREEN_WIDTH * SCREEN_HEIGHT * 2 );
- iprintf( "RENDERCLEARFB\n" );
- }
- // initialize the renderer in the specified mode
- // returns 0 if sucessful, returns anything else if something goes wrong
- int renderer_init ( renderer_type_t type ) {
- if ( type == RENDERER_2D_FB ) {
- powerOn( POWER_ALL );
- videoSetMode( MODE_FB0 );
- vramSetBankA( VRAM_A_LCD );
- sf2ds_renderer = type;
- return 0;
- }
- if ( type == RENDERER_3D ) {
- videoSetMode( MODE_0_3D );
- glInit(); // initialize gl
- glEnable( GL_ANTIALIAS ); // enable antialiasing
- // setup the rear plane
- glClearColor( 0, 0, 0, 31 ); // BG must be opaque for AA to work
- glClearPolyID( 63 ); // BG must have a unique polygon ID for AA to work
- glClearDepth( GL_MAX_DEPTH );
- glViewport( 0, 0, 255, 191 ); // this should work the same as the normal gl call
- gluPerspective( 35, 256.0 / 192.0, 0.1, 80 );
- gluLookAt( 0.0, 0.0, 1.0, // camera possition
- 0.0, 0.0, 0.0, // look at
- 0.0, 1.0, 0.0 ); // up
- glLight( 0, RGB15( 7, 7, 15 ), 0, floattov10( -1.0 ), 0 ); //hard-coded lights for now
- glLight( 1, RGB15( 7, 7, 7 ), 0, floattov10( 1 ) - 1, 0 );
- glLight( 2, RGB15( 7, 7, 7 ), floattov10( -1.0 ), 0, 0 );
- glLight( 3, RGB15( 7, 7, 7 ), floattov10( 1.0 ) - 1, 0, 0);
- glMaterialf( GL_AMBIENT, RGB15( 8, 8, 8 ) );
- glMaterialf( GL_DIFFUSE, RGB15( 16, 16, 16 ) );
- glMaterialf( GL_SPECULAR, BIT( 15 ) | RGB15( 8, 8, 8 ) );
- glMaterialf( GL_EMISSION, RGB15( 5, 5, 5 ) );
- glMaterialShinyness();
- glMatrixMode( GL_TEXTURE );
- glLoadIdentity();
- glPolyFmt( POLY_ALPHA( 31 ) | POLY_CULL_BACK | POLY_FORMAT_LIGHT0 | POLY_FORMAT_LIGHT1 | POLY_FORMAT_LIGHT2 | POLY_FORMAT_LIGHT3 );
- glMatrixMode( GL_PROJECTION ); // any floating point gl call is being converted to fixed prior to being implemented
- glLoadIdentity();
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- return 0;
- }
- else return 1; // someone made an oops
- }
- // to be called every frame, renders all game objects
- void sf2ds_render ( void ) {
- iprintf( "RENDER BEGIN\n" );
- if ( sf2ds_renderer == RENDERER_2D_FB ) {
- renderer_clearfb( RGB15( 31, 31, 31 ) );
- }
- enemy_render( &enemies[0] );
- //enemy_render( &enemies[1] );
- if ( sf2ds_renderer == RENDERER_3D ) {
- glFlush( 0 );
- }
- iprintf( "RENDER END\n" );
- }
Add Comment
Please, Sign In to add comment