示例#1
0
/**********************************
Set sdl_item item for mouse button callback
**********************************/
static void compose_map_button(context_t * ctx)
{
	int x = 0;
	int y = 0;
	item_t * item;
	anim_t * anim = NULL;

	if ( option && option->cursor_over_tile ) {
		anim = imageDB_get_anim(ctx,option->cursor_over_tile);
	}

	for( y=0 ; y < default_layer->map_h ; y++ ) {
		for ( x=0 ; x < default_layer->map_w ; x++ ) {
			item = item_list_add(&item_list);
			item_set_anim_shape(item,map_t2p_x(x,y,default_layer),map_t2p_y(x,y,default_layer),default_layer->tile_width,default_layer->tile_height);
			item_set_user(item,x,y);
			item_set_click_left(item,cb_select_map,item,NULL);
			item_set_click_right(item,cb_redo_map,item,NULL);
			item_set_over(item,cb_over,item,NULL);
			item_set_anim_over(item,anim,0);
		}
	}
}
示例#2
0
void screen_play(SDL_Renderer * render,game_t * game)
{
	char gold_buf[128];
	char mana_buf[128];
	SDL_Event event;
	int tile_num;
	int unit_num;
	int city_num;
	int encounter_num;
	int i;
	int j;
	int x;
	LBXAnimation_t * anim_ptr;
	city_t * city;

	local_render = render;

	end_screen = -1;

	selected_group = NULL;

	city = (city_t*)game->wiz[0].city->data;
	cur_tile_x = city->x;
	cur_tile_y = city->y;

	load_font();

	/* Load resource */
	if(anim==NULL) {
		anim = load_graphics(render,"MAIN.LBX");
		if(anim == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	/* Load tiles */
	if(tile==NULL) {
		tile = load_graphics(render,"TERRAIN.LBX");
		if(tile == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	/* Load unit */
	if(unit1==NULL) {
		unit1 = load_graphics(render,"UNITS1.LBX");
		if(unit1 == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	if(unit2==NULL) {
		unit2 = load_graphics(render,"UNITS2.LBX");
		if(unit2 == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	/* Concat unit1 and unit2 */
	if(unit==NULL) {
		j=0;
		anim_ptr = unit1;
		for(i=0; i<2; i++) {
			while(anim_ptr->num_frame!=0) {
				unit = (LBXAnimation_t*)realloc(unit,(j+1)*sizeof(LBXAnimation_t));
				memcpy(&unit[j],anim_ptr,sizeof(LBXAnimation_t));
				anim_ptr++;
				j++;
			}
			anim_ptr= unit2;
		}
	}

	/* Load back */
	if(back==NULL) {
		back = load_graphics(render,"MAPBACK.LBX");
		if(back == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	j=0;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],0,0,&anim[j]);
	j++;
	x=7;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	item_set_click_left(&item_ui[j],cb_plane,NULL);
	j++;

	/* Main loop */
	while( end_screen == -1) {
		tile_num = draw_tile(game);
		city_num = draw_city(game);
		encounter_num = draw_encounter(game);
		unit_num = draw_unit(game);

		/* Print gold */
		item_init(&item_ui[8]);
		item_set_font(&item_ui[8],font);
		sprintf(gold_buf,"%d GP",game->wiz[0].gold);
		item_set_string(&item_ui[8],gold_buf);
		item_set_frame(&item_ui[8],264,71,NULL);

		/* Print mana */
		item_init(&item_ui[9]);
		item_set_font(&item_ui[9],font);
		sprintf(mana_buf,"%d MP",game->wiz[0].mana);
		item_set_string(&item_ui[9],mana_buf);
		item_set_frame(&item_ui[9],300,71,NULL);

		while (SDL_PollEvent(&event)) {
			if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_ESCAPE) {
				return;
			}

			/* Move selected_unit */
			if(event.type==SDL_KEYDOWN ) {
				move_unit(game,event.key.keysym.sym);
			}

			sdl_screen_manager(&event);
			sdl_mouse_manager(&event,item_ui,ITM_UI_NUM);
			sdl_mouse_manager(&event,item_city,city_num);
			sdl_mouse_manager(&event,item_encounter,encounter_num);
			sdl_mouse_manager(&event,item_tile,tile_num);
			sdl_mouse_manager(&event,item_unit,unit_num);
			sdl_keyboard_manager(&event);
		}

		SDL_RenderClear(render);

		sdl_blit_item_list(item_tile,tile_num);
		sdl_blit_item_list(item_city,city_num);
		sdl_blit_item_list(item_encounter,encounter_num);
		sdl_blit_item_list(item_back,unit_num);
		sdl_blit_item_list(item_unit,unit_num);
		sdl_blit_item_list(item_ui,ITM_UI_NUM);

		sdl_blit_to_screen();

		sdl_loop_manager();
	}

	return;
}
示例#3
0
/**********************************
Draw a single sprite
if image_file_name is not NULL, this file is used as an image rather than the normal sprite image
**********************************/
static void set_up_sprite(context_t * ctx, const char * image_file_name)
{
	anim_t ** sprite_list;
	anim_t ** sprite_move_list;
	item_t * item;
	int px;
	int py;
	int opx;
	int opy;
	Uint32 current_time;
	int angle;
	int flip;
	int force_flip;
	int move_status;
	char * zoom_str = NULL;
	double zoom = 1.0;
	int sprite_align = ALIGN_CENTER;
	int sprite_offset_y = 0;
	bool force_position = false;

	context_t * player_context = context_get_player();

	if( ctx->map == NULL ) {
		return;
	}
	if( ctx->in_game == false ) {
		return;
	}
	if( strcmp(ctx->map,player_context->map)) {
		return;
	}

	item = item_list_add(&item_list);

	current_time = sdl_get_global_time();

	// Force position when the player has changed map
	if( change_map == true ) {
		ctx->move_start_tick = current_time;
		ctx->animation_tick = current_time;
		force_position = true;
	}
	// Force position when this context has changed map
	if( ctx->change_map == true ) {
		ctx->move_start_tick = current_time;
		ctx->animation_tick = current_time;
		ctx->change_map = false;
		force_position = true;
	}

	if( ctx->animation_tick == 0 ) {
		ctx->animation_tick = current_time;
	}

	if( ctx->cur_pos_px == INT_MAX || ctx->cur_pos_py == INT_MAX ) {
		force_position = true;
	}

	// Detect sprite movement, initiate animation
	if( ctx->pos_changed && force_position == false ) {
		ctx->pos_changed = false;
		ctx->move_start_tick = current_time;
		ctx->start_pos_px = ctx->cur_pos_px;
		ctx->start_pos_py = ctx->cur_pos_py;

		/* flip need to remember previous direction to avoid resetting a
		   east -> west flip when a sprite goes to north for instance.
		   On the contrary rotation must not remember previous state, or
		   the rotation will be wrong.
		   Hence the distinction between orientation (no memory) and
		   direction (memory). */
		ctx->orientation = 0;
		// Compute direction
		if( ctx->pos_tx > ctx->prev_pos_tx ) {
			ctx->direction &= ~WEST;
			ctx->direction |= EAST;
			ctx->orientation |= EAST;
		}
		if( ctx->pos_tx < ctx->prev_pos_tx ) {
			ctx->direction &= ~EAST;
			ctx->direction |= WEST;
			ctx->orientation |= WEST;
		}
		if( ctx->pos_ty > ctx->prev_pos_ty ) {
			ctx->direction &= ~NORTH;
			ctx->direction |= SOUTH;
			ctx->orientation |= SOUTH;
		}
		if( ctx->pos_ty < ctx->prev_pos_ty ) {
			ctx->direction &= ~SOUTH;
			ctx->direction |= NORTH;
			ctx->orientation |= NORTH;
		}
	}

	// Select sprite to display
	sprite_list = select_sprite(ctx,image_file_name);
	if( sprite_list == NULL ) {
		return;
	}
	if( sprite_list[0] == NULL ) {
		free(sprite_list);
		return;
	}
	sprite_move_list = select_sprite_move(ctx,image_file_name);

	// Get position in pixel
	px = map_t2p_x(ctx->pos_tx,ctx->pos_ty,default_layer);
	py = map_t2p_y(ctx->pos_tx,ctx->pos_ty,default_layer);

	// Get per sprite zoom
	if(entry_read_string(CHARACTER_TABLE,ctx->id,&zoom_str,CHARACTER_KEY_ZOOM,NULL) == RET_OK ) {
		zoom = atof(zoom_str);
		free(zoom_str);
	}

	// Align sprite on tile
	entry_read_int(CHARACTER_TABLE,ctx->id,&sprite_align,CHARACTER_KEY_ALIGN,NULL);
	if( sprite_align == ALIGN_CENTER ) {
		px -= ((sprite_list[0]->w*default_layer->map_zoom*zoom)-default_layer->tile_width)/2;
		py -= ((sprite_list[0]->h*default_layer->map_zoom*zoom)-default_layer->tile_height)/2;
	}
	if( sprite_align == ALIGN_LOWER ) {
		px -= ((sprite_list[0]->w*default_layer->map_zoom*zoom)-default_layer->tile_width)/2;
		py -= (sprite_list[0]->h*default_layer->map_zoom*zoom)-default_layer->tile_height ;
	}

	// Add Y offset
	entry_read_int(CHARACTER_TABLE,ctx->id,&sprite_offset_y,CHARACTER_KEY_OFFSET_Y,NULL);
	py += sprite_offset_y;

	// Set sprite to item
	item_set_anim_start_tick(item,ctx->animation_tick);

	if( force_position == true ) {
		ctx->start_pos_px = px;
		ctx->cur_pos_px = px;
		ctx->start_pos_py = py;
		ctx->cur_pos_py = py;
	}

	opx = ctx->start_pos_px;
	opy = ctx->start_pos_py;

	item_set_move(item,opx,opy,px,py,ctx->move_start_tick,VIRTUAL_ANIM_DURATION);
	item_set_save_coordinate(item,&ctx->cur_pos_px,&ctx->cur_pos_py);
	item_set_anim_array(item,sprite_list);
	free(sprite_list);
	item_set_anim_move_array(item,sprite_move_list);
	free(sprite_move_list);

	// Get rotation configuration
	angle = 0;
	if( ctx->orientation & NORTH && ctx->orientation & EAST ) {
		entry_read_int(CHARACTER_TABLE,ctx->id,&angle,CHARACTER_KEY_DIR_NE_ROT,NULL);
		item_set_angle(item,(double)angle);
	} else if ( ctx->orientation & SOUTH && ctx->orientation & EAST ) {
		entry_read_int(CHARACTER_TABLE,ctx->id,&angle,CHARACTER_KEY_DIR_SE_ROT,NULL);
		item_set_angle(item,(double)angle);
	} else if ( ctx->orientation & SOUTH && ctx->orientation & WEST ) {
		entry_read_int(CHARACTER_TABLE,ctx->id,&angle,CHARACTER_KEY_DIR_SW_ROT,NULL);
		item_set_angle(item,(double)angle);
	} else if ( ctx->orientation & NORTH && ctx->orientation & WEST ) {
		entry_read_int(CHARACTER_TABLE,ctx->id,&angle,CHARACTER_KEY_DIR_NW_ROT,NULL);
		item_set_angle(item,(double)angle);
	} else if ( ctx->orientation & NORTH ) {
		entry_read_int(CHARACTER_TABLE,ctx->id,&angle,CHARACTER_KEY_DIR_N_ROT,NULL);
		item_set_angle(item,(double)angle);
	} else if ( ctx->orientation & SOUTH ) {
		entry_read_int(CHARACTER_TABLE,ctx->id,&angle,CHARACTER_KEY_DIR_S_ROT,NULL);
		item_set_angle(item,(double)angle);
	} else if ( ctx->orientation & WEST ) {
		entry_read_int(CHARACTER_TABLE,ctx->id,&angle,CHARACTER_KEY_DIR_W_ROT,NULL);
		item_set_angle(item,(double)angle);
	} else if ( ctx->orientation & EAST ) {
		entry_read_int(CHARACTER_TABLE,ctx->id,&angle,CHARACTER_KEY_DIR_E_ROT,NULL);
		item_set_angle(item,(double)angle);
	}

	// Get flip configuration
	force_flip = 0;
	entry_read_int(CHARACTER_TABLE,ctx->id,&force_flip,CHARACTER_KEY_FORCE_FLIP,NULL);
	move_status = ctx->direction;
	if( force_flip == true ) {
		move_status = ctx->orientation;
	}

	flip = 0;
	if( angle == 0 ) {
		if( move_status & NORTH ) {
			entry_read_int(CHARACTER_TABLE,ctx->id,&flip,CHARACTER_KEY_DIR_N_FLIP,NULL);
		}
		if( move_status & SOUTH ) {
			entry_read_int(CHARACTER_TABLE,ctx->id,&flip,CHARACTER_KEY_DIR_S_FLIP,NULL);
		}
		if( move_status & WEST ) {
			entry_read_int(CHARACTER_TABLE,ctx->id,&flip,CHARACTER_KEY_DIR_W_FLIP,NULL);
		}
		if( move_status & EAST ) {
			entry_read_int(CHARACTER_TABLE,ctx->id,&flip,CHARACTER_KEY_DIR_E_FLIP,NULL);
		}

		switch(flip) {
		case 1:
			item_set_flip(item,SDL_FLIP_HORIZONTAL);
			break;
		case 2:
			item_set_flip(item,SDL_FLIP_VERTICAL);
			break;
		case 3:
			item_set_flip(item,SDL_FLIP_HORIZONTAL|SDL_FLIP_VERTICAL);
			break;
		default:
			item_set_flip(item,SDL_FLIP_NONE);
		}
	}

	item_set_click_left(item,cb_select_sprite,ctx->id,NULL);
	item_set_click_right(item,cb_redo_sprite,item,NULL);

	item_set_zoom_x(item,zoom * default_layer->map_zoom );
	item_set_zoom_y(item,zoom * default_layer->map_zoom );
}