LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C graphics program - mystery message (https://www.linuxquestions.org/questions/programming-9/c-graphics-program-mystery-message-4175597458/)

Neophyte1948 01-14-2017 12:05 PM

C graphics program - mystery message
 
I have modified some c code that I found on the net. It is the start of a simple card game and simply draws six rectangles;
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd = DETECT,gm,xt=10,r;

initgraph(&gd,&gm,NULL);
for (r=0;r<6;r++){
rectangle(xt,10,xt+72,96);
xt+=80;
}
delay(5000);

closegraph();
return 0;
}



The program compiles and executes but sometimes I get the following message:
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
pcard: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
I am at a loss to explain as sometimes it executes without any message at all.
Could some-one enlighten me please?

pan64 01-14-2017 12:16 PM

you gave almost no information, so would be nice to explain: what kind of c code did you modify? How did you compile your code?
please use [code]here comes your code[/code] to keep formatting.
http://catb.org/~esr/faqs/smart-questions.html

NevemTeve 01-14-2017 12:56 PM

Well, initgraph, closegraph etc did work well in MS-Dos. For X Window, they might not be the best choice. I would try Java if I were you.

Neophyte1948 01-14-2017 03:31 PM

Pan64 :: What information do you need? I used the GNU compiler - gcc -o "file" -lgraph "file.c"
In the code (pasted from the source) Only the 'for loop' is mine.
The problem is exactly as stated.
I have no more information apart from Linux version is Ubuntu 16.04.
for future reference, What specific information should I have included?

NevernTeve At 69 I have no wish to embark on learning a new language !! Ihave already written most of the underlying code( in c)
The code above is an experiment to produce a visual accompaniment to this.

NevemTeve 01-14-2017 11:15 PM

http://savannah.nongnu.org/projects/libgraph/
Seems to be outdated/abandoned...

DeeDeeK 01-15-2017 05:21 PM

stick with C programming. That's where the power is and it makes your smarter
 
Are you using XCB or Xlib-xcb, or Xlib? I see [xcb] preceding each message you listed.

Does your code have an event loop responding to x events? Might sound like a silly question but it's very possible to ignore x events. The word 'queue' makes me wonder whether you're dealing with events. Really, more information is needed here. Post your code? Description of which x calls, which headers you're including? Do you go fullscreen or in an on-screen window? Do you interact with the window manager? So many questions come to mind!

Perhaps use just xcb calls, instead of xlib? xcb is thread safe.

Threaded code is somewhat nondeterministic so having code which sometimes works and sometimes doesn't isn't a rare problem.

Neophyte1948 says
Quote:

NevernTeve At 69 I have no wish to embark on learning a new language !! Ihave already written most of the underlying code( in c)
The code above is an experiment to produce a visual accompaniment to this.
I've no idea why we C programmers are so frequently told to change languages as an answer to questions about how to make our C code run correctly or questions about error messages we get, our code's unpredictable behavior, etc.

It's like saying "how do I change the oil in my car?" and being told "buy an electric car." Non-sequitor. It's not even an answer, because it isn't meaningfully related to any question we've asked.

That said, it is very hard to guess the cause of the messages without seeing any code. Have you used GDB? I am not smart enough to figure it out, myself, but I read good things. I use Valgrind a lot because it'll tell me where my code messed up many times.

I do a lot of graphics programming and finally made my own little library just using primitive-just drawing pixels or rectangles, actually, with XCB calls.

NevemTeve 01-16-2017 07:35 AM

Nonetheless, you could read the Original Post: the complete program is pasted; it uses a TurboC-compatibility library called libgraph.so

@Neophyte1948: Please edit your first post and add [code] and [/code] tags.

Neophyte1948 01-16-2017 02:15 PM

DeeDeeK

I appreciate your comments no end. Truth to tell I have been stumbling from one web-site to another trying to find a simple linux graphics package, and have installed all sorts of libraries in response to various suggestions as to why the simple things I am trying to achieve have all failed. I have no experience in graphics of any kind but have some basic c coding practice. I intend(ed) to write a program for a friend to practice playing the "cribbage" card game.
I suspect that the code that I copied uses some underlying calls to the X-server which have not been properly terminated but I have no Idea what. Perhaps you could give me some useful links, Ultimately I wish to place six .jpg images of cards on a green-coloured screen and Place a scoring peg-board in the centre. I would be happy with drawing the cards etc. from scratch, as it would all be a good learning experience for me.

Thanks in hopeful anticipation
Neophyte 1948

I am not sure how to add tags to my original post.

astrogeek 01-16-2017 02:56 PM

Quote:

Originally Posted by Neophyte1948 (Post 5655837)
I am not sure how to add tags to my original post.

To place your code snippets inside [CODE]...[/CODE] tags for better readability, you may type those yourself or click the "#" button in the edit controls.

norobro 01-16-2017 09:08 PM

@Neophyte1948 - Take a look at SFML: https://www.sfml-dev.org/ It has very good documentation and tutorials.

I have only dabbled with it in c++ but it has c bindings: http://www.sfml-dev.org/download/csfml/

EDIT: Forgot to mention it is in the repos: http://packages.ubuntu.com/search?keywords=sfml

Neophyte1948 01-17-2017 05:04 PM

Thanks to all who responded to my post. I think I will have to remove some spurious libraries and make a fresh start!
Special thanks to
"DeeDeeK" for your support and sugestions on my orignal post, and to noboro for the pointer (excuse pun),to SFML it looks promising - have not come across that in my searches.
Hopefully the next time I have to bother you good people I will be able to ask a question in a more answerable form.

DeeDeeK 01-17-2017 05:26 PM

I quote Neophyte1948:
Quote:

X-server which have not been properly terminated but I have no Idea what. Perhaps you could give me some useful links, Ultimately I wish to place six .jpg images of cards on a green-coloured screen and Place a scoring peg-board in the centre. I would be happy with drawing the cards etc. from scratch, as it would all be a good learning experience for me.
I'm thinking, that since you wouldn't mind drawing the cards yourself, and it seems that you're interested in expanding your horizons, that a little Xlib and XCB programming would be fun. I'm NOT trying to say this is the 'best' or even most sensible way. It's what I've explored and I found fun and challenging. I don't like lots of high level libraries 'helping' me but really hiding a lot of the details and information I'm interested in learning, for ease and expediency's sake-both valid motivations. But not MY motivation.

YouTube resists allowing me to view anything on Firefox and I'm too annoyed with it to fix the problem. I use video download helper to download anything I want to see, to watch later anyhow. BUT, here are two URLs of recordings of the output of some test code of mine. You'll see my name there and there will be a few more examples for you to see of what you can do with only the information you'll find at freedesktop.org's XCB tutorial pages. I think. You'd need to know how to draw circles and the Newton's formula for gravity and, umm, one for elastic collisions. They're out there to be found on the web.

https://www.youtube.com/watch?v=xeoXl3wzWD8

https://www.youtube.com/watch?v=GcVE2tUhi1k

I'm going to list some URLs, and I'll be happy to think it through more and post more info. I'm actually thinking of writing some "how I taught myself to to do" articles (as opposed to "how-to" articles) documenting my learning process. I like C, I don't want to use any other language, and I want to do it the hard way. More power and ultimately more flexibility as available that way.


https://www.freedesktop.org/wiki/

https://www.x.org/releases/X11R7.7/d...ial/index.html

I hope you check out the Xlib manual in HTML by Tronche I mentioned earlier.

I stripped out the init from the library I'm working on, and simplified it. It compiles but I haven't tested it yet.

I in my still incomplete libraries I use pixmaps and do a sort of double buffering and animation and all sorts of jazz, just using the xcb drawing primitives. Actually, just using the filled rectangle primitive. I do all the math and stuff myself. I could use xcb_point_t's instead of rectangles, but I like drawing with big blocky faux-pixels. I learned my first graphics programming on an Atari 400 with 160x96, 4 color or 320x192 2 color(black and white usually) graphics, y'see.

Code:

#include <X11/Xatom.h>
#include <X11/Xlib-xcb.h>
#include <xcb/xcb_atom.h>



xcb_window_t        window;

xcb_gcontext_t        *globalcolorarray;



void //  sets up the window
little_init(int width, int height, int numcolors){

        dpy = XOpenDisplay(NULL);
        connection = XGetXCBConnection(dpy);
        int scrnnumber = DefaultScreen(dpy);
        int fullscreen = no;

        if((!height)||(!width)){
                fullscreen = yes;
                width = (int)DisplayWidth(dpy, scrnnumber);
                height = (int)DisplayHeight(dpy, scrnnumber);
        }
        xRes = width;
        yRes = height;

        uint32_t value_mask;
        uint32_t value_list[2];

        srand((int)clock());

        globalcolorarray = calloc (numcolors, sizeof(xcb_gcontext_t));

        //connection = xcb_connect (NULL, NULL);
        screen = xcb_setup_roots_iterator (xcb_get_setup (connection)).data;

        value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK ;

        value_list[0] = screen->white_pixel;
        value_list[1] =
                XCB_EVENT_MASK_EXPOSURE                        |        XCB_EVENT_MASK_BUTTON_PRESS                |
                XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION |
                XCB_EVENT_MASK_ENTER_WINDOW  | XCB_EVENT_MASK_LEAVE_WINDOW  |
                XCB_EVENT_MASK_KEY_PRESS          | XCB_EVENT_MASK_KEY_RELEASE ;

        window = xcb_generate_id (connection);

        xcb_create_window (connection,
                XCB_COPY_FROM_PARENT,window, screen->root,
                0,0,        xRes, yRes,
                0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
                screen->root_visual, value_mask, value_list
        );

        if(fullscreen){
                Atom atoms[2] = { XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False), None };
                XChangeProperty(dpy, window, XInternAtom(dpy, "_NET_WM_STATE", False),
                  XA_ATOM, 32, PropModeReplace, (unsigned char*) atoms, 1);
        }
        xcb_map_window(connection, window);

        char *title = "[your title goes here]";
        xcb_change_property (connection, XCB_PROP_MODE_REPLACE, window,
                        XCB_ATOM_WM_NAME, XCB_ATOM_STRING,8, strlen(title), title);
        char *iconTitle = "[your title here]";
        xcb_change_property (connection, XCB_PROP_MODE_REPLACE, window,
                XCB_ATOM_WM_ICON_NAME, XCB_ATOM_STRING, 8, strlen(iconTitle), iconTitle);

        value_mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
        value_list[1]= 0;
        globalcolorarray[0]=xcb_generate_id(connection);
        defaultgraphicscontext = xcb_generate_id(connection);

        xcb_create_gc (connection, defaultgraphicscontext, window, value_mask, value_list);
        xcb_create_gc (connection, globalcolorarray[0], window, value_mask, value_list);

        value_mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES ;//| XCB_GC_FONT ;
        value_list[0] = screen->black_pixel;
        value_list[1]= 0;


        whitegraphicscontext = xcb_generate_id(connection);
        globalcolorarray[1] = xcb_generate_id(connection);
        value_list[0] = screen->white_pixel;

        xcb_create_gc (connection, whitegraphicscontext, window, value_mask, value_list);
        xcb_create_gc (connection, globalcolorarray[1], window, value_mask, value_list);

} //little_init

Get that function working right and then figure out a struct which works with this snippet:
Code:

                while (temp_rectlist){
                        xcb_poly_fill_rectangle (connection, window, temp_rectlist->gc,
                                temp_rectlist->numrects, temp_rectlist->rectarray
                        );
                        temp_rectlist = temp_rectlist->nextcolor;
                }

the struct temp_rectlist has a xcb_gcontext_t, an int, and an xcb_rectangle_t* in it.

the declaration
Code:

xcb_gcontext_t *globalcolorarra;y
can be declared
Code:

xcb_gcontext_t globalcolorarray[n];
if you like.

Instead of changing the foreground color of the graphics context I'm using, I just make a palette of different graphics contexts in an array. I've made over a million individual xcb_gcontext_t's and not run out of space. It just takes a while. But then you can make a function like
Code:

plot(int color, short x, short y, xcb_window_t window);
using int color as an index into your array of pre-prepared graphics contexts.

I'll leave it to you and those URLs to figure how to make it work. I'd learn Bresenham's line drawing a circle algorithms and I bet you can figure out rectangles and so forth from there ;-)

I started this graphics project to learn C and learn about X11 graphics, and how to use GCC and program on a GNU/Linux system, so I'm a real bare-bones guy. If you find junk in that code, or if it doesn't fully work, that's OK. I didn't try to give you a perfect example. I find using xlib-xcb to be fun and challenging, and I bet you'll enjoy learning to create analogous functions to the ones you find in graphics.h!

Remember if someone discourages you from learning something to ignore them! And take advice from people who can show you something. That's how I'm learning what I'm learning. Rather than listen when someone tells me "it won't work that way" I ask myself "how can it work ALMOST that way."

DeeDeeK 01-17-2017 10:05 PM

I forgot to mention I'd checked out your posted code and since the workings of graphics.o or graphics.c (to go with graphics.h I'm imagining) it's impossible for me to guess at the error other than to say I'd bet dollars to donuts the mistake is not yours.

I jammed all the code in my last post to encourage you to spread your wings and explore. Of course if you're more linearly oriented, and just want to write the game straightaway, what I've suggested might be a bit circuitous.

You could use GTK+ and Cairo. Another learning curve. But that graphics.h is a bit of a dead end.

I've learned a bit about them. And also how to use Cairo with Xlib-xcb. You can tell it to render all it's fancy antialiased graphics in a window or pixmap a la XCB.

Here's another source of information I've found very useful: http://zetcode.com/

Neophyte1948 01-18-2017 04:38 AM

Sorted to my satisfaction [SOLVED]
Is it me or would it make sense to add a [solved] button at the bottom of the post with the other options?

rtmistler 01-18-2017 06:31 AM

Quote:

Originally Posted by Neophyte1948 (Post 5656657)
Sorted to my satisfaction [SOLVED]
Is it me or would it make sense to add a [solved] button at the bottom of the post with the other options?

Up on the right, Thread Tools is a pull down menu in the webpage. In there is an option for you to mark this thread as Solved.


All times are GMT -5. The time now is 11:22 PM.