Advertisement

2d background scrolling

Started by January 31, 2004 05:35 PM
0 comments, last by mindemc 21 years, 1 month ago
Trying to create a 2D side-scrolling shooter. Can''t seem to get my tiles scrolling. I''m using SDL and Opengl. My problem is I try to use glTranslatef(scroll/move_delay+0.0f,0.0f,0.0f) and the background(map) will not scroll off screen to the left. It scrolls individual tiles within the tile if i stick the scroll/move_delay inside the glTexCoord2f(scroll/move_delay+0.0f,0.0f,0.0f); Here is the code I''m using: only the parts pertaining to the map and scrolling. GLfloat scroll=0; GLfloat move_delay=500; // int td=0; // Used when checking the time difference int drawBackground(GLvoid) { int tile; for (int y = 0; y < MAP_SIZEY; y++) { for (int x = 0; x < MAP_SIZEX; x++) { scroll += td; tile = map[y][x]; //scroll++; glTranslatef(scroll/move_delay-0.0f,0.0f,0.0f); glBindTexture(GL_TEXTURE_2D, texture[0]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(y), 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1), float(y), 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1), float(y + 1), 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x), float(y + 1), 0.0f); glEnd(); } } return (1); } /*Below is how my screen view is set up*/ int resizeWindow( int width, int height ) { /* Height / width ration */ GLfloat ratio; /* Protect against a divide by zero */ if ( height == 0 ) height = 1; ratio = ( GLfloat )width / ( GLfloat )height; /* Setup our viewport. */ glViewport( 0, 0, ( GLint )width, ( GLint )height ); /* * change to the projection matrix and set * our viewing volume. */ glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); /* Set our perspective */ //gluPerspective( 45.0f, ratio, 0.1f, 100.0f ); // Create Ortho 640x480 View (0,0 At Top Left) glOrtho(0.0f,MAP_SIZEX,MAP_SIZEY,0.0f,-1.0f,1.0f); /* Make sure we''re chaning the model view and not the projection */ glMatrixMode( GL_MODELVIEW ); /* Reset The View */ glLoadIdentity( ); return( TRUE ); } /* Here is my drawing code */ int drawGLScene( GLvoid ) { /* These are to calculate our fps */ static GLint T0 = 0; static GLint Frames = 0; /* Clear The Screen And The Depth Buffer */ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity( ); drawBackground(); //draw_tiles(); /* Draw it to the screen */ SDL_GL_SwapBuffers( ); /* Gather our frames per second */ Frames++; { GLint t = SDL_GetTicks(); if (t - T0 >= 5000) { GLfloat seconds = (t - T0) / 1000.0; GLfloat fps = Frames / seconds; printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps); T0 = t; Frames = 0; } } return( TRUE ); } //my main int main( int argc, char **argv ) {...... td=SDL_GetTicks(); ....... } I''m using tile "0" insted of "tile" because it tiles one image on all tiles of the map.Remeber it''s not complete code the tile map shows up if i use glTexCoor2f(); or take the scroll/move_delay out of the glTranslatef(); Sorry for the long post with code haven''t figured how to make the code box yet. Thanks for your help.
Ok, found out what was wrong.
For some reason i had to add scroll/move_delay after the 0.0f
in glTranslate.
Don''t know why but it works now.
Thanks Anyway

This topic is closed to new replies.

Advertisement