• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

We're also offering banner ads on our site from just \$5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

# MoBaT

Member Since 31 Jan 2013
Offline Last Active Feb 15 2013 06:35 PM

### Topics I've Started

07 February 2013 - 10:29 PM

I cannot grasp the concept of the map loading all around the player. For example if the map loads 10 to the left of the player, 10 right, 10 up, and 10 down, I cannot think of a way to incorporate that into my code. I'm having a brain fart.

```         int xcoord = player->xcoord; // Players x coord will be the original coord * (TILE_SIZE * zoom)
int ycoord = player->ycoord; // Players x coord will be the original coord * (TILE_SIZE * zoom)
//Rectangles
SDL_Rect offset;
SDL_Rect offset2;

// Loop through laters
for (int i = 0; i < map->GetNumLayers(); ++i) {
const Tmx::Layer *layer = map->GetLayer(i);

// TILE_SIZE = 32, ZOOM = 2.0;
// Loop through tile number in layers. Tile #(2, 1) on the map would be 2 * (TILE_SIZE * ZOOMSCALE) down the x and 1 * (TILE_SIZE * ZOOMSCALE) down the y;
// 0 1 2 3 4
// 1
// 2
// 3
// 4

// Loop through tiles from tile (0, 0) to (20, 20);
for (int y = 0; y < 20; ++y)
{
for (int x = 0; x < 20; ++x)
{
// Get tileID
int TileID = layer->GetTileId(x, y);

if (TileID != 0) {

int tilesetID = map->GetLayer(i)->GetTileTilesetIndex(x, y);

int margin = map->GetTileset(tilesetID)->GetMargin() * ZOOMSCALE;
int spacing = map->GetTileset(tilesetID)->GetSpacing() * ZOOMSCALE;

int TileZoom = (TILE_SIZE * ZOOMSCALE);
int TilesWidth = tileSet[tilesetID]->w / TileZoom;

int tileRow = TileID % TilesWidth;
int  tileColumn = ((TileID - tileRow) / TilesWidth);

// Set tileset tile X, Y
offset.x = ((tileRow * TileZoom) + tileRow * margin) + spacing;
offset.y = ((tileColumn * TileZoom) + tileColumn * margin) + spacing;
offset.w = TileZoom;
offset.h = TileZoom;

//Where to draw on gamemap
offset2.x = x * TileZoom;
offset2.y = y * TileZoom;
offset2.w = TileZoom;
offset2.h = TileZoom;

//Blitz.
SDL_BlitSurface(tileSet[tilesetID], &offset, Surf_Display, &offset2);

}
}
}
}
```

### Drawing map takes all CPU

04 February 2013 - 07:27 PM

I'm using tmx files for my map and finally got a tmx loader to work. The map drawing code is lagging my game because of the CPU usage. To draw my map, im using code which is:

```for (int i = 0; i < map->GetNumLayers(); ++i)
{
// Get a layer.
const Tmx::Layer *layer = map->GetLayer(i);

for (int y = 0; y < 20; ++y)
{
for (int x = 0; x < 20; ++x)
{
// Get a test tile which is 32 x 32.
Sprite* Suf2 = new Sprite(Surf_Tileset->getRect(TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE), 1, 0);
// Draw tile to position on map
Suf2->draw(Surf_Display, x * TILE_SIZE, y * TILE_SIZE);
}
}
}
```

So this code is on my Map::OnRender(SDL_Surface* Display). What can I do to keep my cpu from maxing?

### SDL Tile MAP Question

03 February 2013 - 03:10 PM

I got a question for how should I be creating my tilemap. Should I make it where I just draw the map myself and then specify the spots where different collisions are like what this guy did: http://a248.e.akamai.net/camo.github.com/28e1bb15d28335f2d7763576debf5543cf6a255b/687474703a2f2f6b656e2d736f66742e636f6d2f696d616765732f7a656c64612f7a656c6461392e706e67

Or should I make it where I just draw the map based on:
1:0 1:1

0:1 0:1

Where the first part is the map tile id and the second is if it's a certain item like a hole, water, solid etc.

### SDL Event Queue

31 January 2013 - 09:10 PM

I have a problem with the SDL Events. I have a code which looks like:

```while (SDL_PollEvent(&Event)){
OnEvent(&Event);
}
```

then the OnEvent processes the event and directs it to the proper virtual function. So If I were to hold down the right key button, it would go here.

```void Engine::OnKeyDown(SDLKey sym){
switch(sym) {
case SDLK_RIGHT:
x1 += 1;
break;
}
}
```

The problem is that when I am holding the right button key, it is only running one time. The queue does not stack and keep processing the keydown button. What could be the problem?

PARTNERS