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-11-08 15:45:00

deakmania
New member
Registered: 2017-11-08
Posts: 3

Beginner with question on Vertical Scroll issue

I started up doing dev a week or so ago using GBDK v2.94 and I'm running into two issues on my program when I scroll vertically.  Problem details and code snippet are below, any help would be appreciated.  I've been banging my head on this for several days.  Thanks!

1.
I have an 80x80 map that I'm loading as I scroll (every 6 tiles).  In set_bkg_tiles, I included the offset of the distance moved (so for example after 6 tiles down, scrollY = 6) and it is should be doing:
       set_bkg_tiles(0,tempa+6, 32, 1, (unsigned char *) &map_data+cnt);   
However it just fails to do set_bkg_tiles and just loops the tiles loaded earlier.  If I replace the variable scrollY with a number it does the addition correctly (i.e. tempa+6)

2.
The commented out line is what I'd prefer to use over the first "for" loop, but doesn't seem to set the correct value. 


UBYTE scrollX, scrollY, tempa, i;
UWORD cnt;

void drawMap(){
    cnt = 0;
    for (tempa = 0; tempa<scrollY; tempa++){
        cnt+=MapSizeX;
    }
    //cnt = scrollY*MapSizeX;
   
    for( tempa=0; tempa <= 30; tempa++ ){     
        set_bkg_tiles( 0, tempa+scrollY, 32, 1, (unsigned char *) &map_data+cnt);   
        cnt = cnt + MapSizeX;
    }
}

Last edited by deakmania (2017-11-08 17:27:46)

Offline

 

#2 2017-11-08 22:45:37

ssjason123
Member
Registered: 2017-03-21
Posts: 16

Re: Beginner with question on Vertical Scroll issue

I think you need to show some more code. Like how scrollY is being updated and the values/type for MapSizeX.

Your cnt = scrollY * MapSizeX; isn't working is probably caused by typecasting. ScrollY is a UBYTE so your multiplication result is probably being limited to a UBYTE value and doing an 8-bit multiplication instead of the expected 16-bit calculation.

The documentation for set_bkg_tiles in gb.h notes that it expects values in the 0-31 range for x,y,w,h. I think the scrollY is exceeding that you might try using modulo (%) to wrap the tempA+scrollY into the expected range, so it doesn't end up writing random data outside of the tile buffer.

I don't use the GBDK so I am not entirely sure if the header documentation is correct for set_bkg_tiles.

Last edited by ssjason123 (2017-11-08 22:46:00)

Offline

 

#3 2017-11-09 13:48:07

deakmania
New member
Registered: 2017-11-08
Posts: 3

Re: Beginner with question on Vertical Scroll issue

Thanks for your reply, #1 was definitely a casting issue.  Resolved by making scrollY a UWORD. 

Still having difficulties with getting the correct value in for the set_bkg_tiles.   I've taken out my horizontal scroll code (which is working ok) to focus on this.  Here's what I've got:

#include <gb/gb.h>

#define MapSizeX 80
#define MapSizeY 80

extern unsigned char tile_data[];
extern unsigned char map_data[];

UBYTE scrollX, tempa, i;
UWORD cnt, scrollY;

void init();
void checkInput();
void initMap();
void drawMap();

void main(){
    init();
    while(1){
        checkInput();
    }
}

void init(){   
    const UWORD background_palette[] = { 21342, 11342,0, 2239 }; //Color palette for the background

    UBYTE tempa = 0, i = 0;
    UWORD cnt = 0;
    UBYTE scrollX = 0;
    UWORD scrollY = 0;
    SWITCH_ROM_MBC1(2);

    wait_vbl_done();
    disable_interrupts();
    DISPLAY_OFF;
    HIDE_SPRITES;
    HIDE_WIN;
    HIDE_BKG;

    set_bkg_data( 0, 13, &tile_data);
    set_bkg_palette(0,1,background_palette);

    initMap();

    SHOW_BKG;    //show the map
    DISPLAY_ON;
    enable_interrupts();
}
   
void checkInput(){   
        delay(19);  //game speed delay
        wait_vbl_done();

        // read the gb-pad.
        i = joypad();
       
        if(( i & J_DOWN ) && (scrollY < (MapSizeY - 18))){
            for (i =0; i<16; i=i+1)
            {
                delay(4);   
                wait_vbl_done();
                scroll_bkg(0, 1);
            }
            scrollY += 2;
            if (scrollY%6 == 0){
                drawMap();
            }
        }       
}

void initMap(){
    cnt = scrollX; 
    for( tempa=0; tempa <= 30; tempa++ ){     
        set_bkg_tiles( 0, tempa, 32, 1, (unsigned char *) &map_data+cnt);   
        cnt = cnt + MapSizeX;
    }
}

void drawMap(){
    cnt = scrollY*MapSizeX;

    for( tempa=0; tempa <= 30; tempa++ ){ 
        set_bkg_tiles( 0, tempa+scrollY, 32, 1, (unsigned char *) &map_data+cnt);   
        cnt = cnt + MapSizeX;

    }
}

Offline

 

#4 2017-11-09 19:15:07

ssjason123
Member
Registered: 2017-03-21
Posts: 16

Re: Beginner with question on Vertical Scroll issue

drawMap is still calling set_bkg_tiles with a Y outside of the valid range. For example if scrollY is 32 you would be setting a Y in the range of 32-62 where the function expects 0-31. Try using:
   set_bkg_tiles(0, (tempa + scrollY) % 32, 32, 1, (unsigned char *) &map_data+cnt);

Offline

 

#5 2017-11-10 16:50:13

deakmania
New member
Registered: 2017-11-08
Posts: 3

Re: Beginner with question on Vertical Scroll issue

Thanks again for your help, got my scrolling working.  I rewrote my code since I did it based on my misunderstanding how the background and view window work.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson