I was just wondering what programmers do when the write a library. For example, how did programmers write openGL , and how hard is it to write a custom library?
OpenGL isn't a library, it's a specification. However, there are libraries such as Mesa 3D that implement OpenGL.
and how hard is it to write a custom library?
That depends on what kind of library you're writing. It ranges from so easy that a rock could write it (a library with no functionality) to impossible (e.g. a library that destroys the universe).
So if i wanted to write a very simple 2d graphics library that can draw a white square, how can that be done. Would I have to say which pixels go white?
If you were to write a graphics library that would really not be anything simple. Not even close to simple. A simple example would be a library that provides a few functions for analytic geometry, like classes for vectors and matrices and stuff.
Writing a library is no different from any other programming, so basically ask yourself "can I do this?" - and then you know whether you can write a library for it or not.
A 2D library isn't particularly hard, as there's no complex logic or math involved. You just need to make sure all pixels end up where they belong. A 3D library is another matter, though.
Either way, in the end your library will have to use functions provided by your OS to draw something on the screen.
Thanks! That clears it up a bit, but now the only thing I don't know is how does the computer know it's graphical or not?
It doesn't know and doesn't care.
So what exactly do you mean?
Writing a 2D graphics library would require some sort of hardware communication to get things done with reasonable speed. You could greatly simplify this by building on top of something else, like OpenGL or D3D, but as the question was referring to how THESE are written I certainly wouldn't say it is "simple".