Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to create minimap? (sdl/opengl)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 Jossos   Members   -  Reputation: 224

Like
1Likes
Like

Posted 12 January 2013 - 05:21 AM

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



Sponsor:

#2 Nausea   Members   -  Reputation: 258

Like
0Likes
Like

Posted 12 January 2013 - 05:44 AM

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



#3 Jossos   Members   -  Reputation: 224

Like
0Likes
Like

Posted 12 January 2013 - 05:53 AM

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



#4 Nausea   Members   -  Reputation: 258

Like
-1Likes
Like

Posted 12 January 2013 - 05:57 AM

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



#5 Zwonkie   Members   -  Reputation: 508

Like
0Likes
Like

Posted 12 January 2013 - 06:00 AM

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 :/

Edited by Zwonkie, 12 January 2013 - 06:02 AM.


#6 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 12 January 2013 - 06:18 AM

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).


Edited by rocklobster, 12 January 2013 - 06:20 AM.


#7 Aphton   Members   -  Reputation: 277

Like
0Likes
Like

Posted 12 January 2013 - 06:53 AM

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..



#8 Jossos   Members   -  Reputation: 224

Like
0Likes
Like

Posted 12 January 2013 - 07:33 AM

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.



#9 Aphton   Members   -  Reputation: 277

Like
0Likes
Like

Posted 12 January 2013 - 07:57 AM

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



#10 Jossos   Members   -  Reputation: 224

Like
1Likes
Like

Posted 12 January 2013 - 02:35 PM

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 ;)



#11 Aphton   Members   -  Reputation: 277

Like
0Likes
Like

Posted 12 January 2013 - 04:10 PM

Creating a minimap isnt difficult. Its actually really simple.

Forget about what I wrote 2 posts before - you dont need to calculate the data.

Just let opengl do it.

 

Try this:

  drawTiles();
  glTranslatef(miniMapX, miniMapY, 0);
  glScalef(miniMapWidth / scr_Width, miniMapHeight / scr_Height, 1);
  drawTiles();



#12 Jossos   Members   -  Reputation: 224

Like
0Likes
Like

Posted 12 January 2013 - 04:31 PM

Creating a minimap isnt difficult. Its actually really simple.

Forget about what I wrote 2 posts before - you dont need to calculate the data.

Just let opengl do it.

 

Try this:

 

  drawTiles();
  glTranslatef(miniMapX, miniMapY, 0);
  glScalef(miniMapWidth / scr_Width, miniMapHeight / scr_Height, 1);
  drawTiles();

The problem is, I will want to represent buildings and units as blue dots on the map (if the player is blue for example)



#13 Aphton   Members   -  Reputation: 277

Like
0Likes
Like

Posted 12 January 2013 - 04:48 PM

How about this~

void Game::drawTiles(bool forMiniMap)

Depending on the parameter, you either draw a blue dot or the actual building

Edited by Aphton, 12 January 2013 - 04:49 PM.


#14 minibutmany   Members   -  Reputation: 1678

Like
0Likes
Like

Posted 12 January 2013 - 05:09 PM

Usually, if you draw a line with each coordinate the same, it is just a single pixel. Also 1 X 1 rectangles.


Stay gold, Pony Boy.

#15 Jossos   Members   -  Reputation: 224

Like
0Likes
Like

Posted 12 January 2013 - 07:24 PM

Usually, if you draw a line with each coordinate the same, it is just a single pixel. Also 1 X 1 rectangles.

is that efficient or what? I mean, I could do each square as a quad, but 32x32 quads just for a minimap seems very inneficient



#16 Jossos   Members   -  Reputation: 224

Like
0Likes
Like

Posted 12 January 2013 - 08:55 PM

Still need help with this. I cannot figure out how to do it (not the theory, the physical code to write)



#17 Jossos   Members   -  Reputation: 224

Like
0Likes
Like

Posted 13 January 2013 - 02:34 AM

Really, no one has a solution for this? maybe I need to post this in a different section



#18 Jossos   Members   -  Reputation: 224

Like
0Likes
Like

Posted 13 January 2013 - 05:36 PM

Really, no one has a solution for this? maybe I need to post this in a different section

and apparently that's frowned upon



#19 BeerNutts   Crossbones+   -  Reputation: 2999

Like
0Likes
Like

Posted 14 January 2013 - 09:34 AM

Here's some code from by tile-based RTS, One to draw the map, and one to draw the minimap.  FWIW, I keep by map in a one-dimensional array, and determine the X/Y via  Y*Height + X index.

 

My mini-map has a square around where the current camera is on the main map as well.

 

 
#define INDEX(xMapX, xMapY) ((xMapY)*mu32MapHeight+(xMapX))
...
 
 
</p><div>/******************************************************************************</div>
<div>*</div>
<div>* Draw() - Draw the map that's in view</div>
<div>*</div>
<div>******************************************************************************/</div>
<div>BOOL WarTurnsMap::Draw(U32 &u32CameraX, U32 &u32CameraY)</div>
<div>{</div>
<div>    U32 u32FullWidth, u32FullHeight;</div>
<div>    U32 u32MapStartX, u32MapStartY;</div>
<div>    U32 u32MapEndX, u32MapEndY;</div>
<div>    S32 s32DrawStartX, s32DrawStartY;</div>
<div>    S32 s32CurrentX, s32CurrentY;</div>
<div>    U32 x, y;</div>
<div>    </div>
<div>    /* store off the camera */</div>
<div>    mu32CameraX = u32CameraX;</div>
<div>    mu32CameraY = u32CameraY;</div>
<div>    </div>
<div>    </div>
<div>    /* figure out where we need to start drawing */</div>
<div>    u32FullWidth  = mu32MapWidth*mu32TileWidth;</div>
<div>    u32FullHeight = mu32MapHeight*mu32TileHeight;</div>
<div>    </div>
<div>    if (u32CameraX + MAP_SCREEN_WIDTH > u32FullWidth)</div>
<div>    {</div>
<div>        /* move camera back */</div>
<div>        u32CameraX = u32FullWidth - MAP_SCREEN_WIDTH;</div>
<div>    }</div>
<div> </div>
<div>    if (u32CameraY + MAP_SCREEN_HEIGHT > u32FullHeight)</div>
<div>    {</div>
<div>        /* move camera back */</div>
<div>        u32CameraY = u32FullHeight - MAP_SCREEN_HEIGHT;</div>
<div>    }</div>
<div>        </div>
<div>    u32MapStartX = u32CameraX / mu32TileWidth;</div>
<div>    u32MapStartY = u32CameraY / mu32TileHeight;</div>
<div>    </div>
<div>    u32MapEndX = u32MapStartX + MAP_SCREEN_WIDTH/mu32TileWidth + 2;</div>
<div>    u32MapEndY = u32MapStartY + MAP_SCREEN_HEIGHT/mu32TileHeight + 2;</div>
<div>    </div>
<div>    s32DrawStartX = u32CameraX % mu32TileWidth;</div>
<div>    s32DrawStartY = u32CameraY % mu32TileHeight;</div>
<div>    </div>
<div>    s32CurrentX = -s32DrawStartX;</div>
<div>    s32CurrentY = -s32DrawStartY;</div>
<div>    </div>
<div>    for (y = u32MapStartY; y < u32MapEndY; y++)</div>
<div>    {</div>
<div>        for (x = u32MapStartX; x < u32MapEndX; x++)</div>
<div>        {</div>
<div>            if ((s32CurrentX < MAP_SCREEN_WIDTH) &&</div>
<div>                (s32CurrentY < MAP_SCREEN_HEIGHT))</div>
<div>            {</div>
<div>                U32 u32Width  = mu32TileWidth;</div>
<div>                U32 u32Height = mu32TileHeight;</div>
<div>                </div>
<div>                if (s32CurrentX + mu32TileWidth >= MAP_SCREEN_WIDTH)</div>
<div>                {</div>
<div>                    u32Width = MAP_SCREEN_WIDTH - s32CurrentX;</div>
<div>                }</div>
<div>                </div>
<div>                if (s32CurrentY + mu32TileWidth >= MAP_SCREEN_HEIGHT)</div>
<div>                {</div>
<div>                    u32Height = MAP_SCREEN_HEIGHT - s32CurrentY;</div>
<div>                }</div>
<div>                </div>
<div>                /* draw the Map Unit at this location */</div>
<div>                mpMapUnits[INDEX(x, y)]->Draw(s32CurrentX, s32CurrentY,</div>
<div>                                              u32Width, u32Height);</div>
<div>                </div>
<div>                /* Draw Lines around map, above and below */</div>
<div>                if (s32CurrentY + mu32TileHeight <= MAP_SCREEN_HEIGHT)</div>
<div>                {</div>
<div>                    PutImageFX2(pu8HorizBar, s32CurrentX, </div>
<div>                                s32CurrentY + mu32TileHeight-1,</div>
<div>                                u32Width, u32Height);</div>
<div>                }</div>
<div>                </div>
<div>                if (s32CurrentX + mu32TileWidth <= MAP_SCREEN_WIDTH)</div>
<div>                {</div>
<div>                    PutImageFX2(pu8VertBar, s32CurrentX + mu32TileWidth-1, </div>
<div>                                s32CurrentY,</div>
<div>                                u32Width, u32Height);</div>
<div>                }</div>
<div>            }</div>
<div>            </div>
<div>            s32CurrentX += mu32TileWidth;</div>
<div>        }</div>
<div>        s32CurrentX = -s32DrawStartX;</div>
<div>        s32CurrentY += mu32TileHeight;</div>
<div>    }</div>
<div> </div>
<div>}</div>
<div> </div>
<div>/******************************************************************************</div>
<div>*</div>
<div>* MiniMapDraw() - Draw the mini-map</div>
<div>*</div>
<div>******************************************************************************/</div>
<div>void WarTurnsMap::MiniMapDraw(S32 s32X, S32 s32Y, U32 &u32Width, U32 &u32Height)</div>
<div>{</div>
<div>    float fXpixels, fYpixels;</div>
<div>    U32   u32Xpixels, u32Ypixels;</div>
<div>    WarTurnsUnit *pUnit;</div>
<div>    </div>
<div>    /* check if there's less pixels than width */</div>
<div>    if (u32Width < mu32MapWidth)</div>
<div>    {</div>
<div>        printf("FooBar on Map Width!\n");</div>
<div>    }</div>
<div>    if (u32Height < mu32MapHeight)</div>
<div>    {</div>
<div>        printf("FooBar on Map Height!\n");</div>
<div>    }</div>
<div>    </div>
<div>    /* compute the ratio between num tiles and width */</div>
<div>    u32Xpixels = u32Width/mu32MapWidth;</div>
<div>    u32Ypixels = u32Height/mu32MapHeight;</div>
<div>    </div>
<div>    /* adjust the width/Height */</div>
<div>    u32Width = u32Xpixels*mu32MapWidth;</div>
<div>    u32Height = u32Ypixels*mu32MapHeight;</div>
<div>    </div>
<div>    /* save off the minimap size */</div>
<div>    mu32MiniWidth = u32Width;</div>
<div>    mu32MiniHeight = u32Height;</div>
<div>    </div>
<div>    /* draw outline */</div>
<div>    RectFillFX(s32X-2, s32Y-2, u32Width+4, u32Height+4,</div>
<div>                0xff, 0xff, 0xff, 255, NULL);</div>
<div>    </div>
<div>    int x, y;</div>
<div>    U8 u8r, u8g, u8b;</div>
<div> </div>
<div>#if 0    </div>
<div>    /* draw all green 1st */</div>
<div>    RectFillFX(s32X, s32Y, u32Width, u32Height,</div>
<div>                           0, 155, 0, 255, NULL);</div>
<div>#else</div>
<div>    /* draw all black 1st */</div>
<div>    RectFillFX(s32X, s32Y, u32Width, u32Height,</div>
<div>                           1, 1, 1, 255, NULL);</div>
<div>#endif</div>
<div>    </div>
<div>    /* now draw the minimap */</div>
<div>    for (y = 0; y < mu32MapHeight; y++)</div>
<div>    {</div>
<div>        for (x = 0; x < mu32MapWidth; x++)</div>
<div>        {</div>
<div>            /* check we've revealed this space */</div>
<div>            if (mpMapUnits[INDEX(x, y)]->ViewableGet() == FALSE)</div>
<div>            {</div>
<div>                continue;</div>
<div>            }</div>
<div>            /* check there's a Unit on this map tile */</div>
<div>            pUnit = mpMapUnits[INDEX(x, y)]->UnitGet(0);</div>
<div>            </div>
<div>            /* check there's a building on this spot */</div>
<div>            if (mpMapUnits[INDEX(x, y)]->BuildingGet() != NULL)</div>
<div>            {</div>
<div>                /* draw gray for our building, black for enemy building */</div>
<div>                if (mpMapUnits[INDEX(x, y)]->BuildingGet()->OwnerGet() == MyOwnerIdGet())</div>
<div>                {</div>
<div>                    u8r = 160;</div>
<div>                    u8g = 82;</div>
<div>                    u8b = 45;</div>
<div>                }</div>
<div>                else</div>
<div>                {</div>
<div>                    u8r = 1;</div>
<div>                    u8g = 1;</div>
<div>                    u8b = 1;</div>
<div>                }</div>
<div>            }</div>
<div>            else if (pUnit)</div>
<div>            {</div>
<div>                /* Do Later */   </div>
<div>                if (pUnit->OwnerGet() == MyOwnerIdGet())</div>
<div>                {</div>
<div>                    /* Put a white dot here */</div>
<div>                    u8r = 255;</div>
<div>                    u8g = 255;</div>
<div>                    u8b = 255;</div>
<div>                }</div>
<div>                else</div>
<div>                {</div>
<div>                    /* put red dot if visible */</div>
<div>                    //if (Visible)</div>
<div>                    {</div>
<div>                        u8r = 255;</div>
<div>                        u8g = 0;</div>
<div>                        u8b = 0;</div>
<div>                    }</div>
<div>                }</div>
<div>            }</div>
<div>            else</div>
<div>            {</div>
<div>                switch(mpMapUnits[INDEX(x, y)]->TileGet()->eTileType)</div>
<div>                {</div>
<div>                    case TILE_GRASS:</div>
<div>                        //continue;</div>
<div>                        u8r = 0;</div>
<div>                        u8g = 155;</div>
<div>                        u8b = 0;</div>
<div>                        break;</div>
<div>                        </div>
<div>                    case TILE_RESOURCE:</div>
<div>                        u8r = 148;</div>
<div>                        u8g = 0;</div>
<div>                        u8b = 211;</div>
<div>                        break;</div>
<div>                        </div>
<div>                    case TILE_ROCK:</div>
<div>                        u8r = 205;</div>
<div>                        u8g = 201;</div>
<div>                        u8b = 201;</div>
<div>                        break;</div>
<div>                        </div>
<div>                    case TILE_WATER:</div>
<div>                        u8r = 0;</div>
<div>                        u8g = 0;</div>
<div>                        u8b = 255;</div>
<div>                        break;</div>
<div>                        </div>
<div>                    case TILE_SAND:</div>
<div>                        u8r = 155;</div>
<div>                        u8g = 155;</div>
<div>                        u8b = 0;</div>
<div>                        break;</div>
<div>                        </div>
<div>                    case TILE_NONE:</div>
<div>                        u8r = 1;</div>
<div>                        u8g = 1;</div>
<div>                        u8b = 1;</div>
<div>                        break;</div>
<div>                }</div>
<div>            }</div>
<div>            RectFillFX(s32X+(x*u32Xpixels), s32Y+(y*u32Ypixels), </div>
<div>                       u32Xpixels, u32Ypixels,</div>
<div>                       u8r, u8g, u8b, 255, NULL);</div>
<div>        }</div>
<div>    }</div>
<div>    </div>
<div>    /* Finally, draw square around where the camera is */</div>
<div>    {</div>
<div>        U32 u32StartX, u32EndX;</div>
<div>        U32 u32StartY, u32EndY;</div>
<div>        U32 u32Temp;</div>
<div>        </div>
<div>        /* calculate where to start for X */</div>
<div>        u32Temp   = (mu32CameraX*10000)/(mu32MapWidth*mu32TileWidth);</div>
<div>        u32StartX = s32X + (u32Temp*mu32MiniWidth)/10000;</div>
<div>        u32EndX  = u32StartX + (mu32MiniWidth*MAP_SCREEN_WIDTH)/</div>
<div>                    (mu32MapWidth*mu32TileHeight);</div>
<div>        </div>
<div>        u32Temp   = (mu32CameraY*100)/(mu32MapHeight*mu32TileHeight);</div>
<div>        u32StartY = s32Y + (u32Temp*mu32MiniHeight)/100;</div>
<div>        u32EndY  = u32StartY + (mu32MiniHeight*MAP_SCREEN_HEIGHT)/</div>
<div>                    (mu32MapHeight*mu32TileHeight);</div>
<div>        </div>
<div>        RectFillFX(u32StartX, u32StartY, u32EndX - u32StartX, 1, </div>
<div>                   MINI_MAP_COLOR, NULL);</div>
<div>        RectFillFX(u32StartX, u32StartY, 1, u32EndY - u32StartY, </div>
<div>                   MINI_MAP_COLOR, NULL);</div>
<div>        RectFillFX(u32StartX, u32EndY, u32EndX - u32StartX, 1, </div>
<div>                   MINI_MAP_COLOR, NULL);</div>
<div>        RectFillFX(u32EndX, u32StartY, 1, u32EndY - u32StartY, </div>
<div>                   MINI_MAP_COLOR, NULL);</div>
<div>        </div>
<div>    }</div>
<div> </div>
<div>}</div>
<div>

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#20 Jossos   Members   -  Reputation: 224

Like
0Likes
Like

Posted 24 January 2013 - 01:15 AM

Ok so, I kinda abandoned this topic since I couldn't figure it out, but I'm back to try again, and I think i can articulate better what I want.

 

I would like to be able to create a custom texture and store it inside an unsigned int so I can attach it to a opengl quad. unsigned int minimap is what I will call it. and I want to be able to draw each pixel to a color of my choice. Now how can I do this? what does the syntax look like? like minimap.addcolor[0,255,0]; I've no idea. I know I would use 2 for loops for the width and height of the map etc. Just need to know exactly how to do this.

 

Thanks in advance!


Edited by Jossos, 24 January 2013 - 01:16 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS