dMZX Forums: File output, and C++ formatting - dMZX Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

File output, and C++ formatting MZX in, Garbage out

#1 User is offline   Bocco 

  • Some guy
  • PipPipPipPip
  • Group: Members
  • Posts: 514
  • Joined: 09-February 01
  • Gender:Male

Post icon  Posted 23 April 2002 - 09:28 PM

I had this "wonderful" idea for exporting an MZX board to be read in by a C++ textmode graphics program I'm working on. Using MZX 2.62b, I tried this code:
set "boardout.txt" to "fwrite_open"
set "board_y" to -1
: "incy"
if "board_y" > 23 "fin"
set "board_x" to -1
inc "board_y" by 1
: "incx"
if "board_x" > 78 "incy"
inc "board_x" by 1

set "fwrite" to "board_color"
set "fwrite" to "board_char"

put cff Solid p?? at "board_x" "board_y"
.This above line is just to ensure that every tile is read
goto "incx"
: "fin"
set "" to "fwrite_open"

And I read the results in, after I was sure the file was closed, with this C++ code (yes, the matrix is properly declared to 80x25 and the file opened already):
struct colorchar{//just contains ch (the character represented) and col (the color of that character)
	char ch;
	char col;
};
apmatrix<colorchar> getFile(ifstream &fil, apmatrix<colorchar> &mat){
	//reads in a frame from file with pretty simple nested loops:
	for (int x=0;x<mat.numrows(); x++)
		for (int y=0; y<mat.numcols(); y++)
		{
			fil>>mat[x][y].col;
			fil>>mat[x][y].ch;
		}
	return mat;
}


When I printed it out, though, the spacing was off and lines were shifted all over the place. I suspect this has something to do with C++ interpreting the chars '32' and possibly '0' in the file as whitespace. So, I tried adding to the MZX code:
.These lines replace the block of two "set 'fwrite'" lines in the original code.

if "board_color" = 0 "writebig"
if "board_color" = 32 "writebig"
set "fwrite" to "board_color"
if "board_char" = 32 "writezero"
set "fwrite" to "board_char"
: "conb"


.And these lines go at the bottom:

end
: "writebig"
set "fwrite" to 77
.254, the boulder char, is fine for test purposes:
set "fwrite" to 254
goto "conb"
: "writezero"
set "fwrite" to 254
goto "conb"

The output still looks similarly skewed. Anyone know of some way to properly fix this?
Thanks...

EDIT: Whoops... left in a potentially confusing line.
0

#2 User is offline   Wervyn 

  • I can see you
  • Group: DigiStaff
  • Posts: 1,855
  • Joined: 24-December 00
  • Gender:Male
  • Location:Caras Galadhon

Posted 24 April 2002 - 06:22 AM

If I may ask, why would you want to display the color as a character?  Representing it as an integer or a hex digit seems like it would make more sense, if you can't actually display the color.  Then again, you don't show the code that handles the returned matrix, so maybe you are doing one of those.  Even so, you should store the color as an int or a hex. (I forget, does C have a data type for hex?  Ehn, if it doesn't, just write one.)

As for the character, I'm not sure exactly how the text mode you're using handles the first 32 ASCII chars.  Those are all typically formatting commands: 13 is a carriage return (10 a newline), 09 is tab, and 27 is escape. Char 127 is delete, as well.  You need to figure out how to deal with pretty much all of these, I would think.

"I guess the guards we randomly gathered were no match for those mobile suits!"
To lie is to change the truth.
..Ignorance is to be unaware of the truth.
....Incompetence is to be unable to grasp the truth.
......And escape is to run away from the truth.
It is useless to run, since the truth is right next to you.

-Wervyn
0

#3 User is offline   Bocco 

  • Some guy
  • PipPipPipPip
  • Group: Members
  • Posts: 514
  • Joined: 09-February 01
  • Gender:Male

Posted 24 April 2002 - 03:14 PM

I'm not entirely sure what you're saying, but I do display the characters with color, using SetConsoleTextAttribute(). My text output is with the basic 'printf()' function and my file input uses 'fstream.h'. The text mode definitely can handle chars 1-32 as display, since just the code:
printf("%c", char(1));

displays the smiley face as expected.

As for storing color as a char: a char is merely a 1-byte variable, as int is merely a 2- or 3-byte one. And SetConsoleTextAttribute takes a char parameter for the color.
0

#4 User is offline   Exophase 

  • Laughing on the inside.
  • Group: DigiStaff
  • Posts: 7,155
  • Joined: 23-October 00
  • Gender:Male
  • Location:Cleveland, OH

Posted 25 April 2002 - 07:41 PM

Wow, your formatting is horrible.. Anyway, you should not be loading and displaying one character at a time from the file. You should load the entire file at once into a dynamically allocated array (and dude.. apmatrix? This isn't highschool CS class or something)

Something like this, when using stdio, which IMO is much more usable than fstream.

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
?FILE *fp;
?char *board;
?int file_size, i;

?fp = fopen("filename.txt", "rb");
?file_size = fseek(fp, 0, SEEK_END);
?board = (char *)malloc(file_size);
?fwrite(board, file_size, fp);

?for(i = 1; i < file_size; i += 2)
?{
? ?printf("%c", board[i]);
?}

?return(0);
}

Try it. Assuming you stored it in the standard ANSI board format of color/char, 8bits each.

By the way, all modern 32bit systems have 4byte ints. I've never heard of a 24bit (and thus, 3byte int) system.

And don't give it special handling for whitespace... make this as much pure binary as you can. That might very well be your problem.

- Exo
~ ex0 has a kickass battle engine, without it you sux0rz! without it you sux0rz! ~

"The fact that I say I've one of the best, is called honesty." -Akwende
"Megazeux is not ment to be just ASCII, it is ANSI!" - T-bone6
"I hate it when you get all exo on me." - emalkay

Exophase can what Rubi-cant.
exoware is ware ur ware is exoware
ps. not loking 4 new membrs kthx
0

#5 User is offline   Bocco 

  • Some guy
  • PipPipPipPip
  • Group: Members
  • Posts: 514
  • Joined: 09-February 01
  • Gender:Male

Posted 25 April 2002 - 08:24 PM

Thanks, Exo... just one question: I'm not entirely sure what these two lines mean:

Quote

board = (char *)malloc(file_size);
fwrite(board, file_size, fp);

As for my horrible formatting, the indents just didn't copy right from the compiler to my web browser. And about the apmatrix... well, I am in high school AP Comp Sci now...I know about just plain matrices but didn't like the lack of numrows() and numcols() functions...but mainly I just wasn't familiar. Though I suppose using pointers renders that a moot point. I'll try it...
0

#6 User is offline   Exophase 

  • Laughing on the inside.
  • Group: DigiStaff
  • Posts: 7,155
  • Joined: 23-October 00
  • Gender:Male
  • Location:Cleveland, OH

Posted 26 April 2002 - 05:05 PM

Bocco, on April 26 2002,02:24, said:

Thanks, Exo... just one question: I'm not entirely sure what these two lines mean:

Quote

board = (char *)malloc(file_size);
fwrite(board, file_size, fp);

The first is dynamically allocating space for the board, in memory. In C++ you might be more familiar with the new operator:

board = new char[file_size];

And the second one was a mistake on my part, I meant fread, not fwrite. But anyway..

fread(void *destination, size_t size, FILE *source);

It's something like that. Reads from the file to some place in memory.

There's another problem with my code, I forgot to reset the file position after I seeked it. If I were you I'd just use a fixed file size anyway and forget about seeking the end to find the filesize.

- Exo
~ ex0 has a kickass battle engine, without it you sux0rz! without it you sux0rz! ~

"The fact that I say I've one of the best, is called honesty." -Akwende
"Megazeux is not ment to be just ASCII, it is ANSI!" - T-bone6
"I hate it when you get all exo on me." - emalkay

Exophase can what Rubi-cant.
exoware is ware ur ware is exoware
ps. not loking 4 new membrs kthx
0

#7 User is offline   Bocco 

  • Some guy
  • PipPipPipPip
  • Group: Members
  • Posts: 514
  • Joined: 09-February 01
  • Gender:Male

Posted 26 April 2002 - 07:57 PM

Aha...thanks... the one problem is that I want to be able to read in multiple frames and display them as an animation. Actually, that works already with my current code. But I'd need to detect when the file's at EOF to halt read-in...
0

#8 User is offline   Exophase 

  • Laughing on the inside.
  • Group: DigiStaff
  • Posts: 7,155
  • Joined: 23-October 00
  • Gender:Male
  • Location:Cleveland, OH

Posted 27 April 2002 - 07:00 PM

Bocco, on April 27 2002,01:57, said:

Aha...thanks... the one problem is that I want to be able to read in multiple frames and display them as an animation. Actually, that works already with my current code. But I'd need to detect when the file's at EOF to halt read-in...

Or you can check the filesize by fseeking like above and know precisely how large it is...

- Exo
~ ex0 has a kickass battle engine, without it you sux0rz! without it you sux0rz! ~

"The fact that I say I've one of the best, is called honesty." -Akwende
"Megazeux is not ment to be just ASCII, it is ANSI!" - T-bone6
"I hate it when you get all exo on me." - emalkay

Exophase can what Rubi-cant.
exoware is ware ur ware is exoware
ps. not loking 4 new membrs kthx
0

#9 User is offline   MadBrain 

  • Art will make you a hobo.
  • PipPipPipPip
  • Group: Members
  • Posts: 617
  • Joined: 17-February 01
  • Gender:Male

Posted 28 April 2002 - 10:47 PM

Btw, make sure the file is opened in BINARY mode, and not in TEXT mode where evil things like freak carriage returns/line feeds and having '0' stripped out of your file happen. (yes, TEXT mode is the default!!! )
0

#10 User is offline   Exophase 

  • Laughing on the inside.
  • Group: DigiStaff
  • Posts: 7,155
  • Joined: 23-October 00
  • Gender:Male
  • Location:Cleveland, OH

Posted 29 April 2002 - 12:59 AM

Hence the "rb" argument in fopen(). Readonly Binary.

- Exo
~ ex0 has a kickass battle engine, without it you sux0rz! without it you sux0rz! ~

"The fact that I say I've one of the best, is called honesty." -Akwende
"Megazeux is not ment to be just ASCII, it is ANSI!" - T-bone6
"I hate it when you get all exo on me." - emalkay

Exophase can what Rubi-cant.
exoware is ware ur ware is exoware
ps. not loking 4 new membrs kthx
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users