MCPcopy Create free account
hub / github.com/Redot-Engine/redot-engine / draw_tile

Method draw_tile

scene/2d/tile_map_layer.cpp:2607–2671  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2605}
2606
2607void TileMapLayer::draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame, const TileData *p_tile_data_override, real_t p_normalized_animation_offset) {
2608 ERR_FAIL_COND(p_tile_set.is_null());
2609 ERR_FAIL_COND(!p_tile_set->has_source(p_atlas_source_id));
2610 ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_tile(p_atlas_coords));
2611 ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_alternative_tile(p_atlas_coords, p_alternative_tile));
2612 TileSetSource *source = *p_tile_set->get_source(p_atlas_source_id);
2613 TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
2614 if (atlas_source) {
2615 // Check for the frame.
2616 if (p_frame >= 0) {
2617 ERR_FAIL_INDEX(p_frame, atlas_source->get_tile_animation_frames_count(p_atlas_coords));
2618 }
2619
2620 // Get the texture.
2621 Ref<Texture2D> tex = atlas_source->get_runtime_texture();
2622 if (tex.is_null()) {
2623 return;
2624 }
2625
2626 // Check if we are in the texture, return otherwise.
2627 Vector2i grid_size = atlas_source->get_atlas_grid_size();
2628 if (p_atlas_coords.x >= grid_size.x || p_atlas_coords.y >= grid_size.y) {
2629 return;
2630 }
2631
2632 // Get tile data.
2633 const TileData *tile_data = p_tile_data_override ? p_tile_data_override : atlas_source->get_tile_data(p_atlas_coords, p_alternative_tile);
2634
2635 // Get the tile modulation.
2636 Color modulate = tile_data->get_modulate();
2637
2638 // Compute the dest rect.
2639 Rect2 dest_rect;
2640 bool transpose;
2641 compute_transformed_tile_dest_rect(dest_rect, transpose, p_position, atlas_source->get_runtime_tile_texture_region(p_atlas_coords).size, tile_data, p_alternative_tile);
2642
2643 // Draw the tile.
2644 if (p_frame >= 0) {
2645 Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, p_frame);
2646 tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
2647 } else if (atlas_source->get_tile_animation_frames_count(p_atlas_coords) == 1) {
2648 Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, 0);
2649 tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
2650 } else {
2651 real_t speed = atlas_source->get_tile_animation_speed(p_atlas_coords);
2652 real_t animation_duration = atlas_source->get_tile_animation_total_duration(p_atlas_coords) / speed;
2653 real_t animation_offset = p_normalized_animation_offset * animation_duration;
2654 // Accumulate durations unaffected by the speed to avoid accumulating floating point division errors.
2655 // Aka do `sum(duration[i]) / speed` instead of `sum(duration[i] / speed)`.
2656 real_t time_unscaled = 0.0;
2657 for (int frame = 0; frame < atlas_source->get_tile_animation_frames_count(p_atlas_coords); frame++) {
2658 real_t frame_duration_unscaled = atlas_source->get_tile_animation_frame_duration(p_atlas_coords, frame);
2659 real_t slice_start = time_unscaled / speed;
2660 real_t slice_end = (time_unscaled + frame_duration_unscaled) / speed;
2661 RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, animation_duration, slice_start, slice_end, animation_offset);
2662
2663 Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, frame);
2664 tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());

Callers

nothing calls this directly

Calls 15

has_sourceMethod · 0.80
has_tileMethod · 0.80
has_alternative_tileMethod · 0.80
get_runtime_textureMethod · 0.80
get_atlas_grid_sizeMethod · 0.80
get_tile_dataMethod · 0.80
is_uv_clippingMethod · 0.80

Tested by

no test coverage detected