Esempio n. 1
0
 //! A basic drawing function; draw if visible.
 virtual void draw ()
 {
   if (m_visible)
     {
       draw_obj ();
     }
 }
Esempio n. 2
0
void draw ()
{
    draw_backdrop_color ();
    if (eng_is_blank())
        goto end_draw;
    
    for (int priority = PRIORITY_COUNT - 1; priority >= 0; priority --)
    {
        for (int layer = BG_SUB_3; layer >= BG_MAIN_0; layer --)
        {
            if (bg_is_shown (layer) && bg_get_priority (layer) == priority)
                draw_background_layer (layer);
        }
        for (int id = MAIN_OBJ_COUNT + SUB_OBJ_COUNT - 1; id >= 0; id --)
        {
            if (obj_is_shown (id) && obj_get_priority (id) == priority)
                draw_obj (id);
        }
    }
    
 end_draw:

    if (console_is_visible ())
      draw_console_layer ();
    xcairo_surface_mark_dirty (main_screen_surface);
    xcairo_surface_mark_dirty (sub_screen_surface);
}
Esempio n. 3
0
void __draw_hands()
{
    if( game_gui_get_state() != STATE_PC && game_gui_get_state() != STATE_MAIN_MENU )
    {
        if( !key_tab )
            draw_obj( __GUIState_singleton->interact_icon );
    }
}
Esempio n. 4
0
void wall::draw(){
	glPushMatrix();
	glTranslatef(x,0.0,z);
	glRotatef(r,0,1,0);
	glScalef(thick * 2 + 0.5,10.0,l + thick * 2);
	set_mtl(mtl_i);
	draw_obj();
	pop_mtl();
	glPopMatrix();
}
Esempio n. 5
0
void	put_rain(t_mod *mod)
{
	mod->check = 1;
	#ifdef FT
		clear_img(mod, mod->img2);
	#endif
	#ifdef HOME
		clear_img(mod, mod->img2);
		clear_img(mod, mod->img1);
	#endif
// 	particules handling
	if (mod->st_lm)
		set_lm_rain(mod, mod->rain->l_mesh);
	if (mod->upd_sm)
	{
		if (mod->plane && mod->plane->plan)
		{
			set_gbase_trans(mod, mod->plane->plan);
			update_sm(mod, mod->plane->plan);
		}
		set_gbase_trans(mod, mod->rain);
		update_sm(mod, mod->rain);
	}
	#ifdef FT
		draw_obj(mod, mod->rain, mod->img2);
	#endif
	#ifdef HOME
		draw_obj(mod, mod->rain, mod->img1);
 		if (mod->plane && mod->plane->plan)
 			draw_obj(mod, mod->plane->plan, mod->img1);
	#endif
	if (mod->st_lm)
	{
		if (!mod->check)
		{
			mod->st_lm = 0;
			mod->upd_sm = 0;
		}
	}
}
Esempio n. 6
0
File: obj.c Progetto: cjxgm/clabs
void render(void)
{
	unsigned int time = glutGet(GLUT_ELAPSED_TIME);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix(); {
		camApply();
		glTranslatef(0, 0, -5);
		glRotatef(90, 1, 0, 0);
		draw_obj(go);
	} glPopMatrix();

	// draw fps
	char buf[128];
	sprintf(buf, "fps: %.2f", fps);
	glColor3f(0.8, 0, 0);
	renderString(10, 10, buf, NULL);
	// camera matrix
	{
		int y, x;
		for (y=0; y<4; y++)
			for (x=0; x<4; x++) {
				sprintf(buf, "%+.3f", camera[y*4+x]);
				renderString(10 + x * 80, 180 - y*20, buf, NULL);
			}
	}

	float pos[3];
	camGetPosition(pos);
	sprintf(buf, "%+.3f %+.3f %+.3f", pos[0], pos[1], pos[2]);
	renderString(10, 40, buf, NULL);

	glutSwapBuffers();

	if (key[27]) exit(0);

	camBegin();
	if (key['w']) camForward(+4 / fps);
	if (key['s']) camForward(-4 / fps);
	if (key['d']) camTurn(+90 / fps);
	if (key['a']) camTurn(-90 / fps);
	if (key['q']) camPan(+4 / fps);
	if (key['e']) camPan(-4 / fps);
	camEnd();

	fps = 1000.0 / (glutGet(GLUT_ELAPSED_TIME) - time);
}
Esempio n. 7
0
void draw (void)
{
    backdrop_draw(main_screen_context);
    
    if (!eng_is_blank())
    {
        for (int priority = PRIORITY_COUNT - 1; priority >= 0; priority --)
        {
            bg_draw_zlevel(main_screen_context, priority);
            for (int id = MAIN_OBJ_COUNT - 1; id >= 0; id --)
            {
                if (obj_is_shown (id) && obj_get_priority (id) == priority)
                    draw_obj (id);
            }
        }
    }        
    draw_console_layer();

    xcairo_surface_mark_dirty (main_screen_surface);
}
Esempio n. 8
0
void	real_rain(t_mod *mod)
{
	if (mod->pt_surf)
	{
		clear_img(mod, mod->img1);
		tab_zero(mod->img_tab);
		if (mod->upd_sm)
			update_sm(mod, mod->obj);
		draw_obj(mod, mod->obj, mod->img1);
		mod->pt_surf = 0;
	}
	put_rain(mod);
	mod->ind_h++;
	#ifdef FT
		on_window(mod, 1, 1, 0);
	#endif
	#ifdef HOME
		on_window(mod, 1, 0, 0);
	#endif
}
void display() {
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   //  Enable Z-buffering in OpenGL
   glEnable(GL_DEPTH_TEST);
   //  Undo previous transformations
   glLoadIdentity();

   double Ex = -2*dim*Sin(th)*Cos(ph);
   double Ey = +2*dim        *Sin(ph);
   double Ez = +2*dim*Cos(th)*Cos(ph);

   gluLookAt(Ex+movex,Ey+movey,Ez+movez, movex,movey,movez, 0,Cos(ph),0);

   glShadeModel(smooth ? GL_SMOOTH : GL_FLAT);

   //  Translate intensity to color vectors
   ambient = 50;
   float Ambient[]   = {0.01*ambient ,0.01*ambient ,0.01*ambient ,1.0};
   float Diffuse[]   = {0.01*diffuse ,0.01*diffuse ,0.01*diffuse ,1.0};
   float Specular[]  = {0.01*specular,0.01*specular,0.01*specular,1.0};

   //  Light position
   float Position[]  = {distance*Cos(zh),ylight,distance*Sin(zh),1.0};
   // Draw light position as ball (still no lighting here)
   glColor3f(5,5,0);
   glowy_ball(Ambient, Diffuse, Specular, Position, 1, GL_LIGHT0, 0, NULL);

   //draw a cube
   //draw_obj(8, 24, 14, 6, "goddamncube.obj");
   //int num_vertices, int num_normals, int num_tex, int num_faces, char *filename
   // draw_obj(326, 365, 365, 646, "voyagertex.obj");
   draw_obj(1090, 1129, 1403, 2166, "voyagereng.obj");
   // draw_obj(8, 24, 14, 12, "cubetri.obj");
   // cube();

   //  Render the scene
   glFlush();
   //  Make the rendered scene visible
   glutSwapBuffers();
   //glDisable(GL_LIGHTING);
}
Esempio n. 10
0
void display() {
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   //  Enable Z-buffering in OpenGL
   glEnable(GL_DEPTH_TEST);
   //  Undo previous transformations
   glLoadIdentity();

   double Ex = -2*dim*Sin(th)*Cos(ph);
   double Ey = +2*dim        *Sin(ph);
   double Ez = +2*dim*Cos(th)*Cos(ph);

   gluLookAt(Ex+xdist,Ey+ydist,Ez+zdist, 0,0,0, 0,Cos(ph),0);
   // gluLookAt(Ex,Ey,Ez, 0,0,0, 0,Cos(ph),0);

   glShadeModel(smooth ? GL_SMOOTH : GL_FLAT);

   //  Translate intensity to color vectors
   ambient = 50;
   float Ambient[]   = {0.01*ambient ,0.01*ambient ,0.01*ambient ,1.0};
   float Diffuse[]   = {0.01*diffuse ,0.01*diffuse ,0.01*diffuse ,1.0};
   float Specular[]  = {0.01*specular,0.01*specular,0.01*specular,1.0};

   //draw_sky();

   //  Light position
   float Position[]  = {movex,movey,movez,1};
   // Draw light position as ball (still no lighting here)

   // draw the ship
   //int num_faces, double *vertices, double *normals, double *texs, int *faces

   if (ship == 0) {
      draw_obj(0, 0, 0, num_faces_voyager, voyager_vertices, voyager_normals, voyager_texs, voyager_faces, 0.6);
   } else if (ship == 1) {
      draw_obj(0, 0, 0, num_faces_renegade, renegade_vertices, renegade_normals, renegade_texs, renegade_faces, 0.6);
   } else if (ship == 2) {
      draw_obj(0, 0, 0, num_faces_phantom, phantom_vertices, phantom_normals, phantom_texs, phantom_faces, 0.6);
   }

   glPopMatrix();
   glPushMatrix();

   if (movex > 0) {
      glRotatef(orbit, 0, movex, 0);
   } else if (movex < 0) {
      glRotatef(orbit, 0, 0 - movex, 0);
   } else {
      glRotatef(orbit, 0, 1, 0);
   }

   orbit += 0.1;
   if (orbit >= 360.0) {
      orbit = 0.0;
   }

   glColor3f(1,1,1);
   glowy_ball(Ambient, Diffuse, Specular, Position, 6, GL_LIGHT0, 0, NULL, texture[5]);

   glColor3f(1, 1, 1);
   sphere(movex+10,movey,movez+15 , 2, texture[6]);
   sphere(movex-12,movey,movez-16 , 3, texture[8]);
   sphere(movex-3,movey,movez+19 , 3, texture[7]);
   sphere(movex+7,movey,movez-20 , 3, texture[0]);

   sphere(movex-8,movey,movez-31 , 6, texture[9]);
   sphere(movex-28,movey,movez + 30, 6, texture[10]);
   sphere(movex+34,movey,movez + 28 , 6, texture[11]);

   draw_obj(movex + 15, movey+2, movez - 24, num_faces_asteroid, asteroid_1_vertices, asteroid_1_normals, asteroid_1_texs, asteroid_1_faces, 3);
   draw_obj(movex - 12, movey-2, movez + 22, num_faces_asteroid, asteroid_1_vertices, asteroid_1_normals, asteroid_1_texs, asteroid_1_faces, 5);
   draw_obj(movex + 22, movey, movez + 28, num_faces_asteroid, asteroid_1_vertices, asteroid_1_normals, asteroid_1_texs, asteroid_1_faces, 3);
   draw_obj(movex - 6, movey+2, movez - 22, num_faces_asteroid, asteroid_1_vertices, asteroid_1_normals, asteroid_1_texs, asteroid_1_faces, 5);
   draw_obj(movex - 2, movey-2, movez + 26, num_faces_asteroid, asteroid_1_vertices, asteroid_1_normals, asteroid_1_texs, asteroid_1_faces, 4);

   draw_obj(movex + 18, movey+2, movez + 3, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 3);
   draw_obj(movex - 16, movey-2, movez - 8, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 5);
   draw_obj(movex + 4, movey+2, movez + 15, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 3);
   draw_obj(movex + 18, movey-2, movez - 12, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 4);

   draw_obj(movex + 24, movey+2, movez + 15, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 3);
   draw_obj(movex - 8, movey-2, movez - 12, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 5);
   draw_obj(movex + 16, movey+2, movez + 3, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 3);
   draw_obj(movex + 9, movey-2, movez - 8, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 4);

   draw_obj(movex - 24, movey-2, movez - 15, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 3);
   draw_obj(movex + 8, movey+2, movez + 12, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 5);
   draw_obj(movex - 16, movey-2, movez - 3, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 3);
   draw_obj(movex - 9, movey+2, movez + 8, num_faces_asteroid, asteroid_2_vertices, asteroid_2_normals, asteroid_2_texs, asteroid_2_faces, 4);

   draw_obj(movex - 23, movey + 3, movez + 9, num_faces_asteroid, asteroid_city_vertices, asteroid_city_normals, asteroid_city_texs, asteroid_city_faces, 10);
   draw_obj(movex - 23, movey + 3, movez + 9, num_faces_city, city_vertices, city_normals, city_texs, city_faces, 10);


   glPopMatrix();
   glPushMatrix();
   cube(Ex+xdist, Ey+ydist,  Ez+zdist, 20);

   glPopMatrix();
   glPushMatrix();

   if (movex > 0) {
      glRotatef(orbit, 0, movex, 0);
   } else if (movex < 0) {
      glRotatef(orbit, 0, 0 - movex, 0);
   } else {
      glRotatef(orbit, 0, 1, 0);
   }

   glShadeModel(GL_SMOOTH);
   glEnable(GL_BLEND);                
   glBlendFunc(GL_SRC_ALPHA,GL_ONE);                   
   glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);           
   glHint(GL_POINT_SMOOTH_HINT,GL_NICEST); 
   glColor4f(1, 1, 1,0.2);
   sphere(movex-8,movey,movez-31 , 7.5, texture[9]);
   sphere(movex-28,movey,movez+30 , 7.5, texture[10]);
   sphere(movex+34,movey,movez + 28 , 7.5, texture[11]);
   sphere(movex - 23, movey + 4.5, movez + 9, 2.2, 0);
   glDisable(GL_BLEND);
   glEnable(GL_DEPTH_TEST);

   draw_atmosphere(movex+10, movey, movez+15, 3, 1, 1, 1);
   draw_atmosphere(movex-12, movey, movez-16, 4, 1, 0.6, 0.8);
   draw_atmosphere(movex-3, movey, movez+19, 4, 0.5, 0.5, 1);
   draw_atmosphere(movex+7, movey, movez-20, 4, 0.5, 0.5, 1);
   draw_atmosphere(movex-8, movey, movez-31, 9, 1, 1, 0);
   draw_atmosphere(movex-28, movey, movez+30, 9, 0.5, 0.5, 0.5);
   draw_atmosphere(movex+34, movey, movez+28, 9, 0, 1, 1);

   draw_particles(sun_particle, MAX_SUN_PARTICLES, sun_slowdown, sun_xspeed, sun_yspeed, movex/2, movey/2, movez/2, 1, 1, 0, 1.0, 2);

   glPopMatrix();
   draw_particles(ship_particle, MAX_SHIP_PARTICLES, ship_slowdown, ship_xspeed, ship_yspeed, 0, 0, 0, 1, 1, 1, 1.0, 2);

   //  Render the scene
   glFlush();
   //  Make the rendered scene visible
   glutSwapBuffers();
}
Esempio n. 11
0
void character::draw(){
	if(health <= 0) return;

    int ori_use_sphere = get_obj_type();

	int ori_color_i = getColorI();
	setColorI(color_i);

	sun_mode(false);

    glPushMatrix();

    switch(jumpping)
    {
    case 1:
        y += 0.1;
        if(y >= 1) jumpping = 2;
        break;
    case 2:
        y -= 0.1;
        if(y <= 0) jumpping = 0;
        break;
    }

    glTranslatef(x,y,z);
    glRotatef (-r, 0.0, 1.0, 0.0);

	glPushMatrix(); // health bar
    glTranslatef(0.0,3.0,0.0);
    glScalef (health / 100.0, 0.1,0.1);
	sun_mode(true);
    glutSolidCube(1.0);
    glPopMatrix();
	sun_mode(false);

    set_obj_type(is_sphere);

    glPushMatrix(); // body
    glScalef (1.6, 3.0, 0.75);
	nextColor();
    draw_obj();
    glPopMatrix();

    glPushMatrix(); // head
    glTranslatef(0.0,2.0,0.0);
	nextColor();
    draw_obj();
    glPopMatrix();

    glPushMatrix(); // right foot
    glTranslatef(0.8,-1.5,0.0);
	nextColor();
    draw_obj();
    glPopMatrix();

    glPushMatrix(); // left foot
    glTranslatef(-0.8,-1.5,0.0);
	nextColor();
    draw_obj();
    glPopMatrix();

    glPushMatrix(); // right hand
    glTranslatef(0.8,1.0,0.0);
    glScalef (0.4, 0.4, 0.4);
    glRotatef (75, 0.0, 0.0, 1.0);
    glRotatef (90, 0.0, 1.0, 0.0);
    right->draw();
    glPopMatrix();

    glPushMatrix(); // left hand
    glTranslatef(-0.8,1.0,0.0);
    glScalef (-0.4, 0.4, 0.4);
    glRotatef (75, 0.0, 0.0, 1.0);
    glRotatef (90, 0.0, 1.0, 0.0);
    left->draw();
    glPopMatrix();

    glPopMatrix();

	setColorI(ori_color_i);
    set_obj_type(ori_use_sphere);
}
Esempio n. 12
0
/*
 * int main(int argc, char **argl)
 *
 * GiN entry point. Initialize variables, structs, video devices, parsing
 * configuration files, setting up parameters, booting up subsystems,
 * and handling the main game loop, as well as cleaning up.
 */
int main(int argc, char **argl)
{
	const char *__err_launcher = "Please run the game through the launcher!";
	const char *__err_language = "Language list cannot be found! Run generate_language_list.exe to let the language definition be generated once.";
	const char *__wrn_lowres   = "The game requires a monitor with a resolution of at least 1,024 x 768. The game will still run, but certain GUI elements won't be displayed correctly.";
	const char *__wrn_lowram   = "You need at least 3 GB RAM in order to play the game. If you're having less than 3 GB RAM, clicking \"OK\" will force the game to run, but it may causes unexpected issues. If you want to quit now, open Task Manager and close the game process.\n\nSorry for the inconvenience.";
	const char *__wrn_redist   = "One or more required DLLs for music playback cannot be found.\n\nOn Steam, try verifying game files and make sure you've installed the redistributable files before launching the game.\n\nitch.io users can launch the game through Launcher.bat.";

	// See if the game was launched through the Go-based launcher.
	#ifndef    DEBUG
		// ASSERT(game_locker_check() != 0, __err_launcher);
	#endif

	if( sys_metrics(0) < 1024.0 && sys_metrics(1) < 768.0 )
		printf(__wrn_lowres);

	if( os_get_ram(S_MB) < 3072 )
		printf(__wrn_lowram);

	String *temp = "";
	str_cat(temp, os_get_system_directory());
	str_cat(temp, "ogg.dll");

	if( !file_exists(temp) )
	{
		printf(__wrn_redist);
	}

	#ifndef    DEBUG
		ASSERT(file_exists("./translation/__language.pad") != 0, __err_language);
	#else
		if( !file_exists("./translation/__language.pad") )
		{
			printf(__err_language);

			sys_exit((void *) 0);
		}
	#endif

	// Overrides some of the global variables (their default values are way too low)
	game_globals_set();

	// mouse_lock(true);

	// Wait for the video device.
	while( !ready() )
		wait(1.0);

	// After the video device is initialized, parse the video configuration file
	// and apply video settings to the current video device, and set the new game title.
	game_video_cfg_parse();
	game_title_set();

	// Exports the game version to the game folder. This is used by the external launcher
	// which uses the file for updating purposes. The completed, released version should
	// have a versioning file ready already.
	__game_version_export();

	// Pops up the GUI to let the user chooses their desired language.
	// After that, a call to nov_region_init() will be invoked to pick the chosen language and perform string initialization.
	nov_region_init("en");

	// Initializes subsystems (game state, GUI, PhysX, ...)
	nov_modules_init();

	// Static initialization.
	nov_gui_static_init();

	// Initializes the scene list, the scenes, and push them into the list.
	nov_scene_list_init();

	game_gui_set_state(STATE_INTRO);
	game_gui_render();

	Vector3 tx;
	tx.x = screen_size.x - 150.0;
	tx.y = 15.0;
	tx.z = 0;

	Text *todo = txt_create(1, LAYER_DEBUG_1);
	todo->font = font_create("UVN remind#30b");

	gui_text_set_color(todo, COLOR_DARK_GREY);
	gui_text_set_pos(todo, __GUIState_singleton->todo_texture->pos_x + 10.0, __GUIState_singleton->todo_texture->pos_y + 10.0);
	gui_text_set_pos(txtSubtitleHandler, (screen_size.x / 2.0) - 156.0, __GUIState_singleton->todo_texture->pos_y + bmap_height(__GUIState_singleton->todo_texture->bmap) + 25.0);

	// Main game loop, which can be terminated with the "Alt + F4" key.
	while(!(__GameState_singleton->exit_switch))
	{
		__GameState_singleton->exit_switch = key_alt && key_f4;

		// Updates PhysX.
		game_physx_loop();

		// Updates the GUI state.
		game_gui_update();

		// Continuously update the area the player is currently in.
		#ifndef    UI_LESS
			if( STATE_NULL == game_gui_get_state() && game_intro_done ) // Because draw_*() functions don't take layers into account,
			                                                            // I have to rely a lot on GUI states in order to manage rendering orders of GUI objects.
			{
				draw_text(game_region_check(), tx.x, tx.y, COLOR_WHITE);
			}
		#endif

		// Updates FOV.
		if( NULL != camera )
		{
			if( !var_cmp(fov, camera->arc) )
				camera->arc = fov;
		}

		// Updates the global volume.
		if( !var_cmp(volume, game_volume) )
			game_volume = volume;

		// Updates the mouse sensivity.
		mickey.x *= sensivity_mul;
		mickey.y *= sensivity_mul;

		if( key_f1 )
		{
			while ( key_f1 ) wait(1.0);
				switch( __invert_y )
				{
					case    1: __invert_y = -1; break;
					case   -1: __invert_y = 1;
				}
		}

		if( key_tab && (STATE_NULL == game_gui_get_state()) )
		{
			switch ( game_day_get() )
			{
				case DAY_3:
				case DAY_4:
				case DAY_5:
				case DAY_6:
				{
					if( (todo->pstring)[0] != lstr_todo_c3 )
						(todo->pstring)[0] = lstr_todo_c3;
					break;
				}

				case DAY_1:
				{
					draw_obj( __GUIState_singleton->todo_texture );

					if( (todo->pstring)[0] != lstr_todo_c1 )
						(todo->pstring)[0] = lstr_todo_c1;
					break;
				}

				case DAY_2:
				{
					draw_obj( __GUIState_singleton->todo_texture );

					if( (todo->pstring)[0] != lstr_todo_c2 )
						(todo->pstring)[0] = lstr_todo_c2;
				}
			}

			draw_obj( todo );
		}

		if( key_esc && __can_press_esc && (game_gui_get_state() != STATE_ENDING) )
		{
			while(key_esc) wait(1.0);

			wait(1.0);

			__GameState_singleton->menu_switch = 1 - __GameState_singleton->menu_switch;

			switch(__GameState_singleton->menu_switch)
			{
				case    1:    game_gui_set_state(STATE_MAIN_MENU); game_gui_render(); break;
				case    0:    game_gui_set_state(STATE_NULL);      game_gui_render();
			}
		}

		wait(1.0);
	}

	RETURN(0);
}
Esempio n. 13
0
int main()
{
  SDL_Surface* background, *background2, *background3, *background4;
  SDL_Rect src, dest;
  int frames;  
  Uint32 colorkey;


  SDL_AudioSpec desired, obtained;
  sound_t bus_sound;

  /*initialize video and sound subsystem*/
  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0){
    printf("Unable to initialize video: %s\n.", SDL_GetError());
    return 1;
  }

  /*ensure all subsystems exit safely*/
  atexit(SDL_Quit);
  atexit(SDL_CloseAudio);

  /*set video mode*/
  screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF | SDL_HWSURFACE);
  if (screen == NULL) {
    printf("Unable to set video mode: %s\n", SDL_GetError());
    return 1;
  }

  /*load background images*/
  background = SDL_LoadBMP("img/src.bmp");
  if (background == NULL) {
    printf("Unable to load image.");
    return 1;
  }

  background2 = SDL_LoadBMP("img/background2.bmp");
  if (background2 == NULL) {
    printf("Unable to load background2.");
    return 1;
  }

  background3 = SDL_LoadBMP("img/background3.bmp");
  if (background3 == NULL) {
    printf("Unable to load background3.");
    return 1;
  }

  background4 = SDL_LoadBMP("img/background4.bmp");
  if (background4 == NULL) {
    printf("Unable to load background4.");
    return 1;
  }

  /*load bus*/
  bus = SDL_LoadBMP("img/bus.bmp");
  if (bus == NULL) {
    printf("Unable to load image.");
    return 1;
  }
  colorkey = SDL_MapRGB(bus->format, 255, 255, 255);

  /*set color key*/
  SDL_SetColorKey(bus,
		  SDL_SRCCOLORKEY,
		  colorkey);
  
  /*load man*/
  man = SDL_LoadBMP("img/man.bmp");
  if (man == NULL) {
    printf("Unable to load image");
    return 1;
  }

  colorkey = SDL_MapRGB(man->format, 255, 255, 255);

  /*set color key*/
  SDL_SetColorKey(man,
		  SDL_SRCCOLORKEY,
		  colorkey);

  man2 = SDL_LoadBMP("img/man2.bmp");
  if (man2 == NULL) {
    printf("Unable to load image");
    return 1;
  }

  colorkey = SDL_MapRGB(man2->format, 255, 255, 255);

  /*set color key*/
  SDL_SetColorKey(man2,
		  SDL_SRCCOLORKEY,
		  colorkey);

  car1 = SDL_LoadBMP("img/car1.bmp");
  if (car1 == NULL) {
    printf("Unable to load image");
    return 1;
  }

  colorkey = SDL_MapRGB(car1->format, 255, 255, 255);

  /*set color key*/
  SDL_SetColorKey(car1,
		  SDL_SRCCOLORKEY,
		  colorkey);


  car2 = SDL_LoadBMP("img/car2.bmp");
  if (car2 == NULL) {
    printf("Unable to load image");
    return 1;
  }

  colorkey = SDL_MapRGB(car2->format, 255, 255, 255);

  /*set color key*/
  SDL_SetColorKey(car2,
		  SDL_SRCCOLORKEY,
		  colorkey);

  
  //open audio device
  desired.freq = 44100;
  desired.format = AUDIO_S16;
  desired.samples = 4096;
  desired.channels = 2;
  desired.callback = AudioCallback;
  desired.userdata = NULL;

  if (SDL_OpenAudio(&desired, &obtained) < 0) {
    printf("Unable to open audio device: %s\n", SDL_GetError());
    return 1;
  }

  //load sound files and convert them to sound card's format
  if (LoadAndConvertSound("audio/bus-pass.wav", &obtained, &bus_sound) != 0) {
    printf("Unable to load sound.\n");
    return 1;
  }

  ClearPlayingSounds();
  SDL_PauseAudio(0);

  init_bus();
  init_man();
  
  int passenger_in = 0;

  PlaySound(&bus_sound);
  while (psv.x > 0) {
    src.x = 0;
    src.y = 0;
    src.w = background->w;
    src.h = background->h; 
    dest = src;
    SDL_BlitSurface(background, &src, screen, &dest);

    draw_obj(&psv, bus);
    
    if (psv.x < screen->w/2 && !passenger_in) { /*pause bus for passenger to enter*/
      SDL_PauseAudio(1);
      if (passenger.x > psv.x + 40){  /*check if passenger has got in*/
	passenger_in = 1; SDL_PauseAudio(0);
      }

      draw_man();
      SDL_Flip(screen);
      move_obj(&passenger);
    } else {
      SDL_Flip(screen);
      move_obj(&psv);
    }
  }

  psv.x = 639;  psv.y = 320;
  init_cars();
  PlaySound(&bus_sound);
  while (psv.x + bus->w/2 > 0) {
    SDL_BlitSurface(background2, &src, screen, &dest);
    draw_obj(&priv1, car1); draw_obj(&priv2, car2);
    draw_obj(&psv, bus);
    SDL_Flip(screen);
    move_obj(&psv);
    move_obj(&priv1); move_obj(&priv2);
  }

  psv.x = 639; psv.y = 350;
  PlaySound(&bus_sound);
  while (psv.x + bus->w/2 > 0) {
    SDL_BlitSurface(background3, &src, screen, &dest);
    draw_obj(&psv, bus);
    SDL_Flip(screen);
    move_obj(&psv);
  }

  psv.x = 639; psv.y = 267; passenger.y = 270;
  passenger_in = 1;
  int has_paused = 0;
  PlaySound(&bus_sound);
  while (psv.x + bus->w/2 > 0) {
    SDL_BlitSurface(background4, &src, screen, &dest);
    if (screen->w/2 > psv.x && passenger_in == 1) {
      SDL_PauseAudio(1);
      if (has_paused == 0) { SDL_Delay(1000); has_paused = 1;}
      if (passenger.x > 639) {passenger_in = 0; SDL_PauseAudio(0);}
      draw_obj(&psv, bus);
      draw_man();
      SDL_Flip(screen);
      move_obj(&passenger);
    } else {
      draw_obj(&psv, bus);
      SDL_Flip(screen);
      move_obj(&psv);
    }
  }

  //pause and lock sound system
  SDL_PauseAudio(1);
  SDL_LockAudio();

  free(bus_sound.samples);

  SDL_UnlockAudio();


  SDL_FreeSurface(background4);
  SDL_FreeSurface(background3);
  SDL_FreeSurface(background2);
  SDL_FreeSurface(background);
  SDL_FreeSurface(man);
  SDL_FreeSurface(bus);
  return 0;
}