LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2022, 04:20 AM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Cool 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.
 
Old 05-03-2022, 04:54 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
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)
 
1 members found this post helpful.
Old 05-03-2022, 05:26 AM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by pan64 View Post
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?
 
Old 05-03-2022, 05:41 AM   #4
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
actually im not sure but that might have helped some, says they use pixman but now im wondering how pixman makes their libraries.
 
Old 05-03-2022, 05:50 AM   #5
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
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)

Last edited by Michael Uplawski; 05-03-2022 at 05:53 AM.
 
1 members found this post helpful.
Old 05-03-2022, 07:10 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
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.
 
1 members found this post helpful.
Old 05-03-2022, 08:59 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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 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 ...

Last edited by sundialsvcs; 05-03-2022 at 09:01 AM.
 
Old 05-03-2022, 09:21 AM   #8
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,000

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
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
 
Old 05-03-2022, 11:06 AM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
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.
 
Old 05-03-2022, 11:23 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
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.
 
Old 05-04-2022, 08:39 AM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
I remember those days, "pan," and right now do not want to be reminded of them ...
 
Old 05-04-2022, 02:21 PM   #12
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
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.
 
Old 05-05-2022, 06:58 AM   #13
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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
 
Old 05-05-2022, 09:00 AM   #14
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,146
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
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)

Last edited by teckk; 05-05-2022 at 09:01 AM.
 
Old 05-06-2022, 07:39 PM   #15
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 493

Rep: Reputation: 182Reputation: 182
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sound and Graphics not working in Intel D101GGC with ATI graphics and Realtek audio sanu1 Linux - Hardware 2 12-28-2008 07:29 PM
graphics programmes and wacom graphics tablets? bigjohn Linux - Newbie 9 05-29-2005 05:03 PM
getting graphics card to be used instead of integrated graphics Shaun32 Fedora - Installation 5 02-18-2005 03:33 PM
where to find S3 graphics pro savage DDR graphics jcolon Linux - Hardware 5 02-16-2005 04:53 PM
Graphics issues with Intel 82856G Graphics Adapter herrmag Linux - Newbie 1 08-09-2004 02:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration