Question

Here is an example of the sort of C program one could write in the old days:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void main()
{
    int gd=DETECT, gm;

    initgraph(&gd, &gm, "c:\\turboc\\bgi");
    circle(200,100,150);

    getch();
    closegraph();
}

I think this was turbo C under MSDOS. It gives you a drawing on the screen and can be easily extended to do speedy animated graphics such as those found in xscreensaver hacks.

How would I write the equivalent in gcc on ubuntu? Can it be done in Java?

Was it helpful?

Solution

#include<X11/Xlib.h>
#include<stdlib.h>

/* gcc -std=gnu99 -o circle circle.c -lX11 */

int main (int argc, char *argv[])
{

  /* connect to the X server and make a window */
  Display *dpy = XOpenDisplay (getenv ("DISPLAY"));
  Window w = XCreateSimpleWindow (dpy, DefaultRootWindow (dpy),
                                  100, 100, 640, 480, 1,
                                  BlackPixel (dpy, DefaultScreen (dpy)),
                                  WhitePixel (dpy, DefaultScreen (dpy)));

  /* raise it and wait */
  XSelectInput (dpy, w, StructureNotifyMask);
  XMapRaised (dpy, w);
  for(XEvent e; ( e.type != MapNotify );
      XWindowEvent (dpy, w, StructureNotifyMask, &e));

  /* create a graphics context for drawing in the window */
  GC g = XCreateGC (dpy, w, 0, NULL);

  /* draw a circle */
  XDrawArc(dpy,w,g,200,100,150,150,0,360*64);
  XFlush(dpy);

  /*wait for key press*/
  XSelectInput (dpy, w, KeyReleaseMask);
  for(XEvent e; ( e.type != KeyRelease ); 
      XWindowEvent (dpy, w, KeyReleaseMask, &e));

  /*clean up*/
  XDestroyWindow( dpy, w );
  XCloseDisplay (dpy);
}

OTHER TIPS

Ok, few words about basics:

  1. The example you provided uses library called BGI - Borland Graphics Interface - very old stuff from MS DOS era
  2. GCC itself is just a compiler - you must search for a library that supports drawing
  3. On Linux we use several GUI toolkits, but only Gtk and Qt are relevant these days.
  4. If you want low-level graphics library you may look at Allegro (http://alleg.sourceforge.net/) or SDL (http://www.libsdl.org/)

But seriously, I think you're looking at wrong direction. You should focus on modern event-driven GUI programming using modern toolkits (Gtk, Qt), modern languages (C++, C#, Java, Python, etc) and OpenGL for "special effects".

You need to understand that on Linux graphics is generally done thru X11 (perhaps Wayland could become a competitor in the future).

Then you should use some X11 toolkit. If you want it in C, consider GTK or libSDL. But if you know C++, I would recommend Qt (read about its graphics abilities).

You can find some short Qt or Gtk or SDL example programs, in about a hundred lines.

Java has at least Swing.

Notice that Linux is intrinsically a multi-tasking system. So you want to run several graphical programs. In other words, you want several windows (and a window or desktop manager). So, you need an event loop, and you need to take care of resized and/or overlapping windows. Hence the complexity is much bigger than in the TurboC days of the previous century!

Alternatively, consider making your application a specialized HTTP server (and code the graphics in HTML5), e.g. using libonion as a C HTTP server library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top