RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
sprite_render_manager.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7var draw_calls:int = 0
8var sprite_hits:int = 0
9var fallbacks:int = 0
10var missing:int = 0
11var rotated:int = 0
12var requests_recorded:int = 0
13
14
15
16func draw_sprite(canvas:CanvasItem, sprite_manager, sprite_id:String, rect:Rect2, options:Dictionary = {}) -> bool:
17 draw_calls += 1
18 if canvas == null or sprite_manager == null or sprite_id == "":
19 missing += 1
20 _draw_fallback_rect(canvas, rect, options)
21 return false
22 var metadata = options.get("manifest", {})
23 if sprite_manager.has_method("record_sprite_request"):
24 sprite_manager.record_sprite_request(sprite_id, metadata if metadata is Dictionary else {})
25 requests_recorded += 1
26 var texture = sprite_manager.get_texture(sprite_id) if sprite_manager.has_method("get_texture") else null
27 if texture == null:
28 missing += 1
29 _draw_fallback_rect(canvas, rect, options)
30 return false
31 var modulate:Color = Color(options.get("modulate", Color.WHITE))
32 var rotation:float = float(options.get("rotation", 0.0))
33 if abs(rotation) > 0.0001:
34 rotated += 1
35 canvas.draw_set_transform(rect.get_center(), rotation, Vector2.ONE)
36 canvas.draw_texture_rect(texture, Rect2(-rect.size * 0.5, rect.size), false, modulate)
37 canvas.draw_set_transform(Vector2.ZERO, 0.0, Vector2.ONE)
38 else:
39 canvas.draw_texture_rect(texture, rect, false, modulate)
40 sprite_hits += 1
41 return true
42
43
44
45func draw_sprite_with_fallback(canvas:CanvasItem, sprite_manager, sprite_ids:Array, rect:Rect2, options:Dictionary = {}) -> bool:
46 for sprite_id in sprite_ids:
47 if sprite_id == null:
48 continue
49 var id = str(sprite_id)
50 if id == "":
51 continue
52 var sprite_options = options.duplicate(true)
53 sprite_options["draw_fallback"] = false
54 if draw_sprite(canvas, sprite_manager, id, rect, sprite_options):
55 return true
56 _draw_fallback_rect(canvas, rect, options)
57 return false
58
59
60
61func draw_directional_sprite(canvas:CanvasItem, sprite_manager, sprite_ids:Array, rect:Rect2, direction:Vector2, base_direction:String = "right", options:Dictionary = {}) -> bool:
62 var rotation:float = 0.0
63 if sprite_manager != null and sprite_manager.has_method("rotation_for_vector"):
64 rotation = sprite_manager.rotation_for_vector(direction, base_direction)
65 var next_options:Dictionary = options.duplicate(true)
66 next_options["rotation"] = rotation + float(options.get("rotation_offset", 0.0))
67 return draw_sprite_with_fallback(canvas, sprite_manager, sprite_ids, rect, next_options)
68
69
70func _draw_fallback_rect(canvas:CanvasItem, rect:Rect2, options:Dictionary) -> void:
71 if canvas == null or not bool(options.get("draw_fallback", true)):
72 return
73 fallbacks += 1
74 canvas.draw_rect(rect, Color(options.get("fallback_color", Color.WHITE)), true)
75
76
77
78func reset_stats() -> void:
79 draw_calls = 0
80 sprite_hits = 0
81 fallbacks = 0
82 missing = 0
83 rotated = 0
84 requests_recorded = 0
85
86
87
88func get_cache_stats() -> Dictionary:
89 return get_stats()
90
91
92func get_stats() -> Dictionary:
93 return {
94 "draw_calls": draw_calls,
95 "sprite_hits": sprite_hits,
96 "fallbacks": fallbacks,
97 "missing": missing,
98 "rotated": rotated,
99 "requests_recorded": requests_recorded
100 }