How do I do graphics easily in a modern linux? [closed]
Asked Answered
K

3

-4

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?

Kevon answered 6/5, 2014 at 21:37 Comment(6)
What do you mean equivalent in gcc?Harmonica
I mean, can I just draw something easily, or do I have to spend three pages of code negotiating for permission with X?Kevon
@JohnLawrenceAspden What did you find from your research about graphics.h on Linux?Cons
Here is a demonstration of JavaFX. You have to install JavaFX first but once it is running you can see and play with the code for demo. oracle.com/technetwork/java/javase/overview/…Glutenous
Just a remark: BGI graphics was anything but speedy. It was all right for low-resolution graphics but even then it was far slower than the same graphics routines written in assembly.Finicking
if you want to draw on a window, in java you can use the Graphics class that help you drawing object on a surface, for the console i think you need some os depend library. docs.oracle.com/javase/7/docs/api/java/awt/Graphics.htmlFourlegged
K
4
#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);
}
Kevon answered 12/5, 2014 at 22:4 Comment(0)
I
2

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".

Inelegant answered 6/5, 2014 at 23:13 Comment(0)
G
1

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.

Geoff answered 6/5, 2014 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.