Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,399 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,106 people online right now. Registration is fast and FREE... Join Now!




grabbing all the positions of the tiles in an image

 
Reply to this topicStart new topic

grabbing all the positions of the tiles in an image

econobeing
30 Sep, 2006 - 08:27 PM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 1


My Contributions
(SDL, a little bit)

i'm trying to make a class to keep track of maps, i made a struct to keep track of individual tiles(which appears to work). anyway, i'm having trouble with the loadTiles() function(pretty sure that's where the problem is).

i'm using this image:
IPB Image

so i'm trying to go left to right, and grab the position of the top-left pixel of each image, then go down to the next row and start the process over again.

here's my header:
CODE
#include <string>
#include <vector>

#include <SDL.h>
#include <SDL_image.h>

#ifndef _MAPS_H_
#define _MAPS_H_

struct tile{
    Uint16 tileSize;
    Uint16 sourceX;
    Uint16 sourceY;
    Uint16 drawX;
    Uint16 drawY;
    SDL_Surface* sourceImg;
};//struct tile

class tileMap{
    public:
        //data members
        SDL_Surface* sourceImg;
        std::vector<tile> map;
        std::vector<tile> tiles;
        //functions
        tileMap();
        tileMap(const char filename[],Uint16 tileSize,Uint8 gutter_size);
        tileMap(SDL_Surface* source_image,Uint16 tileSize,Uint8 gutter_size);
        void initMap(Uint16 map_width,Uint16 map_height);
        void drawMap(SDL_Surface* dest,int startX,int startY);
        Uint16 getTotalTiles();
        Uint16 getTileSize();
        Uint8 getGutterSize();
        Uint16 getMapWidth();
        Uint16 getMapHeight();
    private:
        //data members
        Uint16 totalTiles;
        Uint16 mapWidth;
        Uint16 mapHeight;
        Uint16 tileSize;
        Uint8 gutterSize;
        //functions
        void loadTiles();
};


#endif // _MAPS_H_


and here's the .cpp file for the header:
CODE
#include <string>
#include <vector>

#include <SDL.h>
#include <SDL_image.h>

#include "maps.h"

using namespace std;

tileMap::tileMap(){
    this->sourceImg = NULL;
}//tileMap::tileMap()

tileMap::tileMap(const char fileName[],Uint16 tile_size,Uint8 gutter_size){
    //fill out class info
    this->sourceImg = NULL;
    this->sourceImg = IMG_Load(fileName);
    this->tileSize = tile_size;
    this->totalTiles = ((sourceImg->w / this->tileSize) + (sourceImg->h / this->tileSize));
    this->gutterSize = gutter_size;

    //load the tiles
    if(this->sourceImg != NULL)
        this->loadTiles();
}//tileManager::tileManager(const char filename[],Uint16 tileWidth,Uint16 tileHeight)

tileMap::tileMap(SDL_Surface* source_image,Uint16 tile_size,Uint8 gutter_size){
    //fill out class info
    this->sourceImg = source_image;
    this->tileSize = tile_size;
    this->totalTiles = ((sourceImg->w / this->tileSize) + (sourceImg->h / this->tileSize));
    this->gutterSize = gutter_size;
}//tileMap(SDL_Surface* source_image,Uint16 tileSize,Uint8 gutter_size)

void tileMap::loadTiles(){
    this->tiles.clear();
    //for each row of tiles
    for(int rowIter = this->gutterSize; rowIter < (this->sourceImg->w / (this->tileSize + this->gutterSize)); rowIter += (this->tileSize + this->gutterSize)){
        //for each column of tiles(per row)
        for(int colIter = this->gutterSize; colIter < (this->sourceImg->w / (this->tileSize + this->gutterSize)); colIter += (this->tileSize + this->gutterSize)){
            tile iter;
            iter.tileSize = this->tileSize;
            iter.sourceImg = this->sourceImg;
            iter.sourceX = rowIter;
            iter.sourceY = colIter;

            this->tiles.push_back(iter);
        }//for each column of tiles(per row)
    }//for each row of tiles
}//void tileManager::loadTiles()

void tileMap::initMap(Uint16 map_width,Uint16 map_height){
    this->mapWidth = map_width;
    this->mapHeight = map_height;

    //Uint16 maxTile=0;
    //Uint16 randTile=0;
    //for(std::vector<tile>::iterator iter = this->tiles.begin(); iter < this->tiles.end(); iter++)
    //    maxTile++;

    //loop through each row of tiles
    for(int tileX=0; tileX < map_width; tileX++){
        for(int tileY=0; tileY < map_height; tileY++){
            //create an iterator to hold temporary data used to fill
            //the map that's being initialized
            tile iter;

            //randTile = rand()%maxTile;

            //randTile=rand()%maxTile;
            iter.sourceImg = this->sourceImg;
            iter.tileSize = this->tileSize;
            iter.sourceX = this->tiles[0].sourceX;
            iter.sourceY = this->tiles[0].sourceY;
            iter.drawX = tileX * this->tileSize;
            iter.drawY = tileY * this->tileSize;

            //add the tile to the map
            this->map.push_back(iter);
        }//for each column
    }//for each row
}//initMap(Uint16 map_width,Uint16 map_height)

void tileMap::drawMap(SDL_Surface* dest,int startX,int startY){
    //for each tile in the map
    SDL_Rect sourcePos;
    SDL_Rect destPos;
    for(std::vector<tile>::iterator iter = this->map.begin(); iter < this->map.end(); iter++){
        //if it is on the surface horizontally (not too far left or right)
        if(iter->drawX + iter->tileSize > 0 && iter->drawX < dest->w){
            //if it is on the surface vertically (not too far up or down)
            if(iter->drawY + iter->tileSize > 0 && iter->drawY < dest->h){
                //grab the source positions and dimensions
                sourcePos.x = iter->sourceX;
                sourcePos.y = iter->sourceY;
                sourcePos.w = iter->tileSize;
                sourcePos.h = iter->tileSize;

                //get the destination position and dimensins
                destPos.x = (startX + iter->drawX);
                destPos.y = (startY + iter->drawY);
                //destPos.w = sourcePos.w;
                //destPos.h = sourcePos.h;

                //(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect))
                SDL_BlitSurface(iter->sourceImg,&sourcePos,dest,&destPos);
            }
        }
    }//for each tile in the map
}//void drawMap(SDL_Surface* dest,int startX,int startY);

Uint16 tileMap::getTotalTiles(){
    return this->totalTiles;
}//Uint16 getTotalTiles()

Uint16 tileMap::getTileSize(){
    return this->tileSize;
}//Uint16 getTotalTiles()

Uint8 tileMap::getGutterSize(){
    return this->gutterSize;
}//Uint16 getTotalTiles()

Uint16 tileMap::getMapWidth(){
    return this->mapWidth;
}//Uint16 tileMap::getMapWidth()

Uint16 tileMap::getMapHeight(){
    return this->mapHeight;
}//Uint16 tileMap::getMapHeight();


i just can't seem to get it right, i'm pretty sure the problem is in the loadTiles() function because the drawMap() function doesn't change anything that's outside the scope of the function...

i've re-written the loadTiles() function twice now and each time i get different results, but they're never right... can anybody point out what i'm doing wrong?

This post has been edited by Dark_Nexus: 1 Oct, 2006 - 02:29 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:06AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month