Coding 2D graphics in C, to be portable - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Coding 2D graphics in C, to be portable

Hello all,

Well, this is my first post in Programmer's Heaven.
Recently, I began developing my own programming library, written in C. When I got to the graphics, I became stumped, because I want the library to be portable, on both OS, and hardware. Every way I think of creating graphics, a different architecture or software platform is excluded. For example, if I used the Windows API, only windows, or possibly Wine can run it; if I use assembler, only one architecture can run it, and I REALLY don't want to re-write the assembly routines for multiple architectures (don't think I could do it for many at all =]...). Any ideas?

Comments

  • anthrax11anthrax11 Posts: 511Member
    Hi and welcome!

    I'm not exactly sure what you might need, but you're probably looking for something like SDL or OpenGL:
    http://www.libsdl.org/
    http://en.wikipedia.org/wiki/OpenGL

    I wonder why you would want to make your own library for this. It's great for learning, but not very practical when other libraries already exist that do a great job at it. I also don't think it's sensible to make a library that does everything (at least not all by yourself). That would be reinventing the wheel. Not to discourage or anything, just wondering :)
  • anthrax11anthrax11 Posts: 511Member
    Duplicate post deleted.
  • phishStixphishStix Posts: 2Member
    No, I totally get it, making a library. My only intentions were to be educational; I was never planning on distributing it (or possibly finising it =]). Anyway, I could never even dream of making a library as good as OpenGL, or others. =] Thanks!
  • Malcolm_McLeanMalcolm_McLean Posts: 253Member ✭✭
    Write all your functions so they take a graphics context as the first argument.

    The graphics context should contain the members

    int width;
    int height;

    long (*getpixel)(int x, int y, void *ptr);
    void (*putpixel)(int x, int y, long color, void *ptr);
    void *ptr;

    width and height are self-explantory. getpixel and putpixel anre fuunction pointers which read and write single pixels to the screen. ptr is a general-purpose hook so the functions don't have to rely on global data.

    These days you can pretty much guarantee truecolour graphics. However you'll have to add extra members if you wish to support paletted images nicely.

    Using this abstraction layer, all your algorithms can work perfectly portably.
  • SumitKumarSumitKumar Posts: 2Member
    This post has been deleted.
Sign In or Register to comment.