Thread: problem with sdl and vectors

  1. #1
    h4x0r1ng t3h m41nfr4m3!!1 r0bbb's Avatar
    Join Date
    Mar 2005
    Location
    West London
    Posts
    9

    problem with sdl and vectors

    hi im having a problem when i call a function for an object in a vector. when i call the objects function when its not in the vector, it works fine, but if i call it in the vector i get an sdl error telling me the surface needs to be locked, even though i dont ever need pixel access, and it doesnt cause the error when called normally. heres what im doing :

    Code:
         testTile.draw(20,20);
         testTile.draw(40,20);
         testTile.draw(40,40);
    
         tiles[0][0].draw(80, 70);
    BlitSurface errorSurfaces must not be locked during blit is the error i get.

    the tiles are all initializing fine, coz other methods work, so im not sure whats causing the problem.

    any help will be great.

    thanks rob

    below is the code in full context

    Code:
    bool Map::init(char *mapName){
    
         ifstream mapFile(mapName);
    
         if(!mapFile.is_open()){ // could not open file
    
              cout << "Unable to open Map " << mapName << "\n";
              mapFile.close();
              return false;
    
         }
         else{ // continue to read map
    
              cout << mapName << " loaded\n";
    
              mapFile >> mapWidth >> mapHeight >> tileWidth >> tileHeight >> xOffset >> yOffset; 
    
              vector< vector<Tile> > loadedTiles(mapHeight, vector<Tile>(mapWidth)); 
    
              testTile.init("stone.bmp", 1, 1);
              
              for(int j = 0; j < 1; j++){ // also here if i make this loop any larger i get Fatal signal: Segmentation Fault (SDL Parachute Deployed)
                   for(int i = 0; i < mapWidth; i++){                    
    
                        char filename[256];
                        int passable, transparent;
    
                        mapFile >> filename >> passable >> transparent;
                        loadedTiles[i][j].init(filename, passable, transparent);
                        loadedTiles[i][j].toString();
                   }
              }
    
              mapFile.close();
              tiles = loadedTiles;
    
              return true;
         }
         
    } // end init
    
    
    bool Map::draw(){
    
         testTile.draw(20,20);
         testTile.draw(40,20);
         testTile.draw(40,40); // ALL DRAW FINE
    
         tiles[0][0].toString();  // WORKS
         tiles[0][0].draw(80, 70); // CAUSES SDL ERROR
         
    
         /*
         for(int j = 0; j < mapHeight; j++){
              for(int i = 0; i < mapWidth; i++){
              
                   tiles[i][j].toString();
    
              }
         }*/
    
         return true;
    } // end draw

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> when i call the objects function when its not in the vector, it works fine,...
    Make a copy of the object and see if it works on the copy.

    gg

  3. #3
    h4x0r1ng t3h m41nfr4m3!!1 r0bbb's Avatar
    Join Date
    Mar 2005
    Location
    West London
    Posts
    9
    when i copy the object i get the same error, so this would suggest some problem with the initialization of the object? just cant see what, any ideas?

  4. #4
    h4x0r1ng t3h m41nfr4m3!!1 r0bbb's Avatar
    Join Date
    Mar 2005
    Location
    West London
    Posts
    9
    ive now tryed the whole thing using a 2d array instead of a vector of vectors, and it works!

    *NEW PROBLEM*

    i need the array to be dynamicaly sized. any ideas how to do this, or how to fix the problem i have with the vectors?

    cheers rob

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> when i copy the object i get the same error, so this would suggest some problem with the initialization of the object?
    Close. Your copy constructors are not right. Either go with a solution that does not involve copying your objects are fix your objects so that they can be copied without side effects.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors of classes
    By Dondrei in forum C++ Programming
    Replies: 26
    Last Post: 03-20-2009, 05:11 AM