Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nerothos

Unsigned char conversion prob

This topic is 5373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having problems with this Targa writing function - i''m trying to convert it from C to C++ (using the fstream class). However, I cannot figure out how to give tga.write() the necessary info without losing any data! Since fwrite takes a void pointer, it automatically converts it without losing data, but write takes a string. I know the answer is right there, but I can''t figure it out! Here is the function:
int WriteTGA(char *file, short int w, short int h, unsigned char *data) {
	unsigned char bs;
	short int ss;
	unsigned char imgt;
	int cm;
	unsigned char cs;
	unsigned char bd;
	int is;
	ofstream tga(file, (ios::out | ios::binary));
	if(!tga.is_open()) {
		tga.close();
		return 0;
	}
	imgt = 2;
	bd = 24;
	cm = 3;
	bs = 0;
	ss = 0;
	tga.write(&bs, sizeof(unsigned char));
	tga.write(&bs, sizeof(unsigned char));
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
	tga.write(&imgt, sizeof(unsigned char));
	tga.write((char *) ss, sizeof(short int));
	tga.write((char *) ss, sizeof(short int));
//	fwrite(&imgt, sizeof(unsigned char), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
	tga.write(&bs, sizeof(unsigned char));
	tga.write((char *) ss, sizeof(short int));
	tga.write((char *) ss, sizeof(short int));
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
//	fwrite(&ss, sizeof(short int), 1, filePtr);
	tga.write((char *) w, sizeof(short int));
	tga.write((char *) h, sizeof(short int));
//	fwrite(&w, sizeof(short int), 1, filePtr);
//	fwrite(&h, sizeof(short int), 1, filePtr);
	tga.write(&bd, sizeof(unsigned char));
	tga.write(&bs, sizeof(unsigned char));
//	fwrite(&bd, sizeof(unsigned char), 1, filePtr);
//	fwrite(&bs, sizeof(unsigned char), 1, filePtr);
	is = (w * h * cm);
	for(int idx = 0; (idx < is); idx += cm) {
		cs = data[idx];
		data[idx] = data[idx + 2];
		data[idx + 2] = cs;
	}
	tga.write(data, (sizeof(unsigned char) * is));
	tga.close();
	return 1;
}
  
Extreme thanks!

Share this post


Link to post
Share on other sites
Advertisement

This topic is 5373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!