Jump to content
  • Advertisement

MarioRossi

Member
  • Content Count

    6
  • Joined

  • Last visited

Community Reputation

103 Neutral

About MarioRossi

  • Rank
    Newbie
  1. Oh... I totally overlooked it, now I feel dumb ç_ç btw: thanks for the replyes, I'm gonna have to learn using the debugger
  2. Thanks for the suggestion, I'll put some comments now :)
  3. Hello everyone, So... today I was expanding my game a bit to make it load 25x25 maps instead of 10x10. here's the function I use for loading the maps: void load_map(Blocco loadedMap[][10],std::string filename,int maxTile) //filename = "map00.txt", the map to read, maxTIle= the maximum ID for a tile accepted { //loadedMap = a matrix to hold the Blocks for later use (like printing the map/ineteractions) std::ifstream map(filename.c_str(),std::ifstream::in); int x=0; int y=0; for(int t=0;t<MAP_SIZE;t++) //MAP_SIZE = how many blocks are in the map { int tileType=-1; map>>tileType; if((tileType>=0)&&(tileType<maxTile)) { SDL_Rect *temp = new SDL_Rect(); //Rectangle to mark wich sprite to use for the block temp->x=(tileType*BLOCK_SIZE);temp->y=0;temp->h=BLOCK_SIZE,temp->w=BLOCK_SIZE; bool solid; if(tileType==2||tileType==3||tileType==5){solid=true;}else{solid=false;} //wheater the block is solid or not Blocco supp(temp,x,y,solid,tileType); //Blocco = Block in Italian lol, you give it the sprite, coordinates and the ID loadedMap[x][y]=supp; x++; if(x==LINE_SIZE) { x=0; y++; } } } //STARTS READING PLAYER COORDS int px,py; map>>px; //READS X map>>py; //READS Y SDL_Rect *pSprite = new SDL_Rect();pSprite->x=0;pSprite->y=0;pSprite->h=BLOCK_SIZE;pSprite->w=BLOCK_SIZE; //CREATES THE SPRITE FOR THE PLAYER player = new Player(pSprite,px,py,true,0,100,100); //Player is similar to a block but has current_health and max_health (100 and 100 in this case) //START READING KEYS int nKeys; //How Many keys are there int xKey=0; // int yKey=0; //The coordinates of the key int keyType; std::string nameKey; std::string descKey; map>>nKeys; for(int i=0;i<nKeys;i++) { map>>xKey; //Reads how many Keys to load map>>yKey; // map>>keyType; //The type of key loaded (green key, red key and blue key SDL_Rect *kSprite = new SDL_Rect();kSprite->x=96+(keyType*BLOCK_SIZE);kSprite->y=0;kSprite->w=BLOCK_SIZE;kSprite->h=BLOCK_SIZE; switch(keyType) //Just sets the name and desc based on the type of key { case 1: nameKey="GreenKey"; descKey="a green key"; break; case 2: nameKey="BlueKey"; descKey="a blue key"; break; case 3: nameKey="RedKey"; descKey="a red key"; break; } Key* add = new Key(kSprite,xKey,yKey,false,true,keyType,nameKey,descKey); keys.push_back(add); //adds the key* to a vector<Key*> that holds them } map.close(); } And this is the file I read: 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 // 02 01 01 01 02 01 01 01 01 01 02 01 01 01 01 01 01 01 02 01 02 01 02 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 02 02 02 02 02 02 // 02 01 01 01 02 01 01 01 01 01 02 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 01 02 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 04 01 01 01 01 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 04 01 01 01 01 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 02 02 02 02 02 02 02 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 01 01 01 02 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 01 01 01 02 // The actual map: 625 Blocks(IDs), 25 per line 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 02 02 02 02 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 01 01 01 02 // 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 01 01 01 02 // 02 01 01 01 01 01 01 01 01 02 02 02 04 02 02 02 02 02 02 02 02 01 01 01 02 // 02 01 01 01 01 01 01 01 01 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 01 01 01 01 01 01 01 01 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 02 02 02 02 01 01 01 01 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 01 01 01 02 01 01 01 01 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 01 01 01 01 01 01 01 01 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 01 01 01 02 01 01 01 01 02 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 // 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 // 01 03 //Player Coords 06 //How many keys are there 19 01 01 //Key 1: x19 y01 type:Green 21 01 02 //Key 2: x21 y01 .... 23 01 03 ... 19 08 01 ... 01 22 03 20 22 02 I modified the constants: MAP_SIZE and LINE_SIZE from 100 and 10 to 625 and 25 and now the program crashes when it gets to the function load map with this error: "Process terminated with status -1073741819 (0 minute(s), 5 second(s))", am I missing something? :S
  4. Thanks for the explanation, I'm not very good with english so sometimes i just misspell or I can't find the right words. btw: now my program works fully :D
  5. Thanks for the reply! Now it works, it was just some problem with the empty pointer (but my code still doesn't do what i want )= )
  6. Hello, First of all: I'm a student with 2 years of experience in Java that still does not have idea if it's matrix[x][y] or matrix[y][x] so this error might be easy to fix or just dumb on my part, sorry if it is. Okay, so i'm making a "port" of a rougue-like game i made in java 5-6 months ago becouse i found out that c++ with the right packages is way better... i finally found a way to make my code work but it gives a strange error when i run it (I'm using SDL), it says "Process terminated with status 3 (0 minute(s), 0 second(s))". the code is: Main.cpp: #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "bin/Graphic.h" #include <math.h> #include <string> #include <fstream> const int MAP_SIZE = 100; const int LINE_SIZE = 10; SDL_Surface *screen=NULL; SDL_Surface *block_textures=NULL; class Blocco { public: SDL_Rect *sprite; int x,y; bool solid; int id; Blocco(){} Blocco(SDL_Rect *_sprite,int _x,int _y,bool _solid,int _id) { this->sprite = _sprite; this->x=_x; this->y=_y; this->solid=_solid; this->id=_id; } void show(SDL_Surface *source,SDL_Surface *destination) { apply_surface(this->x,this->y,source,destination,this->sprite); } }; Blocco mappa[10][10]; class Player:public Blocco { public: int health; int max_health; Player(SDL_Rect *_sprite,int _x,int _y,bool _solid,int _id,int _health,int _max_health):Blocco(_sprite,_x,_y,_solid,_id) { this->health = _health; this->max_health = _max_health; } }; void load_map(Blocco loadedMap[][10],std::string filename,int maxTile) { std::ifstream map(filename.c_str(),std::ifstream::in); int x=0; int y=0; for(int t=0;t<MAP_SIZE;t++) { int tileType=-1; map>>tileType; if((tileType>=0)&&(tileType<maxTile)) { SDL_Rect *temp=NULL;temp->x=(tileType*16);temp->y=0;temp->h=16,temp->w=16; bool solid; if(tileType==0||tileType==2){solid=true;}else{solid=false;} Blocco supp(temp,x,y,solid,tileType); loadedMap[x][y]=supp; x++; if(x==LINE_SIZE) //da testare! { x=0; y++; } } } map.close(); } void refresh() { for(int y=0;y<LINE_SIZE;y++) { for(int x=0;x<LINE_SIZE;x++) { mappa[x][y].show(block_textures,screen); } } } int main(int argc,char *argv[]) { SDL_Init(SDL_INIT_EVERYTHING); screen = SDL_SetVideoMode(160,160,32,SDL_SWSURFACE); block_textures = load_image("textures/blocks.bmp"); load_map(mappa,"maps/map01.txt",MAP_SIZE); refresh(); SDL_Delay(10000); SDL_Quit(); return 0; } Graphic.h: #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include <string> SDL_Surface* load_image(std::string filename) { SDL_Surface* loadedImage = NULL; SDL_Surface* optimizedImage = NULL; loadedImage = IMG_Load(filename.c_str()); if(loadedImage != NULL) { optimizedImage = SDL_DisplayFormat(loadedImage); SDL_FreeSurface(loadedImage); } if(optimizedImage!=NULL) { Uint32 colorkey = SDL_MapRGB(optimizedImage->format,0,0xFF,0xFF); SDL_SetColorKey(optimizedImage,SDL_SRCCOLORKEY,colorkey); } return optimizedImage; } void apply_surface(int x,int y,SDL_Surface* source,SDL_Surface* destination) { SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface(source,NULL,destination,&offset); } void apply_surface(int x,int y,SDL_Surface *source,SDL_Surface *destination,SDL_Rect *clip) { SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface(source,clip,destination,&offset); } I know it's messy and i'll move the function and classes around once i polish it up (or maybe i should do it now?) Anyway, after a bit of testing and troubleshooting i found the error to be in the function "load_map" as expected (I'm not really sure how to use the file interactions in C++)... I googled the problem and a lot of people say it's an empty pointer but i've checked and it dowsn't look like it, anyone has any idea of how to fix this?   Edit: I'm using Code::Blocks and MinGw for editing and compiling, if you need any description of the code (what it does, why is it like this ecc) I'll try to provide more information as soon as I check the post
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!