• Advertisement

Archived

This topic is now archived and is closed to further replies.

Side-scrolling?

This topic is 6061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you get a 2D game to use sidescrolling(using DirectX). I assume once a player reaches the maximum screen width you move the image on screen over so the player can see the next area of the map.

Share this post


Link to post
Share on other sites
Advertisement
Yeah you got the right idea, say a character is centered on the screen, as he goes closer to one direction the background will scroll. If you want the vertical scroll to move the background up when he jumps its the same thing. The background x,y is similar to that of the chr''s x,y

sorry if this isn''t too clear I''m not the best at explaining things...hope this helps, or at least others will post...

~~~~
sjunkim - P0et
Liquid Ice Studios
members.home.net/liquidicestudios/

Share this post


Link to post
Share on other sites
I´m not good at explaining in english but I´ll try...

Let´s say that your screen resolution is set to 640 x 480.

You load an image with the dimensions 1280x1024.

You start by drawing the map/image at (0,0).
Your character is centered on the screen.

When your character moves to the right you should see more of the map to the right.

Simply decrease the xpos of the image when drawing it to the screen. This will scroll the background...

NOTE: If you want to have large backgrounds/maps you should look up tiling or tilemaps.

Share this post


Link to post
Share on other sites
Oh one question are you using tiles? or are you using a larger background without multiple tiles?


~~~~
sjunkim - P0et
Liquid Ice Studios
members.home.net/liquidicestudios/

Share this post


Link to post
Share on other sites
hmmm I''m not quite sure how you would do this in VC++ but in VB bitblt makes it really easy...

If there is some function that would allow you to post only part of your image you could use that...I''m not quite sure...sorry...

I am looking forward to seeing some other solutions by other people though....

~~~~
sjunkim - P0et
Liquid Ice Studios
members.home.net/liquidicestudios/

Share this post


Link to post
Share on other sites
If you want to scroll with using larger bitmaps with no tiles, here's how I implemented it.

Let's say all you're .bmp's are 640x480 (since many times surfaces can't be larger than the screen), and you want to scroll around. All you need to do is when the edge of one background has been reach, blit the next background image at the edge of the last background image. Example:
Let X = where you're blitting the image (left edge) and Xsize is the size of the image horizontally.
if (X + Xsize < SCREEN_WIDTH)
{
Blit(Next_Image, X + Xsize, Y);
}

You'll need a way to know what images go where, so a world array should handle that.

Hope it helps.


Edited by - BeerNutts on July 17, 2001 3:22:40 PM

Share this post


Link to post
Share on other sites
Thanx. Im still in the process of learning. Ive got Windows programming down pretty good now. Now im learning DirectX(just DIrectDraw, DirectSound, and DirectInput). I want to make a 2D side scroller when im done. Also... what does the Sleep() function do??

Share this post


Link to post
Share on other sites
Where did you learn your windows programming? I''m still doing that and its pretty tough...

Sleep() ''freezes'' your program for a certain number of milliseconds, I think...if its the same thing as sleep in Visual Basic...

the sleeping loL! is required when a program runs TOO fast that the graphics flicker or things move to fast etc.

hope this helps,



~~~~
sjunkim - P0et
Liquid Ice Studios
members.home.net/liquidicestudios/

Share this post


Link to post
Share on other sites
Sleep() is only really good for DOS programming, in Windows, since its a multitasking environment you should update all your sprites and backgrounds using floating point variables to update the positions, at every frame work out the difference between the current and previous frame and add that float value to your sprite positions. (use timegettime to calculate the milliseconds).

Here is the code u need to set up a precise update system.
  

// Global variables

float g_fCalcDelta;
__int64 PerfTimerRate;
__int64 PerfTimerDivider = 0;


// Call this on game init

bool InitPerfTimer(void)
{
__int64 tempInt64;

// Aquire the timer frequency

if(!QueryPerformanceFrequency((LARGE_INTEGER*)&PerfTimerRate))
{
return FALSE; // System doesnt have a Performance timer

}

// Check the rate (usually 1193180)

if(PerfTimerRate==0)
{
return FALSE;
}


// Calculate millisecond divider

PerfTimerDivider = (__int64)( (__int64)PerfTimerRate / 1000);


if(!QueryPerformanceCounter((LARGE_INTEGER*)&tempInt64))
{
return FALSE; // win errors

}

return TRUE; // Success

}




WORD FTime(void)
{
__int64 tempInt64;

QueryPerformanceCounter((LARGE_INTEGER*)&tempInt64);
return (WORD)((_int64)tempInt64 / (__int64)PerfTimerDivider);
}


// Call this every frame to get the frame diff.

void UpdateTimeDeltas()
{
static WORD timerDelta = 0;
static WORD oldftime = 0;
static WORD newftime = 0;
static WORD oldnDiff = (WORD)FTime();

// Calculate the frame delta.

newftime = FTime();
if( oldftime > newftime ) oldftime = newftime;
timerDelta = newftime - oldftime;
oldftime = newftime;
float fCalcDelta = float(timerDelta / 1000.0f);


// Set the global fCalcDelta and counter.

g_fCalcDelta = fCalcDelta;
}


// Call every frame to update the sprite positions.

void UpdateSprites(bool bLeft, bool bRight)
{
// Player velocity

vel = 100.0f;

if(bLeft) Player.xpos -= g_fCalcDelta * vel;
if(bRight) Player.xpos += g_fCalcDelta * vel;

// Note ''Player.xpos'' is a float variable.


}





As for the background scrolling, give your background and main sprite a global position, and whenever you update the position of the main sprite, at the same time update the position of the background minus the sprite offset of the main sprite.

For example (640x480):

  

void SetBgndPos()
{

// Center the player.

Level.xpos = WORD(Player.xpos) - 320;
Level.ypos = WORD(Player.ypos) - 240;

}


void UpdateFrame()
{
// Set the Bgnd pos in relation to the player pos.

UpdateTimeDeltas();
UpdateSprites(..DInput status..);
SetBgndPos();

// Blit the background tiles.

Blit(Level.lpDDSurface, Level.xpos-640, Level.ypos);
Blit(Level.lpDDSurface, Level.xpos, Level.ypos);
Blit(Level.lpDDSurface, Level.xpos+640, Level.ypos);

// Blit the sprite

Blit(Player.lpDDSurface, 320, Player.ypos);

}


Note; The Blit() function would be your own routine.

Hope this helps


  Downloads:  ZeroOne Realm

Share this post


Link to post
Share on other sites
I would personally NOT run as fast as possible, as it introduces problems in the long term. To keep things simple use the performace counters to sleep the number of millisecond necessary to get an exact framerate. The nice thing about having an exact framerate is that your game is much easier to program if you know that for each tick of the drawing loop you are going exactly ( or close enough for our work ) a specific number of milliseconds. Your engine only has to know how to deal with one type of update and it will never change.

Another reason to keep a fixed framerate is determinism ( aka reproducability ). The game under identical inputs will produce a slightly diffrent game based on the speed of the system it''s running on. You may not think much of this now, but debugging, multiplayer, and recording of games depend on reproducability.

refrence: gamasutra post moterm X-wing vs Tie Fighter

Good luck.

Share this post


Link to post
Share on other sites
quote:

Another reason to keep a fixed framerate is determinism ( aka reproducability ). The game under identical inputs will produce a slightly diffrent game based on the speed of the system it's running on. You may not think much of this now, but debugging, multiplayer, and recording of games depend on reproducability.



And what will happen if my computer can´t keep up with the framrate you choose ?
Will your program not "produce slightly different game" then?

If my system is capable of 150 fps why should the framerate be locked at say 70fps ??????????????????????????????

I don´t see any good reasons for this..I don´t think many commercial games do this....





Edited by - granat on July 19, 2001 2:15:43 AM

Share this post


Link to post
Share on other sites
If the computer can''t keep up tehn you don''t draw every frame, but you still compute it.

Even if the computer can''t keep up the reptiducability is still there because given the same inputs the game still responds the same way, just a little slower. But as you point out keeping up is not really a problem for most games these days.

How about your senatio... if the computer gets lagged and people just start running through walls because you are not checking at regualr intervals.... how does that make the game more accurate?

150fps... what''s the point you can draw twice as fast as the most monitors can handle, and more than twice most normal people can even consiously percieve. If I were you I''de lock your framerate and use those cycles to improve game interaction ( AI, resource managment, network play, ... ).

Many 2d games work off a set clock, all of the animation is based on specific framerates.

But don''t take my word.. read the recent cover feature from gamsutra, might be an eye opener.

Share this post


Link to post
Share on other sites
Granat:

Yes, commercial games put limits on FPS. One popular example would be halflife the default limit is 72FPS. However there is an option included to change it...

~~~~
sjunkim - P0et
Liquid Ice Studios
members.home.net/liquidicestudios/

Share this post


Link to post
Share on other sites
If you''re looking for some good tutorials, check out http://www.aeon-software.com/ IronBlayde is one to be respected. Although his tutorials focus mainly on creating a 2d top-down RPG, I''ve personally used many of his techniques in different genres.

DracosX

Share this post


Link to post
Share on other sites

  • Advertisement