Author Topic: Now what ?  (Read 1265 times)

foobar

  • Member
  • **
  • Posts: 308
  • Kudos: 0
    • http://www.fuckmicrosoft.com
Now what ?
« on: 25 August 2002, 22:05 »
Hi,
Got this strange problem with C.
I have a program that reads a string, wich contains characters (duuh) that say where a dot should be drawn compared to the current position. 'd' means below the current position, 'l' means left of the current position, et c.
Now it works just fine if you'd just do 'dd', but with longer or more variated strings (like 'dduuullrurrlruuurld') it goes on for about three times ... and then segfaults. Wierd. When i compile, gcc doesn't complain at all. Maybe gdb would, but i don't know how to use it  :D

I'm still in the testing phase, i have already achieved to control the dot, but commented the code out to make the snake's tail work, wich is the hardest part. btw, it's a classical snake game.

If you would like to compile it, make sure you've got svgalib libraries (/lib/libvga.so.*) properly installed. It's C, so say:

# gcc ./sneek.c -o ./sneek -lvga

Or do, what i always do for my own comfort:

# gcc ./sneek.c -o ./sneek -lvga && echo " " && ./sneek

Here it goes: i appreciate all help !!! (now you know my real name  :D )

Code: [Select]
Linux user #283039

Gosh, I love Linux Quake.


foobar

  • Member
  • **
  • Posts: 308
  • Kudos: 0
    • http://www.fuckmicrosoft.com
Now what ?
« Reply #1 on: 26 August 2002, 23:04 »
Hm. Seems my post got posted over, or nobody had a clue about my post.
I beg you to take a look at it ... plz ?
Linux user #283039

Gosh, I love Linux Quake.


badkarma

  • VIP
  • Member
  • ***
  • Posts: 497
  • Kudos: 0
Now what ?
« Reply #2 on: 27 August 2002, 02:51 »
Sorry ... didn't notice post untill now

I'll have a look at it tomorrow after work ... it's late and I'm getting ready for bed...

btw ... vgalib? why not use SDL?
If you can't learn to do something well, learn to enjoy doing it poorly.

badkarma

  • VIP
  • Member
  • ***
  • Posts: 497
  • Kudos: 0
Now what ?
« Reply #3 on: 27 August 2002, 02:54 »
btw ... first run 'ulimit -c unlimited' then run your program. It should now core dump when you run your program and save it to a file (usually either called just 'core' or 'core.processid').
Now type 'gdb sneek core' (or core.processid, just do a ls -ail core* to see the correct filename), wait for gdb to load the coredump and type 'where'. Paste the output in a post here if you still can't figure it out  
If you can't learn to do something well, learn to enjoy doing it poorly.

badkarma

  • VIP
  • Member
  • ***
  • Posts: 497
  • Kudos: 0
Now what ?
« Reply #4 on: 27 August 2002, 03:08 »
a few thoughts:

Why redraw the whole snake body every frame? It's a lot more efficient (not that it matters much for this program, but hey, you wanted to learn right?  ;) ) to draw the new snake bit once every frame and to not clear the rest of the screen. This immediatly solves the problem of you having to keep track of the snakes path as well, all you care about is where it is going next, and you can check for a collision by comparing pixel data of where you are about to draw)

C Strings (arrays in general) are evil! They are very prone for errors (and my guess, the reason you're program segfaults). Take a small step extra and learn C++ and more importantly then STL (a collection of template classes aimed to alleviate a programmer of these problems (there are some really handy list based classes for your problem at hand, I have never used STL much but Qt also offers STL like classes))

and again ... vgalib? why did you chose it?
If you can't learn to do something well, learn to enjoy doing it poorly.

badkarma

  • VIP
  • Member
  • ***
  • Posts: 497
  • Kudos: 0
Now what ?
« Reply #5 on: 27 August 2002, 16:00 »
never mind this ... will repost it later ...

(damn UBB ate up half my post when editing)

(.... now I'm really confused ... post showed up after this one .... *bangs head against desk*)
[ August 27, 2002: Message edited by: BadKarma ]

[ August 27, 2002: Message edited by: BadKarma ]

If you can't learn to do something well, learn to enjoy doing it poorly.

badkarma

  • VIP
  • Member
  • ***
  • Posts: 497
  • Kudos: 0
Now what ?
« Reply #6 on: 27 August 2002, 16:03 »
quote:
Originally posted by BadKarma:
a few thoughts:

Why redraw the whole snake body every frame? It's a lot more efficient (not that it matters much for this program, but hey, you wanted to learn right?   ;)  ) to draw the new snake bit once every frame and to not clear the rest of the screen. This immediatly solves the problem of you having to keep track of the snakes path as well, all you care about is where it is going next, and you can check for a collision by comparing pixel data of where you are about to draw)



Note that this ofcourse would not work as well when the snake is not moving over a static background (i.e. there is an animating picture in the background). It also doesn't work if you want the actual snake game behaviour of expanding the snake length when an item is picked up. In this case you would need to keep track of all the snake bodyparts because you will need to move the tail when you move the head. You will also need to know if the snake is expanding at the moment.

In the case of a non expanding snake, a map of snake body parts would be sufficient, i.e.:

Code: [Select]

then you put a one in the map where you want to draw the snake, and a 0 if you don't want to draw it. All movement by the player updates the map and when you draw you just render the map. Ofcourse this doesn't work for a variable snake length because you will not know how the snake moved. Consider the following:

HXXX
 XXX <-- you wont know how it moved here
 XXX
   X
   T

H = head
T = tail
X = snake body

[to be continued]
If you can't learn to do something well, learn to enjoy doing it poorly.

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
Now what ?
« Reply #7 on: 29 August 2002, 00:54 »
Same problem as your last post regarding strings.

You've defined tailroute as an array of 6400 string pointers, but not reserved any memory for them to point to. I'm not entirely sure why you need 6400 strings though.

Anyway, before you assign "dd" to the first element of tailroute you need to allocate memory for it with malloc. You did this with temp2 in DrawTail, but not with temp or tailroute, for some reason.

I assume with this line

strcpy (temp2, &temp[1]);

you're trying to copy all of temp, bar the first character, into temp2? If so it should just be:

strcpy(temp2, temp + 1);

And where you've said strlen(&temp[1]) you actually just mean strlen(temp)

In DrawTail you're reserving memory but never freeing any of it.

GCC didn't throw any errors as C is very permissive and allows you to do this kind of thing.
"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca


foobar

  • Member
  • **
  • Posts: 308
  • Kudos: 0
    • http://www.fuckmicrosoft.com
Now what ?
« Reply #8 on: 29 August 2002, 18:24 »
Wow. Thanks alot for the bunches of possible material you already gave me.

Ok. Isn't it possible to first make a snake of a static length, say, 4, and later figure out how to make variable length ?

The snake will simply be gliding (english word for how a snake moves ?) over a blank black screen, so don't mind about moving backgrounds.

And, vgalib was the first thing i ever saw as graphics library.

As for the 6400 array tailroute, the screen consist of 320 pixel width and 200 pixel height.
Multiply the two, you get 6400. Therefore, the maximum length of the tail that you could ever have would be 6400. Just like you said in a previous post, to make an array of SCREEN_BOUNDARY_X and SCREEN_BOUNDARY_Y , i already did  
Linux user #283039

Gosh, I love Linux Quake.