void pixel_size(struct WorldCoor *wcs,
		  double x_centre,
		  double y_centre,
		  double & x_size,
		  double & y_size,
		  int x_off, int y_off) {
    double p1a, p1b, p2a, p2b;  // axis A and B of point 1 and 2

    pixel_to_world(wcs,
		   x_centre - 0.5, y_centre, &p1a, &p1b,
		   x_off, y_off);
    pixel_to_world(wcs,
		   x_centre + 0.5, y_centre, &p2a, &p2b,
		   x_off, y_off);
    x_size = ::wcsdist(p1a, p1b, p2a, p2b);

    pixel_to_world(wcs,
		   x_centre, y_centre - 0.5, &p1a, &p1b,
		   x_off, y_off);
    pixel_to_world(wcs,
		   x_centre, y_centre + 0.5, &p2a, &p2b,
		   x_off, y_off);
    y_size = ::wcsdist(p1a, p1b, p2a, p2b);
  }
Exemple #2
0
int main(int argc, char** argv){
    printf("hello world\n");
    
    behaviors_init();
   
    log_file_open("log.txt");

    if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
		printf("%s\n", SDL_GetError());
		return 1;
	}
    printf("sdl init success\n");

    SDL_Renderer *renderer = NULL;
    SDL_Window *window = NULL;
    
	//Setup our window and renderer
	window = SDL_CreateWindow("Stick Fighter", SDL_WINDOWPOS_CENTERED, 
                                               SDL_WINDOWPOS_CENTERED, 
                                               SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
	if (window == NULL){
		printf("%s\n", SDL_GetError());
		return 2;
	}
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (renderer == NULL){
		printf("%s\n", SDL_GetError());
		return 3;
	}
    
    SDL_Texture *background1 = NULL;
    SDL_Texture *background2 = NULL;
    SDL_Texture *image = NULL;

    //background = sprite_load_image(renderer, "res/background_rcr.png");
    background1 = sprite_load_image(renderer, "res/background_rcr.png");
    background2 = sprite_load_image(renderer, "res/background_new.png");

    SDL_Texture* backgrounds[2];
    backgrounds[0] = background1;
    backgrounds[1] = background2;
    
    SDL_Rect bg;
    //Query the texture to get its width and height to use
    SDL_QueryTexture(background1, NULL, NULL, &bg.w, &bg.h);
    float bg_width = bg.w;
    float bg_height = bg.h;
    float world_width = bg_width / 10;
    float world_height = bg_height / 10;

    printf("bg_width: %f bg_height: %f world_width: %f world_height: %f\n", bg_width, bg_height, world_width, world_height);

    //printf("bg 1: [%p]\n", backgrounds[0]);
    //printf("bg 2: [%p]\n", backgrounds[1]);
	
    image = sprite_load_image(renderer, "res/output.png");
    //printf("after sprite load image\n");
        
    //load the meta info
    char* filename = "res/animation_meta_info.txt";
    DArray meta_info;
    darray_init(&meta_info);
    sprite_load_meta_file(filename, &meta_info);
    darray_print(&meta_info);

    float interval = 0.0f;
    float start = 0.0f;
    int quit = 0;
    
    float delay = 1000.0f / FPS;
    Sprite* player = sprite_create(PLAYER, WALK, 200, 300, delay, image);
    player->x_speed = 0;
    player->y_speed = 0;
    
    Sprite* enemy = sprite_create(ENEMY, WALK, 0, 300, delay, image);
    enemy->x_speed = 2;
    enemy->y_speed = 2;
    enemy->advance_frame = 1;
    //set white background
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    
    pixel_to_world(bg_width, bg_height, world_width, world_height,
                   enemy->x, enemy->y, &enemy->location->coords->x, &enemy->location->coords->y);
    
    int dest_x = 0;
    int dest_y = 0;
    
    //TODO:
    //animations for punch and basic kick
    //animation for getting hit
    //animations for walk left and run left
    //2 more ai behaviors
    //health bars above units and health stats
    //dying animation
    //dying logic
    //generate enemies off screen and have them walk on to screen
    //redo tornado kick and spinning back kick so stick figure is same size
    //figure out how to color stick figure without having to make new sprites on sheet...need to make figures white to set modulation
    //add rolling punch move
    //add game state which keeps track of game state
    //add buy moves screen in between levels
    //add a generator which generates guys for 100 levels

    int i = 0;
    while (!quit){
        printf("iteration: %i\n", i);
		
        start = SDL_GetTicks();
        
        quit = player_input(player, &meta_info);
        //printf("quit: %i\n", quit);  
       
        //log_msg(LOG_DEBUG, "after player input: current frame: %i\n", player->current_frame[HIT]);
        
        //pixel location to world location
        pixel_to_world(bg_width, bg_height, world_width, world_height, 
                       player->x, player->y, &player->location->coords->x, &player->location->coords->y);

        //world location to pixel location
        world_to_pixel(bg_width, bg_height, world_width, world_height,
                       enemy->location->coords->x, enemy->location->coords->y, &dest_x, &dest_y);
        
        //boundry check
        boundry_check(SCREEN_WIDTH, SCREEN_HEIGHT, enemy, &meta_info, &dest_x, &dest_y);

        //printf("2nd before moveto = enemy x:%i enemy y:%i dest_x:%i dest_y:%i\n", enemy->x, enemy->y, dest_x, dest_y);
        
        //update enemy sprite by sprite's speed
        moveto_coordinates(enemy, dest_x, dest_y);
        
        //are we at the original world location in pixel coordinates?
        int arrived = within_coordinates(enemy, dest_x, dest_y);
        
        printf("arrived: %i\n", arrived);
        if(arrived == 1) {
            //wander(enemy); 
            // we reached last behavior's destination so do new AI behavior
            wander_stall_attack(player, enemy, &meta_info, 3);
        }

        sprite_update_bounding_box(player, &meta_info);
        sprite_update_bounding_box(enemy, &meta_info);

        //check collision
        int collision = sprite_check_collision(player, enemy);
        
        //printf("collision: %i\n", collision);
        
        if(collision) {
            sprite_handle_collision(player, enemy, &meta_info);
        }
        
        //handle collision
        //if animation is an attack then check frame range that triggers a hit
        //check if attack animation triggers a hi or low hit
        //if sequence of attacks is within delta time then 4th hit triggers a knockdown
        //if attack would make health below 0 then it triggers a knockdown
        
        //handle opposite attack from opposing sprite
        //if is in attack animation then check frame range for a hit

        //update animation frame
        sprite_update(player, &meta_info, renderer);
        sprite_update(enemy, &meta_info, renderer);
        
        //Rendering
		SDL_RenderClear(renderer);
       
        //printf("player->x: %i\n", player->x);
        //printf("moving right: %i\n", player->moving_right);
        //printf("moving left: %i\n", player->moving_left);
        int current_background = 0;
        sprite_draw_background(renderer, backgrounds, 2, &current_background, player->x, player->moving_right, player->moving_left, &player->scroll);
        
        sprite_draw_health_bar(renderer, player, &meta_info, 100); 
        sprite_draw_health_bar(renderer, enemy, &meta_info, 100); 
        
        //draw sprite
        sprite_render_frame(SCREEN_WIDTH, SCREEN_HEIGHT, player, &meta_info, renderer, 0);
        sprite_render_frame(bg_width, bg_height, enemy, &meta_info, renderer, 0);

		//Update the screen
		SDL_RenderPresent(renderer);
         
        interval = SDL_GetTicks() - start;
        
        if(interval > 0) {
            //float fps = 1.0f / (interval / 1000.0f);
            //printf("%f fps\n", fps);
        }
        i++;
	}
    
    //SDL_Delay(4000);

	//Destroy the various items
    sprite_destroy(player);
    sprite_destroy(enemy);
    darray_destroy(&meta_info);
	SDL_DestroyTexture(backgrounds[0]);
	SDL_DestroyTexture(backgrounds[1]);
	SDL_DestroyTexture(image);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
    
    printf("after destory text rend win\n");

	SDL_Quit();
    
    log_file_close();

    return 0;
}