Advertisement
snake5

SGScript / sgs-sdl - 3D rendering

Jun 7th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. include "fmt", "engine/all.sgs";
  2.  
  3.  
  4. global Config =
  5. {
  6.     ModelDir = "../x_dff/",
  7.     TextureDir = "../x_txd/",
  8.     MapFile = "../map.smp",
  9. };
  10.  
  11.  
  12.  
  13. function sg_LoadMesh( filename )
  14. {
  15.     mesh = unserialize( io_file_read( filename ) );
  16.     foreach( submesh : mesh.submeshes )
  17.     {
  18.         submesh.vfmt = make_vertex_format( submesh.format );
  19.         foreach( part : submesh.parts )
  20.         {
  21.             if( part.texture_name != "" )
  22.                 part.texture = create_texture( Config.TextureDir $ part.texture_name $ ".dds" );
  23.             else
  24.                 part.texture = null;
  25.         }
  26.     }
  27.     return mesh;
  28. }
  29.  
  30. function sg_DrawMesh( mesh, sid )
  31. {
  32.     submesh = mesh.submeshes[ sid ];
  33.     {
  34.         foreach( part : submesh.parts )
  35.         {
  36.             draw_packed
  37.             (
  38.                 part.texture,
  39.                 submesh.vfmt,
  40.                 submesh.vertices,
  41.                 part.index_from,
  42.                 part.index_count,
  43.                 GL_TRIANGLE_STRIP,
  44.                 submesh.indices
  45.             );
  46.         }
  47.     }
  48. }
  49.  
  50.  
  51.  
  52. function configure()
  53. {
  54.     global sys_exit;
  55.  
  56.     editor = false;
  57.    
  58.     for( i = 0; i < sys_args.size; ++i )
  59.     {
  60.         var arg = sys_args[ i ];
  61.         if( arg == "-profile" )
  62.         {
  63.             enable_profiler();
  64.             println( "PROFILER ENABLED" );
  65.         }
  66.         else if( arg == "-profile-ops" )
  67.         {
  68.             enable_profiler2();
  69.             println( "PROFILER [mode 2] ENABLED" );
  70.         }
  71.     }
  72. }
  73.  
  74. function initialize()
  75. {
  76.     keys.resize( SDLK_LAST );
  77.    
  78.     set_caption( "The SGS-SDL Game Framework" );
  79.     set_video_mode( 1024, 576, 32, "" );
  80.    
  81.     global UI = UIFrame.create();
  82.    
  83.     println( "Loading map data..." );
  84.     t1 = ftime();
  85.    
  86.     global MAP = unserialize( io_file_read( Config.MapFile ) );
  87.     t2 = ftime();
  88.     foreach( item : MAP )
  89.     {
  90.         item.mesh_obj = sg_LoadMesh( Config.ModelDir $ item.mesh $ ".sme" );
  91.     }
  92.     t3 = ftime();
  93.     println( fmt_text( "Map loaded in {f} seconds, other resources: {f} seconds", t2-t1, t3-t2 ) );
  94.     println( "Number of objects: ", MAP.size );
  95.    
  96.     global MESH = sg_LoadMesh( Config.ModelDir $ "airportgate.sme" );
  97. }
  98.  
  99. global lasttime = ftime();
  100. global rotation = 0;
  101.  
  102. global cMousePos = [0,0];
  103. global pMousePos = [0,0];
  104.  
  105. global keys = [];
  106.  
  107. global Camera =
  108. {
  109.     Pos = [-3,-6,200],
  110.     Angle = 0,
  111.     ZAngle = -1.5,
  112.     Dir = [1,2,-1],
  113.     Up = [0,0,1],
  114. };
  115.  
  116. function Camera.HandleKeys( delta )
  117. {
  118.     global keys;
  119.    
  120.     tspd = delta;
  121.     mspd = delta * 20;
  122.    
  123.     this.Angle -= ( keys[ SDLK_RIGHT ] - keys[ SDLK_LEFT ] ) * tspd;
  124.     this.ZAngle += ( keys[ SDLK_UP ] - keys[ SDLK_DOWN ] ) * tspd;
  125.    
  126.     this.Dir =
  127.     [
  128.         sin( this.Angle ) * cos( this.ZAngle ),
  129.         cos( this.Angle ) * cos( this.ZAngle ),
  130.         sin( this.ZAngle ),
  131.     ];
  132.    
  133.     if( keys[ SDLK_W ] )
  134.     {
  135.         this.Pos[0] += this.Dir[0] * mspd;
  136.         this.Pos[1] += this.Dir[1] * mspd;
  137.         this.Pos[2] += this.Dir[2] * mspd;
  138.     }
  139.     if( keys[ SDLK_S ] )
  140.     {
  141.         this.Pos[0] -= this.Dir[0] * mspd;
  142.         this.Pos[1] -= this.Dir[1] * mspd;
  143.         this.Pos[2] -= this.Dir[2] * mspd;
  144.     }
  145. }
  146.  
  147. function update()
  148. {
  149.     global lasttime, rotation, P;
  150.     var curtime = ftime();
  151.     var delta = curtime - lasttime;
  152.     if( delta > 1.0/15.0 )
  153.         delta = 1.0/15.0;
  154.     lasttime = curtime;
  155.    
  156.     rotation += delta;
  157.    
  158.     var cc = [0.1,0.2,0.4];
  159.     clear( cc );
  160.    
  161.     Camera.HandleKeys( delta );
  162.    
  163.     set_depth_test( true );
  164.     set_camera_3d( Camera.Pos, Camera.Dir, Camera.Up, 1.5, 1024/576, 1, 1000 );
  165.    
  166. //  sg_DrawMesh( MESH, 0 );
  167.    
  168.     foreach( item : MAP )
  169.     {
  170.         matrix_push( item.matrix );
  171.         sg_DrawMesh( item.mesh_obj, 0 );
  172.         matrix_pop();
  173.     }
  174.     set_depth_test( false );
  175.  
  176.     set_camera_ui( 0, 1024, 0, 576 );
  177.    
  178.     UI.draw();
  179.    
  180.     present();
  181.    
  182.     sleeptime = 15 - delta * 1000;
  183.     if( sleeptime > 0 ) sleep( sleeptime );
  184. }
  185.  
  186. function on_event( e )
  187. {
  188.     if( e.type == SDL_QUIT )
  189.         global sys_exit = true;
  190.     else if( e.type == SDL_MOUSEMOTION )
  191.         global cMousePos = [e.x,e.y];
  192.     else if( e.type == SDL_KEYDOWN || e.type == SDL_KEYUP )
  193.     {
  194.         nv = e.type == SDL_KEYDOWN;
  195.         keys[ e.keycode ] = nv;
  196.     }
  197.  
  198.     UI.event( e );
  199. }
  200.  
  201. function cleanup()
  202. {
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement