Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include "fmt", "engine/all.sgs";
- global Config =
- {
- ModelDir = "../x_dff/",
- TextureDir = "../x_txd/",
- MapFile = "../map.smp",
- };
- function sg_LoadMesh( filename )
- {
- mesh = unserialize( io_file_read( filename ) );
- foreach( submesh : mesh.submeshes )
- {
- submesh.vfmt = make_vertex_format( submesh.format );
- foreach( part : submesh.parts )
- {
- if( part.texture_name != "" )
- part.texture = create_texture( Config.TextureDir $ part.texture_name $ ".dds" );
- else
- part.texture = null;
- }
- }
- return mesh;
- }
- function sg_DrawMesh( mesh, sid )
- {
- submesh = mesh.submeshes[ sid ];
- {
- foreach( part : submesh.parts )
- {
- draw_packed
- (
- part.texture,
- submesh.vfmt,
- submesh.vertices,
- part.index_from,
- part.index_count,
- GL_TRIANGLE_STRIP,
- submesh.indices
- );
- }
- }
- }
- function configure()
- {
- global sys_exit;
- editor = false;
- for( i = 0; i < sys_args.size; ++i )
- {
- var arg = sys_args[ i ];
- if( arg == "-profile" )
- {
- enable_profiler();
- println( "PROFILER ENABLED" );
- }
- else if( arg == "-profile-ops" )
- {
- enable_profiler2();
- println( "PROFILER [mode 2] ENABLED" );
- }
- }
- }
- function initialize()
- {
- keys.resize( SDLK_LAST );
- set_caption( "The SGS-SDL Game Framework" );
- set_video_mode( 1024, 576, 32, "" );
- global UI = UIFrame.create();
- println( "Loading map data..." );
- t1 = ftime();
- global MAP = unserialize( io_file_read( Config.MapFile ) );
- t2 = ftime();
- foreach( item : MAP )
- {
- item.mesh_obj = sg_LoadMesh( Config.ModelDir $ item.mesh $ ".sme" );
- }
- t3 = ftime();
- println( fmt_text( "Map loaded in {f} seconds, other resources: {f} seconds", t2-t1, t3-t2 ) );
- println( "Number of objects: ", MAP.size );
- global MESH = sg_LoadMesh( Config.ModelDir $ "airportgate.sme" );
- }
- global lasttime = ftime();
- global rotation = 0;
- global cMousePos = [0,0];
- global pMousePos = [0,0];
- global keys = [];
- global Camera =
- {
- Pos = [-3,-6,200],
- Angle = 0,
- ZAngle = -1.5,
- Dir = [1,2,-1],
- Up = [0,0,1],
- };
- function Camera.HandleKeys( delta )
- {
- global keys;
- tspd = delta;
- mspd = delta * 20;
- this.Angle -= ( keys[ SDLK_RIGHT ] - keys[ SDLK_LEFT ] ) * tspd;
- this.ZAngle += ( keys[ SDLK_UP ] - keys[ SDLK_DOWN ] ) * tspd;
- this.Dir =
- [
- sin( this.Angle ) * cos( this.ZAngle ),
- cos( this.Angle ) * cos( this.ZAngle ),
- sin( this.ZAngle ),
- ];
- if( keys[ SDLK_W ] )
- {
- this.Pos[0] += this.Dir[0] * mspd;
- this.Pos[1] += this.Dir[1] * mspd;
- this.Pos[2] += this.Dir[2] * mspd;
- }
- if( keys[ SDLK_S ] )
- {
- this.Pos[0] -= this.Dir[0] * mspd;
- this.Pos[1] -= this.Dir[1] * mspd;
- this.Pos[2] -= this.Dir[2] * mspd;
- }
- }
- function update()
- {
- global lasttime, rotation, P;
- var curtime = ftime();
- var delta = curtime - lasttime;
- if( delta > 1.0/15.0 )
- delta = 1.0/15.0;
- lasttime = curtime;
- rotation += delta;
- var cc = [0.1,0.2,0.4];
- clear( cc );
- Camera.HandleKeys( delta );
- set_depth_test( true );
- set_camera_3d( Camera.Pos, Camera.Dir, Camera.Up, 1.5, 1024/576, 1, 1000 );
- // sg_DrawMesh( MESH, 0 );
- foreach( item : MAP )
- {
- matrix_push( item.matrix );
- sg_DrawMesh( item.mesh_obj, 0 );
- matrix_pop();
- }
- set_depth_test( false );
- set_camera_ui( 0, 1024, 0, 576 );
- UI.draw();
- present();
- sleeptime = 15 - delta * 1000;
- if( sleeptime > 0 ) sleep( sleeptime );
- }
- function on_event( e )
- {
- if( e.type == SDL_QUIT )
- global sys_exit = true;
- else if( e.type == SDL_MOUSEMOTION )
- global cMousePos = [e.x,e.y];
- else if( e.type == SDL_KEYDOWN || e.type == SDL_KEYUP )
- {
- nv = e.type == SDL_KEYDOWN;
- keys[ e.keycode ] = nv;
- }
- UI.event( e );
- }
- function cleanup()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement