TkTkorrovi 69 Junior Poster

I have a new version which uses cairo, the graphics is antialiased, and no graphics contexts nowhere... It would be up when i can put it up. I honestly don't see now anything to make better, if someone thinks that anythyng is wrong, then please say... The new version is somewhat slow in windows, i think antialiasing is badly implemented there, if it's slow for you also, take the antialiasing off, no such problems in linux.

TkTkorrovi 69 Junior Poster

Who argues that the code in this game is not that good, it's right, it isn't. But then one may look at the debian source package, how much more horrible it was... What would help here is writing the whole thing from scratch, which is not that difficult and likely would also result in much shorter code. But this is not what i'm going to do because my aim was only to port that game from xlib to gtk, which is simple at least because the graphics functions of the two are almost the same, i mean the standard gdk functions. At least i who almost never have to use such graphics functions, would never need any more advanced functions from cairo or such.

TkTkorrovi 69 Junior Poster

I created this thread http://www.daniweb.com/forums/post418804.html#post418804 about how to install GTK and MinGW in Windows, and how to compile GTK programs there. All that is much easier in Linux and Mac.

TkTkorrovi 69 Junior Poster

I'm glad that you like it :) There is a new version up now, but i don't think that i would change it again, as i don't know any more what to make better... This once very popular vector asteroids game is likely not good as a game any more, but now it can serve as a programming example. Because GTK is a cross-platform library, this code compiles exactly as it is in linux, mingw (windows), and likely also in mac and other platforms.

TkTkorrovi 69 Junior Poster

Thanks to Samreid, in #DaniWeb irc channel, for testing this code in Debian. Based on that, you should have GTK development package installed in Linux, to compile GTK applications. If something is wrong, or you are not sure, run "apt-get install libgtk2.0-dev" in Debian, guess this should also work in Ubuntu. It also appeared useful to change the font in the code, from "dejavu sans mono bold 12" to "mono bold 12", because not all have dejavu font installed, or for some reason that font doesn't work, and the result is the default size, which is usually smaller and would cause incorrect placement of text.

TkTkorrovi 69 Junior Poster

You likely want something graphical, where the things move. GTK version of the classic arcade game Asteroids.

TkTkorrovi 69 Junior Poster

A small demo about how to use GTK label as a simple console. To compile GTK program, `pkg-config gtk+-2.0 --cflags --libs` should finally be somewhere in the compiler command line in the bash-like shell.

TkTkorrovi 69 Junior Poster

Oh i'm sorry, in accordance with my limits.h the biggest size of the string is 2147483647, some 2 GB so i guess large enough for most things...

TkTkorrovi 69 Junior Poster

I tested what a* does, in the beginning of the expression, in repeated replace. And guess what it does? It fills the string with replace pattern until it is full, the code finds it and then exits. But it's ok to use it in the beginning of the expression, if the search is not repeated. I'm really sorry for writing so many comments.

TkTkorrovi 69 Junior Poster

If you want to test it, just run it in some bash-like shell, giving the regular expressions and the replace pattern as arguments, preferably in the form $'expression' if you want to use escape sequences such as \t and \n. Then enter the text, and if your expression is correct, the the replaced line will appear, end with ctrl-d. In the basic regular expression you can use . for any character, range or set like [a-z0-9;-] or inverted like [^ ], and * or \{n,m\} for repeated character, range or subexpression. Subexpression is between \( and \), and you can refer to it with \n both in expression and replace pattern, where n is the number of subexpression, like in \1. Remember about *, that regular expression is evaluated from left to right, so the construct like a* must follow some other characters, and cannot be in the beginning of the expression, because * means none or more, and construct like a* alone doesn't determine any particular place.

TkTkorrovi 69 Junior Poster

Replaces every occasion of the pattern, or only the first occasion if there is a subexpression, between \( and \), anywhere in the regular expression, as repeated replace is not what one would expect in that case. The string size is restricted in POSIX regular expressions to the size of the int, approximately 32 kbytes, but otherwise such replace should be enough for anything necessary in real life.

TkTkorrovi 69 Junior Poster

Thank you, good algorithm! The same using GTK, which shows that it's not very difficult to port simple graphics to another graphics library, i don't know though whether i guessed the colors right.

#include <gtk/gtk.h>
#include <stdlib.h>

int diskisin [5], posinpeg [5],
  diskcount [3], width = 655,
  height = 430, delay = 100000,
  ysize = 20, kbhit, n;
double cl [] [3] = {{1, 1, 1},
  {.5, .5, .5}, {0, 0, 1},
  {0, 1, 0}, {0, 1, 1},
  {1, 0, 0}, {1, 0, 1}};
GdkPixmap *pixmap;
GtkWidget *da;
cairo_t *cr;

void drawscreen ()
{
  if (kbhit) return;
  gtk_widget_queue_draw_area
  (da, 0, 0, width, height);
  for (g_usleep (delay);
    g_main_context_iteration
    (0, 0); );
}

void rectangle (int x, int y,
  int width, int height,
  int cn)
{
  cairo_set_source_rgb (cr,
  cl [cn] [0], cl [cn] [1],
    cl [cn] [2]);
  cairo_rectangle (cr, x, y,
    width, height);
  cairo_fill (cr);
}

void dostep (int *x, int *y,
  int xd, int yd, int size,
  int disk)
{
  rectangle (*x, *y, size,
    ysize, 0);
  rectangle (*x += xd,
    *y += yd, size, ysize,
    disk + 1);
  drawscreen ();
}

void movedisk (int disk,
  int from, int to)
{
  int size = disk * 40, tx, ty,
    x, y;

  x = 5 + diskisin [disk - 1] *
    215 + (5 - disk) * 20;
  y = 150 +
    posinpeg [disk - 1] * 30;
  while (y > 60)
    dostep (&x, &y, 0, -30,
      size, disk);
  tx = 5 + to * 215 +
    (5 - disk) * 20; …
TkTkorrovi 69 Junior Poster

Thank you for putting up this, it's nice, and certainly much more advanced than mine, though I just wanted to provide a minimal kit which enables to edit files. I have a few questions though, not necessarily about your code, but about such things in general, my code is not better either in that respect.

1. If it doesn't succeed to open a file, it should return abnormally (return 1).

2. Don't know whether using getch is even the best, if we execute the program inside shell script, we don't know whether it read all files, or only part of them. Maybe it's better to use fread, so that we can distinguish between end of file and read error, or then we must find out the file size and count characters.

3. It's a question whether using pipes and redirection, there is any need to check output errors. Good pipes should provide that, so that the system would not become too messed up when we have a faulty hard drive or other devise, but likely at least redirection doesn't provide any such possibility. Well, our programs may be perfect though, in spite that the system is not so perfect.

Also, who don't know, at least with some compilers, if you use wildcards in file name in this program, it supplies all files it finds in a command line, this is the only way to do it in strict ansi c, which has no glob function.

TkTkorrovi 69 Junior Poster

A very rudimentary more, press enter to view next screenful of text, like with print command in ed text editor.

TkTkorrovi 69 Junior Poster

Similar to cat in unix textutils. You can actually edit files with these. Remember that ctrl-z sends end-of-file in windows, and ctrl-d in linux.

TkTkorrovi 69 Junior Poster

Text fragment extractor (usage: fragment /pattern/[n] [/pattern/[n]] file), similar to the print command in ed and vi, with pattern range. If the second pattern is omitted, prints to the end of file. Use quotation marks when there are spaces in the argument. Finds the first pattern, then the second pattern, then calculates the range, so considering that make sure that the range is correct.

TkTkorrovi 69 Junior Poster

You are sadist, Narue, evidently. Also you said in irc, that what you did against me, was "because of fun". I don't find that sadism is fun, and no reasonable person doesn't.

TkTkorrovi 69 Junior Poster

I was banned from DaniWeb IRC after jtwenting saying to me that if i were in gulag, i didn't last five minutes when i didn't lick the shoes of the party apparatchic, and after i dared to say anything to defend myself, what i said was only that whether he finds that that which he said was funny. When will abusing me here end? How many here find it funny and approve it?

joshSCH commented: Childish.. -2
TkTkorrovi 69 Junior Poster

There should be some examples in borland c, try to compile one graphics example and see whether it works. Then you know whether the problem is in your code, or in your installation of the compiler. Don't become desperate and think patiently what the problem may be, there is no mystery, all the things in programming can be found out by one testing or another, most often a very simple testing. You may also try to reinstall the compiler, as this is always simpler than finding mistakes in the installation.

TkTkorrovi 69 Junior Poster

Please at least don't start lying to me, the system don't let me send private messages to you. But if this is some system fault, then think how you treat people, from that depends how understanding are people towards you.

TkTkorrovi 69 Junior Poster

Well and you, Ancient Dragon, should understand the same as well. You in addition to everything blocked your private messages for me, in spite that i didn't write you anything offensive, except for asking to leave me alone. I always thought you are intelligent person with university education, so you must be able to understand.

TkTkorrovi 69 Junior Poster

Narue, don't you still understand that a peson, whose tongue is cut, no not only by you, but not this is important here, cannot reply you, so it doesn't make any sense to talk to me any more.

TkTkorrovi 69 Junior Poster

I think you well understand that you can allow yourself anything now, you cut my tongue, and i may not even be able to reply. All the words are meaningless after that.

TkTkorrovi 69 Junior Poster

OK, what i can say here, i'm forcefully violated against, i let it to everyone to think, what interests this may serve. My posts are edited against my will, a possibility to protect myself is taken from me here, if you even can read these words.

These of you who are interested, let's create our own place, only about c and linux. Because this place has even not so many visitors, and it's impossible to achieve peace here.

TkTkorrovi 69 Junior Poster

Well, at least i don't listen to you any more.

TkTkorrovi 69 Junior Poster

> That's not how buffering in C works. You're confused about the connection between a command shell and a C program.

One should understand that it's not possible to implement standard input with line buffering without the canonical mode.

> That explanation is completely worthless to anyone except those of us who already know how it works. Perhaps instead of blowing a lot of hot air, you could give examples of how the bits are changed according to the truth tables of the bitwise operators.

Look, you even didn't say that my explanation is wrong, what it isn't, anyone who knows anything about logical operators can confirm that. This kind of thing can be said about any sentence said by anyone, because this is a subjective claim, deliberately left unsubstantiated, or in the other words simply a vituperation put into a decent looking form. And now i have a substantiated reason to be angry, so i'm justified to use all caps, sorry but some person here cause me to do that, i can obviously otherwise have no hope that he/she would understand, though there likely cannot be no such hope at all. NARUE, YOU OFFENDED ME BRUTALLY AND FOULLY <offensive material snipped>

And so, i was unfortunate enough to get offended, only because i posted here, trying to explain things and help newbies, with no bad intentions whatsoever towards anyone. WHAT MISTAKE DID I DO BY WRITING A POST TRYING TO HELP A PERSON, TO DESERVE …

Rashakil Fol commented: Aliens are not allowed to get offended. -1
TkTkorrovi 69 Junior Poster

Yes it sounds complicated, and any stdafx.h is not a standard c header, either. What is GPP? If you mean gcc, then why it's complicated? Use command line, gcc something.c -o something.exe (in mingw and cygwin) is all you need to compile a standard c program, nothing can be any simpler. A bit more complicated when you use graphics user interface like gtk, but it's not compiling what is complicated, maybe installing the libraries may be a trouble for some, though it's simple if to know a few simple things. It's better to use free open source and standard (posix) compiler gcc, than any non-standard ones, like visual c and borland c, gcc is an extremely good compiler as well, in some ways better than visual c, and can make as fast programs, or even faster.

TkTkorrovi 69 Junior Poster

Well we would not start to teach you mathematics here, finally this is a programming forum, you may ask these questions in some mathematics or physics forum. But in short, there are other ways to calculate x^y than to multiply x y times, which is even not possible when y is not an integer. For practical programming, it's also sometimes useful to use function tables, may make things very fast.

TkTkorrovi 69 Junior Poster

Well it's getchar but getchar is not exactly equivalent to the getche in borland c... By default, the standard input in c is in the canonical mode, which means that all input is line buffered and the line would be received only after the linefeed. This means that no matter what function we use for input, we have to press enter after anything we write, for it to become available. At least it must be so by standard, because standard input must be line buffered, if any compiler implements it otherwise for some standard c functions, then this compiler does not conform to standard. So, we cannot use input in non-canonical mode, in standard c, which means also that one cannot use compiler flags like -std=c89, to force a strict standard c, one must compile in that case without these flags.

So we cannot switch off the canonical mode by standard c functions, but we can do it using standard posix functions. POSIX is a standard which somewhat can be considered as an extension of the standard c library, as standard c library is a subset of it, but posix enables to do everything which is necessary to communicate with the operating system. In posix, there is a header termios.h, which provides functions necessary to change the terminal attributes, and the canonical mode is one of these. Unfortunately windows doesn't implement posix (though one can buy from ms some posix extension to windows or such), in spite that it's …

TkTkorrovi 69 Junior Poster

I may help if i knew how far you actually are. It is not very difficult to write a simple text editor from scratch, first you likely have to implement a doubly linked list of pointers to lines, at least this is how it is implemented in many text editors. I have not so much experience with text editors, but i once ported to windows an old editor stevie, and replaced its regular expressions with public domain regular expressions, so that it's completely public domain... You should at least implement regular expression and command filtering, this is kind of minimal for a programmers editor... You can even take code from editors like stevie, as this is public domain, to be honest, i know no other public domain text editors, there can be a few, but there are not many such.

Attached is the source code of the editor stevie which also works in windows.

TkTkorrovi 69 Junior Poster

The simplest bubble sort...

#include <stdio.h>

int main ()
{
  int t, i, j, arr [] =
    {3, 6, 1, 2, 3, 8, 4, 1, 7, 2, 0};

  for (i = 0; arr [i]; i++);
  for (; i > 1; i--)
    for (j = 1; j < i; j++)
      if ((t = arr [j - 1]) > arr [j]) {
        arr [j - 1] = arr [j];
        arr [j] = t;
      }
  for (i = 0; arr [i]; i++)
    printf ("%d ", arr [i]);
  printf ("\n");
  return 0;
}
TkTkorrovi 69 Junior Poster

Yes, Salem is right, %c gives you a character with a corresponding code, not a number, 1 1 0 1 0 1 1 1 is exactly what your program outputs when you use %d. Also, when you initialize the arrays, you don't always have to provide the size, like in your program ipb [] = {1, 0, 1, 1} would work as well. One more thing, when you use the initialized arrays, it's often better to not use the array size anywhere, to avoid an unnecessary magic number. Like, in your array ep, the elements most likely cannot be 0, so, you can end this array with zero, and iterate until zero.

TkTkorrovi 69 Junior Poster

Load shared library, and call a function in it. Don't use debugger, instead add debug statements to the code, it's much more efficient and faster that way.

TkTkorrovi 69 Junior Poster

I don't know, maybe it's c++, like there are every kind of weird << and >> in cout... But at least it's not c. Following the typedef identifier should be a declaration, which mostly starts with a type, not with map> something... With typedef you define type, you can use this to declare another type, or array of another type, or use it in whatever way how you can use simple types like char and int.

WolfPack commented: One of the most absurd half-assed answers I have ever seen. If you are not sure, please don't reply and confuse others. -1
TkTkorrovi 69 Junior Poster

What is the end condition in your for statement? There, your i is 10, you iterate up to the value of some element of your array, which is bigger than the array size. You go beyond the limit of the array, which may cause any kind of unexpected behavior. But, to understand better what happens, get used to add debug statements in your code, these are most often printf functions, printing the value of some variable, sometimes you may add some condition, or even make them more complicated. Distinguish the debug statements somehow, the advanced way is to put them between something like #ifdef DEBUG and #endif, but i mostly just used to add a comment /***/, after them... Then you'll see exactly what happens.

TkTkorrovi 69 Junior Poster

Maybe it's useful to explain, what are all these libraries in gtk, otherwise gtk seems like a bunch of strange libraries for unknown purpose...

The libraries which are actually part of the gtk are:

gtk+ -- this is for widgets, and also includes gdk, which is necessary for creating drawing surfaces

glib -- this does all the processing, allocating and freeing objects, and has many other things like for working with dll-s, linked lists etc

pango -- for rendering texts

cairo -- for drawing, including drawing for widgets

atk -- accessibility toolkit, enables to use some accessibility devices, but must be installed

The rest can also be found in gnuwin32 http://gnuwin32.sourceforge.net/packages.html where are also many other useful things. These are additional libraries, which may often be necessary also when using other graphics libraries, like sdl. But they have to be installed for gtk.

libiconv -- for locale-specific conversion of strings

gettext -- for substituting messages, in other languages

libpng -- for loading images in png format

zlib -- for compression necessary for images

These must not be installed, but are useful, also when using any other graphics libraries:

pkg-config -- to get the compiler options necessary for using certain libraries

wget -- to download files from the internet, also by using a list of internet addresses

zip -- to create zip files

unzip -- to extract zip files

gzip -- for …

TkTkorrovi 69 Junior Poster

I don't know the borland c graphics api and don't recommend to use it, as it's just a commercial library, not standard, not open source and not cross-platform, not very good either. No one agrees with gtk seemingly, but then at least use opengl, or even sdl. But why the screen is blank, i can tell you what i know from other graphics api-s. Look at the initialization for graphics, it has to be done strictly right, and exactly in the right order. There, the next functions often use the data from the previous initialization functions, and if you use them before these functions, they cannot obtain proper data, because this data would be created as a result of certain initialization, and the result is that certain surface would never be drawable, which usually results in the black or gray screen. Unfortunately the compiler cannot give any errors or warnings about that, because graphics is done by functions and not by language constructs. The api cannot warn you either if you run it only in window. Only when you run the program from console, then the graphics api usually writes errors in the console, that a certain surface is not drawable or such.

TkTkorrovi 69 Junior Poster

There is xsane for windows http://www.xsane.org/xsane-win32.html free and open source, this can be used separately or together with gimp.

TkTkorrovi 69 Junior Poster

Apparently you must be speaking from ignorance of the capabilities of a modern linter.

I likely don't know, what you call the "modern linter", but does it really demand (void) before some function calls? When the function's return value was not used, this means that we didn't need it, no need for such warnings.

TkTkorrovi 69 Junior Poster

Since some compilers (and lint) will warn about discarded return values, an explicit cast to (void) is a way of saying "Yes, I've decided to ignore the return value from this call.

lint is outdated, compilers have now good enough error checking, so that we don't need lint. gcc at least doesn't give such warning even when all warnings are on, i don't know any compiler which gives such warning, though maybe there is some.

TkTkorrovi 69 Junior Poster

You didn't seem to care so much about me, but ok, it's enough if you try to improve your appearance.

TkTkorrovi 69 Junior Poster

You are free to think or guess what you want, but why do you write it here to everyone?

TkTkorrovi 69 Junior Poster

> Ignoring technical details, that attitude makes me less inclined to listen to your advice.

> In the absence of technical arguments, I made that clear before too, your attitude toward alternatives is exactly what I called it: narrow minded.

Look, you explain in your own words perfectly the mistake you do -- you draw things out of context.

There are several methods of demagogy, and one of these is to tell only half of the story, and ignore the rest. This is useful for demagogy because the things are often opposite to each other, and one opposite without another easily enables to exaggerate something and to give it a completely different meaning, like someone wants it to see.

TkTkorrovi 69 Junior Poster

It's for some reasons better to compile and run the programs using command line, but if you want to use ide, then you may write something like this in the end:

printf ("== press enter ==\n");
getchar ();

Notice that you cannot use any other single character than newline for that, when you use only standard c, because in standard c the standard input is line buffered.

TkTkorrovi 69 Junior Poster

Hamrick, you are totally wrong, and the only thing which i can do is to hope that you would once understand that. If you care to read, i substantiate what i say, and why i say that, so the way to argue is not talking bad about me, but criticizing my arguments. If you prove them wrong, then i also agree that i was wrong, but this is what you didn't talk about, and likely also didn't think about, at all.

And finally, this *thread* is about gtk. Then people come here and talk about other libraries, when they well could create separate threads about these, and write there why they think they are good for. But ok, if people want to talk about gtk compared to other libraries, i accept their wish and explain the advantages of gtk. But then some come to accuse me that i'm narrow-minded and talk only about something which i like. The reason is not that i like it, i want to find out what library is the best to use, based on rational arguments, but anyway concerning such accusations, somewhere is the limit, peoples.

TkTkorrovi 69 Junior Poster

Look you guys really drive me nuts with your talk about other graphics libraries and gui toolkits. A good library for graphics is opengl, it's cross-platform, and it's the best when you want the most access to the graphics hardware. But it only makes sense to learn it, when the only thing what you want to do is writing 3d games. SDL is *not* good, it does not enable the best graphics quality, good hardware acceleration, and is not the most modern graphics library either. And so are not good any gui libraries based on sdl. The same can be said about windows gdi, xlib and anything such. In these, the things like graphics contexts and such are a terribly old and totally outdated concept. A modern open source and cross-platform graphics library is cairo, its functions are such that they easily enable to send commands to the graphics hardware, and therefore enable graphics acceleration whenever possible. Whether such graphics acceleration is done, of course depends on implementation and not on the graphics interface, but cairo enables it, so knowing it is all what is necessary for 2d graphics. I said it's the best to go with some serious thing. Maybe they are really going to totally replace gdk in gtk with cairo, in newer version almost all the widgets are rendered with cairo and not with gdk. This would not be bad, when they make cairo to work directly through opengl, and not through xlib, the whole graphics in …

TkTkorrovi 69 Junior Poster

I don't know what this "opening a folder" exactly supposed to mean, but one thing which is usually always available, no matter what desktop you use, is xterm. This opens xterm in the directory /pathname/foldername and prints the directory contents:

system ("xterm -e 'cd /pathname/foldername && ls && bash'");

Then you can do everything with the files in that directory.

TkTkorrovi 69 Junior Poster

Well in the windows api there is a function CreateThread which is similar to pthreads function pthread_create, this is the msdn link http://msdn2.microsoft.com/en-us/library/ms682516.aspx Visual c certainly implements at least this function. In essence, these functions have a function pointer as an argument, which is the function where the new thread would start to run, this is a bit different from fork () which is posix as well, somewhat simpler, and shows somewhat better that which happens when the new thread would be created.

TkTkorrovi 69 Junior Poster

Now the other thread of me was spawned, which says that fork () does not exactly create a copy of the code, but creates a new process which runs the same code in the memory. I tried to explain it in a simplified way. The difference it makes, is that the global and shared variables would be common for threads, because the code in memory which they run, is one and the same. Running the same code simultaneously by several processes may also happen at running some kernel functions. The way to avoid the problems which that may cause, is making the code so-called "re-entrant", which in essence means using global and shared variables as few as possible, avoiding the conflicts from using the existing global variables, and implementing freeing the memory of commonly used objects by dereferencing, freeing it only when no thread would no longer need the object.

TkTkorrovi 69 Junior Poster

I wrote this example as a reply to one earlier post about interprocess communication (sockets there provide two-way communication between threads):

#include <stdio.h>
#include <sys/socket.h>

int sv [2];

int main ()
{
  socketpair (AF_UNIX,
    SOCK_STREAM, 0, sv);
  if (fork ()) { /* parent */
    char buf [MAX_INPUT]; 
    char sbuf [MAX_INPUT];
    FILE *f;

    f = fopen ("prodfile", "w");
    while (1) {
      if (!fgets (buf, MAX_INPUT, stdin))
        break;
      fputs (buf, f);
      fflush (f);
      send (sv [0], "read",
        MAX_INPUT, 0);
      while (recv (sv [0], sbuf, 
        MAX_INPUT, MSG_DONTWAIT) == -1)
        usleep (10000);
      if (strcmp (sbuf, "send")) break;
    }
  } else { /* child */
    char buf [MAX_INPUT];
    char sbuf [MAX_INPUT];
    FILE *f;

    f = fopen ("prodfile", "r");
    while (1) {
      while (recv (sv [1], sbuf, 
        MAX_INPUT, MSG_DONTWAIT) == -1)
        usleep (10000);
      if (strcmp (sbuf, "read")) break;
      if (!fgets (buf, MAX_INPUT, f))
        break;
      fputs (buf, stdout);
      send (sv [1], "send",
        MAX_INPUT, 0);
    }
  }
  return 0;
}

In essence, after the function fork () an exact copy of the program would be created, and two identical copies of the code would run starting from the function fork (). Which means that fork () returns into both threads, with the only difference that in one thread it returns 0, and in the other thread it returns 1, which is the only way how the code finds in what thread it runs.

Unfortunately i don't know, how the threading is done in visual c, but if it is standard compliant, then it should implement fork …