LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using C for Graphics (https://www.linuxquestions.org/questions/programming-9/using-c-for-graphics-4175711610/)

errigour 05-03-2022 04:20 AM

Using C for Graphics
 
Hello again fellow users, I want to ask a question that maybe someone can point me in a direction that is helpful. I have in the past learned you can make your own libraries and header files for c programs like stdio.h but I want to know how does someone like x11 make a c program render graphics? is there any way to make my own functions that will print colors on the screen and text on the screen without using someone elses graphics library? How are they doing it? I want to make my own graphics library for fun and maybe learn something. Thanks for your time answering my questions.

pan64 05-03-2022 04:54 AM

There are a lot of different ways to do graphics.
Probably I would start here: https://www.x.org/wiki/Development/ (if I understand it well)

errigour 05-03-2022 05:26 AM

Quote:

Originally Posted by pan64 (Post 6350432)
There are a lot of different ways to do graphics.
Probably I would start here: https://www.x.org/wiki/Development/ (if I understand it well)

Hey thanks but i want to know how they make the libraries that do what their libraries do not learn how to use their libraries. Have any idea how they do it?

errigour 05-03-2022 05:41 AM

actually im not sure but that might have helped some, says they use pixman but now im wondering how pixman makes their libraries.

Michael Uplawski 05-03-2022 05:50 AM

You want to program against hardware in the way of “libraries” (and other stuff) that you do not want to use. A graphics-driver. Look there.

For the time when you are done, bookmark this:
https://www.x.org/releases/X11R7.5/doc/libxcb/tutorial/

... then a few of the others (that you do not yet want to use)

pan64 05-03-2022 07:10 AM

if you want to write directly into the video you need to write first a kernel driver and a library to use that driver, finally you can implement an app which can use that library.
Otherwise you must use libraries implemented by others.

sundialsvcs 05-03-2022 08:59 AM

Wisdom read at the bottom of a bird cage:

Quote:

Actum Ne Agas: "Do Not Do A Thing Already Done."
Somebody else already worked out :banghead: a bunch of graphics libraries for you. Even linked them easily to easier-to-use interpreted languages like Python. Did all the hard stuff so you don't have to. Just grab it and go ...

EdGr 05-03-2022 09:21 AM

I recommend using GTK 3 for GUIs and Cairo for 2D drawing.

There is a lot of plumbing between the high-level graphics APIs and the hardware. You don't want to re-invent that. Rather, you can draw pixels on a Cairo image surface and call Cairo to display them. This provides enough flexibility to draw anything.
Ed

dugan 05-03-2022 11:06 AM

Target a retro platform if you don’t want to use libraries. In a modern platform, the lowest level you could really touch is the graphics driver, and you use libraries to interact with that.

pan64 05-03-2022 11:23 AM

Probably some of you can remember the hercules cards I wrote a program (in assembler) to directly write video memory to draw what I wanted (something like this, just monochrome: https://i.stack.imgur.com/wMyWm.gif), but that was hm, 40 years ago.

sundialsvcs 05-04-2022 08:39 AM

I remember those days, "pan," and right now do not want to be reminded of them ... :)

Michael Uplawski 05-04-2022 02:21 PM

Aaah! Addressing registers...

My Boss did not give me a card with an unknown controller on it and said: "Make it work".

He just said: Find a vendor of this kind of card and write a Linux-driver that we can use in a real-time application (I ordered some books, first).

But hey... here is an argument *AGAINST* using what has already been written: I spent a lot of time talking to the engineers of the company which produced the interface-card and the micro-controller, on the phone and by mail. My learning-curve was close to vertical.

Guttorm 05-05-2022 06:58 AM

There is a way to work with the screen without using any libraries - FrameBuffer. It doesn't work with modern desktop managers, but if you open a console (Control-Alt-F2), you can write to /dev/fb0 and have direct control of the pixels on the screen. Each pixel is 4 bytes - RGBA - and you just use mmap to get an array to work with. I found a guide here:

https://cmcenroe.me/2018/01/30/fbclock.html

teckk 05-05-2022 09:00 AM

Quote:

but I want to know how does someone like x11 make a c program render graphics?
Quote:

Somebody else already worked out a bunch of graphics libraries for you.
Quote:

I recommend using GTK 3 for GUIs and Cairo for 2D drawing.
As others have said, you don't have to reinvent the wheel. That's been done for us.

Use a tool kit, like gtk2, and a graphics drawing/rendering library, like cairo.

Trying to find something that I have, that I can put together, small enough to post, and works, that one could actually see working...

Here, everyone needs to look at a pdf.

pdfviewer.c
Code:

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <cairo.h>
#include <poppler.h>

static PopplerDocument* doc;
static PopplerPage* page;

static void
on_destroy(GtkWidget* w, gpointer data) {
    gtk_main_quit();
}

static gboolean
on_expose(GtkWidget* w, GdkEventExpose* e, gpointer data) {
    cairo_t* cr;
    cr = gdk_cairo_create(w->window);
    poppler_page_render(page, cr);
    cairo_destroy(cr);
    return FALSE;
}

int main(int argc, char* argv[]) {
    GtkWidget* win;
    GError* err = NULL;

    if (argc != 2) {
        printf("Usage: pdfviewer file:///path/to/file.pdf\n");
        return 1;
    }

    gtk_init(&argc, &argv);

    doc = poppler_document_new_from_file(argv[1], NULL, &err);
    if (!doc) {
        printf("%s\n", err->message);
        g_object_unref(err);
        return 2;
    }

    page = poppler_document_get_page(doc, 0);
    if(!page) {
        printf("Could not open first page of document\n");
        g_object_unref(doc);
        return 3;
    }

    int pages = poppler_document_get_n_pages(doc);
    printf("There are %d pages in this pdf.\n", pages);
   
    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(win), "destroy",      G_CALLBACK(on_destroy), NULL);
    g_signal_connect(G_OBJECT(win), "expose-event", G_CALLBACK(on_expose), NULL);
    gtk_widget_set_app_paintable(win, TRUE);
    gtk_window_set_default_size(GTK_WINDOW (win), 780, 580);
    gtk_widget_show_all(win);

    gtk_main();

    g_object_unref(page);
    g_object_unref(doc);

    return 0;
}

//gcc pdfviewer.c -o pdfviewer $(pkg-config --cflags --libs gtk+-2.0 poppler-glib)


rclark 05-06-2022 07:39 PM

Quote:

I remember those days, "pan,"
Me too, in DOS land. At the time (for me), it was fun to write some assembly code to draw points, lines, circles, and other primitives to use in 'C' programs. But now ... SDL2 or some such library is the way to go. It is as close as I need to get to graphics hardware any more! I've even got a thick OpenGL Programming Guide book which I decided after a few pages of reading, I really don't want to learn this! So there it sits.... Save it for the gung-ho youngsters!

Here is another reference I found to a simple 'x11' example :

https://stackoverflow.com/questions/...ng-improvement


All times are GMT -5. The time now is 04:02 PM.