[SOLVED] SGDK - Parallax + Vertical Scroll (tearing issue)

SGDK only sub forum

Moderator: Stef

Post Reply
realbrucest
Interested
Posts: 27
Joined: Wed Sep 21, 2011 9:00 pm
Location: Sevilla, Spain
Contact:

[SOLVED] SGDK - Parallax + Vertical Scroll (tearing issue)

Post by realbrucest » Fri Jun 14, 2013 2:18 pm

---------------------------------------------
Code with (a) solution a few posts below
---------------------------------------------

Hi everyone, I'm trying to combine horizontal tile scroll with vertical plane scroll but atm I can't find out how to do that avoiding that creepy tearing.

I supose tearing comes because every time I move scroll down (or up) the horizontal rows have only moved 1 pixel right (or left) so at the end of the tile I get some sort of steps across.

Close?

I've prepared a demo code to show that, full of comments and (I hope) relatively easy to read. In case it can been added to SGDK tutorials when fixed some day I Stef is OK with it (just an idea xD).

Here's the code:

Code: Select all

/******************************************************************************
  DEMO TEST HORIZONTAL PARALLAX + VERTICAL PLANE SCROLL

  Spaceships sprites made by Gregah: gregah.devianart.com

******************************************************************************/
#include <genesis.h>

// CONSTANTS ==================================================================

// Some data to deal with graphical data
#define NUM_COLUMNS_PLANE               64
#define NUM_COLUMNS_SCREEN              40
#define NUM_ROWS                        64

#define NUM_COLUMNS_SPACESHIPS          32
#define NUM_ROWS_SPACESHIPS             16
#define NUM_TILES_TILESET_SPACESHIPS    85
#define NUM_TILES_TILESET_STARS         3

// Places where we place tiles at Genesis VRAM
#define VRAM_POS_TILE1                  1
#define VRAM_POS_STARS                  VRAM_POS_TILE1
#define VRAM_POS_SPACESHIPS             VRAM_POS_STARS + NUM_TILES_TILESET_STARS

// A few useful dimensions
#define SCREEN_WIDHT                    320
#define SCREEN_HEIGHT                   224
#define PLANE_WIDHT                     512
#define PLANE_HEIGHT                    512

// Vertical coords where we ought to stop vertical scrolling
#define VERTICAL_SCROLL_MARGIN_TOP      FIX16(0)
#define VERTICAL_SCROLL_MARGIN_BOTTOM   FIX16(PLANE_HEIGHT - SCREEN_HEIGHT)

// Do we move, up or down?
#define VERTICAL_SCROLL_STOP            FIX16(0)
#define VERTICAL_SCROLL_UP              FIX16(-1)
#define VERTICAL_SCROLL_DOWN            FIX16(1)

//=============================================================================
// GFX STUFF
//=============================================================================

// PARALLAX DATA ==============================================================

// It stores how fast is going to move every tile row
const fix16 parallax_speed_table[NUM_ROWS] =
{
    FIX16(-0.5), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16(-0.3), FIX16(-0.3),
    FIX16(-0.4), FIX16(-0.4), FIX16( 0.2), FIX16( 0.2), FIX16( 0.2), FIX16( 0.2), FIX16(   0), FIX16(-0.5),
    FIX16(-0.5), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16(-0.3), FIX16(-0.3),
    FIX16(-0.4), FIX16(-0.4), FIX16( 0.2), FIX16( 0.2), FIX16( 0.2), FIX16( 0.2), FIX16(   0), FIX16(-0.5),
    FIX16(-0.5), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16(-0.3), FIX16(-0.3),
    FIX16(-0.4), FIX16(-0.4), FIX16( 0.2), FIX16( 0.2), FIX16( 0.2), FIX16( 0.2), FIX16(   0), FIX16(-0.5),
    FIX16(-0.5), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16( 0.1), FIX16(-0.3), FIX16(-0.3),
    FIX16(-0.4), FIX16(-0.4), FIX16( 0.2), FIX16( 0.2), FIX16( 0.2), FIX16( 0.2), FIX16(   0), FIX16(-0.5),
};

// TILEMAP ====================================================================
const u32 spaceships_tilemap[NUM_COLUMNS_SPACESHIPS * NUM_ROWS_SPACESHIPS] = {
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0001,0x0002,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0003,0x0004,0x0005,0x0006,0x0007,0x0008,0x0000,0x0000,0x0009,0x000A,0x000B,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x000C,0x000D,0x000E,0x000F,0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,0x0018,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F,0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0027,0x0028,0x0029,0x002A,0x002B,0x0000,0x0000,0x0000,0x0000,0x002C,0x002D,0x002E,0x002F,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0030,0x0031,0x0032,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0039,0x003A,0x003B,0x003C,0x003D,0x003E,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0039,0x003A,0x003B,0x003C,0x003D,0x003E,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x003F,0x0040,0x0041,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,0x0048,0x0049,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x004A,0x004B,0x004C,0x004D,0x004E,0x004F,0x0050,0x0051,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0052,0x0053,0x0054,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0001,0x0002,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
};


// TILESET ====================================================================
const u32 stars_tileset[NUM_TILES_TILESET_STARS * 8] =
{
    // TILE 1: big star
    0x11112111, 0x11113111, 0x11134311, 0x12345432, 0x11134311, 0x11113111, 0x11112111, 0x11111111,
    // TILE 2: aqua star (yup, a simple dot/pixel)
    0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11141111, 0x11111111, 0x11111111, 0x11111111,
    // TILE 3: cyan star (yup, the same, different color, different into-tile place)
    0x11131111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111, 0x11111111,
};

const u32 spaceships_tileset[NUM_TILES_TILESET_SPACESHIPS * 8] =
{
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, // TILE 00
    0x00000000, 0x00000000, 0x00000000, 0x00000222, 0x00002453, 0x00025622, 0x00002200, 0x00000000,
    0x00000000, 0x00000000, 0x00002200, 0x00023320, 0x22234462, 0x66647720, 0x22222200, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00222222, 0x02333333,
    0x00000000, 0x00000000, 0x00000002, 0x00000023, 0x00002234, 0x00223344, 0x22334444, 0x33444443,
    0x00000000, 0x00000000, 0x22222222, 0x33333333, 0x44333334, 0x43222223, 0x32788872, 0x27777787,
    0x00000000, 0x02222222, 0x23333333, 0x34444444, 0x44444444, 0x44444444, 0x34444444, 0x23444444,
    0x00000000, 0x20000000, 0x32200000, 0x43322000, 0x46633220, 0x64444332, 0x44444663, 0x44446444,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000, 0x32000000, 0x43200000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00002222, 0x00026355,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x22000000, 0x33200000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00222220, 0x02635532,
    0x23444444, 0x23444444, 0x26666666, 0x23444446, 0x23444444, 0x23444444, 0x23433333, 0x23432222,
    0x44444443, 0x44444443, 0x66664443, 0x64466443, 0x66446643, 0x46644664, 0x44664466, 0x34466446,
    0x27722222, 0x27778999, 0x27778898, 0x27772222, 0x27777777, 0x32777772, 0x43222223, 0x66666666,
    0x22222222, 0x99999999, 0x98989898, 0x22222222, 0x26666666, 0x34444432, 0x44444399, 0x66666888,
    0x24444444, 0x24444444, 0x24444444, 0x64444444, 0x33333344, 0x22222264, 0x99992646, 0x99926644,
    0x43200000, 0x32720000, 0x32772200, 0x43287722, 0x44322877, 0x44433228, 0x44444332, 0x64444444,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x22222222, 0x78778787, 0x22222222, 0x33333333,
    0x00000002, 0x00000023, 0x00002236, 0x00223646, 0x22334466, 0x72344446, 0x22344664, 0x33466444,
    0x22234433, 0x33344444, 0x44444444, 0x66446353, 0x44644436, 0x44464444, 0x66666666, 0x44444444,
    0x64322222, 0x44433333, 0x44444444, 0x53553553, 0x36336336, 0x44444444, 0x66444444, 0x44666666,
    0x23443363, 0x34444444, 0x44444444, 0x55355444, 0x33633444, 0x44444446, 0x44466666, 0x66644444,
    0x22000000, 0x33222200, 0x44333322, 0x44664433, 0x66444466, 0x44444644, 0x66666666, 0x46444444,
    0x00000000, 0x00000000, 0x22000000, 0x33200000, 0x44320000, 0x44432200, 0x64443320, 0x44444430,
    0x23432777, 0x23432878, 0x23432787, 0x23432777, 0x23432887, 0x23432777, 0x23432887, 0x23432777,
    0x23446644, 0x72344664, 0x78234466, 0x77234444, 0x88234444, 0x77233333, 0x88722222, 0x77878787,
    0x46662222, 0x44399999, 0x66888999, 0x44388888, 0x44432222, 0x33333333, 0x22222234, 0x87778723,
    0x22244388, 0x92644432, 0x26644333, 0x82643222, 0x22239999, 0x33388899, 0x44438888, 0x44443222,
    0x88882644, 0x22222244, 0x33334446, 0x22226663, 0x99264432, 0x92664432, 0x88264432, 0x22224432,
    0x46666666, 0x64444444, 0x44444443, 0x33333332, 0x22222227, 0x78778778, 0x77877877, 0x22222222,
    0x66666666, 0x33344444, 0x22234444, 0x78723435, 0x77872343, 0x77782353, 0x87787233, 0x22222222,
    0x66644444, 0x44433333, 0x44432222, 0x35432888, 0x33332777, 0x53532787, 0x33332777, 0x22222222,
    0x44444444, 0x33333333, 0x22222222, 0x88888888, 0x87778777, 0x87878787, 0x77777777, 0x22222222,
    0x44444464, 0x44446464, 0x34444666, 0x23444464, 0x82344464, 0x78234444, 0x77823444, 0x22222334,
    0x44444444, 0x64443334, 0x44432223, 0x44328872, 0x43277787, 0x43277728, 0x43272892, 0x44327289,
    0x46666666, 0x44444444, 0x44635355, 0x34443633, 0x24635535, 0x23443363, 0x23444444, 0x23444446,
    0x66664430, 0x44444442, 0x35355442, 0x63633442, 0x35535443, 0x63363443, 0x44644442, 0x46664442,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000, 0x20000000, 0x00000000, 0x00000000,
    0x23432887, 0x23432777, 0x23432887, 0x23432777, 0x23432787, 0x23443278, 0x02344327, 0x00234432,
    0x87878787, 0x87777777, 0x88888888, 0x77888888, 0x87888888, 0x77788888, 0x78788888, 0x88777888,
    0x87888872, 0x77778724, 0x87887244, 0x87772443, 0x87882432, 0x87772432, 0x87882432, 0x87772432,
    0x34333333, 0x43222222, 0x32000000, 0x20000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x33333320, 0x22222200, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000223, 0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x34432228, 0x23446662, 0x02344446, 0x00233444, 0x00022344, 0x00000233, 0x00000022, 0x00000000,
    0x92446466, 0x89246644, 0x22444444, 0x66444433, 0x44333322, 0x33222200, 0x22000000, 0x00000000,
    0x64444430, 0x44443320, 0x44332200, 0x33220000, 0x22000000, 0x00000000, 0x00000000, 0x00000000,
    0x00023432, 0x00023443, 0x00023344, 0x00002234, 0x00000023, 0x00000002, 0x00000000, 0x00000000,
    0x77787777, 0x27878787, 0x32778787, 0x43222222, 0x44444444, 0x33333333, 0x22222222, 0x00000000,
    0x77872432, 0x87782432, 0x87222432, 0x22444432, 0x44433332, 0x33322220, 0x22200000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00002223, 0x00223336,
    0x00000000, 0x00000000, 0x00000022, 0x00022233, 0x02233344, 0x23344553, 0x36664333, 0x44446535,
    0x00000000, 0x00000000, 0x20000000, 0x32200000, 0x43322200, 0x53536322, 0x33344433, 0x36666666,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00222222, 0x22333363, 0x33333644, 0x66222344,
    0x00000000, 0x00000002, 0x00000002, 0x00000023, 0x22222234, 0x33333343, 0x44443344, 0x43332234,
    0x22222222, 0x33333332, 0x34444320, 0x44444320, 0x22243200, 0x28823200, 0x32782320, 0x43222432,
    0x02334464, 0x23433333, 0x23322222, 0x23329999, 0x02336666, 0x00223333, 0x00002222, 0x00000000,
    0x66664333, 0x33444444, 0x22444433, 0x99633322, 0x66322200, 0x33200000, 0x22000000, 0x00000000,
    0x43333333, 0x32222222, 0x27878787, 0x22222222, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x32777234, 0x27778234, 0x87778234, 0x22778723, 0x00277872, 0x00022222, 0x00000000, 0x00000000,
    0x49992723, 0x49882723, 0x46662234, 0x44444444, 0x33333333, 0x22222222, 0x00000000, 0x00000000,
    0x44333332, 0x43222220, 0x32000000, 0x43200000, 0x32000000, 0x20000000, 0x00000000, 0x00000000,
    0x02000000, 0x23200000, 0x23200000, 0x23422000, 0x23633200, 0x23444320, 0x23464320, 0x23464432,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000022,
    0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000,
    0x23464443, 0x23464444, 0x02364444, 0x02346666, 0x02364443, 0x02364437, 0x27264437, 0x28766667,
    0x20022233, 0x32233344, 0x43333333, 0x32222222, 0x77777777, 0x79999999, 0x99999999, 0x79999999,
    0x32220000, 0x43332200, 0x33333333, 0x22222244, 0x77777244, 0x99992644, 0x99996666, 0x99996644,
    0x00000000, 0x00000000, 0x20000000, 0x32222222, 0x43787878, 0x43787722, 0x44222233, 0x64333364,
    0x00000000, 0x00000000, 0x00000000, 0x22222222, 0x87878772, 0x22222223, 0x63336364, 0x46444646,
    0x00000000, 0x00002222, 0x02234463, 0x23344446, 0x34446353, 0x44444636, 0x44464444, 0x44644444,
    0x00000000, 0x00000000, 0x53322220, 0x34433332, 0x55554443, 0x33334444, 0x44446666, 0x44464444,
    0x00000000, 0x00000000, 0x00000000, 0x22200000, 0x33320000, 0x44432000, 0x66666200, 0x44444320,
    0x28846467, 0x27724466, 0x27782464, 0x02777244, 0x00288823, 0x00278772, 0x00222000, 0x00000000,
    0x77888888, 0x77777777, 0x62222222, 0x46666666, 0x32222222, 0x20022777, 0x00000027, 0x00000002,
    0x88886644, 0x77772644, 0x22222244, 0x66666344, 0x24444444, 0x72444444, 0x77724444, 0x28882222,
    0x44444443, 0x44444332, 0x44433220, 0x44322000, 0x43262222, 0x32662888, 0x44299999, 0x22222222,
    0x33333333, 0x22222222, 0x00000000, 0x00000000, 0x20000000, 0x82000000, 0x00000000, 0x00000000,
    0x33333333, 0x23222222, 0x02788888, 0x00227777, 0x00007777, 0x00002222, 0x00000000, 0x00000000,
    0x33363278, 0x22233277, 0x88233277, 0x78822777, 0x77787777, 0x77222772, 0x00000000, 0x00000000,
    0x26363432, 0x82643432, 0x78222320, 0x77888200, 0x77222000, 0x22000000, 0x00000000, 0x00000000,
    0x02729999, 0x00282222, 0x00027266, 0x00002884, 0x00000223, 0x00000000, 0x00000000, 0x00000000,
    0x92326666, 0x24432266, 0x66443322, 0x44644433, 0x44466443, 0x22334466, 0x00223333, 0x00002222,
    0x22222200, 0x28888200, 0x22222000, 0x99992222, 0x22226662, 0x66666600, 0x33322200, 0x22200000, // TILE 84
};


// PALETTE ====================================================================
const u16 stars_pal[16] = {
	0x0000,0x0000,0x0420,0x0E20,0x0EA2,0x0CCC,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
};

const u16 spaceships_pal[16] = {
    0x0E0E,0x0E0E,0x0000,0x0064,0x0084,0x06EE,0x0042,0x0222,
    0x0846,0x0A8A,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
};

//=============================================================================
// SOME OTHER GLOBAL THINGIES
//=============================================================================
fix16 vertical_scroll_pos;
fix16 vertical_scroll_speed;

//=============================================================================
// FUNCTIONS PROTOTYPES
//=============================================================================
void drawStarfield(void);   // Plane B (background)
void drawSpaceships(void);  // Plane A (foreground)

void readJoypadInput(u16, u16, u16);

//=============================================================================
// MACROS
//=============================================================================
#define InitializeScrollTable(); \
    for(row=0; row<NUM_ROWS; row++) parallax_data[row] = FIX16(0);

#define drawSpaceshipsTilemap(r, c); \
    for(col = 0; col < NUM_COLUMNS_SPACESHIPS; col++) \
    for(row = 0; row < NUM_ROWS_SPACESHIPS; row++) \
        VDP_setTileMap( APLAN, TILE_ATTR_FULL(PAL1, 0, 0, 0, VRAM_POS_SPACESHIPS + spaceships_tilemap[(row * NUM_COLUMNS_SPACESHIPS) + col]), col + (c), row + (r));

#define updateVerticalScrollPosition(); \
    vertical_scroll_pos = fix16Add(vertical_scroll_pos, vertical_scroll_speed); \
    if(vertical_scroll_pos == VERTICAL_SCROLL_MARGIN_BOTTOM || vertical_scroll_pos == VERTICAL_SCROLL_MARGIN_TOP) \
    vertical_scroll_speed = VERTICAL_SCROLL_STOP;

//=============================================================================
// MAIN =======================================================================
//=============================================================================
int main()
{
    // Local data _____________________________________________________________
    u8 row;                         // Row index
    fix16 parallax_data[NUM_ROWS];  // Scroll value buffer
    s16 aux[NUM_ROWS];              // Needed for VDP_setHorizontalScrollTile


    // Initialization lot _____________________________________________________

    // Scroll
    vertical_scroll_speed = VERTICAL_SCROLL_STOP;
    vertical_scroll_pos = FIX16(0);
    InitializeScrollTable();

    // Input
    JOY_init();
    JOY_setEventHandler(&readJoypadInput);

    // Process ________________________________________________________________

    VDP_setScrollingMode(HSCROLL_TILE, VSCROLL_PLANE);

    drawStarfield();
    drawSpaceships();

    // MAIN LOOP ______________________________________________________________
    while(1)
    {

        // Calculate next values for horizontal parallax scroll
        for(row = 0; row < NUM_ROWS; row++)
        {
            // Sum the speed value
            parallax_data[row] = fix16Add(parallax_speed_table[row], parallax_data[row]);

            // When the horizontal margin is reached reset row scroll value
            if(fix32ToInt(parallax_data[row]) >= PLANE_WIDHT)
                parallax_data[row] = FIX16(0);

            // An auxiliar "regular integer" buffer is needed for VDP_setHorizontalScrollTile
            aux[row] = fix16ToInt(parallax_data[row]);
        }

        // Set Horizontal Scroll (TODO: set scroll only for visible rows)
        VDP_setHorizontalScrollTile(APLAN, 0, aux, NUM_ROWS, 1);

        // Set Vertical scroll (not doing what I wanted to xD)
        updateVerticalScrollPosition();
        VDP_setVerticalScroll( APLAN, fix16ToInt(vertical_scroll_pos)); // Spaceships
        VDP_setVerticalScroll( BPLAN, fix16ToInt(vertical_scroll_pos)); // Starfield

        // Vertical Sync
        VDP_waitVSync();

    }// end main loop

    return 0;   // Ok ... we'll return something

} // end main()

/******************************************************************************
 FUNCTIONS DEFINITION

 - drawSpaceships()
 - drawStarfield()
 - readJoyPadInput()

******************************************************************************/

//=============================================================================
// DRAW SPACESHIPS
// Draw four times the spaceships image/tilemap to fill the whole plane
//=============================================================================
void drawSpaceships()
{
    // Local data _____________________________________________________________
    u8 row, col; // Indexes needed for drawSpaceshipsTilemap() macro
    u8 i, j;     // Tilemap * 8 placement

    // Process ________________________________________________________________

    // Load tile stuff and color data into VRAM/CRAM
    VDP_setPalette(PAL1, (u16 *) spaceships_pal);
    VDP_loadTileData( (const u32 *) spaceships_tileset, VRAM_POS_SPACESHIPS, NUM_TILES_TILESET_SPACESHIPS, 1);

    for(i = 0; i < NUM_ROWS; i += NUM_ROWS_SPACESHIPS)
    for(j = 0; j < NUM_COLUMNS_PLANE; j += NUM_COLUMNS_SPACESHIPS)
    {
        drawSpaceshipsTilemap(i, j);    // (macro)
    }

} // end drawSpaceShips()

//=============================================================================
// DRAW STARFIELD
//=============================================================================
void drawStarfield()
{
    // Local data _____________________________________________________________
    u8 row, col;

    // Process ________________________________________________________________

    // Load tile stuff and color data into VRAM/CRAM
    VDP_setPalette(PAL0, (u16 *) stars_pal);
    VDP_loadTileData( (const u32 *) stars_tileset, VRAM_POS_STARS, NUM_TILES_TILESET_STARS, 1);

    for(row = 0; row < NUM_ROWS; row++)
    for(col = 0; col < NUM_COLUMNS_SCREEN; col++)
    {
        // 20 percent chances of getting a star
        if(random()%5 == 0)
        VDP_setTileMap( BPLAN, VRAM_POS_STARS + (random()%3), col, row);
    }

}// end drawBackground()

//=============================================================================
// READ INPUT
// If UP scroll up, if DOWN scroll down ;)
//=============================================================================
void readJoypadInput( u16 joy, u16 changed, u16 state)
{
    if(joy == JOY_1)
    {
        // Press UP ...........................................................
        if (state & BUTTON_UP)
            vertical_scroll_speed = VERTICAL_SCROLL_UP;

        // Press DOWN .........................................................
        if (state & BUTTON_DOWN)
            vertical_scroll_speed = VERTICAL_SCROLL_DOWN;

    } // end (joy1)

} // end readJoypadInput()
Battleships graphics are free to use according with his author provided that he will be credited somewhere.

Many thanks in advance anyway.

Rom + Source Code:
https://www.dropbox.com/s/varjj6xt4zari ... Scroll.zip
Last edited by realbrucest on Sun Jul 28, 2013 10:38 am, edited 2 times in total.

djcouchycouch
Very interested
Posts: 710
Joined: Sat Feb 18, 2012 2:44 am

Post by djcouchycouch » Fri Jun 14, 2013 3:01 pm

Looking at my old code, you should wait for vsync first before updating your scrolling.

realbrucest
Interested
Posts: 27
Joined: Wed Sep 21, 2011 9:00 pm
Location: Sevilla, Spain
Contact:

Post by realbrucest » Fri Jun 14, 2013 3:49 pm

Thanks djcouchycouch for taking the time, but I'm not quite sure about that.

Maybe I'm missing something else or doing something wrong.

Fact is that I took the trouble of filling my core with VDP_waitVSync() just for the sake of testing and I couldn't get rid of that tearing effect.

:cry:

By any chance, can you see something weird into my main loop ???

Code: Select all

    while(1) 
    { 

        // Calculate next values for horizontal parallax scroll 
        for(row = 0; row < NUM_ROWS; row++) 
        { 
            // Sum the speed value 
            parallax_data[row] = fix16Add(parallax_speed_table[row], parallax_data[row]); 

            // When the horizontal margin is reached reset row scroll value 
            if(fix32ToInt(parallax_data[row]) >= PLANE_WIDHT) 
                parallax_data[row] = FIX16(0); 

            // An auxiliar "regular integer" buffer is needed for VDP_setHorizontalScrollTile 
            aux[row] = fix16ToInt(parallax_data[row]); 
        } 

        // Set Horizontal Scroll (TODO: set scroll only for visible rows) 
        VDP_setHorizontalScrollTile(APLAN, 0, aux, NUM_ROWS, 1); 

        // Set Vertical scroll (not doing what I wanted to xD) 
        updateVerticalScrollPosition(); 
        VDP_setVerticalScroll( APLAN, fix16ToInt(vertical_scroll_pos)); // Spaceships 
        VDP_setVerticalScroll( BPLAN, fix16ToInt(vertical_scroll_pos)); // Starfield 

        // Vertical Sync 
        VDP_waitVSync(); 

    }// end main loop

Code: Select all

#define updateVerticalScrollPosition(); \ 
    vertical_scroll_pos = fix16Add(vertical_scroll_pos, vertical_scroll_speed); \ 
    if(vertical_scroll_pos == VERTICAL_SCROLL_MARGIN_BOTTOM || vertical_scroll_pos == VERTICAL_SCROLL_MARGIN_TOP) \ 
    vertical_scroll_speed = VERTICAL_SCROLL_STOP;
Is there anything that i am overlookin?

(I guess so ...)

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Fri Jun 14, 2013 5:19 pm

The first thing i see is that you use :

Code: Select all

 if(fix32ToInt(parallax_data[row]) >= PLANE_WIDHT) 
where i would expect

Code: Select all

 if(fix16ToInt(parallax_data[row]) >= PLANE_WIDHT) 
as you array is fix16 type.

Other thing is that you update VDP stuff in main loop where it is from good practices to prepare data in main loop and do VDP updates in the VInt handler so you are sure you are updating them during blank period.

At least if you want to keep all in main loop, you should prepare your data first then use waitVSync() and do VDP update so they happen in vblank. If your updates take too much time they can end during active period and make some tears to appear (with horizontal scroll for instance).

But except that you code looks perfectly right and it would be glad to use it as a tutorial for SGDK :)

realbrucest
Interested
Posts: 27
Joined: Wed Sep 21, 2011 9:00 pm
Location: Sevilla, Spain
Contact:

Post by realbrucest » Fri Jun 14, 2013 10:11 pm

And now thanks a lot to you Stef.

I still need to do some research about that because I'm still a newbie, but there's more I know already and next steps should be much easier now.

I'm also so glad in case I can contribute to sgdk. I've spotted a few other tiny unimportant mistakes among code comments, and of course my English is a bit crappy/scary/Yoda-style.

Not so offtopic: last week I wrote a small demo-tutorial about line scroll and hilight/shadow mode for elotrolado forums, (classic consoles subforum / SGDK programming thread). It's not been written using "pure SGDK" but it can be ported easily (it also needs to be translated, just tell me if you are interested and I'll try to have it done soon).
Maybe it can be useful as a SGDK tutorial too http://www.elotrolado.net/viewtopic.php ... 1732701384 :roll:
Bin + (Spanish) Source Code

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Sat Jun 15, 2013 9:34 am

No worries, i'm sure you will quickly figure out all that as you seems to have good code programming skill and generally good understanding of all that :)

The highlight shadow example is very nice too and i could probably use it as basis for background H/S example then add the sprite features too (by using 0xE/0xF mask) :)

Helghast
Interested
Posts: 29
Joined: Sat May 25, 2013 8:53 pm
Location: Amsterdam, Netherlands
Contact:

Post by Helghast » Mon Jun 17, 2013 9:40 am

I am having the very same issue.
I wait for my VBlank, then redraw/scroll tiles/plane where needed, but I keep seeing this horrible tearing.

I'll post my source code later tonight, but would like to figure out what could cause this in the first place?

regards,
Dennis

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Mon Jun 17, 2013 11:32 am

Well so you can read my first message, it should apply on your case too :
Other thing is that you update VDP stuff in main loop where it is from good practices to prepare data in main loop and do VDP updates in the VInt handler so you are sure you are updating them during blank period.

At least if you want to keep all in main loop, you should prepare your data first then use waitVSync() and do VDP update so they happen in vblank. If your updates take too much time they can end during active period and make some tears to appear (with horizontal scroll for instance).

Helghast
Interested
Posts: 29
Joined: Sat May 25, 2013 8:53 pm
Location: Amsterdam, Netherlands
Contact:

Post by Helghast » Mon Jun 17, 2013 11:58 am

I've been searching the documentation (as well as the propellor example, from which I learned a lot), but cannot find a good lead on how to process during Vint handler?
any pointers or sample code somewhere to get me going?

regards, Dennis

Moon-Watcher
Very interested
Posts: 117
Joined: Sun Jan 02, 2011 9:14 pm
Contact:

Post by Moon-Watcher » Mon Jun 17, 2013 1:45 pm

Helghast wrote:any pointers or sample code somewhere to get me going?
According to the explanations I understand something like this but not sure where to place VDP_waitVSync()

Code: Select all

_voidCallback *VIntCallback ( )
{
	VDP_setVerticalScroll ( ... );
	// other VDP updates

	return 0;
}

int main ( )
{
	SYS_setInterruptMaskLevel ( 4 );
	SYS_setVIntCallback ( (_voidCallback*) VIntCallback );

	while ( main_loop )
	{
		// main loop code

		VDP_waitVSync();
	}
}

Helghast
Interested
Posts: 29
Joined: Sat May 25, 2013 8:53 pm
Location: Amsterdam, Netherlands
Contact:

Post by Helghast » Mon Jun 17, 2013 2:14 pm

Great!!
That gave me a great read through the manual ;)
Am gonna try this when I get home tonight.

regards,
Dennis

Helghast
Interested
Posts: 29
Joined: Sat May 25, 2013 8:53 pm
Location: Amsterdam, Netherlands
Contact:

Post by Helghast » Tue Jun 18, 2013 9:27 am

even with the Vint handler I get tearing, if I setEnable it works, but flickers the screen black everytime I perform scrolling. Will have to debug this some more.

thanks, D.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Wed Jun 19, 2013 3:48 pm

What you mean by setEnable ?
Also you have to make your code fast enough in the vint handler so it fits in the blank period only.

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

Post by Chilly Willy » Thu Jun 20, 2013 12:20 am

You need to apply the idea of double-buffering to the vram, even though you aren't using bitmaps. You need all the currently displayed tiles to remain in vram even as you load tiles for the next display. The name table has to stay unchanged for the current display until the new tiles are loaded and you have made the new name table entries.

The latter is easy - if you use a 128x32 name table, you can actually triple-buffer the name table. You can dma or write the name table at any time, then use the scroll registers to flip to the new name table.

The former means keeping track of tile memory usage and being sure that both the current display and the next display don't use more vram than you have available. As an example, Sonic games only change a few tiles per frame. The levels are designed so that only slow changes occur to the tile set to give the engine time to load the changing tiles.

realbrucest
Interested
Posts: 27
Joined: Wed Sep 21, 2011 9:00 pm
Location: Sevilla, Spain
Contact:

Problem solved

Post by realbrucest » Sun Jul 28, 2013 10:19 am

Hi again.

In the end a friend came out with a simpler solution even though it makes things trickier in the way later optimization becomes a must.

Activating line scroll.

Source code and rom (free to use).

Codeblcoks project (src+bin)
Only rom

Thanks to doragasu for the solution.

Post Reply