Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Screen "Scrolling"

Credits go to Ariesnl, gnolam, James Stanley, Kikaru, LennyLen, and Tobias Dammers for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Screen "Scrolling"
Ariesnl
Member #2,902
November 2002
avatar

Top down games need animations too but it's simple just have a pImage pointer that will point to the correct frame

1TExplosion::UpdateFrame()
2{
3 if (mytimer<=0)
4 {
5 mytimer=MILLISECONDS_PER_FRAME
6 frame++
7 if (frame>=NUMBER_OF_EXPLOSIONFRAMES)
8 {
9 destroyed=true;
10 }
11 else
12 {
13 pImage=im_Explosion[frame];
14 }
15 }
16 
17}

mytimer will be decreased by some timer function

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Simsimius
Member #6,017
July 2005

Thanks for the animation code.
Seems useful.

I'm just still trying to figure out how to scroll. I've got a pen and paper, trying to work out how to get any method of scrolling to work.

____
There's no place like 127.0.0.1

Kikaru
Member #7,616
August 2006
avatar

My method for animation works like this: you declare an array of *BITMAPS, one for every frame, then a number in the object, like int frame; to say which array element to draw. It works really well with draw_sprite functions too! ;)

Ariesnl
Member #2,902
November 2002
avatar

Take one piece of paper... draw something on it

take a second piece of paper .. cut a rectangle hole in it..

put the second one over the 1st one .. ;);D

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

gnolam
Member #2,030
March 2002
avatar

Quote:

EDIT: No, hang on, the above can't be right. Would it be possible to explain just a little. It seems simple, but I can't figure it out completely. I just can't think today >_<

Think of it like this: instead of moving the camera, you're moving the world.

draw_sprite(playerspr, screenbuffer, x, y);
Draw playerspr on the buffer at x,y.

scroll_x = 10, scroll_y = 0;
draw_sprite(playerspr, screenbuffer, x - scroll_x, y - scroll_y);

Draw playerspr at 10 pixels left of the previous position. This is equivalent to the "camera" moving 10 pixels to the right.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Ariesnl
Member #2,902
November 2002
avatar

NO!

NEVER just move the world .. always seperate WORLD and SCREEN coords !! Otherwise you'll have a hell of a time implementing intelligence. Try implementing A* when recalculating everything according to a scroll value..

Work with simple World coords and let the draw function decide where to draw according to the camara's centerpoint like I showed you

also if you do something like this

if ((pPlayer!=NULL)&&(!pPlayer->destroyed))
{
    camx=pPlayer->x;
    camy=pPlayer->y;
}
else
{
 // Do some end of game scrolling

}

the camera will follow the player nicely ;)

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

gnolam
Member #2,030
March 2002
avatar

Quote:

NEVER just move the world .. always seperate WORLD and SCREEN coords !!

No shit Sherlock - that's exactly what the concept of a camera does.
Moving the world is, however, just what you're doing when you're transforming world space coordinates into screen space.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Ariesnl
Member #2,902
November 2002
avatar

you can't say shit here ? ;D

I guess you're right.. I was thinking about some OpenGL tut I once had that told me to move the world instead of the cam .. and I (inexperienced as I was thought it was smart... it wasn't)

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Simsimius
Member #6,017
July 2005

Trying to take into account what you both say, I tried almost all methods here.

The camera wasn't too easy, as I was unable to workout how to implement it in my game (and I found it hard for objects to keep to the cam co-ordinates).

The scroll_x and scroll_y seems to work quite well. I just keep the player fixed in the centre and force the scroll_y and scroll_x to implement depending on what direction the player moves. The only trouble here is the AI. But I suppose, as the code above states, using -scroll_x and -scroll_y for the X and Y for all code, I'm sure it can work, right?

Btw, does this thread get locked if I say the question has been answered?

____
There's no place like 127.0.0.1

Ariesnl
Member #2,902
November 2002
avatar

In this thread http://www.allegro.cc/forums/thread/587038
I attached some code .. look for the paperclip in my (second ?) post...

Download and view the code and the exe

just ignore the tilemap code if it's too complicated

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Simsimius
Member #6,017
July 2005

Thanks for the link.

A lot of code there, but it does seem useful. (Btw, opened the exe. Looks good).

____
There's no place like 127.0.0.1

Ariesnl
Member #2,902
November 2002
avatar

There's also A* in it you'll need that once you go coding complex AI in a map based game

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Simsimius
Member #6,017
July 2005

At the moment I intend to stay simple - at least to get an engine of some sort fully operational, complete with a sample level and graphics, etc.

I intend to create some AI that fire invisible rectangles, which are destroyed with a collision with a wall, and, when collide with a player, cause the AI to attack. Hopefully, it will work. Even if it is just for the sake of getting everything else working.

____
There's no place like 127.0.0.1

Evert
Member #794
November 2000
avatar

Quote:

Because in order to create a larger area I defined the size of the virtual screen.

It's fine to use this feature if available (though probably a waste of time, see below), but you should be aware that not all drivers are capable of doing this.
In practice, Mode-X can do it, which is only available in DOS (real DOS) and Linux and a bit obscure these days, most DOS VESA implementations claim they can do it and X11 can sortof fake it. Windows can't really do it at all.
So in practice you will hardly ever be in a position to use this feature, and it's better to just implement things differently.

Tobias Dammers
Member #2,604
August 2002
avatar

Let me take a shot at explaining the camera.

First of all, forget that there is such a thing as a screen. All your object coordinates are "world" coordinates, and can be whatever you want them to be - meters, feet, yards, miles, you name it. For a simple 2d game, you may choose pixels, but for clarity's sake, I'll use meters as an example. Implement all your game logic (object movement and interaction) using this "world" coordinate set. All positions, velocities, sizes, and whatnot, are stored as meters (or whichever unit you chose).

Now comes the drawing part (which, ideally, is separated from logic). To draw the scene, you need to transform "world" coordinates into "screen" coordinates, and the object that achieves this is a camera or camera transform. The camera must do two things: Scale, and translate. Let's assume you specify the camera position in world coordinates; then you'd start with translating. This is easy: Just subtract the camera position from each object's position:

x_trans = x - cam->x;
y_trans = y - cam->y;

Now for the scaling. You need to define a scaling factor for the world units you use, in other words, you need to know how many pixels one unit has. Let's assume 32 pixels per meter:

cam->scalefac = 32;

x_screen = (x - cam->x) * cam->scalefac;
y_screen = (y - cam->y) * cam->scalefac;

And finally, you want the camera location displayed at screen center, not top-left, so we'll add another translation to the mix:

cam->scalefac = 32;

x_screen = (x - cam->x) * cam->scalefac + SCREEN_W / 2;
y_screen = (y - cam->y) * cam->scalefac + SCREEN_H / 2;

There you go. If you happen to choose pixels for your units, the scale factor is 1, so you can then leave out the multiplication step.

I leave it as an exercise for you to reverse the translation (read: find the world coordinates for a given screen position).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

 1   2 


Go to: