Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coordinate types pretty print #1636

Merged
merged 6 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 62 additions & 18 deletions etc/gdb_pretty/printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand All @@ -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'

Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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
8 changes: 7 additions & 1 deletion libopenage/coord/coord.h.template
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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<typename CoordType, typename Absolute, typename Relative>
struct Coord${camelcase}Absolute {
Expand Down Expand Up @@ -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<typename CoordType, typename Absolute, typename Relative>
struct Coord${camelcase}Relative {
Expand Down
5 changes: 4 additions & 1 deletion libopenage/curve/keyframe.h
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 <typename T>
class Keyframe {
Expand Down
3 changes: 3 additions & 0 deletions libopenage/util/fixed_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ constexpr static
* For example,
* FixedPoint<int64_t, 32>
* 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 <typename int_type, unsigned int fractional_bits>
class FixedPoint {
Expand Down
3 changes: 3 additions & 0 deletions libopenage/util/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <size_t N, typename T>
class Vector : public std::array<T, N> {
Expand Down
Loading