3

Okay, I'd like to write a simple C app for Linux (say Ubuntu with Gnome) that would do the following:

  1. Open a Window
  2. Draw something in that window using a main loop, e.g. the current loop number. I don't want to use controls, but to draw directly on the window surface
  3. Close the window & the app

I can do that in Windows, but I've no idea how I could do that in Linux.

Thanks!

Albus Dumbledore
  • 12,368
  • 23
  • 64
  • 105

6 Answers6

4

Unless you want a full-blown GUI (in which case I'd recommend Qt or GTK), then SDL is a very popular and extremely simple free cross-platform library that gives you a drawing surface and some simple IO facilities. It's natively C, but has bindings to a large number of other languages.

gspr
  • 11,144
  • 3
  • 41
  • 74
3

There are various "Hello World" examples for X11 programming.

Using GTK+: http://library.gnome.org/devel/gtk-tutorial/2.13/c39.html

Using Qt: http://doc.qt.nokia.com/latest/tutorials-widgets-toplevel.html

Using wxWidgets: http://www.wxwidgets.org/docs/tutorials/hello.htm

There are a lot more toolkits: Fox, FLTK, Tk, EFL ...

So far these have all been cross-platform, so let's have a look at X11-specific exampls:

This is using Xlib: http://en.literateprograms.org/Special:Downloadcode/Hello_World_(C,_Xlib)

And this is using Xcb: http://xcb.freedesktop.org/tutorial/basicwindowsanddrawing/

datenwolf
  • 159,371
  • 13
  • 185
  • 298
2

If you only want to draw something, why not just use OpenGL and GLUT. The latter provides simple methods to create a window with an OpenGL context.

Setting up a GLUT application is very straighforward and there are lots of tutorials out there , e.g. Lighthouse3d.com. This tutorial works with visual studio, but it's not hard to translate this to compiling an application on Linux.

Alternatively, you could also work with Qt, which is a more advanced and easy to use GUI toolkit, and which would not necessarily require you to write OpenGL code.

Ithildin
  • 56
  • 2
2

Since you mentioned C, there is Glade if you want to make use of GTK+ for a nice little editor that allows you to draw controls onto a window.

Alternatively if you have access to a C++ compiler you can have a look at Qt which provides similar functionality.

Daniel Sloof
  • 12,568
  • 14
  • 72
  • 106
1

Well, if you're familiar with making gui apps in windows I'm going to take a guess that you've done it with .net or something similar. An easy transition would be to use mono. A cross platform .NET development platform - http://mono-project.com/Main_Page

There's also has a variety gui toolkits to use: http://www.mono-project.com/Gui_Toolkits

digitalWestie
  • 2,647
  • 6
  • 28
  • 45
1

If you want to draw directly onto the window, have you considered X11?
It's not going to be as nice as working with a toolkit like GTK or Qt, but it's about as low level as you can get in the windowing system.

I don't have any experience with programming straight X11, so I can't recommend any starting material.

jonescb
  • 22,013
  • 7
  • 46
  • 42