Comments (4)
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.
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.
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.
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)
- Crash calling to_Image() on graphics lump objects HOT 4
- Create setup.py
- Polygon functions for usable sector shapes
- tall patches
- Graphic.to_Image() doesn't support transparency HOT 2
- TextureDef.to_Image()
- use standard data sizes in Graphic.to_raw
- PNAMES entries not being stripped correctly in py3
- omg.WAD should support reading from read-only WAD files. HOT 1
- Discrepancy between same map data pre-and-post to_lumps HOT 4
- mirror.py demo script has issues saving certain maps HOT 5
- randomly generate maps with different layouts HOT 6
- ACS script is not written into the new wad file HOT 3
- UDMF support HOT 1
- pk3 file support HOT 1
- Potential PK3 Support HOT 1
- inconsistent api for creating lumps from various classes HOT 3
- complains about missing SECTORS lump, but it's there HOT 4
- How to set started location and goal? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from omgifol.