Beispiel #1
0
bool update_disp(Image* background, Ship* myShip, std::vector<Bullet*> &bullets, std::vector<EnemyShip*> &enemies, std::vector<Bullet*> &enemy_bullets, std::vector<Powerup*> &powerups)
{
  SDL_RenderClear(renderer);
    //Apply the background to screen
  //  apply_texture( 0, 0, background->width, background->height, background->image, renderer, 0 , Color(255,255,255));
    apply_texture( 0, 0, background->width, background->height, background->image, 0 , Color(0,0,0));

    //Apply bullets to screen
    if(!bullets.empty())
    {
        for(std::vector<Bullet*>::iterator it = bullets.begin(); it != bullets.end(); it++){
	  //            apply_texture(it->x, it->y, it->bullet_image, screen);
	  apply_Bullet(*it);
        }
    }

    //Apply Ship to screen
    apply_Ship(myShip);

    //Apply enemie bullets to screen
    if(!enemy_bullets.empty())
    {
        for(std::vector<Bullet*>::iterator it = enemy_bullets.begin(); it != enemy_bullets.end(); it++){
	  //           apply_texture(it->x, it->y, it->bullet_image, screen);
	  apply_PhysicalObject(*it);
        }
    }


    //Apply enemies to screen
    if(!enemies.empty())
    {
        for(std::vector<EnemyShip*>::iterator it = enemies.begin(); it != enemies.end(); it++){
	  //            apply_texture(it->x, it->y, it->enemy_image, screen);
	  apply_enemyShip(*it);
        }
    }
    
    if(!powerups.empty())
    {
        for(auto it = powerups.begin(); it != powerups.end(); it++){
            apply_PhysicalObject(*it);
        }
    }

    //Update the screen
    SDL_RenderPresent(renderer);

    //SDL_UpdateWindowSurface(gWindow);
    return 1;
}
// This function initializes our background. It should be called once in
// the beginning of the program
void initialize_background(void){
	// First we load the file "texture.png" located in the directory "images"
	surface *texture = new_image("texture.png");
	// Next we allocate space for our background. It'll have the same
	// width than the screen. But how the first 100 lines of the screen
	// will be occupied by the scores, it'll have a height 100 pixels
	// smaller.
	background = new_surface(window_width, window_height - 100);
	// Now we apply the texture to our bigger surface.
	apply_texture(texture, background);
	// Done. Now we don't need the texture anymore. We destroy the surface
	// that stores the texture.
	destroy_surface(texture);
}
Beispiel #3
0
void apply_visible_equipment(Visible_equipment *obj)
{
  apply_texture(*(obj->x_ptr) - (obj->my_image)->width/2, *(obj->y_ptr) - (obj->my_image)->height/2,  (obj->my_image)->width,  (obj->my_image)->height, (obj->my_image)->image, obj->angle, obj->color);
}
Beispiel #4
0
void apply_PhysicalObject(PhysicalObject *obj, int index = 0)
{
  apply_texture(obj->x - (*(obj->my_images))[index]->width/2, obj->y - (*(obj->my_images))[index]->height/2,  (*(obj->my_images))[index]->width,  (*(obj->my_images))[index]->height, (*(obj->my_images))[index]->image, obj->angle, obj->color);
}
Beispiel #5
0
int game(int type){
  int game_over = 0;
  {
    surface *background = new_image("background.png");
    apply_texture(background, window);
    destroy_surface(background);
  }
  initialize_cameras();initialize_star();initialize_ships(2);initialize_dust();
  initialize_shot();initialize_ai();
  play_music("music.ogg");
  draw_tank(0, (window_width / 2 - 421), window_height / 2 - 150);
  draw_tank(1, window_width / 2 + 401, window_height / 2 - 150);
    // Main loop
  for(;;){
    int i;
    get_input();
    if(keyboard[ESC] || game_over){
      break;
    }
    
    for(i = 0; i < 2; i ++)
      if(ship[i].status == DEAD){
	struct timeval now;
	gettimeofday(&now, NULL);
	if((int) (now.tv_sec - ship[i].time.tv_sec) > 2)
	  game_over = 1;
      }



    erase_ships();    
    erase_shot();     
    
    if(type != CPU_X_CPU){ // Rotation: 10 Propulse: 20 Fire: 20 Hyper: 180 
      int my_ship;
      // Player 1 moves
      if(type == CPU_X_PLR){
	ai_play(0);
	my_ship = 1;
      }
      else if(type == PLR_X_CPU){
	my_ship = 0;
	ai_play(1);
      }
      else
	my_ship = 0;
      if(keyboard[LEFT])
	rotate_ship(my_ship, LEFT); 
      if(keyboard[RIGHT])
	rotate_ship(my_ship, RIGHT);
      if(keyboard[UP])
	propulse_ship(my_ship);
      if(keyboard[DOWN])
	ship_fire(my_ship);
      if(keyboard[RIGHT_CTRL])
	goto_hyperspace(my_ship);
    }
    else{
      ai_play(0);
      ai_play(1);
    }

    if(type == PLR_X_PLR){ 
      // Player 2 moves
      if(keyboard[A])
	rotate_ship(1, LEFT);
      if(keyboard[D])
	rotate_ship(1, RIGHT);
      if(keyboard[W])
	propulse_ship(1);
      if(keyboard[L])
	blow_up(1);
      if(keyboard[S])
	ship_fire(1);
      if(keyboard[LEFT_CTRL])
	goto_hyperspace(1);
    }
    
    update_star(); // 6
    update_ships(); // 2
    update_dust(); // 145
    update_shot(); // 16
    film_ships(); // Até 16
    film_dust(); // 20
    film_shot(); // Até 20
    weaver_rest(10000000);
  }
  stop_music();
  destroy_star();
  destroy_ships();
  destroy_cameras();
  destroy_dust();
  destroy_shot();
  clean_keyboard();
  return 0;
}
Beispiel #6
0
void options(char ip[])
{
    int quit = 0;
    int menu_choice = 0;
    //fps
    struct timer_t2 fps;

    init_options();
    init_font();

    //wait for user to continue or exit
    while( quit != 1 )
    {
        //Start the frame timer
        timer_start(&fps);
        //While there are events to handle
        while( SDL_PollEvent( &event ) )
        {

            if( event.type == SDL_QUIT )
            {
                //exit
                clean_up();
                exit(0);
            }
            if( event.type == SDL_KEYDOWN )
            {
                switch(event.key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        quit = 1; break;
                }
            }

            mouse_over_options(event.button.x, event.button.y);

            if( event.type == SDL_MOUSEBUTTONDOWN)
            {
                switch(event.button.button)
                {
                    case SDL_BUTTON_LEFT:
                        press_options(event.button.x, event.button.y);
                        break;
                }
            }


            if( event.type == SDL_MOUSEBUTTONUP)
            {
                switch(event.button.button)
                {
                    case SDL_BUTTON_LEFT:
                        menu_choice = press_options(event.button.x, event.button.y);
                        break;
                }
            }

            switch(menu_choice)
            {
                case 1:
                    handle_input(event,ip);
                    printf("String in graphics.c (%s)\n",ip);
                    break;
                case 2:
                    quit = 1;
                    printf("Försöker bryta ut ur options\n");
                    break;
                default:
                    break;
            }
        }
        apply_letter(ip);
        apply_texture();

        startDraw();
        //draw menu
        drawOptions(SCREEN_WIDTH, SCREEN_HEIGHT);
        drawBack();
        endDraw();

        if( timer_get_ticks(&fps) < 1000 / FRAMES_PER_SECOND )
        {
            //delay the as much time as we need to get desired frames per second
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - timer_get_ticks(&fps) );
        }
    }
}