diff --git a/etc/gdb_pretty/printers.py b/etc/gdb_pretty/printers.py index e5bbb42d84..ca8f92083c 100644 --- a/etc/gdb_pretty/printers.py +++ b/etc/gdb_pretty/printers.py @@ -7,6 +7,8 @@ import re import gdb # type: ignore +# TODO: Printers should inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. + class PrinterControl(gdb.printing.PrettyPrinter): """ @@ -92,12 +94,62 @@ 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 types (CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ). + """ + + 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. + """ + 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. + """ + for child in self._parent_type.fields(): + yield (child.name, self.__val[child.name]) + + @printer_typedef('openage::time::time_t') class TimePrinter: """ Pretty printer for openage::time::time_t. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): @@ -109,11 +161,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' @@ -128,8 +180,6 @@ def children(self): class FixedPointPrinter: """ Pretty printer for openage::util::FixedPoint. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): @@ -141,11 +191,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): @@ -167,8 +216,6 @@ def children(self): class VectorPrinter: """ Pretty printer for openage::util::Vector. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): @@ -214,8 +261,6 @@ def display_hint(): class KeyframePrinter: """ Pretty printer for openage::curve::Keyframe. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): @@ -235,7 +280,6 @@ def children(self): yield ('value', self.__val['value']) # TODO: curve types -# TODO: coord types # TODO: pathfinding types # TODO: input event codes # TODO: eigen types https://github.com/dmillard/eigengdb diff --git a/libopenage/coord/coord.h.template b/libopenage/coord/coord.h.template index d4af555ea0..8aeafadc5f 100644 --- a/libopenage/coord/coord.h.template +++ b/libopenage/coord/coord.h.template @@ -1,4 +1,4 @@ -// Copyright 2016-2023 the openage authors. See copying.md for legal info. +// Copyright 2016-2024 the openage authors. See copying.md for legal info. #pragma once @@ -24,6 +24,9 @@ namespace coord { * * 'Absolute' and 'Relative' are the absolute and relative types of the * derived class (CRTP). + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template struct Coord${camelcase}Absolute { @@ -87,6 +90,9 @@ struct Coord${camelcase}Absolute { * * 'Absolute' and 'Relative' are the absolute and relative types of the * derived class (CRTP). + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template struct Coord${camelcase}Relative { diff --git a/libopenage/curve/keyframe.h b/libopenage/curve/keyframe.h index 82e36ed147..484085cc70 100644 --- a/libopenage/curve/keyframe.h +++ b/libopenage/curve/keyframe.h @@ -1,4 +1,4 @@ -// Copyright 2019-2023 the openage authors. See copying.md for legal info. +// Copyright 2019-2024 the openage authors. See copying.md for legal info. #pragma once @@ -11,6 +11,9 @@ namespace openage::curve { /** * A element of the curvecontainer. This is especially used to keep track of * the value-timing. + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template class Keyframe { diff --git a/libopenage/util/fixed_point.h b/libopenage/util/fixed_point.h index 7558ac9cb8..4e97e77323 100644 --- a/libopenage/util/fixed_point.h +++ b/libopenage/util/fixed_point.h @@ -81,6 +81,9 @@ constexpr static * For example, * FixedPoint * can store values from -2**32 to +2**32 with a constant precision of 2**-32. + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template class FixedPoint { diff --git a/libopenage/util/vector.h b/libopenage/util/vector.h index 8e7c7bceb8..e79283fb64 100644 --- a/libopenage/util/vector.h +++ b/libopenage/util/vector.h @@ -20,6 +20,9 @@ namespace openage::util { * * N = dimensions * T = underlying single value type (double, float, ...) + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template class Vector : public std::array {