#28978 - Caedes - Tue Nov 09, 2004 3:50 pm
I have a big problem. The following code should display a character stored in 8 bytes. It is 8 pxl wide and 8 pxl high. One row is stored in one pixel.
I checked over 100 times, but it still doesn't work!
The screenmode is MODE_4 | BG2_ENABLE.
Please help me.
Code: |
int VideoBufferPos(int x, int y)
{
return (y*240+x);
}
void DrawCharMode4(int x, int y, int ch, u8 Color, u8 Color2)
{
u16* VideoBuffer = (u16*)0x6000000;
int i,j;
int k=VideoBufferPos(x,y);
int l;
u8 nC;
for(j=0;j<8;j++)
{
for(i=0;i<8;i++)
{
if ((1<<i) & Chars[ch*8+j])
{
nC=Color;
}
else
nC=Color2;
l=k/2;
int DblWrdMask=(0xFF<<(8*(k%2)));
int NewColMask=(nC<<(8*(k%2)));
VideoBuffer[l]=(VideoBuffer[l] & !DblWrdMask) | NewColMask;
k++;
}
k+=(240-8);
}
} |
_________________
Kirby is our master. (>?)7
Last edited by Caedes on Tue Nov 09, 2004 4:19 pm; edited 1 time in total
#28989 - sajiimori - Tue Nov 09, 2004 7:04 pm
Debugging tips:
Name all your values. Don't use magic numbers.
Name more expressions by seperating them into functions. For instance, (1<<i)&Chars... can become a function with a sensible name.
After you have the components seperated, test them one at a time.
You'll want a printf function for sure, because otherwise you're limited to seeing what ends up on screen (which is often not enough information to find the problem). I'd recommend Dan's posprintf in combination with VBA's console print SWI.
#28996 - Caedes - Tue Nov 09, 2004 9:33 pm
I'm very interested in these console print SWI. I use VBA so this surely would help me.
In fact one of my main problems _is_ that I have no feedback from my program _what_ it does.
I will see what I can find about Dan's posprintf, but please can you explain SWI?
PS:Sorry for my(maybe) bad English.
_________________
Kirby is our master. (>?)7
#28997 - sajiimori - Tue Nov 09, 2004 9:34 pm
It's in the VBA FAQ on forgotten's site.
#29030 - Cearn - Wed Nov 10, 2004 10:06 am
The only thing that I can't really consider bad is in:
Code: |
VideoBuffer[l]=(VideoBuffer[l] & !DblWrdMask) | NewColMask; |
DblWrdMask will be 0x00FF or 0xFF00 for even and odd pixels, respectively. The expression " !DblWrdMask " is a conditional expression, not a logical one, and will either be true or false, depending on whether DblWrdMask is zero or not. In this case, the result will always be false (i.e. zero), and a 0-mask will clear both pixels, rather than just the one you wanted to mask. The operator to invert all the bits for the mask is ~.
Also, division and modulo aren't quite the fastest operators on the GBA; using shift-right (x/2 is x>>1) and ANDing (x%2 is x&1) might be a better idea, as is using unsigned ints when using them for masks.
As for debugging, VBA has a memory viewer which peek and poke at. And you can always try the code in a normal PC program and use the debugger in that compiler.
#29040 - sajiimori - Wed Nov 10, 2004 5:19 pm
*, /, and % will reduce to bit operations if you're working with constant powers of 2 (which he is).
#29190 - Caedes - Sat Nov 13, 2004 9:41 am
@Cearn
Thank you very much for your help!
Now it works. I confused ! and ~. Such an unexpected mistake...
Thank you
_________________
Kirby is our master. (>?)7
#29196 - Cearn - Sat Nov 13, 2004 2:07 pm
sajiimori wrote: |
*, /, and % will reduce to bit operations if you're working with constant powers of 2 (which he is). |
They do?
*runs test case with -S flag*
Well, I'll be a monkey's uncle, they do! Thanks for pointing that out. (devkitARM still gives me a lot of excess code for the modulo replacement, though)
#29208 - tepples - Sat Nov 13, 2004 9:05 pm
Cearn wrote: |
sajiimori wrote: | *, /, and % will reduce to bit operations if you're working with constant powers of 2 (which he is). |
They do?
*runs test case with -S flag*
Well, I'll be a monkey's uncle, they do! Thanks for pointing that out. (devkitARM still gives me a lot of excess code for the modulo replacement, though) |
There won't be as much "excess code" if you're dealing with unsigned numbers.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.