All pastes #336102 Raw Edit

Stuff

public text v1 · immutable
·published 2007-02-01 20:34 UTC
rendered paste body
in main:
	Video.SpriteSheetName["Floors"] = Video.LoadSpriteSheet("../images/floors.png", 32, 32);
	Video.SpriteSheetName["Walls"] = Video.LoadSpriteSheet("../images/walls.png", 32, 32);

			// Iterate over the visible part of the map and display it.
			for(temp_x = (Player1.X - screen_width / 2) - 1; temp_x <= (Player1.X + screen_width / 2); temp_x++)
			{
				for(temp_y = (Player1.Y - screen_height / 2) - 1; temp_y <= (Player1.Y + screen_height / 2); temp_y++)
				{
					RealX = temp_x % iMapWidth;
					while(RealX < 0) RealX += iMapWidth;
					RealY = temp_y % iMapHeight;
					while(RealY < 0) RealY += iMapHeight;

					tile_x = map_x + (temp_x - (Player1.X - screen_width / 2)) * 32;
					tile_y = map_y + (temp_y - (Player1.Y - screen_height / 2)) * 32;
	
					if(Map.iMapLight[iLevel][RealX][RealY] > 2) glColor3f(1.0f, 1.0f, 1.0f);
					if(Map.iMapLight[iLevel][RealX][RealY] == 2) glColor3f(0.6f, 0.6f, 0.7f);
					if(Map.iMapLight[iLevel][RealX][RealY] == 1) glColor3f(0.4f, 0.4f, 0.5f);
					if(Map.iMapLight[iLevel][RealX][RealY] == 0) glColor3f(0.0f, 0.0f, 0.0f);
					if(Map.iMapLight[iLevel][RealX][RealY] == -1) glColor3f(0.2f, 0.2f, 0.3f);
	
					if(Map.iMap[iLevel][RealX][RealY] == iTileInpassable) { Video.DrawSpriteFromSheet(Video.SpriteSheetName["Walls"], 1 + ((int) trunc(iLevel/4)), tile_x, tile_y); }
					if(Map.iMap[iLevel][RealX][RealY] == iTilePassable) { Video.DrawSpriteFromSheet(Video.SpriteSheetName["Floors"], 6 + ((int) trunc(iLevel/4)), tile_x, tile_y); }
					if(Map.iMap[iLevel][RealX][RealY] == iTileExit) { Video.DrawSpriteFromSheet(Video.SpriteSheetName["Floors"], 6 + ((int) trunc(iLevel/4)), tile_x, tile_y); }
					glColor3f(1,1,1);
				}
			}

Other files:
void cVideoEngine::DrawSpriteFromSheet(int SheetNumber, int SpriteNumber, int X, int Y)
{
	// Reset the view, then move to x,y.
	glLoadIdentity();
	glTranslatef(X, Y, 0);

	// Do the actual drawing
	glCallList(this->SpriteSheetList.at(SheetNumber) + SpriteNumber - 1);
}

int cVideoEngine::LoadSpriteSheet(const char* Filename, int SpriteWidth, int SpriteHeight)
{
	// Insert a placeholder for the texture and store it's position.
	this->SpriteSheetTexture.push_back(false);
	int CurrentTexture = this->SpriteSheetTexture.size() - 1;

	// Generate texture-meta-data for the texture.
	glGenTextures(1, &this->SpriteSheetTexture.at(CurrentTexture));

	// Create a temporary surface and load the image to it.
	SDL_Surface* SpriteSheetSurface = IMG_Load(Filename);

	// Store the size of the sprites and the spritesheet;
	int SheetWidth = SpriteSheetSurface->w;
	int SheetHeight = SpriteSheetSurface->h;
	int SpritesPerRow = SheetWidth / SpriteWidth;
	int Sprites = SpritesPerRow * (SheetHeight / SpriteHeight);

	// Bind with our texture, then transfer the pixel-data from the surface to the texture and
    // set some wrap and scaling attributes.
	glBindTexture(GL_TEXTURE_2D, this->SpriteSheetTexture.at(CurrentTexture));
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SpriteSheetSurface->w, SpriteSheetSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, SpriteSheetSurface->pixels);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	// Create a List to hold the precompiled Sprites.
	this->SpriteSheetList.push_back(glGenLists(Sprites));
	int CurrentList = this->SpriteSheetList.size() - 1;

	float TextureTop, TextureBottom, TextureLeft, TextureRight;
	int SpriteRow, SpriteCol;

	// For each sprite in the spritesheet..
	for(int CurrentSprite = 0; CurrentSprite < Sprites; CurrentSprite++)
	{
		// Create list-item for the current sprite.
		glNewList(this->SpriteSheetList.at(CurrentList) + CurrentSprite, GL_COMPILE);
	
			// Calculate what row and col the sprite is on
			SpriteRow = (int) ((CurrentSprite) / SpritesPerRow);
			SpriteCol = (int) ((CurrentSprite) - (SpritesPerRow * SpriteRow));

			// Calculate the normalized values for the sprites texture position
			TextureLeft = ((float) SpriteCol * 32) / ((float) SheetWidth);
			TextureRight = ((float) SpriteCol * 32 + 32) / ((float) SheetWidth);
			TextureTop = ((float) SpriteRow * 32) / ((float) SheetHeight);
			TextureBottom = ((float) SpriteRow * 32 + 32) / ((float) SheetHeight);

			// Bind to the current texture
			glBindTexture(GL_TEXTURE_2D, this->SpriteSheetTexture.at(CurrentTexture));

			// Draw a rectangle (Quad = four points)
			// Counter-clockwise to have the polygon face the camera
			// Clockwise to have the polygon face away from the camera
			glBegin(GL_QUADS);

				// Top Left
				glTexCoord2f(TextureLeft, TextureTop);
				glVertex2i(0, 0);

				// Bottom Left
				glTexCoord2f(TextureLeft, TextureBottom);
				glVertex2i(0, SpriteHeight);

				// Bottom Right
				glTexCoord2f(TextureRight, TextureBottom);
				glVertex2i(SpriteWidth, SpriteHeight);

				// Top Right
				glTexCoord2f(TextureRight, TextureTop);
				glVertex2i(SpriteWidth, 0);

			// Finish the GL command
			glEnd();
	
		// Finish the Compiled List-item.
		glEndList();
	}

	// Free the surface
	SDL_FreeSurface(SpriteSheetSurface);

	return CurrentList;
}