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
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
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,
, 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);
}