• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Jossos

How to create minimap? (sdl/opengl)

19 posts in this topic

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

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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

0

Share this post


Link to post
Share on other sites
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

-1

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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 ;)

1

Share this post


Link to post
Share on other sites

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:

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

0

Share this post


Link to post
Share on other sites

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)

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
Really, no one has a solution for this? maybe I need to post this in a different section

and apparently that's frowned upon

0

Share this post


Link to post
Share on other sites

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.

 

[code]  #define INDEX(xMapX, xMapY) ((xMapY)*mu32MapHeight+(xMapX)) ...    

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

Share this post


Link to post
Share on other sites

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
0

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  
Followers 0