#13177 - krozen - Sun Dec 07, 2003 12:50 pm
Hey guys,
im trying to use flipped mode 5 with sprites. i cant display the sprites tho! heres my code for setting up mode 5
Code: |
void setmode5flipped(int page)
{
u16 *ioreg=(u16*)0x4000000;
*ioreg=5+((page&1)<<4)+ (1<<6)+ (1<<10)+(1<<12);
ioreg[0x10]=0;
ioreg[0x11]=256;
ioreg[0x12]=128;
ioreg[0x13]=0;
} |
Ive set the first sprite at location 512, cause of the double buffer. Its really annoying me!
Any suggestions?
#13184 - sajiimori - Sun Dec 07, 2003 6:20 pm
Were you just trying to set REG_DISPCNT (the 32 bit word located at 0x4000000)? You're writing to all sorts of different places.
It's much simpler to do it this way:
Code: |
#define REG_DISPCNT *(volatile u32*)0x4000000
#define MODE_0 0x0
#define MODE_1 0x1
#define MODE_2 0x2
#define MODE_3 0x3
#define MODE_4 0x4
#define MODE_5 0x5
#define BACKBUFFER 0x10
#define H_BLANK_OAM 0x20
#define OBJ_MAP_2D 0x0
#define OBJ_MAP_1D 0x40
#define FORCE_BLANK 0x80
#define BG0_ENABLE 0x100
#define BG1_ENABLE 0x200
#define BG2_ENABLE 0x400
#define BG3_ENABLE 0x800
#define OBJ_ENABLE 0x1000
#define WIN1_ENABLE 0x2000
#define WIN2_ENABLE 0x4000
#define WINOBJ_ENABLE 0x8000
f()
{
REG_DISPCNT = MODE_5 | OBJ_ENABLE | whatever_you_want;
}
|
It seems like you're misunderstanding one or more of: array access, hexadecimal notation, or pointer arithmetic.
When you type array[index], what that means is *(array + index). And when you have a u16*, adding 1 to it moves the pointer up by 16 bits (2 bytes). So this code...
Code: |
u16 *ioreg=(u16*)0x4000000;
ioreg[0x12]=128;
|
...really writes to the address 0x4000024 (0x4000000 + 0x12 * 2), which happens to be REG_BG2PC (nowhere near what you're trying to write to).
#13185 - krozen - Sun Dec 07, 2003 6:42 pm
Thanks for reply. This is code i got off a tutorial to stretch mode 5 so that it fits the whole screen. So u get a full 32 bit colour, a full screen plus double buffering. the code is available on the main gbadev.org site.
I was trying to set the mode 5 with the sprite BG enable, BG 2 enabled and linear array for sprites, just as detailed in devotos tutorials in this line
Code: |
*ioreg=5+((page&1)<<4)+ (1<<6)+ (1<<10)+(1<<12); |
#13186 - sajiimori - Sun Dec 07, 2003 7:38 pm
Oh, I see. The author of that code might consider entering the Obfuscated C Contest. If you write code that you can read, you'll have an easier time solving problems.
P.S. You actually get 15 bit color, not 32 bit. Still pretty good though.
#13188 - sgeos - Sun Dec 07, 2003 9:16 pm
sajiimori wrote: |
Oh, I see. The author of that code might consider entering the Obfuscated C Contest. |
Looks like a neat way doing things to me. I had never considered treating a pointer to 0x04000000 as an array for writes to registers. Hidden behind macro definitions, I'm not convinced it's a bad way of doing things.
-Brendan
#13213 - Miked0801 - Mon Dec 08, 2003 7:10 pm
Oh, but it is. Did you understand what he was doing? Did sajiimori at first glance? Did I? No. Setting display registers doesn't require neat tricks or anything for speed or space. It requires easy to read code that anyone can pick up and understand - especially in a tutorial. Easy to read, understandable code is the hallmark of a great programmer. Obfuscated, hard to read code is either done to look cool (when not working in a team envirnoment - otherwise it's reason for a smackdown), for job security (only I understand what this does so therefore my job is safe - err sortof), or out of ignorance.
Mike
#13214 - sajiimori - Mon Dec 08, 2003 7:54 pm
I guess it all depends on whether you want to write code that elegantly expresses an idea or code that sits there being neat.
#13216 - Cyberman - Mon Dec 08, 2003 10:18 pm
This sounds like the start of a good lynching ;)
Well seriously you do a diservice to yourself and everyone else by being cute. Even if it might be more verbose you can at least maintain a project better by taking up some time to carefully define all your macros etc.
Packaging helps however I remember a 'little' bug I had once in a system that controled an HVAC system, it would turn on and off all the relays with a quick snap every so often. I found this bug was from my destructor for manipulating some bits in IO. So... when you make something make sure it's doing what you intend it to do by being blantantly obvious with your code ;)
Cyb
_________________
If at first you don't succeed parachuting is NOT for you.