Giter VIP home page Giter VIP logo

Comments (4)

devinacker avatar devinacker commented on August 16, 2024

I personally like the struct system a lot, even though the code that actually generates them is kind of a huge hack. The performance difference is probably pretty minor (if even noticeable) compared to the convenience of being able to define a C-style struct in just a few lines instead of having to manually handle all the fields and properties of every data structure.

from omgifol.

devinacker avatar devinacker commented on August 16, 2024

After considering adding "texture to image" functions, I started to see why ditching the struct system in favor of normal classes might be beneficial. But instead, I came up with a semi-hacky "best of both worlds" sort of alternative.

In omg.util:

from contextlib import contextmanager

@contextmanager	
def structmethods(name):
	oldglobals = globals().keys()
	yield
	newglobals = [i for i in globals().keys() if i not in oldglobals]
	for key in newglobals:
		setattr(name, key, globals()[key])
		del globals()[key]

In omg.txdef:

TextureDef = make_struct(
  "TextureDef",
  """Class for texture definitions""",
  [["name",     '8s', "-"],
   ["dummy1",   'i',  0  ],
   ["width",    'h',  0  ],
   ["height",   'h',  0  ],
   ["dummy2",   'i',  0  ],
   ["npatches", 'h',  0  ]],
  init_exec = "self.patches = []"
)

with structmethods(TextureDef):
    def to_pixels(self):
        pass

    def to_raw(self, tran_index=None):
        pass

    def to_Image(self, mode='P'):
        pass

Now, those three functions are exclusively methods of TextureDef and aren't part of omg.txdef's global namespace. You could probably also do away with init_exec and add a proper constructor this way, too.

from omgifol.

devinacker avatar devinacker commented on August 16, 2024

Alternately, a much less hacky way (that'd require me to actually rewrite the struct generation code) would be to just use a class decorator, i.e.:

@wadstruct
class TextureDef:
    """Class for texture definitions"""

    __fields__ = [
      ["name",     '8s', "-"],
      ["dummy1",   'i',  0  ],
      ["width",    'h',  0  ],
      ["height",   'h',  0  ],
      ["dummy2",   'i',  0  ],
      ["npatches", 'h',  0  ],
      ["patches",  'x',  [] ]
    ]

    def to_pixels(self):
        pass

And then the decorator would basically generate the same fields, methods and properties without having to do a bunch of shit involving exec()-ing formatted strings. This is probably what I'll actually end up doing.

(The way the existing struct system actually defines fields is something I want to keep intact, one way or the other)

(Also, texture-to-image functions will probably have to take an additional parameter with all the actual patch lumps but I didn't bother reflecting that in either example)

from omgifol.

devinacker avatar devinacker commented on August 16, 2024

https://gist.github.com/devinacker/65cb931709d601d832e43852d922abe0

Finally working on a solid idea for how to do this.

The meat of the example:

class Vertex(Struct):
	"""Represents a map vertex""",
	
	__fields__ = [
	("x", "h", 0),
	("y", "h", 0),
	]
	
	def foo(self):
		print("My coordinates are ({0}, {1}).".format(self.x, self.y))

from omgifol.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.