Experiencing slowdown with scroll region.

SGDK only sub forum

Moderator: Stef

Post Reply
orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

Experiencing slowdown with scroll region.

Post by orlanrod » Fri Oct 02, 2015 11:07 pm

Every time i scroll the tilemap region, it slows down the player's animation for some odd reason. The scrolling is going fast, so no visual indication of frame loss going on.

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

Re: Experiencing slowdown with scroll region.

Post by Stef » Sat Oct 03, 2015 9:59 am

I would say there is no evident reason for that and without seeing the code we cannot say where is the problem.
I guess you should try to measure some part of your code maybe (you can use timer.h method for that) :)

orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

Re: Experiencing slowdown with scroll region.

Post by orlanrod » Sat Oct 03, 2015 6:27 pm

Stef wrote:I would say there is no evident reason for that and without seeing the code we cannot say where is the problem.
I guess you should try to measure some part of your code maybe (you can use timer.h method for that) :)
This is where i update the map region. I have to set this timer up to 10 or so to have the player's animation run at the same speed.
Can't figure out what to do.

static void updateCamera()
{
u16 value = JOY_readJoypad(JOY_1);

if ((value & BUTTON_RIGHT) && MapXPosInTile < 60 && mleft != 1)
{
if (shiftMapTimer < 4)
{
shiftMapTimer++;
}
else
{
MapXPosInTile += 1;
indb = TILE_USERINDEX;
VDP_setMapEx(BPLAN, bgb_image.map, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, indb), PlanXPosInTile, PlanYPosInTile, MapXPosInTile, MapYPosInTile, MapWidthInTile, MapHeightInTile);
//indb += bgb_image.tileset->numTile;
shiftMapTimer = 0;
}


}

if ((value & BUTTON_LEFT) && MapXPosInTile > 0 && mright != 1)
{
if (shiftMapTimer < 4)
{
shiftMapTimer++;
}
else
{
MapXPosInTile -= 1;
indb = TILE_USERINDEX;
VDP_setMapEx(BPLAN, bgb_image.map, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, indb), PlanXPosInTile, PlanYPosInTile, MapXPosInTile, MapYPosInTile, MapWidthInTile, MapHeightInTile);
//indb += bgb_image.tileset->numTile;
shiftMapTimer = 0;
}

}

}


This is the player animation code

static void updateAnim()
{
panimspeed[0] = 50; //idle
panimspeed[1] = 20; //walk
panimspeed[2] = 2; //sleep

//if (yorder == 0 && ((xorder == -1) | (xorder == +1))) current_anim = ANIM_WALKSIDES;

//if (yorder == -1 && xorder == 0) current_anim = ANIM_WALKUP;

//if (yorder == +1 && xorder == 0) current_anim = ANIM_WALKDOWN;

if ((current_anim == ANIM_STANDSIDES) | (current_anim == ANIM_STANDUP) | (current_anim == ANIM_STANDDOWN))
{
if (animpingpong > 0)
{
if (panimtimer < panimspeed[0])
{
panimtimer++;
}
else
{
if (panimframe < 2){ panimframe++; SPR_setFrame(&sprites[0], panimframe); panimtimer = 0; } else { animpingpong = -1; panimtimer = 0; }
}
}

if (animpingpong < 0)
{
if (panimtimer < panimspeed[0])
{
panimtimer++;
}
else
{
if (panimframe > 0)
{
panimframe--;
SPR_setFrame(&sprites[0], panimframe);
panimtimer = 0;
}
else
{
animpingpong = +1; panimtimer = 0;
}
}
}

}

if ((current_anim == ANIM_WALKSIDES) | (current_anim == ANIM_WALKUP) | (current_anim == ANIM_WALKDOWN))
{
animpingpong = +1;

if (panimtimer < panimspeed[1])
{
panimtimer++;
}
else
{
if (panimframe < 3)
{
panimframe++;
SPR_setFrame(&sprites[0], panimframe);
panimtimer = 0;
}
else
{
panimframe = 0;
SPR_setFrame(&sprites[0], panimframe);
panimtimer = 0;
}

}
}


if (yorder > 0 && xorder == 0)
{
current_anim = ANIM_WALKDOWN; SPR_setAnim(&sprites[0], ANIM_WALKDOWN);
}
else if (yorder < 0 && xorder == 0)
{
current_anim = ANIM_WALKUP; SPR_setAnim(&sprites[0], ANIM_WALKUP);
}
else if (yorder == 0 && (xorder < 0) | (xorder > 0))
{
current_anim = ANIM_WALKSIDES; SPR_setAnim(&sprites[0], ANIM_WALKSIDES);
}
else if (yorder == 0 && xorder == 0)
{
if (current_anim == ANIM_WALKDOWN) { SPR_setAnim(&sprites[0], ANIM_STANDDOWN); current_anim = ANIM_STANDDOWN; }
if (current_anim == ANIM_WALKUP) { SPR_setAnim(&sprites[0], ANIM_STANDUP); current_anim = ANIM_STANDUP; }
if (current_anim == ANIM_WALKSIDES) { SPR_setAnim(&sprites[0], ANIM_STANDSIDES); current_anim = ANIM_STANDSIDES; }
}
}

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

Re: Experiencing slowdown with scroll region.

Post by Stef » Sat Oct 03, 2015 6:31 pm

Do you remember i told you it's better to unpack the map before to avoid unpacking it each time you use VDP_setMapEx(..) method ?
I guess that is the problem :)

orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

Re: Experiencing slowdown with scroll region.

Post by orlanrod » Sat Oct 03, 2015 6:36 pm

Stef wrote:Do you remember i told you it's better to unpack the map before to avoid unpacking it each time you use VDP_setMapEx(..) method ?
I guess that is the problem :)
"Map *map = unpackMap(bgb_image.map, NULL);"

Oh, i skipped that part :oops:

But i am not sure where i put it in the code. it gave me "error: initializer element is not constant"

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

Re: Experiencing slowdown with scroll region.

Post by Stef » Sat Oct 03, 2015 7:16 pm

orlanrod wrote: "Map *map = unpackMap(bgb_image.map, NULL);"

Oh, i skipped that part :oops:

But i am not sure where i put it in the code. it gave me "error: initializer element is not constant"
My fault, try to separate code in 2 lines:

Code: Select all

Map *map;

map = unpackMap(bgb_image.map, NULL);

orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

Re: Experiencing slowdown with scroll region.

Post by orlanrod » Sat Oct 03, 2015 7:25 pm

Stef wrote:
orlanrod wrote: "Map *map = unpackMap(bgb_image.map, NULL);"

Oh, i skipped that part :oops:

But i am not sure where i put it in the code. it gave me "error: initializer element is not constant"
My fault, try to separate code in 2 lines:

Code: Select all

Map *map;

map = unpackMap(bgb_image.map, NULL);
Still got an error.

||=== Build: default in cosmicbrow (compiler: Sega Genesis Compiler) ===|
src\main.c|69|warning: type defaults to `int' in declaration of `map'|
src\main.c|69|error: conflicting types for 'map'|
src\main.c|64|error: previous declaration of 'map' was here|
src\main.c|69|warning: initialization makes integer from pointer without a cast|
src\main.c|69|error: initializer element is not constant|
||=== Build failed: 3 error(s), 6 warning(s) (0 minute(s), 1 second(s)) ===|

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

Re: Experiencing slowdown with scroll region.

Post by Stef » Sat Oct 03, 2015 8:21 pm

I really need to see your code with line number as i don't understand which error is for what.

orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

Re: Experiencing slowdown with scroll region.

Post by orlanrod » Sat Oct 03, 2015 8:27 pm

Stef wrote:I really need to see your code with line number as i don't understand which error is for what.
Here is the code in its entirety. Ignore the tile collision code, as i am trying to figure it out to work with scroll region. 8P

#include <genesis.h>
#include <vdp.h>
#include <vdp_pal.h>
#include <gfx.h>

#define ANIM_STANDSIDES 0
#define ANIM_WALKSIDES 1
#define ANIM_SLEEPSIDES 2
#define ANIM_STANDDOWN 3
#define ANIM_WALKDOWN 4
#define ANIM_SLEEPDOWN 5
#define ANIM_STANDUP 6
#define ANIM_WALKUP 7

#define MIN_POSX FIX32(10)
#define MAX_POSX FIX32(400)
#define MIN_POSY FIX32(10)
#define MAX_POSY FIX32(400)

//TODO: MAP COLLISION, CITIZANS, PLAYER/NPC Z ORDER,

// forward
static void handleInput();
static void joyEvent(u16 joy, u16 changed, u16 state);

static void updateAnim();
static void updateCamera();

// sprites structure
Sprite sprites[2];
//
fix32 camx; //Viewport
fix32 camy;

u16 php = 99; //player hp
u16 psp = 0; //player special power.
int posx;
int posy;
s16 xorder;
s16 yorder;
u16 current_anim = ANIM_STANDSIDES;
u8 mleft = 0;
u8 mright = 0;
u8 mup = 0;
u8 mdown = 0;
u16 panimspeed[5]; //0 = idle, 1 = walk, 2 = sleep/knockedout
u16 panimframe = 0;
u16 panimtimer = 0;
s8 animpingpong = 1;

char str[25];
char strtwo[25];


int PlanXPosInTile = 0;
int PlanYPosInTile = -3;
u16 MapXPosInTile = 0;
u16 MapYPosInTile = 0;
u16 MapWidthInTile = 40;
u16 MapHeightInTile = 28;
u16 indb;
u16 inda;
int shiftMapTimer = 0;


Map *map;

map = unpackMap(bgb_image.map, NULL);

int pInMAX = 0;
int pInMAY = 0;
#define TILEMAP_WIDTH 96
#define TILEMAP_HEIGHT 14
int tilemap[TILEMAP_WIDTH][TILEMAP_HEIGHT]; // a tilemap
int tileset_properties[TILEMAP_WIDTH][TILEMAP_HEIGHT];
int tile = 0;
int tileproperty = 0;
#define TILEPROP_NOTSOLID 0
#define TILEPROP_SOLID 1


int main()
{
u16 palette[64];

// disable interrupt when accessing VDP
SYS_disableInts();
// initialization
VDP_setScreenWidth320();

// init sprites engine
SPR_init(0);

VDP_setPaletteColors(0, palette_black, 64);
//VDP_setTextPalette(palette_grey[0]);
//VDP_setPalette(PAL0, jason_sprite.palette->data);
//VDP_setPalette(PAL1, bgb_image.palette->data);
//VDP_setPalette(PAL2, bga_image.palette->data);
// load background
indb = TILE_USERINDEX;
VDP_loadTileSet(bgb_image.tileset, indb, TRUE);
VDP_setMapEx(BPLAN, bgb_image.map, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, indb), PlanXPosInTile, PlanYPosInTile, MapXPosInTile, MapYPosInTile, MapWidthInTile, MapHeightInTile);
indb += bgb_image.tileset->numTile;

//inda = TILE_USERINDEX;
//VDP_loadTileSet(bga_image.tileset, inda, TRUE);
//VDP_setMapEx(BPLAN, bga_image.map, TILE_ATTR_FULL(PAL2, TRUE, FALSE, FALSE, inda), 0, 25, 0, 0, MapWidthInTile, MapHeightInTile);
//inda += bga_image.tileset->numTile;

// VDP process done, we can re enable interrupts
SYS_enableInts();

camx = -1;
camy = -1;
posx = 150;
posy = 101;
xorder = 0;
yorder = 0;
current_anim = 0;

//VDP_setTextPalette(1);
// init jason sprite
SPR_initSprite(&sprites[0], &jason_sprite, posx, posy, TILE_ATTR(PAL1, TRUE, FALSE, FALSE));
SPR_initSprite(&sprites[1], &point_sprite, posx, posy, TILE_ATTR(PAL1, TRUE, FALSE, FALSE));
SPR_update(sprites, 1);

// prepare palettes
memcpy(&palette[0], bgb_image.palette->data, 16 * 2);
memcpy(&palette[16], jason_sprite.palette->data, 16 * 2);

// fade in
VDP_fadeIn(0, (3 * 16) - 1, palette, 20, FALSE);

JOY_setEventHandler(joyEvent);

while(TRUE)
{
handleInput();
updateAnim();
updateCamera();

intToStr(posx, str, 1);
VDP_drawText(str, 8, 25);
intToStr(pInMAX, strtwo, 1);
VDP_drawText(strtwo, 1, 25);
// update sprites (only one to update here)
SPR_update(sprites, 2);

VDP_waitVSync();
}

return 0;
}

static void handleInput()
{
tileset_properties[0][0] = TILEPROP_SOLID;
tileset_properties[0][1] = TILEPROP_SOLID;
tileset_properties[0][2] = TILEPROP_SOLID;
tileset_properties[0][3] = TILEPROP_SOLID;
tileset_properties[1][0] = TILEPROP_SOLID;
tileset_properties[1][1] = TILEPROP_SOLID;
tileset_properties[1][2] = TILEPROP_SOLID;
tileset_properties[1][3] = TILEPROP_SOLID;
tileset_properties[2][0] = TILEPROP_SOLID;
tileset_properties[2][1] = TILEPROP_SOLID;
tileset_properties[2][2] = TILEPROP_SOLID;
tileset_properties[2][3] = TILEPROP_SOLID;
tileset_properties[3][0] = TILEPROP_SOLID;
tileset_properties[3][1] = TILEPROP_SOLID;
tileset_properties[3][2] = TILEPROP_SOLID;
tileset_properties[3][3] = TILEPROP_SOLID;

tileset_properties[11][0] = TILEPROP_SOLID;
tileset_properties[11][1] = TILEPROP_SOLID;
tileset_properties[11][2] = TILEPROP_SOLID;
tileset_properties[11][3] = TILEPROP_SOLID;
tileset_properties[12][0] = TILEPROP_SOLID;
tileset_properties[12][1] = TILEPROP_SOLID;
tileset_properties[12][2] = TILEPROP_SOLID;
tileset_properties[12][3] = TILEPROP_SOLID;
tileset_properties[13][0] = TILEPROP_SOLID;
tileset_properties[13][1] = TILEPROP_SOLID;
tileset_properties[13][2] = TILEPROP_SOLID;
tileset_properties[13][3] = TILEPROP_SOLID;

tileset_properties[14][0] = TILEPROP_SOLID;
tileset_properties[14][1] = TILEPROP_SOLID;
tileset_properties[14][2] = TILEPROP_SOLID;
tileset_properties[14][3] = TILEPROP_SOLID;


u16 value = JOY_readJoypad(JOY_1);
pInMAX = posx/16;
pInMAY = posy/16;
tileproperty = tileset_properties[pInMAX][pInMAY];

SPR_setPosition(&sprites[1], pInMAX, pInMAY);

if ((value & BUTTON_UP) && posy > 5 && tileproperty == TILEPROP_NOTSOLID) { posy -= 1; SPR_setPosition(&sprites[0], posx, posy); }

if ((value & BUTTON_DOWN) && posy < 176) { posy += 1; SPR_setPosition(&sprites[0], posx, posy); }

if ((value & BUTTON_RIGHT)) { SPR_setAttribut(&sprites[0], TILE_ATTR(PAL1,TRUE,FALSE,FALSE)); }

if ((value & BUTTON_LEFT)) { SPR_setAttribut(&sprites[0], TILE_ATTR(PAL1,TRUE,FALSE,TRUE)); }


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

if (value & BUTTON_UP) yorder = -1;
else if (value & BUTTON_DOWN) yorder = +1;
else yorder = 0;
}

static void joyEvent(u16 joy, u16 changed, u16 state)
{
// START button state changed
if (changed & BUTTON_START)
{

}

if (changed & BUTTON_LEFT)
{
if ((state & BUTTON_LEFT) && (mright != 1)) { mleft = 1; } else { mleft = 0; }
if ((state & BUTTON_LEFT)) { } else { shiftMapTimer = 0; }
}

if (changed & BUTTON_RIGHT)
{
if ((state & BUTTON_RIGHT) && (mleft != 1)) { mright = 1; } else { mright = 0; }
if ((state & BUTTON_LEFT)) { } else { shiftMapTimer = 0; }
}

}

static void updateAnim()
{
panimspeed[0] = 50; //idle
panimspeed[1] = 20; //walk
panimspeed[2] = 2; //sleep

//if (yorder == 0 && ((xorder == -1) | (xorder == +1))) current_anim = ANIM_WALKSIDES;

//if (yorder == -1 && xorder == 0) current_anim = ANIM_WALKUP;

//if (yorder == +1 && xorder == 0) current_anim = ANIM_WALKDOWN;

if ((current_anim == ANIM_STANDSIDES) | (current_anim == ANIM_STANDUP) | (current_anim == ANIM_STANDDOWN))
{
if (animpingpong > 0)
{
if (panimtimer < panimspeed[0])
{
panimtimer++;
}
else
{
if (panimframe < 2){ panimframe++; SPR_setFrame(&sprites[0], panimframe); panimtimer = 0; } else { animpingpong = -1; panimtimer = 0; }
}
}

if (animpingpong < 0)
{
if (panimtimer < panimspeed[0])
{
panimtimer++;
}
else
{
if (panimframe > 0)
{
panimframe--;
SPR_setFrame(&sprites[0], panimframe);
panimtimer = 0;
}
else
{
animpingpong = +1; panimtimer = 0;
}
}
}

}

if ((current_anim == ANIM_WALKSIDES) | (current_anim == ANIM_WALKUP) | (current_anim == ANIM_WALKDOWN))
{
animpingpong = +1;

if (panimtimer < panimspeed[1])
{
panimtimer++;
}
else
{
if (panimframe < 3)
{
panimframe++;
SPR_setFrame(&sprites[0], panimframe);
panimtimer = 0;
}
else
{
panimframe = 0;
SPR_setFrame(&sprites[0], panimframe);
panimtimer = 0;
}

}
}


if (yorder > 0 && xorder == 0)
{
current_anim = ANIM_WALKDOWN; SPR_setAnim(&sprites[0], ANIM_WALKDOWN);
}
else if (yorder < 0 && xorder == 0)
{
current_anim = ANIM_WALKUP; SPR_setAnim(&sprites[0], ANIM_WALKUP);
}
else if (yorder == 0 && (xorder < 0) | (xorder > 0))
{
current_anim = ANIM_WALKSIDES; SPR_setAnim(&sprites[0], ANIM_WALKSIDES);
}
else if (yorder == 0 && xorder == 0)
{
if (current_anim == ANIM_WALKDOWN) { SPR_setAnim(&sprites[0], ANIM_STANDDOWN); current_anim = ANIM_STANDDOWN; }
if (current_anim == ANIM_WALKUP) { SPR_setAnim(&sprites[0], ANIM_STANDUP); current_anim = ANIM_STANDUP; }
if (current_anim == ANIM_WALKSIDES) { SPR_setAnim(&sprites[0], ANIM_STANDSIDES); current_anim = ANIM_STANDSIDES; }
}
}

static void updateCamera()
{

u16 value = JOY_readJoypad(JOY_1);

if ((value & BUTTON_RIGHT) && MapXPosInTile < 60 && mleft != 1)
{
if (shiftMapTimer < 4)
{
shiftMapTimer++;
}
else
{
MapXPosInTile += 1;
indb = TILE_USERINDEX;
VDP_setMapEx(BPLAN, bgb_image.map, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, indb), PlanXPosInTile, PlanYPosInTile, MapXPosInTile, MapYPosInTile, MapWidthInTile, MapHeightInTile);
//indb += bgb_image.tileset->numTile;
shiftMapTimer = 0;
}


}

if ((value & BUTTON_LEFT) && MapXPosInTile > 0 && mright != 1)
{
if (shiftMapTimer < 4)
{
shiftMapTimer++;
}
else
{
MapXPosInTile -= 1;
indb = TILE_USERINDEX;
VDP_setMapEx(BPLAN, bgb_image.map, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, indb), PlanXPosInTile, PlanYPosInTile, MapXPosInTile, MapYPosInTile, MapWidthInTile, MapHeightInTile);
//indb += bgb_image.tileset->numTile;
shiftMapTimer = 0;
}

}

}

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

Re: Experiencing slowdown with scroll region.

Post by Stef » Sat Oct 03, 2015 8:39 pm

O_o ??

You put the :

Code: Select all

map = unpackMap(bgb_image.map, NULL);
line outside of the main method ??

You can declare the map variable wherever you want (well not really) but of course the code should be in the main() block at least.
You can put it after the basic VDP initialization or SPR_init(..) method, then afterward you have to use map instead of bgb_image.map to avoid unnecessary unpacking operation.

orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

Re: Experiencing slowdown with scroll region.

Post by orlanrod » Sat Oct 03, 2015 8:53 pm

Stef wrote:O_o ??

You put the :

Code: Select all

map = unpackMap(bgb_image.map, NULL);
line outside of the main method ??

You can declare the map variable wherever you want (well not really) but of course the code should be in the main() block at least.
You can put it after the basic VDP initialization or SPR_init(..) method, then afterward you have to use map instead of bgb_image.map to avoid unnecessary unpacking operation.
Yeah, i am jumping into this coding stuff blind, just off scripting knowledge (no C knowledge). This is why i am doing a lot of posting. xD

Cool, that works and now it is indeed fast. 8)

Thanks! xD

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

Re: Experiencing slowdown with scroll region.

Post by Stef » Sun Oct 04, 2015 8:58 am

orlanrod wrote: Yeah, i am jumping into this coding stuff blind, just off scripting knowledge (no C knowledge). This is why i am doing a lot of posting. xD
Haha, you're crazy :D

orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

Re: Experiencing slowdown with scroll region.

Post by orlanrod » Sun Oct 04, 2015 7:42 pm

Stef wrote:
orlanrod wrote: Yeah, i am jumping into this coding stuff blind, just off scripting knowledge (no C knowledge). This is why i am doing a lot of posting. xD
Haha, you're crazy :D
That's how most artist are! ;D

So far i cleared the biggest hurdle, which is the collision per tile. Now just need to figure out the second biggest hurdle... player/foe hitboxes.

Post Reply