Romhacking.net

Romhacking => ROM Hacking Discussion => Topic started by: Hypnotic_rabbit on August 25, 2015, 07:01:33 AM

Title: Nes sprite editing with only hex editor
Post by: Hypnotic_rabbit on August 25, 2015, 07:01:33 AM
Some please answer my question, When changing the 8x8 pixels of a tile on an nes rom with an editor this also changes the hex values at the address where it is stored. Basicly what i want to know is how can I make the change with just a hex editor? Iv tried reverse engineering it but I cant make sense of the resulting hex values.
Title: Re: Nes sprite editing with only hex editor
Post by: Grimlock on August 25, 2015, 08:15:07 AM
Altering the pixels in an app such as tile layer pro affects only the look of the tiles not the location they're stored in memory.  What approach are using to edit your graphics, also what results are you after?
Title: Re: Nes sprite editing with only hex editor
Post by: Hypnotic_rabbit on August 25, 2015, 08:37:57 AM
It does change the hex when you edit with tile editor pro. If you open up mario 3 and go to the very first tile which is one of marios ears and make the entire thing black and then go to the address where the sprite is it changes to all 00's. Iv also been experimenting with whats being done. If you fill the first line across with one of the four colors the result is the first 00 becoming FF, and then if you do the second line across the next 00s finish off on FF. The blue color(marios face color) on the other hand does not start where the other three colors do, it starts on the second set of 8 hex values of the 16 all together. Ill figure it out eventually with trial and error but to have someone tell me an editor isnt changing the hex values in the rom makes me laugh.

August 25, 2015, 08:40:12 AM - (Auto Merged - Double Posts are not allowed before 7 days.)

Also the result im after is to make a game specific tile editor, which of course I will have to know how the values are written to the rom after the changes are made in my program.
Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 25, 2015, 09:15:09 AM
I think Grimlock misunderstood -- he thought you were saying the address of the data was changing, not the data itself.

You are correct.  The data does in fact change.

NES graphics are stored in a 2BPP planar format


- A "bitplane" is an 8x8 image with 2 colors per pixel.
- 1 byte of data is a 8x1 image, where each bit determines the color of one pixel ($80 is left-most pixel, then $40, then $20, $10, $08, $04, $02, then $01 is right-most)
- 8 bytes of data form the full 8x8 bitplane, where each byte represents a different row of pixels.
- NES uses 16 bytes (2 bitplanes) for each 8x8 tile.  First, 8 bytes for the "low" bitplane, then 8 more bytes for the "high" bitplane.  This allows for 2*2 = 4 colors per pixel  (bitplanes * colors per pixel on one bitplane = total colors per pixel)
- The "low" bitplane has a weight of 1, the "high" bitplane has a weight of 2

Example:


16 bytes:

41 C2 44 48 10 20 40 80     01 02 04 08 16 21 42 87
|    low bitplane     |     |    high bitplane    |

  low bitplane          high bitplane
$41 = %01000001       $01 = %00000002
$C2 = %11000010       $02 = %00000020
$44 = %01000100       $04 = %00000200
$48 = %01001000       $08 = %00002000
$10 = %00010000       $16 = %00020220
$20 = %00100000       $21 = %00200002
$40 = %01000000       $42 = %02000020
$80 = %10000000       $87 = %20000222

    full image (low + high) .. 0 replaced with '.' for clarity:
   
        .1.....3
        11....3.
        .1...3..
        .1..3...
        ...3.22.
        ..3....2
        .3....2.
        3....222




EDIT:  Note that while NES graphics have to be in this form eventually, it does not mean they will always be stored this way in the ROM.  99% of the time, they will be -- but every once in a while you'll run into a game which compresses graphics to save space -- in which case you'll have to figure out how to decompress/recompress the data.
Title: Re: Nes sprite editing with only hex editor
Post by: Hypnotic_rabbit on August 25, 2015, 09:44:49 AM
Ahh that makes a little more sense to me, the higher and lower part anyway. So this means the first four lines across the 8x8 is the lower and the next four lines across are the higher? This works with three of the colors but when I put the fourth color across the first line of the 8x8 it changes the hex starting at the higher hex values rather then the lower, why is that? Ill look more into this bitplane concept I guess but any links will be appreciated. Also can you explain what you mean by weight?
Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 25, 2015, 10:00:54 AM
QuoteSo this means the first four lines across the 8x8 is the lower and the next four lines across are the higher?

No.  Each bitplane is an 8x8 image.  Think of it like two sheets of transparent paper that you lay on top of each other.

QuoteAlso can you explain what you mean by weight?

Each tile is 16 bytes.

The first 8 bytes is the low bitplane.  So take the first 8 bytes, and use it to form an 8x8 image.  This bitplane has a weight of 1... which means that pixels in the image can either be color 0 or color 1.

The second 8 bytes is the high bitplane.  So take those bytes and form a separate 8x8 image.  This bitplane has a weight of 2... which means that pixels can be either color 0 or color 2.

This gives you two separate 8x8 images... each of which are only 2-color.  To form the final 4-color image... you add the two bitplanes together:

final color 0 = pixel is 0 on both bitplanes
final color 1 = pixel is 1 on the low bitplane, 0 on the high bitplane
final color 2 = pixel is 0 on the low bitplane, 2 on the high bitplane
final color 3 = pixel is 1 on low bitplane, 2 on high bitplane


The example in my previous post illustrates this in action.
Title: Re: Nes sprite editing with only hex editor
Post by: Hypnotic_rabbit on August 25, 2015, 10:22:16 AM
Well from what I thought I knew about it from trial and error I would say you just made it more complicated but im sure you have giving me the right way to think about it, thank you. Just the keywords you gave me should help a lot with looking it up. Im still a little fuzzy on this concept but at least I know more now then I did before.
Title: Re: Nes sprite editing with only hex editor
Post by: dougeff on August 25, 2015, 07:50:44 PM
You don't need to understand it. Nobody edits graphics with a hex editor. Use a tile editor.
Title: Re: Nes sprite editing with only hex editor
Post by: SunGodPortal on August 25, 2015, 08:21:43 PM
QuoteYou don't need to understand it. Nobody edits graphics with a hex editor. Use a tile editor.

I'm with this guy. Editing graphics with a hex editor seems like the most inefficient way imaginable.
Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 25, 2015, 08:29:00 PM
Guys... he wants to understand the format so he can write an editor.

This is a perfectly legit question & approach.  If you can do it in a hex editor, you can write a program to do it.
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 25, 2015, 10:19:45 PM
Here's a visualization of what Mr. Disch explained above:

(http://i59.tinypic.com/20ggxz5.png)
Title: Re: Nes sprite editing with only hex editor
Post by: freem on August 25, 2015, 11:11:17 PM
Quote from: Dr. Floppy on August 25, 2015, 10:19:45 PM
Here's a visualization of what Mr. Disch explained above:

(http://www.baddesthacks.net/forums/download/file.php?id=154)

this image link will only work if you have a baddesthacks account; mind re-hosting somewhere else?
Title: Re: Nes sprite editing with only hex editor
Post by: SunGodPortal on August 25, 2015, 11:25:34 PM
QuoteGuys... he wants to understand the format so he can write an editor.

Mayks sens. :)
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 25, 2015, 11:30:13 PM
Quotethis image link will only work if you have a baddesthacks account; mind re-hosting somewhere else?

Not too much. What's your preference?
Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 25, 2015, 11:38:48 PM
imgur / tinypic are easy go-to's for free image hosting.

you might have to sign up for tinypic, but pretty sure you don't have to for imgur
Title: Re: Nes sprite editing with only hex editor
Post by: freem on August 26, 2015, 12:38:59 AM
imgur doesn't require a login; that's typically a good place for images since it (hopefully) isn't going to screw things up like Imageshack did :p
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 26, 2015, 02:29:23 AM
Imgur isn't behaving, so I'ma try something else. Does this show up?

(http://www.baddesthacks.net/hacks/old/stories/ppu_4.png)

Okay, apparently it doesn't. Let's try yet another option:

http://www.baddesthacks.net/hacks/old/stories/ppu_4.png (http://www.baddesthacks.net/hacks/old/stories/ppu_4.png)
Title: Re: Nes sprite editing with only hex editor
Post by: Hypnotic_rabbit on August 26, 2015, 04:40:04 AM
Ok so I have taken the time to understand what is going on thanks to Disch and the few resources I could find on the topic. Disch is right, If you can do it manually you can do it programmatically and thats my goal here. I understand how all 16 of  the hex values become what they become now and I can do it manually to get the results that I want in terms of being able to place any "color" I want anywhere I want on the tile manually with a hex editor. Im talking about the defaults colors(black,green,blue,teal) in tile editor pro and not the pallette elsewhere in the hex btw. My question now becomes how to decode which is which with code when i read the data into the program, there are way too many possibilities to try and code for each one of them. Surely there is a logic he be found here givin that each "color" has its own unique traits it gives the resulting hex values. I guess what I need would be the source to an emulator too see how it does it but I havent had any luck finding a function that I could use to do what I want. But yeah, thats where Im at now, how to decipher which colored block is where based off of the hex values I find stored in the hex at any time when i read it into my program, any help on this? Any code in any language would be nice and very helpful here.

August 26, 2015, 08:46:09 AM - (Auto Merged - Double Posts are not allowed before 7 days.)

I think i understand a little better now but I just want to ask, the lower plane generates 8 hex values that each represent a line on  the 8x8 tile. Converting these hex values into binary give you the 0 or 1 that determine if it is displayed or not. Doing the same with the higher plane tells you the 0 or 1 for its version of the 8x8. When the two planes are combined on a single plane the 0s and 1 will be opposite for the lower and higher planes that tells you which one to display  and if they match on 0's its transparent?  The part I dont get is where 2 and 3 come from, im guessing these are the other two colors of the four?
Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 26, 2015, 10:03:20 AM
Quote from: Hypnotic_rabbit on August 26, 2015, 04:40:04 AM
My question now becomes how to decode which is which with code when i read the data into the program, there are way too many possibilities to try and code for each one of them. Surely there is a logic he be found here givin that each "color" has its own unique traits it gives the resulting hex values.


I'm not entirely sure I understand this question.

Once you have a 4-color image, the game will assign a palette to that image, which will tell you what colors to use for each pixel value.  If you take a look at the PPU viewer in FCEUX, you'll see that it has 8 palettes at the bottom.

On the NES... "pixel 0" is always transparent, and pixels 1-3 get mapped to the desired palette entry.  But for an editor, you can usually ignore the transparent thing and assign a 4th color for "pixel 0"... unless you want to draw multiple tiles on top of each other.

Where the palette is in the ROM is dependent on the game.  I'm sure there are docs available on this site which go over changing palettes in NES games.

Quotehow to decipher which colored block is where based off of the hex values I find stored in the hex at any time when i read it into my program, any help on this? Any code in any language would be nice and very helpful here.

?? Are you asking for a code example of how to decode NES graphics?

Here's some pseudo-code:


//  'output' is a 2D array to hold generated image.
//  output[y][x] represents 1 pixel in the image, and can have a value between 0-3

for( int y = 0; y < 8; ++y )
{
    byte lo = ROM[ offset + y     ];    // low bitplane byte for this row
    byte hi = ROM[ offset + y + 8 ];    // high bitplane byte for this row
   
    // left-most pixel is in bit 7 of the lo/hi bytes.  So we need to extract that bit
    //
    // ((hi >> 6) & 2)  <- this moves bit 7 to bit position 1:  H....... becomes ......H.
    //     then masks out the interesting 'X' bit
    //
    // ((lo >> 7) & 1)  <- this moves bit 7 to bit position 0:  L....... becomes .......L
    //
    // |'ing them together combines them:  ......H. | .......L becomes ......HL  which is our desired pixel
   
    output[y][0] = ((hi >> 6) & 2) | ((lo >> 7) & 1);
    output[y][1] = ((hi >> 5) & 2) | ((lo >> 6) & 1); // <- same but with bit 6
    output[y][2] = ((hi >> 4) & 2) | ((lo >> 5) & 1); // <- bit 5
    output[y][3] = ((hi >> 3) & 2) | ((lo >> 4) & 1); // <- bit 4
    output[y][4] = ((hi >> 2) & 2) | ((lo >> 3) & 1); // <- bit 3
    output[y][5] = ((hi >> 1) & 2) | ((lo >> 2) & 1); // <- bit 2
    output[y][6] = ( hi       & 2) | ((lo >> 1) & 1); // <- bit 1
    output[y][7] = ((hi << 1) & 2) | ( lo       & 1); // <- bit 0
}


QuoteWhen the two planes are combined on a single plane the 0s and 1 will be opposite for the lower and higher planes

Not necessarily.  The low plane and high plane can be completely different.

QuoteThe part I dont get is where 2 and 3 come from, im guessing these are the other two colors of the four?

Yes, there are 4 pixel values:  0, 1, 2, and 3

If the coresponding bit in the low plane is set, you add 1 to the pixel value
If the coresponding bit in the high plane is set, you add 2 to the pixel value

pixel 0 = bit is clear in both planes
pixel 1 = bit is set in low plane, but not in high plane
pixel 2 = bit is set in high plane, but not in low plane
pixel 3 = bit is set in both planes
Title: Re: Nes sprite editing with only hex editor
Post by: henke37 on August 26, 2015, 10:12:39 AM
In other words, each pixel is a 2 bit binary value. But they have for some inane reason decided to store the bits individually in separate parts of the data. :banghead:
Title: Re: Nes sprite editing with only hex editor
Post by: Hypnotic_rabbit on August 26, 2015, 10:17:21 AM
I know i posted a lot but this one should be my last in this thread, it took me awhile to understand but I totally get it now but for anyone reading this that still dont get it i guess i can give you my take on it in as well. (pic related)

http://i.imgur.com/bFMpouk.png

16 bytes all together
First 8 is the lower plane that creates its own version of the 8x8
Second 8 is higher plane that creates its own version of the 8x8 as well
Each 1 byte in the 8 represents a line across on the tile
Convert each byte from hex into its binary and put the planes in binary format on top of each other as in the picture
When layed on top of each other you get 4 different possibilities (the colors) between the two planes
--------------------
0 1 0 1                                                                                                               
0 0 1 1
---------------------



0 = black
0

1 = green
0

0 = blue
1

1 = teal
1


Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 26, 2015, 10:34:32 AM
Quote from: henke37 on August 26, 2015, 10:12:39 AM
for some inane reason

Simpler hardware design.

You can have a different shift register for each plane, and can change which bit you extract depending on the fine horizontal scroll.  You could accomplish the same thing with a more traditional 2bpp graphics format but it would probably require more circuitry.
Title: Re: Nes sprite editing with only hex editor
Post by: Grimlock on August 26, 2015, 02:03:10 PM
Quote from: Hypnotic_rabbit on August 25, 2015, 08:37:57 AM
but to have someone tell me an editor isnt changing the hex values in the rom makes me laugh.

I think it was pretty obvious that I was referring to the address...  It makes me laugh when someone completely misunderstands my post right after I completely misunderstand theirs  :D  :crazy:.
Title: Re: Nes sprite editing with only hex editor
Post by: Hypnotic_rabbit on August 26, 2015, 02:24:28 PM
Quote from: Grimlock on August 26, 2015, 02:03:10 PM
I think it was pretty obvious that I was referring to the address...  It makes me laugh when someone completely misunderstands my post right after I completely misunderstand theirs  :D  :crazy:.

Fair enough, but i have to admit i didnt explain what i was asking about very well to begin with. Take in to consideration that i was clueless about the topic when i started this thread. Everythings good now, aint nothing left to do but implement what iv learned here.
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 26, 2015, 06:28:26 PM
(http://i59.tinypic.com/20ggxz5.png)
Title: Re: Nes sprite editing with only hex editor
Post by: Azkadellia on August 26, 2015, 06:49:26 PM
@Dr. Floppy
None of your images are showing up.
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 26, 2015, 08:59:27 PM
Quote from: Azkadellia on August 26, 2015, 06:49:26 PM
@Dr. Floppy
None of your images are showing up.

Still?!?!?   :banghead:

Rotwang just fixed the permissions to allow non-members to view imagery...   :huh:
Title: Re: Nes sprite editing with only hex editor
Post by: mz on August 26, 2015, 09:07:30 PM
Quote from: Dr. Floppy on August 26, 2015, 08:59:27 PMRotwang just fixed the permissions to allow non-members to view imagery...   :huh:
This has nothing to do with members or non-members of a site.

Most servers, including that one, block hotlinking to prevent bandwidth stealing.

Just use a image hosting server like everybody else in the world. Imgur is the best and most popular one, you should learn how to use it.
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 26, 2015, 10:03:00 PM
Quote from: mz on August 26, 2015, 09:07:30 PM
This has nothing to do with members or non-members of a site. Most servers, including that one, block hotlinking to prevent bandwidth stealing.
Hotlinking has worked just fine before. Rotwang turned certain permissions off last week to experiment with blocking Russian spambots.

QuoteJust use a image hosting server like everybody else in the world. Imgur is the best and most popular one, you should learn how to use it.
Yeah, I already tried Imgur twice and it sucked lobster dick both times (see prior thread). PostImg seems to be working, though.  :)
Title: Re: Nes sprite editing with only hex editor
Post by: mz on August 26, 2015, 10:14:51 PM
Quote from: Dr. Floppy on August 26, 2015, 10:03:00 PMYeah, I already tried Imgur twice and it sucked lobster dick both times (see prior thread).

I know you did. That's why I said "you should learn how to use it."

PostImg is a huge piece of shit and it doesn't work at all for anybody else. Only works for you because, again, you already have that image in your browser cache.

My final advice: Stop using shitty servers and learn how to use Imgur.
Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 26, 2015, 10:55:57 PM
Quote from: Dr. Floppy on August 26, 2015, 10:03:00 PM
Yeah, I already tried Imgur twice and it sucked lobster dick both times (see prior thread). PostImg seems to be working, though.  :)

Que?

go to imgur.com, click on "upload", drag+drop image (or click browse and select it), copy/paste link.

It's about as simple as possible.

Also if you are using PostImg it does not appear to be working, because I still haven't seen any image you have posted.


EDIT:

See how easy it is?

(http://i.imgur.com/2IEtKdZ.png)


(as you can see, I'm currently playing Destiny)
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 27, 2015, 12:27:41 AM
Quote from: mz on August 26, 2015, 10:14:51 PM
I know you did. That's why I said "you should learn how to use it."

PostImg is a huge piece of shit and it doesn't work at all for anybody else. Only works for you because, again, you already have that image in your browser cache.

My final advice: Stop using shitty servers and learn how to use Imgur.

Have I been rude or unprofessional towards you?


Quotego to imgur.com, click on "upload", drag+drop image (or click browse and select it), copy/paste link.

It's about as simple as possible.

I'm sure it is. Which means there's no learning curve involved; Imgur simply isn't working on my end. I gave it permissions via No$cript and AdBlock and it still wouldn't cooperate. I tried a different browser. Still nothing. Exactly how much time am I supposed to waste before I'm allowed to just try some other imagehost?
Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 27, 2015, 12:31:08 AM
weird.  Have you tried tinypic?  They're my #2
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 27, 2015, 12:36:33 AM
(http://i59.tinypic.com/20ggxz5.png)
Title: Re: Nes sprite editing with only hex editor
Post by: Disch on August 27, 2015, 12:44:19 AM
tinypic seems to be working!  yay
Title: Re: Nes sprite editing with only hex editor
Post by: Dr. Floppy on August 27, 2015, 12:48:07 AM
Thank the great good Lord.

And thank you, Mr. Disch. I'll be using TinyPic henceforth.
Title: Re: Nes sprite editing with only hex editor
Post by: Jeville on August 28, 2015, 05:10:16 AM
Quote from: Dr. Floppy on August 27, 2015, 12:27:41 AMI'm sure it is. Which means there's no learning curve involved; Imgur simply isn't working on my end. I gave it permissions via No$cript and AdBlock and it still wouldn't cooperate. I tried a different browser. Still nothing. Exactly how much time am I supposed to waste before I'm allowed to just try some other imagehost?
Error happens if you don't use a mouse click at all when selecting a picture. Just don't use the keyboard for everything.