yy-chr: set file size

A place for your artistic side. Discuss techniques and tools for pixel art on the NES, GBC, or similar platforms.

Moderator: Moderators

Post Reply
EnigmaOverdrive
Posts: 9
Joined: Sat Jul 09, 2016 10:16 am

yy-chr: set file size

Post by EnigmaOverdrive »

I noticed that some chr files are different sizes (some are 8kb, some are 2kb...) and I need mine to be 1kb in size, but whenever I create a new file in YY-CHR it's 8KB large by default. How can I change this?
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: yy-chr: set file size

Post by thefox »

AFAIK it's not possible. You can edit smaller files though (you can use a hex editor like HxD to create a file full of zeros). Caution: If you make changes in areas that don't fall within the original size of the file, the changes won't be saved.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
EnigmaOverdrive
Posts: 9
Joined: Sat Jul 09, 2016 10:16 am

Re: yy-chr: set file size

Post by EnigmaOverdrive »

That works, thanks!
User avatar
Light-Dark
Posts: 53
Joined: Sun Dec 08, 2013 9:53 pm
Location: Canada

Re: yy-chr: set file size

Post by Light-Dark »

Because Tokumaru's tile compressor takes a max of 4kb of tile data I wrote a simple C program that carves up the standard 8kb chr file for myself. Here's an example program for you that fits your needs:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
char* extractKB(char* indata,int size){
	char* cut = malloc(size);
	
	memcpy(cut,indata,size);	//copy first kb
	
	return cut;
}

int getFileSize(FILE* fp){
	int count = 0;
	while(fgetc(fp)!=EOF)
		count++;
	fseek(fp,0,0);
	return count;
}


int main(int argc,char** argv) {
	if(argc < 3)
		return 1;
	FILE* fp = fopen(argv[1],"rb");
	int size = getFileSize(fp);
	char* buffer=  malloc(size);
	fread(buffer,1,size,fp);
	fclose(fp);
	
	fp = fopen(argv[2],"wb");
	char* extracted = extractKB(buffer,1024);
	
	fwrite(extracted,1,1024,fp);
	
	fclose(fp);
	
	free(buffer);
	free(extracted);
	return 0;
}
It takes two command line args, the chr file and then the output file.
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: yy-chr: set file size

Post by koitsu »

You can't be serious with that getFileSize() routine, are you? You don't need the while/fgetc at all. Try:

Code: Select all

long getFileSize(FILE *fp) {
  long count;

  fseek(fp, 0, SEEK_END);
  /* TODO handle when ftell() returns -1 (error; errno is set) */
  count = ftell(fp);
  fseek(fp, 0, SEEK_SET);

  return count;
}
Note that ftell() returns long, not int, so other portions of the program will need to be updated. Also, fread() takes a size_t for its size and member counts, so you might have to force-cast (no guarantee size_t is a long on every arch).

I'm intentionally choosing not to comment on the rest. :-)
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: yy-chr: set file size

Post by thefox »

Python is quite a bit more practical for tasks like that:

Code: Select all

import sys; open( sys.argv[2], "wb" ).write( open( sys.argv[1], "rb" ).read( 1024 ) )
(That's the whole program.)

On *nix you could achieve the same thing with dd.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
Light-Dark
Posts: 53
Joined: Sun Dec 08, 2013 9:53 pm
Location: Canada

Re: yy-chr: set file size

Post by Light-Dark »

koitsu wrote:You can't be serious with that getFileSize() routine, are you? You don't need the while/fgetc at all. Try:

Code: Select all

long getFileSize(FILE *fp) {
  long count;

  fseek(fp, 0, SEEK_END);
  /* TODO handle when ftell() returns -1 (error; errno is set) */
  count = ftell(fp);
  fseek(fp, 0, SEEK_SET);

  return count;
}
Note that ftell() returns long, not int, so other portions of the program will need to be updated. Also, fread() takes a size_t for its size and member counts, so you might have to force-cast (no guarantee size_t is a long on every arch).

I'm intentionally choosing not to comment on the rest. :-)
That's okay, critique is welcome. I'm certainly not the Da Vinci of C code :lol: (slapped it together on the way home from work).
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: yy-chr: set file size

Post by koitsu »

thefox wrote:On *nix you could achieve the same thing with dd.
There is dd for Windows (which also implements /dev/null, /dev/zero, and /dev/random "devices" for convenience). I've used this for a long time with great success.
Post Reply