Skip to content

Commit

Permalink
etc: Pretty print coord values in to_string().
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Apr 19, 2024
1 parent 7e73cef commit e2bca02
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions etc/gdb_pretty/printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,57 @@ def _register_printer(printer):
return _register_printer


def format_fixed_point(value: int, fractional_bits: int) -> float:
"""
Formats a fixed point value to a double.
:param value: The fixed point value.
:type value: int
:param fractional_bits: The number of fractional bits.
:type fractional_bits: int
"""
to_double_factor = 1 / pow(2, fractional_bits)
return float(value) * to_double_factor


@printer_regex('^openage::coord::(camhud|chunk|input|phys|scene|term|tile|viewport)(2|3)?(_delta)?')
class CoordPrinter:
"""
Pretty printer for openage::coord::CoordNeSe.
Pretty printer for openage::coord types (CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ).
TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros.
"""

def __init__(self, val: gdb.Value):
self.__val = val

# Each coord type has one parent which is either
# of CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ
# From this parent we can get the fields
self._parent_type = self.__val.type.fields()[0].type

def to_string(self):
"""
Get the coord as a string.
"""
return self.__val.type.name
field_vals = []
for child in self._parent_type.fields():
# Include the fixed point coordinates in the summary
val = self.__val[child.name]
num = format_fixed_point(
int(val['raw_value']),
int(val.type.template_argument(1))
)
field_vals.append(f"{num:.5f}")

# Example: phys3[1.00000, 2.00000, 3.00000]
return f"{self.__val.type.tag.split('::')[-1]}[{', '.join(field_vals)}]"

def children(self):
"""
Get the displayed children of the coord.
"""
# Each coord type has one parent which is either
# of CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ
# From this parent we can get the fields
parent_type = self.__val.type.fields()[0].type
for child in parent_type.fields():
for child in self._parent_type.fields():
yield (child.name, self.__val[child.name])


Expand All @@ -138,11 +163,11 @@ def to_string(self):
Format: SS.sss (e.g. 12.345s)
"""
fractional_bits = int(self.__val.type.template_argument(1))
seconds = format_fixed_point(
int(self.__val['raw_value']),
int(self.__val.type.template_argument(1))
)

# convert the fixed point value to double
to_double_factor = 1 / pow(2, fractional_bits)
seconds = float(self.__val['raw_value']) * to_double_factor
# show as seconds with millisecond precision
return f'{seconds:.3f}s'

Expand Down Expand Up @@ -170,11 +195,10 @@ def to_string(self):
Format: 0.12345
"""
fractional_bits = int(self.__val.type.template_argument(1))

# convert the fixed point value to double
to_double_factor = 1 / pow(2, fractional_bits)
num = float(self.__val['raw_value']) * to_double_factor
num = format_fixed_point(
int(self.__val['raw_value']),
int(self.__val.type.template_argument(1))
)
return f'{num:.5f}'

def children(self):
Expand Down Expand Up @@ -231,7 +255,7 @@ def num_children(self):
"""
return self.__val.type.template_argument(0)

@staticmethod
@ staticmethod
def display_hint():
"""
Get the display hint for the vector.
Expand Down

0 comments on commit e2bca02

Please sign in to comment.