Horizontal Scroll not showing all background image

SGDK only sub forum

Moderator: Stef

danibus
Very interested
Posts: 135
Joined: Sat Feb 03, 2018 12:41 pm

Re: Horizontal Scroll not showing all background image

Post by danibus » Wed Jan 15, 2020 3:23 pm

Thanks Chilly Willy, changed with

Code: Select all

offsetTILES = (offset>>3) % 128;
//offset=0..1023 then offsetTILES=0..127

Anyway, I can see 1st tile-column (left part of visible tiles) updating.

I think I have to update in another place but not sure why, as 1st column dissapearing at left part (tile column 0 i.e.) would be the first tile to appear in the right part (non-visible part to scrolling)

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Re: Horizontal Scroll not showing all background image

Post by Chilly Willy » Wed Jan 15, 2020 10:43 pm

Oh, one other issue - you set the horz scroll, THEN you check if the value is > 1023 and wrap around. You should be checking for the wrap before setting the scroll offset. The way you have it now, for one frame, the hscroll regs will be -1024 while the tiles will be set to 0.

danibus
Very interested
Posts: 135
Joined: Sat Feb 03, 2018 12:41 pm

Re: Horizontal Scroll not showing all background image

Post by danibus » Wed Jan 15, 2020 10:51 pm

Thanks again Chilly Willy

Now it's like this

Code: Select all

[...]
    VDP_drawImageEx(PLAN_B, &bgd_image, TILE_ATTR_FULL(PAL2, FALSE, FALSE, FALSE, ind), 0, 0, FALSE, TRUE);
    ind += bgd_image.tileset->numTile;
    VDP_setScrollingMode(HSCROLL_PLANE  ,VSCROLL_PLANE);

    s16 offset=512;           //PIXELS from 0 to 1023px
    s16 offsetTILES = 0;    //TILES, from 0 to 63 tiles

    while(TRUE) //main Loop
    {
	//here some sprite and control stuff
	[...]

	//now scrolling part 
        offsetTILES = (offset>>3) % 128;
        if(offset>1023){ offset = 0; offsetTILES = 0;}

        VDP_setHorizontalScroll(PLAN_B, -offset);
        VDP_setMapEx(PLAN_B, bgd_image.map, TILE_ATTR_FULL(PAL2, FALSE, FALSE, FALSE, TILE_USERINDEX),offsetTILES, 0, offsetTILES, 0, 1, 28);
        offset++;
        
        VDP_waitVSync();
    }
[...]    
Anyway problem remains :|

I think I must update another tile-column, I tried different ways but not working properly.
My way is to update every tile-column when wraps using VDP_setMapEx() but it's done 1 or 2 pixels before moving out TV.

Here small gif to understand
(edit: slow motion video added)
https://prnt.sc/qoa1jh

Grind
Very interested
Posts: 69
Joined: Fri Jun 13, 2014 1:26 pm
Location: US
Contact:

Re: Horizontal Scroll not showing all background image

Post by Grind » Thu Jan 16, 2020 1:23 am

Offset 0 is always going to be the leftmost pixel/tile of the screen. Have to move it back and wrap it.

VDP_setMapEx(PLAN_B, bgd_image.map, TILE_ATTR_FULL(PAL2, FALSE, FALSE, FALSE, TILE_USERINDEX),offsetTILES, 0, (offsetTILES-1) % 128, 0, 1, 28);

Try that? That -1 could be anything else depending on where you want to move the draw column.

danibus
Very interested
Posts: 135
Joined: Sat Feb 03, 2018 12:41 pm

Re: Horizontal Scroll not showing all background image

Post by danibus » Fri Jan 17, 2020 9:18 am

This is working but not for column-tile 127, not sure why but tile 127 are being painted with column-tile 63.
Just wait for moving 8 pixels to update tile, so it's painted out of "TV zone"


Code: Select all

[...]
s16 offset=512;
s16 offsetTILES = 0;
s16 offset_old = offset;

    while(TRUE)
    {
        offsetTILES = (offset>>3) & 127;

        VDP_setHorizontalScroll(PLAN_B, -offset);

        if((offset-offset_old)==8)
        {
                    VDP_setMapEx(PLAN_B, bgd_image.map, TILE_ATTR_FULL(PAL2, FALSE, FALSE, FALSE, TILE_USERINDEX),
                                (offsetTILES-1) % 128, 0, (offsetTILES-1) % 128, 0, 1, 28);
                    offset_old = offset;
        }

        offset++;
        if(offset>1023){ offset = 0; offsetTILES = 0; offset_old = 0; }

        VDP_waitVSync();
    }

danibus
Very interested
Posts: 135
Joined: Sat Feb 03, 2018 12:41 pm

Re: Horizontal Scroll not showing all background image

Post by danibus » Wed Jan 29, 2020 12:46 pm

Hi again folks

Finally it's working! I just change the code a bit and works perfectly, only scrolling in one direction, that means pressing DPAD right it's ok, scroll works and the image is showing complete :D
Remember, I'm working with a 1024x224px PNG file.

But not sure why moving in the other direcction (DPAD left) it's not working properly :x
Starts ok, but when scrolling some tiles away (around offset = -320, pls see PLANE EXPLORER in GensKmod), get some corruption in background.

Too much time with this code, :evil: , could you pls help me?

Code: Select all

#include <genesis.h>

#include "gfx.h"
#include "sprite.h"

//FUNCTIONS
static void handleInput();
static void updatePhysic();
static void updateCamera();
static void showdebug();

//VARIABLES
s16 Xorder;                 //move left (-1) or right (1), not move (0)
s16 offset = 0;             //in pixels
s16 cuentaPixels = 0;       //adds 1 every offset(px), every 8px becomes 0, to know changes a tile
s16 column_to_update = 0;   //tile to be updated, from 0 to 127 tiles
u16 ind;                    //tile index (background)

//MAIN LOOP
int main()
{
    //320x224px
    VDP_setScreenWidth320();
    VDP_setScreenHeight224();

    //init sprite engine with default parameters
    SPR_init();

    //palettes
    VDP_setPalette(PAL0, bgb_image.palette->data);
    VDP_setPalette(PAL1, bga_image.palette->data);
    VDP_setPalette(PAL2, bgd_image.palette->data);
    ind = TILE_USERINDEX;

    //backgrounds
    VDP_drawImageEx(PLAN_B, &bgd_image, TILE_ATTR_FULL(PAL2, FALSE, FALSE, FALSE, ind), 0, 0, FALSE, TRUE);
    ind += bgd_image.tileset->numTile;

    //Scroll type
    VDP_setScrollingMode(HSCROLL_PLANE  ,VSCROLL_PLANE);

    while(TRUE)
    {
        handleInput();
        updatePhysic();
        showdebug();

        SPR_update();
        VDP_waitVSync();
    }

    return 0;
}

//Movement (PAD)
static void handleInput()
{
    u16 value = JOY_readJoypad(JOY_1);

    if (value & BUTTON_LEFT) Xorder = -1;
    else if (value & BUTTON_RIGHT) Xorder = +1;
        else Xorder = 0;
}


static void updatePhysic()
{
    if (Xorder > 0) //"moving right" = background scrolls from right to left
    {
        offset++; cuentaPixels++;  if(cuentaPixels>7) cuentaPixels=0;  //from 0 to 7 are 8px=1 TILE
    }
    else if (Xorder < 0)   //"moving left"
    {
        offset--; cuentaPixels--;     if(cuentaPixels<-8) cuentaPixels=-1;  //from -1 to -8px  
    }

    updateCamera();
}

static void updateCamera()
{
    //everytime reach PNG end, goest to start point
    if(offset>1023) offset=0;
    //same in the other direction
    if(offset<0) offset=1023;

    //MOVING RIGHT, background goes from right to left THIS IS WORKING!
    //We update ONLY 1 tile-column, the one to enter from right to left in
    //visible TV zone, just before doing the scroll
    if(cuentaPixels==0)
    {
        column_to_update = ((offset + 320) >> 3) & 127;     //320=screen_width, 127=total image tiles
        VDP_setMapEx(PLAN_B, bgd_image.map, TILE_ATTR_FULL(PAL2, FALSE, FALSE, FALSE, TILE_USERINDEX),
                     column_to_update, 0, column_to_update, 0, 1, 28);
    }
    //MOVING LEFT, backg goes from left to right, NOT WORKING properly
    // +88 tiles to reach other side in PNG image
    if(cuentaPixels==-1)
    {
        column_to_update = (((offset + 320) >> 3) & 127)+88;     //320=screen_width, 127=total image tiles
        VDP_setMapEx(PLAN_B, bgd_image.map, TILE_ATTR_FULL(PAL2, FALSE, FALSE, FALSE, TILE_USERINDEX),
                     column_to_update, 0, column_to_update, 0, 1, 28);
    }

    VDP_setHorizontalScroll(PLAN_B, -offset);
}


static void showdebug()
{
char integer_string[32];

sprintf(integer_string, "%4d   %4d   %4d", offset, cuentaPixels,column_to_update);
VDP_setTextPalette(PAL3);
VDP_drawText("offset/cuentaPixels/column_to_update:", 1, 22);
VDP_drawText(integer_string, 3, 23);
}


Post Reply