Gameboy Development Forum

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.

Ads

#1 2017-02-05 13:30:34

Mills
Member
Registered: 2012-12-21
Posts: 132

sctruct in gbdk not working

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.

Code:

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

 

#2 2017-02-05 14:10:36

DonaldHays
Member
From: Seattle
Registered: 2016-08-01
Posts: 36
Website

Re: sctruct in gbdk not working

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

 

#3 2017-02-05 14:44:26

svendahlstrand
Member
Registered: 2016-12-07
Posts: 17
Website

Re: sctruct in gbdk not working

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:

Code:

#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

 

#4 2017-02-06 07:33:27

Mills
Member
Registered: 2012-12-21
Posts: 132

Re: sctruct in gbdk not working

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:

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

 

#5 2017-02-06 10:00:56

svendahlstrand
Member
Registered: 2016-12-07
Posts: 17
Website

Re: sctruct in gbdk not working

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

Code:

#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

 

#6 2017-02-06 14:53:27

Mills
Member
Registered: 2012-12-21
Posts: 132

Re: sctruct in gbdk not working

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

 

#7 2017-02-06 15:11:15

svendahlstrand
Member
Registered: 2016-12-07
Posts: 17
Website

Re: sctruct in gbdk not working

You're most welcome, and I'm glad I could help. smile

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson