Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
Hi, i was testing some code and I realised i'm doing something wrong.
In this code, I use a struct to move the BKG, But for some reason it is not working.
struct TEST{ UINT8 x; UINT8 y; } struct TEST* pos; void main(){ //Load some BKG to see if it moves pos->x = 0; while(1) { SCX_REG = pos->x; pos->x ++; wait_vbl_done(); } }
BKG is not moving at all...
What's wrong?
Thanks!
Last edited by Mills (2017-02-05 13:32:30)
Offline
Looks to me like you're not initializing pos to anything in memory, so maybe it's initially pointing at memory that can't be written to?
Offline
Hello! Your example is not complete and won't compile, so it's hard to tell whats wrong. But one thing that looks a bit strange is that you have a pointer, pos, that points to nothing (or garbage).
I rewrote your code a bit and here's a complete example that scrolls:
#include <gb/gb.h> unsigned char smile[] = { 0x0F,0x0F,0x30,0x30,0x40,0x40,0x40,0x40, 0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84, 0x84,0x84,0x84,0x84,0x80,0x80,0x80,0x80, 0x44,0x44,0x43,0x43,0x30,0x30,0x0F,0x0F, 0xF0,0xF0,0x0C,0x0C,0x02,0x02,0x02,0x02, 0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21, 0x21,0x21,0x21,0x21,0x01,0x01,0x01,0x01, 0x22,0x22,0xC2,0xC2,0x0C,0x0C,0xF0,0xF0, 0x0F,0x0F,0x30,0x30,0x40,0x40,0x40,0x40, 0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84, 0x84,0x84,0x84,0x84,0x80,0x80,0x80,0x80, 0x44,0x44,0x43,0x43,0x30,0x30,0x0F,0x0F, 0xF0,0xF0,0x0C,0x0C,0x02,0x02,0x02,0x02, 0x01,0x01,0x01,0x01,0x01,0x01,0xF9,0xF9, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x22,0x22,0xC2,0xC2,0x0C,0x0C,0xF0,0xF0 }; struct TEST{ unsigned char x; unsigned char y; }; struct TEST pos; int main(void){ //Load some BKG to see if it moves set_bkg_data(0, 8, smile); SHOW_BKG; pos.x = 0; while(1) { SCX_REG = pos.x; pos.x++; wait_vbl_done(); } return 0; }
Last edited by svendahlstrand (2017-02-05 14:51:51)
Offline
svendahlstrand wrote:
Hello! Your example is not complete and won't compile, so it's hard to tell whats wrong. But one thing that looks a bit strange is that you have a pointer, pos, that points to nothing (or garbage).
So, if I write this code:
#include <gb/gb.h> unsigned char smile[] = { 0x0F,0x0F,0x30... }; struct ANIMATION { UINT8 x; UINT8 y; }; void Load_Animation(struct ANIMATION animation){ animation.x = 0; animation.y = 0; } int main(void){ set_bkg_data(0, 8, smile); SHOW_BKG; Load_Animation(pos); pos.x = 0; while(1) { SCX_REG = pos.x; pos.x++; wait_vbl_done(); } return 0; }
It shows error at "void Load_Animation(struct ANIMATION animation)": struct passed as argument changed to pointer.
What's wrong again?
Thanks a lot!
Last edited by Mills (2017-02-06 07:33:58)
Offline
There are a couple of problems with that code. You're passing the variable pos to a function, but that variable is never declared/instantiated. And also you can't pass structs as function parameters, at least not using the sdcc compiler:
structures and unions cannot be assigned values directly, cannot be passed as function parameters or assigned to each other and cannot be a return value from a function
http://sdcc.sourceforge.net/doc/sdccman.pdf
One solution is to make the function Load_Animation take a pointer to a struct instead. Something like this
#include <gb/gb.h> unsigned char smile[] = { 0x0F,0x0F,0x30,0x30,0x40,0x40,0x40,0x40, 0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84, 0x84,0x84,0x84,0x84,0x80,0x80,0x80,0x80, 0x44,0x44,0x43,0x43,0x30,0x30,0x0F,0x0F, 0xF0,0xF0,0x0C,0x0C,0x02,0x02,0x02,0x02, 0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21, 0x21,0x21,0x21,0x21,0x01,0x01,0x01,0x01, 0x22,0x22,0xC2,0xC2,0x0C,0x0C,0xF0,0xF0, 0x0F,0x0F,0x30,0x30,0x40,0x40,0x40,0x40, 0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84, 0x84,0x84,0x84,0x84,0x80,0x80,0x80,0x80, 0x44,0x44,0x43,0x43,0x30,0x30,0x0F,0x0F, 0xF0,0xF0,0x0C,0x0C,0x02,0x02,0x02,0x02, 0x01,0x01,0x01,0x01,0x01,0x01,0xF9,0xF9, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x22,0x22,0xC2,0xC2,0x0C,0x0C,0xF0,0xF0 }; struct ANIMATION { UINT8 x; UINT8 y; }; struct ANIMATION pos; void Load_Animation(struct ANIMATION *animation){ animation->x = 0; animation->y = 0; } int main(void){ set_bkg_data(0, 8, smile); SHOW_BKG; Load_Animation(&pos); while(1) { SCX_REG = pos.x; pos.x++; wait_vbl_done(); } return 0; }
Last edited by svendahlstrand (2017-02-06 10:02:16)
Offline
svendahlstrand wrote:
There are a couple of problems with that code. You're passing the variable pos to a function, but that variable is never declared/instantiated. And also you can't pass structs as function parameters, at least not using the sdcc compiler:
structures and unions cannot be assigned values directly, cannot be passed as function parameters or assigned to each other and cannot be a return value from a function
http://sdcc.sourceforge.net/doc/sdccman.pdf
One solution is to make the function Load_Animation take a pointer to a struct instead. Something like this
Thanks a lot, It worked well, I knew there was some limitation in sdcc, and i didn't know how to solve it very well.
Offline
You're most welcome, and I'm glad I could help.
Offline