-
fseek() with stdout
This is about my snake clone again. I figure it goes here instead of Game Programming because it's more about C in general than Game specific stuff.
Code:
fseek(stdout, 80 * (snakepos[0][1] - 1) + snakepos[0][0], SEEK_SET);
fscanf(stdout, "%c%c", &test[0], &test[1]);
if ((test[0] != 32 || test[1] != 32) && (test[0] != apple[0] && test[1] != apple[1]))
alive = 0;
Anyway what I'm trying to do here is to go to the line where the head of the snake is, and before it moves there it gets which characters is placed there, if it's not spaces or an apple you'll die. Problem is that after fscanf, test[0] and test[1] will contain 0 (null) and I don't know why. I tried changin the position with a constant like, 3, but it still gave me zeroes.
Am I doing something wrong or is it just not possible?
-
It's not possible, use gotoxy(x,y) (conio.h), SetConsoleCursorPosition(hOuput,scrn) (windows.h), or something from the curses library to position the cursor.
And store what's on the screen in an array, it's a waste of time trying to read what is there and much easier to store it as you make it.
-
Oh yeah I forgot, I'm using Borland C++ on Windows.
I can imagine using gotoxy would be a lot easier, but I don't know of any way to read in a character where the cursor is.
-
You don't "read in a character where the cursor is". You simply read a character. The cursor position has nothing to do with reading a character. It is an output item.
If you want to "read the character that's currently under the cursor", then you'll need to keep track of the screen's contents in a buffer some place, and seek around through it to figure out what's under the cursor based on where you're moving it.
If you want to make it look like the cursor it some place, and input (their typing) appears there, and you read input "from where the cursor is", like I said, you simply read input. The cursor is only an output tool. It doesn't have anything to do with reading input.
Quzah.
-
> Am I doing something wrong or is it just not possible?
You're making life very hard for yourself.
Code:
char snakepit[80][25];
Use this to keep track of the snake, food, walls etc.
If you want to find out where something is, you look in here.
When you make changes here, you update the screen.