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.