How to create minimap? (sdl/opengl)

Started by
18 comments, last by Jossos 11 years, 3 months ago

Hey there. I was wondering how I would go about creating a minimap for this application. It needs to be dynamic, in that the screen could be any size, and the map could be any size (probably no more than 256x256). binding this to an opengl quad would solve any resizing issues, but i dont know if that's possible.

Help is appreciated!

scrshotty.jpg

Advertisement

I have really no idea, but I would think that you translate each tile into a pixel on the "minimap".

I have really no idea, but I would think that you translate each tile into a pixel on the "minimap".

That's the idea. Just no idea how

I have really no idea, but I would think that you translate each tile into a pixel on the "minimap".

That's the idea. Just no idea how

Well maybe you can figure it out if you read some SDL about pixel manipulation. And let's hope someone qualified shows up in here :P good luck

I haven't tried myself and maybe this is to simple for your application.

But if your world is made up of tiles, cant you paint a small pixel (4x4px) on the map in that color of the tile.
Eg. the grass tile would be a green 4x4 pixel on your map and the ground would be light sandish 4x4px?

Since you know how to generate the tile world, you should know all the coordinates for each tile in the world and translating this position into map position should be easy..

*Update* Sorry that was basically the same advice as Nausea :/

Are your tiles in an array? Or more preferably a 2D array?

[ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ]

Just using your tile data structure should be plenty for creating a minimap.

You could create a texture of width * height size and store a different colour for each type of tile, then just scale it up. (Just an idea).

You need to project the map data into a smaller area

You have 2 center vertices - lets call them centerM (Map) and centerMM (MiniMap).

Furthermore you need to define a scaling factor~

mmScaling = 1/3;

Lets extract a vertex from a tile/rectangle - assuming a tile's structure looks like this (X, Y, Width, Height) we take v = (X,Y) for example.

Now you need to do some 2D vector-maths.


// retrieve the relative position to centerM first
v = centerM - v;
// then scale down
v.setSize(v.getSize() * mmScaling);
// finally calculate the position on the minimap
v = centerMM + v;

Thats it..

Ok maybe I should make it more clear my problem.

I know the theory of how to do it. I have a pointer to a bunch of pointers that makes up my map. it acts like a 2d array, so it's all good.

It's the syntax I can't figure out.

Then show some code pls. I cant foresee what you've already implemented and what you are lacking or what the problem precisely is.. =/

Then show some code pls. I cant foresee what you've already implemented and what you are lacking or what the problem precisely is.. =/

I'm not sure if u really need code to tell me how to draw pixels, but I'll provide some relevant code as you wish.



struct TILE
{
	int TileType;
	bool obstructed;
};

class map
{
public:
	map(int h, int w);
	~map();

	int getTileTexture(int x, int y);
private:
	int m_height, m_width;
	TILE **m_map;
};

My tile struct and map class.


class Game : public GameState
{
public:
	Game(int w, int h);
	~Game();

	void HandleEvents();
	void logic();
	void render();
	
	void loadTiles();

	void scroll();
	void scrollStuff();
	void checkScreen();

	void drawHUD();
	void drawTiles();
private:
	map *myMap;
	int m_width;
	int m_height;

	float scrPosX;
	float scrPosY;

	float scrTilesW;
	float scrTilesH;

	unsigned int hud;
	unsigned int *tileTextures;

	bool scrMoveDown;
	bool scrMoveUp;
	bool scrMoveLeft;
	bool scrMoveRight;

	bool scrBMoveDown;
	bool scrBMoveUp;
	bool scrBMoveLeft;
	bool scrBMoveRight;
};

My game class (all you really need to care about is the "map *myMap;")


void Game::render()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glOrtho(0, Screen_Width, Screen_Height, 0, -1, 1);

	drawTiles();

	drawHUD();
	drawCursor();
}

The render function


void Game::drawTiles()
{
	// Set i to the very left tile (in regards to the screen)
	for (int i = (scrPosX - (scrTilesW / 2)); i < scrPosX + (scrTilesW / 2); i++)
	{
		// Set j to the very top tile (in regards to the screen)
		for (int j = (scrPosY - (scrTilesH / 2)); j < scrPosY + (scrTilesH / 2); j++)
		{
			if (i >= 0 && i < m_width && j >= 0 && j < m_height)
			{
				int startX = (i - (scrPosX - (scrTilesW / 2))) * (Screen_Width / scrTilesW);
				int startY = (j - (scrPosY - (scrTilesH / 2))) * (Screen_Height / scrTilesH);

				// Bind tile texture
				glPushMatrix();		// Start phase
				glEnable(GL_TEXTURE_2D);
				glBindTexture(GL_TEXTURE_2D, tileTextures[myMap->getTileTexture(i, j)]);
				glBegin(GL_QUADS);
					glTexCoord2d(0, 0); glVertex2f(startX, startY);
					glTexCoord2d(1, 0); glVertex2f(startX + (Screen_Width / scrTilesW), startY);
					glTexCoord2d(1, 1); glVertex2f(startX + (Screen_Width / scrTilesW), startY + (Screen_Height / scrTilesH));
					glTexCoord2d(0, 1); glVertex2f(startX, startY + (Screen_Height / scrTilesH));
				glEnd();
				glPopMatrix();		// End phase
			}
		}
	}
}

The function that actually draws the tiles on the screen (and only tiles u can actually see)

No idea what kinda code you really wanted, so I just provided everything that's relevant. I think I underestimated the difficulty of creating a minimap. Still, I'm waiting for a genius to solve this ;)

This topic is closed to new replies.

Advertisement