How can I write my own GUI libraries and API in C and C++?

At the very bottom of X is two types of drawables: pixmap and windows. These are just a series of bytes that represent the window. The other piece is the visual which determines how to interpret those bytes. Thought it might be rare to find now you might have a monochrome screen that can show only black and white. In that case general a single bit is used per pixel on screen. If this bit is on the pixel is white, if it is off the pixel is black. If the screen is 800×600 you need
800 / (8 bits / 1 bpp) = 100 bytes per line * 600 lines = 60,000 bytes to represent the screen

Now let's instead say you have a screen capable of showing 16 colors. Since you have 16 colts you need 4 bits for each pixel to choose the color for each pixel. Now youonly get 2 pixels per byte. An 800 x 600 screen is now

800 / (8  / 4 bbp ) = 400 bytes per line * 600 ines = 240,000 bytes

If you have a true color screen with 256 values for each of the three color components you end up with 3 bytes per pixel. The same 800×600 screen is now

800 * 3 = 2,400 bytes per line * 600 lines 1,440,000 bytes to represent the screen.

Generally that memory is viewed as a linear array of bits that represent the pixels. On the black and white screen if you want to draw horizontal line you need to determine which byte each bit falls in and turn it on using an assignment for full bytes and bit operations for bytes that aren't fully covered. For each of these visuals you will need to determine how to set the pixels for each primitive you want to create such as set_pixel, draw_line, draw_circle, etc.

You'll also need all the X calls to create a window, get the visual the screen si using, get window size, handle events, etc. You'll find these in Xlib and XCB the X client libraries. That is the very bottom level if you want to you it all yourself and it will be a great long term project.

There are other libraries you can use to do all the really low level work like create and managing windows and drawing lines and such. Fortunately the base X client library Xlib has graphic primitives for you like XFillRectangle(). But you can also go with GDK which will work with more underlying graphic device interfaces and allow easy porting to Mac, Windows, etc. Others will combine graphic primitives with some widgets like FTLK. If you're familiar with OpenGL you might choose to use the GLX extension will will let you handle display lists and be fairly portable while still remaining very low level. Though if you want the whole experience stick to the bits above then you can say it was all you (except for X — you can without that as well if you want and use the frame buffer directly though\now we're talking life long project).

Answer by Mick Stute:

At the very bottom of X is two types of drawables: pixmap and windows. These are just a series of bytes that represent the window. The other piece is the visual which determines how to interpret those bytes. Thought it might be rare to find now you might have a monochrome screen that can show only black and white. In that case general a single bit is used per pixel on screen. If this bit is on the pixel is white, if it is off the pixel is black. If the screen is 800×600 you need
800 / (8 bits / 1 bpp) = 100 bytes per line * 600 lines = 60,000 bytes to represent the screen

Now let's instead say you have a screen capable of showing 16 colors. Since you have 16 colts you need 4 bits for each pixel to choose the color for each pixel. Now youonly get 2 pixels per byte. An 800 x 600 screen is now

800 / (8  / 4 bbp ) = 400 bytes per line * 600 ines = 240,000 bytes

If you have a true color screen with 256 values for each of the three color components you end up with 3 bytes per pixel. The same 800×600 screen is now

800 * 3 = 2,400 bytes per line * 600 lines 1,440,000 bytes to represent the screen.

Generally that memory is viewed as a linear array of bits that represent the pixels. On the black and white screen if you want to draw horizontal line you need to determine which byte each bit falls in and turn it on using an assignment for full bytes and bit operations for bytes that aren't fully covered. For each of these visuals you will need to determine how to set the pixels for each primitive you want to create such as set_pixel, draw_line, draw_circle, etc.

You'll also need all the X calls to create a window, get the visual the screen si using, get window size, handle events, etc. You'll find these in Xlib and XCB the X client libraries. That is the very bottom level if you want to you it all yourself and it will be a great long term project.

There are other libraries you can use to do all the really low level work like create and managing windows and drawing lines and such. Fortunately the base X client library Xlib has graphic primitives for you like XFillRectangle(). But you can also go with GDK which will work with more underlying graphic device interfaces and allow easy porting to Mac, Windows, etc. Others will combine graphic primitives with some widgets like FTLK. If you're familiar with OpenGL you might choose to use the GLX extension will will let you handle display lists and be fairly portable while still remaining very low level. Though if you want the whole experience stick to the bits above then you can say it was all you (except for X — you can without that as well if you want and use the frame buffer directly though\now we're talking life long project).

How can I write my own GUI libraries and API in C and C++?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s