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

  • 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 :)
  • Duplicate post deleted.
  • 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!
  • 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.
  • This post has been deleted.
Sign In or Register to comment.

Howdy, Stranger!

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

Categories