//Map setup
#define MAPWIDTH 16
#define MAPHEIGHT 24
int MAPDATA[MAPWIDTH*MAPHEIGHT] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,
70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,
92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,
110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,
158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,
174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,
190,191,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,
63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,
104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,
120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,
136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,
152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,
168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191
};
void DrawTiles()
{
int tilex, tiley;
int columns, rows;
int x, y;
int tilenum;
//calculate starting tile position
tilex = ScrollX / TILEWIDTH;
tiley = ScrollY / TILEHEIGHT;
//calculate the number of columns and rows
columns = WINDOWWIDTH / TILEWIDTH;
rows = WINDOWHEIGHT / TILEHEIGHT;
//draw tiles onto the scroll buffer surface
for (y=0; y<=rows; y++)
{
for (x=0; x<=columns; x++)
{
//retrieve the tile number from this position
tilenum = MAPDATA[((tiley + y) * MAPWIDTH + (tilex + x))];
//draw the tile onto the scroll buffer
DrawTile(tiles,tilenum,TILEWIDTH,TILEHEIGHT,16,scrollbuffer,
x*TILEWIDTH,y*TILEHEIGHT);
}
}
}
//This function updates the scrolling position and speed
void UpdateScrollPosition()
{
SpeedY = -7;
//update vertical scrolling position and speed
ScrollY += SpeedY;
if (ScrollY < 0)
{
ScrollY = 0;
SpeedY = 0;
}
else if (ScrollY > GAMEWORLDHEIGHT + WINDOWHEIGHT)
{
ScrollY = GAMEWORLDHEIGHT + WINDOWHEIGHT;
SpeedY = 0;
}
}
Vertical Scrolling Problem( Video Game)
Hey guys its me again, geez I always have issues huh? Well I guess thats just part of the learning process, umm ok this time this has to do with my scrolling background. This is what I am using for my scrolling background, mind you it scrolls only on the y axis this is supposed to be a top down shooter like in an airplane type thing.
This is the pertanent functions that need to be modified because as of now it will only scroll down and only if SpeedY = 7 instead of the -7 which would make it scroll up which is what I want.
Anyways my question is what can I do to make this scrollup what do I need to change.
Thanks!
[Edited by - xM1k3x on July 2, 2008 5:48:36 PM]
Also can anyone tell me why it keeps scrolling even when there is no more background and its just black??
Thanks
Thanks
get rid of the line that makes the scroll speed 0 if the position is less than 0. And I don't think that the final if statement should have an else before it
If your going to have this many problems making a scolling game, you should aim for something easier maybe lol. Once you start having a lot of code, you can't post it and ask people to help, you need to be able to know what everything in your code does and where it is. I see you commented it which is good, but looking through that would take me a little time to understand. For your second post, that should be easy, just think about it.... Once the scroll is too far in a direction, don't update towards that direction anymore I would guess.
Quote:Original post by I Am Legend
get rid of the line that makes the scroll speed 0 if the position is less than 0. And I don't think that the final if statement should have an else before it
Ok well now it doesnt scroll and the background now flashes in place.
Also something I forgot to mention earlier was how I called all these functions:
#include "game.h"LPDIRECT3DTEXTURE9 player_ship;LPD3DXSPRITE sprite_handler;PlayerShip Player;int ScrollX, ScrollY; //current scroll positionint SpeedX, SpeedY; //scroll speedLPDIRECT3DSURFACE9 scrollbuffer; //scroll bufferLPDIRECT3DSURFACE9 tiles; //source image containing tiles //timing variablelong start = GetTickCount();HRESULT result;int MAPDATA[MAPWIDTH*MAPHEIGHT] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191};//initializes the gameint Game_Init(HWND hwnd){ //set random number seed srand(time(NULL)); if (!Init_DirectInput(hwnd)) { MessageBox(hwnd, "Error initializing the Direct Input","Error",MB_OK); } if(!Init_Mouse(hwnd)) { MessageBox(hwnd, "Error initializing the mouse","Error",MB_OK); } if(!Init_Keyboard(hwnd)) { MessageBox(hwnd, "Error initializing the keyboard","Error",MB_OK); } //create sprite handler object result = D3DXCreateSprite(d3ddev, &sprite_handler); if (result != D3D_OK) return 0; //load the background image tiles = LoadSurface("CloudBackground.BMP", D3DCOLOR_XRGB(0,0,0)); if (tiles == NULL) return 0; //load the Player's Ship player_ship = LoadTexture("PLAYERSHIP.bmp",D3DCOLOR_XRGB(255,0,255)); if (player_ship == NULL) return 0; //Initializing the Player's Ships components Player.x = 270; Player.y = 300; Player.width = 96; Player.height = 96; Player.movex = 7; Player.movey = 7; //create the scroll buffer surface in memory, slightly bigger //than the screen result = d3ddev->CreateOffscreenPlainSurface( SCROLLBUFFERWIDTH, SCROLLBUFFERHEIGHT, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &scrollbuffer, NULL); start = GetTickCount(); //return okay return 1;}//the main game loopvoid Game_Run(HWND hwnd){ //make sure the Direct3D device is valid if (d3ddev == NULL) return; Poll_Mouse(); Poll_Keyboard(); //after short delay, ready for next frame? //this keeps the game running at a steady frame rate if (GetTickCount() - start >= 30) { //reset timing start = GetTickCount(); //move the sprite Player.CallMovement(); //update the scrolling view UpdateScrollPosition(); //start rendering if (d3ddev->BeginScene()) { //erase the entire background d3ddev->StretchRect(tiles, NULL, backbuffer, NULL, D3DTEXF_NONE); //start sprite handler sprite_handler->Begin(D3DXSPRITE_ALPHABLEND); //create vector to update sprite position D3DXVECTOR3 position((float)Player.x, (float)Player.y, 0); //draw the sprite sprite_handler->Draw( player_ship, NULL, NULL, &position, D3DCOLOR_XRGB(255,255,255)); //draw tiles onto the scroll buffer DrawTiles(); //draw the scroll window onto the back buffer DrawScrollWindow(); //stop rendering d3ddev->EndScene(); } } //display the back buffer on the screen d3ddev->Present(NULL, NULL, NULL, NULL); //check for escape key (to exit program) if (Key_Down(DIK_ESCAPE)) DestroyWindow(hwnd);}//This function updates the scrolling position and speedvoid UpdateScrollPosition(){ SpeedY = -4; //update horizontal scrolling position and speed ScrollX += SpeedX; if (ScrollX < 0) { ScrollX = 0; SpeedX = 0; } else if (ScrollX > GAMEWORLDWIDTH - WINDOWWIDTH) { ScrollX = GAMEWORLDWIDTH - WINDOWWIDTH; SpeedX = 0; } //update vertical scrolling position and speed ScrollY += SpeedY; if (ScrollY > GAMEWORLDHEIGHT - WINDOWHEIGHT) { ScrollY = GAMEWORLDHEIGHT - WINDOWHEIGHT; SpeedY = 0; }}//This function does the real work of drawing a single tile from the //source image onto the tile scroll buffer. Parameters provide much //flexibility.void DrawTile(LPDIRECT3DSURFACE9 source, // source surface image int tilenum, // tile # int width, // tile width int height, // tile height int columns, // columns of tiles LPDIRECT3DSURFACE9 dest, // destination surface int destx, // destination x int desty) // destination y{ //create a RECT to describe the source image RECT r1; r1.left = (tilenum % columns) * width; r1.top = (tilenum / columns) * height; r1.right = r1.left + width; r1.bottom = r1.top + height; //set destination rect RECT r2 = {destx,desty,destx+width,desty+height}; //draw the tile d3ddev->StretchRect(source, &r1, dest, &r2, D3DTEXF_NONE);}//This function fills the tilebuffer with tiles representing//the current scroll display based on scrollx/scrolly.void DrawTiles(){ int tilex, tiley; int columns, rows; int x, y; int tilenum; //calculate starting tile position tilex = ScrollX / TILEWIDTH; tiley = ScrollY / TILEHEIGHT; //calculate the number of columns and rows columns = WINDOWWIDTH / TILEWIDTH; rows = WINDOWHEIGHT / TILEHEIGHT; //draw tiles onto the scroll buffer surface for (y=0; y<=rows; y++) { for (x=0; x<=columns; x++) { //retrieve the tile number from this position tilenum = MAPDATA[((tiley + y) * MAPWIDTH + (tilex + x))]; //draw the tile onto the scroll buffer DrawTile(tiles,tilenum,TILEWIDTH,TILEHEIGHT,16,scrollbuffer, x*TILEWIDTH,y*TILEHEIGHT); } }}//This function draws the portion of the scroll buffer onto the back buffer//according to the current "partial tile" scroll position.void DrawScrollWindow(){ //calculate the partial sub-tile lines to draw using modulus int partialx = ScrollX % TILEWIDTH; int partialy = ScrollY % TILEHEIGHT; //set dimensions of the source image as a rectangle RECT r1 = {partialx,partialy,partialx+WINDOWWIDTH,partialy+WINDOWHEIGHT}; //set the destination rectangle //This line draws the virtual scroll buffer to the screen exactly as is, //without scaling the image to fit the screen. If your screen does not //divide evenly with the tiles, then you may want to scale the scroll //buffer to fill the entire screen. It's better to use a resolution that //divides evenly with your tile size. //use this line for scaled display //RECT r2 = {0, 0, WINDOWWIDTH-1, WINDOWHEIGHT-1}; //use this line for non-scaled display RECT r2 = {0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1}; //draw the "partial tile" scroll window onto the back buffer d3ddev->StretchRect(scrollbuffer, &r1, backbuffer, &r2, D3DTEXF_NONE);}//frees memory and cleans up before the game endsvoid Game_End(HWND hwnd){ if (player_ship != NULL) player_ship->Release(); if (tiles != NULL) tiles->Release(); if (sprite_handler != NULL) sprite_handler->Release();}
Something that is strange I first built the program without the scrolling and I had my background and a sprite that represented the players ship and ever since I implemented the scrolling my ship wont show up.... any ideas as too why that is?
Thank you for your patience
[Edited by - xM1k3x on July 2, 2008 5:19:01 PM]
Please put the code inside [sou.rce] ... [/sou.rce] brackets (without the periods). And use more descriptive titles. It seems like a programmers job isn't to ask people to find problems with their code, but to write the code and fix it as they go. As you make the game, add one thing at a time so everything works right. Now that your already going though, you'll have a little trouble fixing it.
Quote:Original post by Tenac
Please put the code inside [sou.rce] ... [/sou.rce] brackets (without the periods). And use more descriptive titles. It seems like a programmers job isn't to ask people to find problems with their code, but to write the code and fix it as they go. As you make the game, add one thing at a time so everything works right. Now that your already going though, you'll have a little trouble fixing it.
The problem is I am learning and I dont know how to fix my program. So I am trying to learn by asking questions and getting help.
I was adding things one at a time I had the game at the point where my background loaded and my ship was loaded but I need the background to scroll. So I began to implement that however it is not working the way I wish it would hence the point of this post. Whether or not it is the job of a programmer to ask people to find problems with there code, I have a problem so i'm asking for help.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement