screen scrolling solution and/or fail

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
x_eqis
Posts: 18
Joined: April 10th, 2012, 5:37 pm

screen scrolling solution and/or fail

Post by x_eqis » April 15th, 2012, 11:42 am

Hey everybody,
I passed the last hours trying to figure out how to scroll (horizontically / vertically), so that the level can be larger than the screen.

Finally made a first model, but it still has some annoying bug.

Here is what I did:
first i needed some new variables.

uppery (100),// outline borders of the whole level
lowery (2200),
leftx (100),
rightx (3500),
screenrightx (1870), //limits of the screen
screenlowery (1000),
screenleftx (100),
screenuppery (100),
offsetx (0), //if the player moves out of the initial screen, an offset is built
offsety (0),

mespeed (2.4), //the player variables
mex (900),
mey (400),
mexoff (0),
meyoff (0),
mesize (40),
mehealth (10000),

then i let the program check whether there is an offset to consider....

if (mex > screenrightx + offsetx) {offsetx = mex - screenrightx;} // x axis offset
if (mex < screenleftx + offsetx) {offsetx = mex - screenleftx;}

if (mey > screenlowery + offsety) {offsety = mey - screenlowery;} //y axis offset
if (mey < screenuppery + offsety) {offsety = mey - screenuppery;}

...so that the player sprite stays inside the screen.

void Game::drawme (float mex, float mey, float offsetx, float offsety, int mesize, int mehealth, int mer, int meg, int meb)
{
int mecurrentx = mex - offsetx; //mecurrentx - current position inside screen for this particular frame
int mecurrenty = mey - offsety;
gfx.drawcircle (mecurrentx, mecurrenty, mesize, mer, meg, meb);
}


When drawing the background, the program has to check whether the background objects are within the screen borders or else the program will crash and the pc will implode:

void Game::drawbackground (int offsetx, int offsety)
{
int mecurrentx = mex - offsetx;
int mecurrenty = mey - offsety;
for (int aighx = leftx; aighx < rightx; aighx = aighx + 7)
{
for (int aighy = uppery; aighy < lowery; aighy = aighy + 7)
{
if (aighx < screenrightx + offsetx && aighx - offsetx > 0 && aighy < screenlowery + offsety && aighy - offsety > 0) //if not inside screen, object ain't drawn
{
gfx.drawline (aighx - offsetx, aighy - offsety, aighx - offsetx, aighy + 2 - offsety, aighx / 6, aighy / 2, aighx / 5);
}
}
}

}





In general, this model sort of works.
However:
when for example I start the program and do nothing, the ball will fall until hitting the lower level border. it then bounces back up and falls again. The problem is: as the ball hits the floor repeatedly, the offsety will re-adjust so that the background will move up a bit more.
This problem goes for all level borders, not just the bottom.

My bug description probably sucks but it becomes more clear when running the code..

if anyone has any suggestions or tips, those would make me a happy man for the rest of my insignificant life :?
Attachments
scroll.rar
(167.15 KiB) Downloaded 213 times

Dlotan
Posts: 32
Joined: April 10th, 2012, 4:58 pm

Re: screen scrolling solution and/or fail

Post by Dlotan » April 15th, 2012, 2:06 pm

i didnt change a thing in ur code and get an error by instant.. That kind of error, where u draw a pxl outside the screen

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: screen scrolling solution and/or fail

Post by chili » April 15th, 2012, 2:34 pm

It doesn't crash when I run it on my pc, but I see a different problem. It seems that the boundaries are different for the top and left than for the right and bottom (top and left the boundary is the same as the background, bottom and right the boundary is beyond the background). I also see the problem you described, but that is mostly due to the way you handle the ball bouncing off the walls (offset = -offset).
Chili

KPence
Posts: 18
Joined: April 21st, 2012, 12:18 am

Re: screen scrolling solution and/or fail

Post by KPence » April 21st, 2012, 10:32 pm

void D3DGraphics::PutPixel( int x,int y,int r,int g,int b )
{
x=min(799,max(1,x));y=min(599,max(1,y));
((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = D3DCOLOR_XRGB( r,g,b );
}
Would this prevent that glitch from ever happening?
Needs more foo

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: screen scrolling solution and/or fail

Post by chili » April 22nd, 2012, 8:38 am

That would prevent the game from crashing when it tries to draw off of the screen, but it would not the optimum solution to that problem either because it's just masking the underlying flaw in the game engine and it's slowing down a time-critical function.

I would however recommend putting some assert() macros in PutPixel(). Maybe I'll quickly go over assert() in a future lesson...
Chili

x_eqis
Posts: 18
Joined: April 10th, 2012, 5:37 pm

Re: screen scrolling solution and/or fail

Post by x_eqis » April 23rd, 2012, 5:30 pm

hey there,
thanks for your answers and sorry for being offline for a bit..the reason why the program may crash is that I messed around in the "windows.cpp" and tried to match the game to 1920x1080. So I had the same thing when I tried it on my netbook..
still didn't work out a clean way of programming but I put it on ice anyway in order to make my little pong clone. but it looks like kpence posted exactly the type of scrolling I failed to implement, so I will now beg him to release code as well.

Post Reply