开发者

Framework agnostic struct issue

开发者 https://www.devze.com 2023-01-28 17:31 出处:网络
This is going to be quite a vague question but I am wondering how to get around the issue im facing...

This is going to be quite a vague question but I am wondering how to get around the issue im facing...

I currently have many different projects that I have written a while ago that all have a similar framework to deal with spatial partitioning and sorting, however each project has had its own version of this framework implemented. They are all graphically based applications but have implementations in:

  • Silverlight
  • Winforms (GDI)
  • XNA
  • DirectX
  • OpenGL (TAO)

Although there are 5 different versions of this framework they all do EXACTLY the same thing, just accept different objects. An example would be GDI uses System.Drawing.Point for most of its drawing related tasks whereas XNA uses Vector2 however they are both objects that represent an X/Y, same with the other libraries, some store the x/y values as int,float,double but ultimately at the heart they represent the same data.

The main job of the spatial partitioning framework is to work off these 2/3 dimensional values and generate trees or do other sorting based tasks around these numbers.

Im currently trying to unify alot of my distributed frameworks into a single centralised one so it can have a single project, with tests written to prove everything is working ok.

So my first thought was to create some agnostic objects to deal with this X/Y problem like a custom Point2D, Point3D, Quaternion, Rect etc. However then i would have to convert them back to their native version for rendering, and if i were to do that for each point in each object every render cycle then it sounds like a massive performance penalty (especially when this framework is meant to be as performant as possible).

I was thinking about having interfaces that would solve this problem and just wrap up the native object internally on the implementation part, but im still not sure if this is the best way to go, as ive never really done much cross platform based development so never had to solve these issues.

Anyway will stop waffling now, and any advice would be grea开发者_运维技巧t!


My recommendation would be to implement your own maths types and do all your calculations using those. I would probably use XNA's as a base. Not only because they're pretty good, but also because you can get open-source implementations of them from Mono.XNA, SilverSprite or XNATouch. (And of course the framework itself, in XNA.) Thus saving you a lot of work.

The major problem I can see is that your different platforms use different types. GDI uses integers, XNA, DirectX and OpenGL use single-precision floats (generally), and Silverlight uses double-precision floats.

If that wasn't the case you'd be able to just assign your type to the identically-laid-out native one and depend on the JIT to optimise it away.

The nice thing about OpenGL, DirectX and XNA (excluding things like SpriteBatch) is that you basically tell the API your data layout - and you can use whatever you like internally.

For GDI and Silverlight, you're probably going to struggle to do ultra-high-performance graphics anyway. So I would just take the hit of converting to float and back. Just write the simple conversion functions for when you go to display your data. Converting types will be an extremely tiny, minuscule percentage of your CPU time, compared to rendering.

(I've actually done this for Silverlight - and it really is not worth worrying about.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号