gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Graphics > Plotting only one pixel

#20007 - mymateo - Thu Apr 29, 2004 9:22 pm

It's me again, throwing myself at the mercy of smarter, more experienced people.

I would like to know how to place a single pixel at a time. Before you reply, read through.

Here's my entire source code for you gurus to pick at. I don't see anything wrong, except for the excrutiating slow PutPixel routine. I was having troubles getting it to place an odd X numbered pixel on the screen in an odd X numbered spot, and even X's in even X's spot. I thought this would work, but it doesn't. Don't worry, I would never commit such a haneous crime against fast code in a real game or demo.

--- BEGIN CODE ---
// Program to demonstrate plotting "one" pixel.

typedef unsigned int u8;
typedef unsigned short u16;
typedef unsigned long u32;

u16* Palette = (u16*)0x05000000;
u16* videoBuffer = (u16*)0x06000000;
u32* VideoMode = (u32*)0x04000000;

void SetPalette(u8 PalNum,u8 R,u8 G,u8 B){Palette[PalNum] = ((B<<10) | (G<<5) | R);}

void PutPixel(u8 pX,u8 pY,u16 C)
{
int x,y,TempX;
x = 0;
for (y = 0; y < pX; y++)
{
x = 1 - x; // Switches between "1" and "0" alternating -- Fake binary
}
TempX = pX / 2; //Change to "TempX = pX;" and see what happens!
if (x = 0){videoBuffer[(pY * 120) + TempX] = C | 0 << 8; } // First pixel
if (x = 1){videoBuffer[(pY * 120) + TempX] = 0 | C << 8; } // Second pixel
//For some reason, the result looks like "x" is ALWAYS 1 no matter what...
}

void AgbMain()
{
int X, Y;

*VideoMode = 0x0404;
SetPalette( 0, 5, 5, 5);
SetPalette( 1,20,20,20);
for (X = 0; X < 100; X++)
{
Y = X;
PutPixel(X,Y,1); // Having Y is redundant, but for pity's sake
}
while(1); // Let's not have any hoo-haw here
}
--- END CODE ---

SO, if anyone sees anything wrong, maybe you could help me out? I'm all for figuring out something for myself, but I've stared at this until I've gone blind. If you don't want to give away the farm, then I'm happy as a clam just to get a hint as to where to look. But I'm not proud, either. I'll take a free hand-out if someone wants to fix my code and hand it back!

Anyway, thanks everyone. This forum has so many helpful people on it! (I wish I could be one...)

#20009 - niltsair - Thu Apr 29, 2004 9:49 pm

Quote:
x = 0;
for (y = 0; y < pX; y++)
{
x = 1 - x; // Switches between "1" and "0" alternating -- Fake binary
}
Replace this by : x = (pX & 0x01); Keep only the last bits, which mean it'll alternate Even and Odd.

Quote:
TempX = pX / 2; //Change to "TempX = pX;" and see what happens!
You need to divide by 2 because you fit 2 pixel per Memory Entry (16bits write);

Quote:

if (x = 0){videoBuffer[(pY * 120) + TempX] = C | 0 << 8; } // First pixel
if (x = 1){videoBuffer[(pY * 120) + TempX] = 0 | C << 8; } // Second pixel
//For some reason, the result looks like "x" is ALWAYS 1 no matter what...
Always =1 because of you're missing a '=' in your 'if' if (x == 0)

You also need to mask the current color to keep the left(or right part) intact when modifying other pixel.
Code:
x = (pX & 0x01);
TempX = pX >> 1; //Same as divide by 2 but faster(if compiler don't optimize it that way itself)
if (x == 0)videoBuffer[(pY*120)+TempX] = (videoBuffer[(pY*120)+TempX]&0xFF00) | C;
if (x == 1)videoBuffer[(pY*120)+TempX] = (videoBuffer[(pY*120)+TempX]&0x00FF) | (C<<8);


Ideally, you would create a Macro(#define) with this, instead of a function, faster.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#20010 - Miked0801 - Thu Apr 29, 2004 10:58 pm

If you poke around a bit with the search feature, you'll find a couple of nice putpixel routines coded in assembly for you already :)

#20011 - mymateo - Thu Apr 29, 2004 11:54 pm

For anyone interested, my address is:

Big Dummy
123 Kikmehard St.
Idiotsville

Please come over to my house and beat some sense into me, 'cause apparently I need it.

The extra '=' was the only thing I needed to accomplish my task as I set out to. I feel so stupid. Anyone who knows how to do anything in C should know that, right? Thanks guys, this is becoming a bad habit for me. Sigh, stupid little mistakes. Tell you what, niltsair, if you're ever in my area, I owe you an ice cream. Or a beer. Whatever your poison is.

#20012 - dagamer34 - Thu Apr 29, 2004 11:56 pm

Your code would be a lot easier to read if it were organized better. And you wouldn't make silly mistakes either.
_________________
Little kids and Playstation 2's don't mix. :(

#20015 - mymateo - Fri Apr 30, 2004 2:20 am

I'm all open for suggestions on organization; however, you should know that I didn't preview my message. The actual source code is indented to make it much easier to read.

This is how the PutPixel function SHOULD have looked: (I have used underscores as spaces turn out to do NOTHING on this board... html, grr)

--- BEGIN ---
void PutPixel(u8 pX,u8 pY,u16 C)
{
__int x,y,TempX;
__x = 0;
__for (y = 0; y < pX; y++)
__{
____x = 1 - x; // Switches between "1" and "0" alternating -- Fake binary
__}
__TempX = pX / 2; //Change to "TempX = pX;" and see what happens!
__if (x = 0){videoBuffer[(pY * 120) + TempX] = C | 0 << 8; } // First pixel
__if (x = 1){videoBuffer[(pY * 120) + TempX] = 0 | C << 8; } // Second pixel
__//For some reason, the result looks like "x" is ALWAYS 1 no matter what...
}
--- END ---

An, of course, since I've started using Ultra Edit, the color coding and (some) automatic corrections have made my life much easier. Any other ideas on how to better organize, now that you know what I'm actually working with? Thanks for your reply!

#20017 - niltsair - Fri Apr 30, 2004 2:33 am

Put your code in between (code) ..... (/code) and it'll keep it's tab. Just replace the () by [] for it to work.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)