Dreaded Scrolling

Started by
8 comments, last by GeneralK99 21 years, 3 months ago
Hello, I''m new to this website and forums and I''m a beginner. Recently I''ve started working on Sams Teach it Yourself Game Programming for 24 hours and slowly I''m learning Game Programming using Win API. I realized however that the book doesn''t teach you how to work Game Scrolling around. This is my problem cause if I wanted to start making 2d scroll games or RTS I''m gonna need to learn this. If anyone has good source code or good sources in general to help me learn scrolling I would be greatly apperciated. The downside is that I need to learn it in Win API since I know nothing about Direct X yet and plan on learning it later. Any help would be greatly apperciated. P.S. I apologize if this has been asked a million times.
Advertisement
What's so difficult about scrolling? All you have to do is figure out which part of the game world you want to draw.

object.screen_x = object.global_x - (centerpoint.global_x - centerpoint.screen_x)

If the player is always in the center of the screen, then centerpoint_xxx is the player's coordinates. But in a RTS game you might want to use an imaginary centerpoint..

[edited by - viajero on January 21, 2003 10:42:16 AM]
Yeah, that''s pretty much it. In a drawing program I''m writing now, you can scroll around a picture. I call it a "camera" but its essentially the same idea as what Viajero posted.
Peon
Hey thanks for the reply. I''m sorry but I don''t understand the code as I said I''m beginner and still working on my book. If its not to much to ask can you go over the code and explain it, for instance what are the variables representing. I think an example will be helpful. I''m sorry if i''m being annoying. Again thanks for help anything is good.
For a RTS, if you use 2D graphics, you probably have tiles representing the ground. Now you figure what tiles you need to draw.
Let''s call the current coordinates of the top-left corner of the "viewing window" on the map screen_x and screen_y (this has nothing to do with the code above).
The screen has the width of screen_wid and screen_hei.
Each tile has a global_x and global_y position on the map, counting from the top left corner on the map.
Now you draw every tile whose global_x is between screen_x (left border) and screen_x + screen_wid (right border of the screen); AND whose global_y is between screen_y (upper border) and screen_y + screen_hei (lower border).

Note: You can of course also count from the bottom left corner. It''s up to you. However, drawing to the screen counts from the top left corner in most (if not all) APIs. Don''t know about Win32 in specific.

Hope that made it clearer.

"George W. Bush Geography Simplification Initiative"

More info on George W. Bush
My Homepage (C++ SDL OpenGL Game Programming)
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Yes very helpful thank you very much. It''ll help me greatly for possible RTS games. As for the scrolling part, randomZ, how would I go about doing that. I know you have to get the values of mouse position and if its at the border scroll it. But the actual scrolling is what gets me.
Imagine a camera that shows you the screen. It has global coordinates just as all tiles, units and whatnot. You just compare the coordinates of the camera and the objects you want to draw. You have to convert the global coordinates to local screen coordinates. When you move the mouse to the left side of the screen, you just change the coordinates of the camera, and a different part of the game world will be drawn.
I answered a similiar post earlier, and I even posted has a "picture" : http://www.gamedev.net/community/forums/topic.asp?topic_id=134330

If you still can''t figure it out, take out a notebook (with "grid texture") and draw the game world, the screen, the camera and an object and think how you could find out the coordinates of the object, you''ll figure it out
So what your saying is, That I load all my tiles to the global map. Then I make a camera that draws viewable tiles to the screenmap. I give the camera coordinates and when I need to scroll I simply move the camera and it''ll draw all the things automatically. Intresting, i''m starting to understand it all slowly. Thanks so much.
Okay, here's a more in-depth explanation of how I did it in my program:

- I store all my pixels in an array. If you're doing some sort of tile based 2D scroller (probable, I think), you'd likely do something similar.

- I create a RECT structure called camera (if unfamiliar, a RECT defines the bounds of a rectangle ie: the four "corner points"). The bounds of the camera are equal to the amount of tiles I have on the screen. For example, if my level is 100 tiles wide, and I want to display 20 tiles at once, I would set camera.left to 0, and camera.right to 20 (this is for horizontal scrolling of course; vertical scrolling would use .bottom and .top) .

- My drawing routine take the bounds the camera, and I loop through a for-loop, starting at camera.left and ending at camera.right, for a total of 20 tiles (the size of my screen) I then look for those points in my array and plot them accordingly. To clarify, if my camera.left is 5, I will start in my array at[some value][5] and continue until [some value][25].

- To actually scroll, simply increase the .left and .right elements of the RECT camera structure. OBviously you will need some checking to make sure that the scrolling (the value of camera.right or camera.left) doesn't exceed the array, or you'll end up crashing the program.

Anyway, all this is assuming you're using a tile based system, holding the location of the tiles in arrays (IMO, the easiest way to do it) In any case, the principle is still the same I think. Lemme know if anything is unclear and I'll try and explain some more :D

IT's kind of funny actually. For a long time, the concept of scolling (among other things) baffled me I didn't understand how it was done. Afterall, I started programming (nothing serious, but just beginning to try it) in 5-6th grade or so in GW-BASIC. Granted, I didn't understand hardly anything, barely having touched algebraic concepts or any programming theory, but I was still determined to program an RTS :D (I ended up making simple 'Madlib' type games...) I have yet to do it, but scrolling used to keep me back. Then one day not long ago (I'm 17; grade 12 now) I just sat down and wrote some scrolling code that worked in less than half an hour. It suddenly all made sense to be. But anyway, yea, it was kinda like a magic moment... hehe.

Peon

[edited by - Peon on January 22, 2003 9:13:40 PM]
Peon
Heh thanks Peon that was very helpful. I just started programming now, i''m 15 taking C++ in school and working on the book i just got. Me and my friend always had a dream of Making our own games and we want to get a scholarship to DigiPen college. The book teaches alot even though i''m not to fond of classes and fucntions yet(next chapter in school is functions but the class is going really slow), but i''m understanding slowly at a time. Me and my friend have a good idea on a game summer project we wanna start once summer hits. It''ll be 2d so i''m trying to get as much info on scrolling as possible. My friend is good in art and maybe i''ll tell him to check out this sites art sections.

The array idea sounds really good though. I see if I can learn more than I already know about arrays from some sites I vist at times.

Good luck on your RTS Peon.

This topic is closed to new replies.

Advertisement