#### Archived

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

# 2D Scroller Problem

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

## Recommended Posts

I''m trying to write a 2D scroller-type game, and I''m hoping someone here could help me out with a problem that I can''t seem to solve that would help me out immensely. In the code I''m posting below, the program basically loops through an array (called "Map") and returns the value of the current entry in the array. This number is then used to bind a texture to a quad. Now, my problem is this: I can''t seem to figure out how to draw a row of quads. Right now, it just draws one long quad. I''ve attempted to try some different things (such as incrementing each vertex''s X value by 1), but I can''t seem to make it draw 5 (the size of the array) seperate quads in a row. I know the solution is simple, but I have no clue how to do this. ANy help would be appreciated!

for (int i = 0; i < 5; i++)
{

tex = Map[i];

glBindTexture(GL_TEXTURE_2D, texture[1]);

glTexCoord2f(1.0f,0.0f); glVertex3f(pos1+i,0.2f,0.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f(pos2+i,-0.2f,0.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(pos3+i,-0.2f,0.0f);
glTexCoord2f(0.0f,0.0f); glVertex3f(pos4+i,0.2f,0.0f);
glEnd();

}


##### Share on other sites
hmmm i''m only a newbie but this is what i would do.....

try putting in a glTranslatef(i,0,-30) in your loop.

then just keep the vertex commands something like

glVertex3f(0.0,1.0,0.0); and so on

this is something i have recently discovered and i think it si the easiest way to go.

example from my tiling program

  	for (int x=0;x < 28;x++)		{		for (int y=0;y<25;y++)			{			glLoadIdentity();			glTranslatef(-16.0f+x,12.0f-y,-30.0f);						glBindTexture(GL_TEXTURE_2D, texture[(mapdata[x][y])].texID);			glBegin(GL_QUADS);				glTexCoord2d(0.0,1.0);				glVertex3f(-0.5f, 0.5f, 0.0f);				glTexCoord2d(1.0,1.0);				glVertex3f(0.5f, 0.5f, 0.0f);				glTexCoord2d(1.0,0.0);				glVertex3f(0.5f, -0.5f, 0.0f);				glTexCoord2d(0.0,0.0);				glVertex3f(-0.5f, -0.5f, 0.0f);			glEnd();			}		}

this may not be exactly what you want but it hopefully will give u some ideas.....

Werdy666

My Homepage : http://werdy666.20megsfree.com/

##### Share on other sites
Assuming you are drawing rectangles, are:

pos1 = pos2,
pos3 = pos4,

and pos1, pos2 = pos3 + 1, pos4 + 1 ?

Using variables like these should draw 5 contiguous rectangles using your code.

##### Share on other sites
I am not sure if this is what you are looking for, but you might want to try my function, that draws a test floor, with different colors. My z and y axes are inverted, so you might want to invert them, if your y points up, and z points inside the monitor.

This draws a 160x160 floor, so you might want to change the values in the fors.

draw_some_floor(){	int x,y;	int y_scaled,x_scaled;	int terrain_scale;	terrain_scale=1;	glDisable(GL_TEXTURE_2D);	glBegin(GL_QUADS);	//glColor3f(1.0f,1.0f,1.0f);	for(y=-80;y<80;y++)		{			y_scaled=y*terrain_scale;			for(x=-80;x<80;x++)			 {				 if(y%2==0)				 	{				 		if(x%3==0)glColor3f(1.0f,1.0f,1.0f);				 		else				 		if(x%3==1)glColor3f(1.0f,0.0f,0.0f);				 		else				 		if(x%3==2)glColor3f(0.0f,0.0f,1.0f);					}				 else				 	{				 		if(x%3==0)glColor3f(0.0f,0.0f,0.0f);				 		else				 		if(x%3==1)glColor3f(0.0f,1.0f,1.0f);				 		else				 		if(x%3==2)glColor3f(0.0f,1.0f,0.0f);					}				x_scaled=x*terrain_scale;				glVertex3f((float)x_scaled,(float)y_scaled+terrain_scale, 0.0f);				glVertex3f((float)x_scaled,(float)y_scaled, 0.0f);				glVertex3f((float)x_scaled+terrain_scale, (float)y_scaled,0.0f);				glVertex3f((float)x_scaled+terrain_scale, (float)y_scaled+terrain_scale,0.0f);			 }		}	glEnd();}

Which is worse? Ignorance or apathy? Who knows?! Who cares?!

[edited by - Raduprv on October 23, 2002 5:37:40 PM]

##### Share on other sites
If you are seeing one big quad then you are "zoomed in" too much.

Inside the loop, before you draw the poly, use glTranslatef to go deeper into the screen.

##### Share on other sites
Thanks alot guys! Shortly after I posted this, I found the solution here at Gamedev, in the excellent article "Tiling in OpenGL" by Martin Estevao.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×