C GUIs in Linux

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Mortabis
Posts: 50
Joined: Fri Feb 08, 2008 12:24 am UTC

C GUIs in Linux

Postby Mortabis » Fri Feb 08, 2008 12:49 am UTC

I'm guessing a lot of people here know that currently the AP Computer Science test is in Java. As a result, my introductory CS class is taught in Java. Personally, I think it's not a bad language to learn in, but it has a lot of issues once you're past the basics. My CS class is mainly about the mathematics, so my instructor says he doesn't mind what language we do our labs in, but he'll only teach us Java. I grabbed a book on C from the library, which turns out to be written by the creators of the language. In just a couple days I think I've gotten the hang of it. It's a great language and I'd love to switch to it, but there is no standard library for GUIs, which are required for some of the labs.

Now, getting straight to the point: How do I make a GUI in C, for a Linux environment?

From what I've managed to find, GTK+ is the API I'll be using. (It is an API, right?) But...how do I use it? Or should I use a different library?

Thanks in advance.
Image

mrkite
Posts: 336
Joined: Tue Sep 04, 2007 8:48 pm UTC

Re: C GUIs in Linux

Postby mrkite » Fri Feb 08, 2008 2:12 am UTC

Mortabis wrote:From what I've managed to find, GTK+ is the API I'll be using. (It is an API, right?) But...how do I use it? Or should I use a different library?


Here's a tutorial for writing gtk apps. I recommend learning how to use Glade, which lets you use a GUI to design your Gtk apps.
http://library.gnome.org/devel/gtk-tutorial/stable/

User avatar
Mortabis
Posts: 50
Joined: Fri Feb 08, 2008 12:24 am UTC

Re: C GUIs in Linux

Postby Mortabis » Fri Feb 08, 2008 2:52 pm UTC

Okay, thanks for the link. Wow, it looks a lot more complicated than swing. But probably a lot better too. :)

Most of what we do with GUIs is stuff like computer-generated images, such as graphs, and more recently recreating that screensaver where the stars are flying by. That means for the most part, all I need are text labels and how to draw rectangles, so that I can work pixel by pixel.
Image

darren
Posts: 188
Joined: Fri Feb 08, 2008 5:37 pm UTC

Re: C GUIs in Linux

Postby darren » Fri Feb 08, 2008 5:47 pm UTC

Last edited by darren on Tue Sep 09, 2008 12:12 am UTC, edited 1 time in total.

mrkite
Posts: 336
Joined: Tue Sep 04, 2007 8:48 pm UTC

Re: C GUIs in Linux

Postby mrkite » Fri Feb 08, 2008 6:20 pm UTC

Mortabis wrote:Most of what we do with GUIs is stuff like computer-generated images, such as graphs, and more recently recreating that screensaver where the stars are flying by. That means for the most part, all I need are text labels and how to draw rectangles, so that I can work pixel by pixel.


Sounds like instead of a widget library like Gtk you would be just fine with SDL.

http://www.libsdl.org/

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: C GUIs in Linux

Postby EvanED » Fri Feb 08, 2008 7:09 pm UTC

mrkite wrote:Sounds like instead of a widget library like Gtk you would be just fine with SDL.

http://www.libsdl.org/

Actually, this is a really good idea. If you're just doing pixel drawing rather than using widgets, this is very likely easier.

User avatar
Mortabis
Posts: 50
Joined: Fri Feb 08, 2008 12:24 am UTC

Re: C GUIs in Linux

Postby Mortabis » Fri Feb 08, 2008 8:59 pm UTC

Thanks! I'm really glad I asked now...SDL looks somewhat easier to learn. But I only know basic C, no opengl. SDL looks easy to learn. But I don't know OpenGL, only basic C. And OpenGL seems to be needed for SDL, unless I'm mistaken?

The only books available for OpenGL in the library were 3 times as thick as the C book (though to be fair the C book was 272 pages). All I want out of whatever library I'm using is a really easy function to make rectangles, because any other shape I can comprise of 1x1 rectangles. Scanning the entire screen each time I redraw something isn't that pleasing to me, but I'm fine with that if absolutely necessary. And I imagine that learning how to use SDL will help me with real applications in the future. But for the moment, if there's something even simpler...I'd love to know.
Image

HappySmileMan
Posts: 52
Joined: Fri Nov 09, 2007 11:46 pm UTC

Re: C GUIs in Linux

Postby HappySmileMan » Fri Feb 08, 2008 9:06 pm UTC

Mortabis wrote:Thanks! I'm really glad I asked now...SDL looks somewhat easier to learn. But I only know basic C, no opengl. SDL looks easy to learn. But I don't know OpenGL, only basic C. And OpenGL seems to be needed for SDL, unless I'm mistaken


No it's not AFAIK, you only need it if you want accelerated 3D rendering, which would be very complicated I'm assuming, and wouldn't be needed for anything like what you're doing.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: C GUIs in Linux

Postby EvanED » Fri Feb 08, 2008 11:14 pm UTC

Even using OpenGL for the stuff you are talking about wouldn't be too bad. Once you do some initialization stuff, drawing a rectangle with a specific color would take 7 lines which could be easily encapsulated in a function:

Code: Select all

void drawRectangle(double x, double y, double height, double width, int color_r, int color_g, int color_b)
{
    glBegin(GL_QUADS);
        glColor3f(color_r / 255.0, color_g / 255.0, color_b / 255.0);
        glVertex3f(x, y, 0);
        glVertex3f(x+width, y, 0);
        glVertex3f(x+width, y+height, 0);
        glVertex3f(x, y+height, 0);
    glEnd();
}


Managing the scale would probably be the most difficult part; one way to do it would be to set up the camera so that the window covered the range [0,1]x[0,1]. Then a rectangle a quarter of the area of the screen and centered would be drawRectangle(0.25, .025, 0.5, 0.5, ...). The last three parameters give the RGB coordinates of the color. This function takes them as an integer between 0 and 255; they are divided by that later because OpenGL takes them as a floating point number between 0.0 (black) and 1.0 (full intensity).

While I think you could deal with this, using SDL_surface and such would probably be easier. See here; that may have some good information. (Page 5 has an example.)

nteon
Posts: 26
Joined: Sun Oct 07, 2007 9:15 pm UTC

Re: C GUIs in Linux

Postby nteon » Fri Feb 08, 2008 11:53 pm UTC

yes, and the same code in Cairo is 3 lines :)

Code: Select all

cairo_set_source_rgb[a] (context, red, green, blue[, alpha]);
cairo_rectangle (context, x, y, width, height);
cairo_fill (context);


I spent 3 months on a project which did all its drawing with openGL (not through the SDL, just with the C# wrappers for the OpenGL API), it was a pain in the butt and I never did get text rendering quite the way I wanted it. I ported the whole thing to Cairo in a matter of hours, it looked nicer on some hardware (curve edges were antialiased automatically, with OpenGL I used multisampling which isn't supported on older hardware, or through some virtual machines like parallels), and its a lot easier to understand and maintain.

I've never used SDL, but if you have to use OpenGL with it, for these projects do yourself a favor and use Cairo. Once your comfortable with C and want to do something actually 3D (why use a 3D api to only draw 2d points, shapes and paths when thats what cairo was designed for?), pick up OpenGL.
It's 106 miles to Chicago, we got a full tank of gas, half a pack of cigarettes, it's dark, and we're wearing sunglasses. Hit it.

User avatar
Mortabis
Posts: 50
Joined: Fri Feb 08, 2008 12:24 am UTC

Re: C GUIs in Linux

Postby Mortabis » Sat Feb 09, 2008 2:56 pm UTC

Okay, thanks :) I feel like each time I reply someone posts an API that is 10 times easier to use. Is there any way to move rects in cairo?

/me thinks he should probably read the documentation

And apparently firefox 3 is going to use cairo. Sounds like a winner.
Image

darren
Posts: 188
Joined: Fri Feb 08, 2008 5:37 pm UTC

Re: C GUIs in Linux

Postby darren » Sat Feb 09, 2008 8:30 pm UTC

Last edited by darren on Tue Sep 09, 2008 12:13 am UTC, edited 1 time in total.

User avatar
Mortabis
Posts: 50
Joined: Fri Feb 08, 2008 12:24 am UTC

Re: C GUIs in Linux

Postby Mortabis » Sat Feb 09, 2008 8:38 pm UTC

That's alright, I'm used to moving rectangles by drawing over the old one and then creating a new one. And most images I make are static.
Image


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests