Sign in to follow this  
Jossos

SDL_Surface help -- Drawing a minimap

Recommended Posts

Jossos    343

So I want to draw a minimap for my app pixel-by-pixel, but I have no idea how, and google has yielded no results (mainly stuff loading already existing images - I already know how to do)

 

I'll place my minimap Class for you to peruse and I hope someone can help me. It's the syntax I don't know about. (there's a bit of opengl in here too, but it's mostly an sdl question).

class smMINIMAP
{
private:
	float xPos, yPos, width, height;
	smMAP* mapRef;
	SDL_Surface *map_img;

	void PutPixel(int x, int y, int color);
public:
	smMINIMAP(smMAP& map);
	~smMINIMAP();

	void Draw(int x, int y, int w = 200, int h = 200);
};
smMINIMAP::smMINIMAP(smMAP& map)
{
	*mapRef = map;

	map_img = SDL_CreateRGBSurface(0, mapRef->getWidth(), mapRef->getLength(), 32, 0, 0, 0, 0);
}

smMINIMAP::~smMINIMAP()
{
	delete mapRef;
}

void smMINIMAP::Draw(int x, int y, int w, int h)
{
	Uint32* pixels = (Uint32*)map_img->pixels;

	for (int w = 0; w < mapRef->getWidth(); w++)
	{
		for (int l = 0; l < mapRef->getLength(); l++)
		{
			PutPixel(w, l, 0xff0000);
		}
	}

	glEnable(GL_TEXTURE_2D);

	GLuint ret;

	glGenTextures(1, &ret);
	glBindTexture(GL_TEXTURE_2D, ret);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, map_img->w, map_img->h, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, map_img->pixels);

	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex2f(x,     y);
		glTexCoord2f(1, 0); glVertex2f(x + w, y);
		glTexCoord2f(1, 1); glVertex2f(x + w, y + h);
		glTexCoord2f(0, 1); glVertex2f(x,     y + h);
	glEnd();
}

void smMINIMAP::PutPixel(int x, int y, int color)
{
	unsigned int* ptr = (unsigned int*)map_img->pixels;
	int lineoffset = y * (map_img->pitch / 4);
	ptr[lineoffset + x] = color;
}

 

 

smMAP is just a 2d array of tiles. It's irrelevant right now, as I just want to make each pixel red to get something on the screen - then I can colorize it.

 

Help is greatly appreciated!

Edited by Jossos

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this