CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Nov 2006
    Posts
    357

    Cross Platform Objects Question

    Hi,

    Ive currently got a library which isnt *exactly* cross platform, but the same principle would apply.

    The current problem i have is that i have my own Point class, that is the building blocks for custom Rectangle, Circle, Polygon classes. Now they all have their own collision detection built in and can be transformed amongst each other.

    Anyway the issue i have is that now i want to draw my polygon to the screen, so it exposes a list of its custom Point objects, but the FillPolygon in GDI takes a list of System.Drawing.Point. So im sure this is a common problem faced by people who do cross platform development.

    So my only option that i can see at the moment is whenever i need to draw convert all internal custom points to .net points and shoot them over. However this would be done every redraw event, meaning it would end up throwing memory away all over the place, which sounds quite bad to me.

    Also this is just for GDI, it may be used with other drawing libraries, but all options seem to end with duplicating the data, i can possibly inherit a custom GDIPoint from that class or something that makes sense, and then have a cached list of points that GDI can use, but is there any trick to this, or do you always have to duplicate your data? (i guess in C/C++ you can manually fudge the memory into the correct objects making it alot faster)

    Any advice would be great!

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Cross Platform Objects Question

    Converting your custom points to the GDI .Net points in the "redraw event" each and every time is not a suggestable method. It might effect your performance.

    As you tell try to inherit a new GDIPoint class from your custom point class and try to manipulate with them instead coverting them each and every time you redraw.

  3. #3
    Join Date
    Nov 2006
    Posts
    357

    Re: Cross Platform Objects Question

    Yeah, thats what sounded the most efficient method, but i just wanted to know if there were any pros on the subject who could share any tips or tricks, as to make a GDIPoint and other XXXPoint i would need some sort of manager or factory in place to dish out the platform specific versions, and then it starts becoming a whole new pile of madness...

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Cross Platform Objects Question

    If this is all in C#, have a look at mono.

    You may be able to port your application without any additional work.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Cross Platform Objects Question

    You're after confusing cross platform with cross-gui toolkit I think.

    If you want your app to be able to render it's shapes using different graphics toolkits, then that'll require you to translate your data into a form that particular graphics toolkit requires. For example System.Windows.Forms versus GTK+. Both can render and draw primitive shapes like Rectangles and Ellipses, but the API is different.

    It's highly unusual for one application to run unmodified on more than one graphics toolkit. In fact, I don't think I've ever seen that While technically it would be possible, you'd be mad to try it If you want two different graphics toolkits, you write two different frontends to your code.

    So what is it exactly that you want to do? Do you want your application to be able to run unmodified on multiple platforms, or do you want it to be able to run using multiple different GUI toolkits?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Nov 2006
    Posts
    357

    Re: Cross Platform Objects Question

    I basically want to be able to run the same classes in GDI/XNA/Silverlight, they all use the same data, but each one has a slightly different Rectangle class to use, so i just wanted one central one that i can use between all of them... I would need to make 3 seperate builds against each coresponding framework anyway, but the more i could keep the same the better...

    This all started out because Silverlight has a Rect class which is ALMOST identical to the normal Rectangle class, however its IntersectsWith() method just seems to waste memory, so i wanted to make my own version that i can use with my winforms toolkit and within the silverlight app. Also i needed to add polygon and circle shape support. After writing it all though i then realised that when i went to draw in GDI or Silverlight, they were expecting system.drawing.point objects and i was handing it my custom ones, so i wanted to find the best way to expose my data for them to use.

    For most calls i can just use the raw x,y values, but for drawing polygons in GDI they require an array of points, which would make me have to copy all the data within my polygon, which lead me to ask this question of the best way to do this so i can keep my own classes as *unbutchered* as possible, while making sure i can expose the relevent objects for the GUI library in question...

    Thanks for all your answers so far!

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: Cross Platform Objects Question

    This all started out because Silverlight has a Rect class which is ALMOST identical to the normal Rectangle class, however its IntersectsWith() method just seems to waste memory
    I'm gonna say this is exactly where it started going wrong for you. What made you think that the silverlight Rect class 'wastes' memory when you use IntersectsWith? I assume you're referring to Rect.Intersect (Rect) by the way. It's a struct so there's going to be no actual memory allocations going on there...

    Anyway, you were going to have to use custom classes/structs regardless of performance. If you want your library to be able to run on GDI, XNA, Silverlight and whatever else, then you have to make absolutely sure that your library does not use *any* type defined in those toolkits. This immediately rules out using any of the pre-existing Point/Shape classes. You should then write a translation layer for each toolkit which will take your Point/Polygon/Rect types and mangle them into the correct form for the current toolkit. Sure, it will involve duplicating some stuff, but don't worry about performance until you profile and see that's where the problem is. You can always add caching so that you don't have to do the translation *every* time.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  8. #8
    Join Date
    Nov 2006
    Posts
    357

    Re: Cross Platform Objects Question

    Yeah pretty much what i thought, i would have to transform the data at some point...

    All the classes in my library are my own, so they dont contain any .net classes or any type...

    When SL does the intersectsWith(Rect) it creates a new rect with the result, as it is passed an immutable Rect so it has to generate a new one to return out with the intersection, so thats a new Rect for every intersection check you do... and i was doing ALOT of them...

  9. #9
    Join Date
    May 2007
    Posts
    1,546

    Re: Cross Platform Objects Question

    so thats a new Rect for every intersection check you do... and i was doing ALOT of them...
    A rect is a struct. This loop will allocate exactly zero memory.

    Code:
    Rect rect;
    for (int i = 0; i < 100000; i++)
        rect = new Rect (0, 0, i, i * 10);
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured