#156772 - Polaris - Tue May 13, 2008 9:54 pm
Here is my current code to accomplish this task, it doesn't work. I'm omitting stuff as Vram bank set up, to make it as short as possible.
I found this digging on the forums in the following thread
http://forum.gbadev.org/viewtopic.php?t=7160&highlight=plotting+pixels+mode.
There the original poster said it was incomplete code, but I took it any way to see if I could complete it. Turns out it's a bit tougher than expected.
Anyone familiar with this, probably already knows that the main problem with this approach is saving the actual pixel you want, because writes into VRAM are only 16 bit and pixels are 4 bit, so you end up taking 4 pixels at a time.
It took me a little while, but I realised this particular part is not right, because myPixel always ends up as 0.
Any ideas as to what I should do in order to tell into which one of the four pixels I should be writting in?
The other thing I don't yet understand fully is the bit masking part.
I do understand that & 0xFFF0 clears the lowers bits, which is exactly what I want.
What I don't get that much is the part where the color is added. Shouln't I be doing something like
To my understanding that should set the apropiate bits. But my understanding is not very reliable, so some help would be apreciated.
I'll leave it at that for now.
Code: |
#define TOTALCOLS 32
#define PIXELSPERTILE 64 u16 *bg0MapMemory; u16 *bg0TileData; void initBackgrounds() { bg0MapMemory =(u16*)BG_MAP_RAM(31); bg0TileData =(u16*)BG_TILE_RAM(0); BACKGROUND.control[0] = BG_32x32 | BG_COLOR_16 | BG_MAP_BASE(31) | BG_TILE_BASE(0) | BG_PRIORITY(0); BG_PALETTE[1] = RGB15(0,31,0); //This gives each tile an idividual value. int row,col; int tileIndex = 0; for (row=0; row<32; row++) { for (col=0; col<32; col++) { bg0MapMemory[row*32 + col] = tileIndex; tileIndex++; } } } void plotPixels(int screenPosX, int screenPosY, unsigned short int color) { int tileX,tileY,interiorX,interiorY,pixelNumber,myIndex,myPixel; tileX = screenPosX/8; // figure out the "column" we're writing to tileY = screenPosY/8; // figure out the "row" interiorX = screenPosX - tileX*8; // figure our X offset within the tile interiorY = screenPosY - tileY*8; // figure our Y offset withing the tile // this next bit calculates the pixel "index" as if the tileData // were a u4 array pixelNumber = (tileY*(TOTALCOLS*PIXELSPERTILE) + tileX*PIXELSPERTILE) + ((interiorY*8) + interiorX); // divide by 4 to get the u16 index // (remember that this u16 contains 4 pixels worth of data) myIndex = pixelNumber/4; // remember which of the 4 pixels we're trying to write to myPixel = pixelNumber - myIndex*4; // pull a copy of the "original" color, since we need to change // only one 4-bit section of the thing u16 originalColor = bg0TileData[myIndex]; // do some bit math to mask out our pixel and then add it to // our new color to set just that one pixel if (myPixel==0) { bg0TileData[myIndex] = (originalColor & 0xFFF0) + color; } else if (myPixel==1) { bg0TileData[myIndex] = (originalColor & 0xFF0F) + color<<4; } else if (myPixel==2) { bg0TileData[myIndex] = (originalColor & 0xF0FF) + color<<8; } else if (myPixel==3) { bg0TileData[myIndex] = (originalColor & 0x0FFF) + color<<12; } } int main() { initBackgrounds(); //Everthing bellow is a really really crud way of plotting many pixels :P int bla = 0; int delay = 100000; while(1){ if(bla < 1024){ if(delay > 0){ delay--; } else{ delay = 100000; plotPixels(bla, 0, RGB15(0,31,0)); bla++; } } } return 0; } |
I found this digging on the forums in the following thread
http://forum.gbadev.org/viewtopic.php?t=7160&highlight=plotting+pixels+mode.
There the original poster said it was incomplete code, but I took it any way to see if I could complete it. Turns out it's a bit tougher than expected.
Anyone familiar with this, probably already knows that the main problem with this approach is saving the actual pixel you want, because writes into VRAM are only 16 bit and pixels are 4 bit, so you end up taking 4 pixels at a time.
It took me a little while, but I realised this particular part is not right, because myPixel always ends up as 0.
Code: |
// divide by 4 to get the u16 index // (remember that this u16 contains 4 pixels worth of data) myIndex = pixelNumber/4; // remember which of the 4 pixels we're trying to write to myPixel = pixelNumber - myIndex*4 |
Any ideas as to what I should do in order to tell into which one of the four pixels I should be writting in?
The other thing I don't yet understand fully is the bit masking part.
Code: |
bg0TileData[myIndex] = (originalColor & 0xFFF0) + color;
|
I do understand that & 0xFFF0 clears the lowers bits, which is exactly what I want.
What I don't get that much is the part where the color is added. Shouln't I be doing something like
Code: |
bg0TileData[myIndex] = (originalColor & 0xFFF0);
bg0TileData[myIndex] |= color; |
To my understanding that should set the apropiate bits. But my understanding is not very reliable, so some help would be apreciated.
I'll leave it at that for now.