Jump to content
  • Advertisement

Elit3d

Member
  • Content count

    110
  • Joined

  • Last visited

Community Reputation

358 Neutral

About Elit3d

  • Rank
    Member

Personal Information

  • Interests
    Programming
  1. I really appreciate the help, great community here
  2. Ah great, many thanks! I can see my mistake now, I'm so stupid, I'm supposed to create a new instance of the object otherwise the position is going to be transferred to each pickup this is now my code: pickups = new Pickup("images/icon-power.png", sf::Vector2f(obj[i].rect.left, obj[i].rect.top)); if (bSpawnNoMore == false) pickupVector.push_back(pickups);
  3. Pickup::Pickup(std::string imgpath) { texture.loadFromFile(imgpath); sprite.setTexture(texture); } its just a simple entity class really that lets me set the pickup sprite entity class: class Entity { public: sf::Sprite sprite; sf::Texture texture; }; basically, when the level loads, depending on how many "item" points there are on the parsed map, will spawn the correct amount of items (i know how to spawn the certain cap of items already) on each grid spot as shown, like Alberth said, its something to do with the sprite position updating to the last position passed to sprite.setPosition
  4. What is the best way to deal with this?
  5. So I am trying to get an item to spawn on specific locations. I am using a xml parser to get the objects name and location (solid walls, start point, end point and item spawn points). It will only spawn on the last location and totally avoids the first. Anyway here is my code that I have so far: the problem: The spawn locations i want to place on: The code: for (int i = 0; i < obj.size(); i++) { if (obj[i].name == "item") { pickups->sprite.setPosition(obj[i].rect.left, obj[i].rect.top); pickupVector.push_back(pickups); } } so I want to spawn it on the first one location, then push it onto the array, then spawn the next one and put on array and so on..
  6. For anyone browsing this later on, I fixed it like so:   in the setup function: for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { block_sprite[x][y].setTexture(block_texture); if (SpawnSolidBlock() == true) { block_sprite[x][y].setPosition(x * 32, 32 + (y * 64)); block_vector.push_back(block_sprite[x][y]); } } } for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { block2_sprite[x][y].setTexture(block2_texture); if (SpawnSolidBlock() == false) { block2_sprite[x][y].setPosition(x * 32, 0 + (y * 32)); block_vector.push_back(block2_sprite[x][y]); } } } then just draw the vector in the draw function.    There are probably better ways to handle the blank tiles but this is my method.
  7.   Im going for a downwell sort of thing with my map. My randomisation is handled like so: bool Game::SpawnSolidBlock() { int blockNum = rand() % 2; if (blockNum == 1) { return true; } return false; } void Game::Update() { if (SpawnSolidBlock() == true) { block_vector.push_back(block_sprite); }
  8. So I am trying to display a vector of blocks that will be randomly generated x and y eg.    [media]https://i.gyazo.com/782325225d1020bb2a4dec987a534383.png[/media]   however my code is displaying:   [media]https://i.gyazo.com/ce0aa34a4822fa3440deebc28a92427c.gif[/media]   I know why it is doing this but I dont know a better way of dealing with this. Any ideas?   my code: for (int i = 0; i < block_vector.size(); i++) { if (block_vector[i].getPosition().x >= 320) { currentBlockPos.y = i + 32; block_vector[i].setPosition(0, currentBlockPos.y); } else { currentBlockPos.x = i * 32; block_vector[i].setPosition(currentBlockPos.x, currentBlockPos.y); } }
  9. Elit3d

    Level loading causing lag/stutter

    Just wanted to feedback to this thread that I managed to fix my issue, thanks everyone for the help as it allowed me to tackle my problem from another angle. What I decided to do was to load my level file from the load screen state and then I changed how my collision worked. So now it all works without lag. Many thanks everyone for the help!
  10. Elit3d

    Level loading causing lag/stutter

      I'll try to be more specific, and yes most likely I will have a loading screen(I also tried to load the maps using the loading screen state but that doesn't allow my collision to work). So after level 1 is finished, I would like to then load level 2 but because the setup already calls level 1 to begin with I cant set the next level as its not updating, wouldn't I need to handle the level changing in the update rather than the setup function? However doing this method in the Update function lags my game as it is constantly loading the LoadFromFile(std::string filename) function over and over.
  11. So I am loading a tiled map file using TinyXML but the only problem is, if I load the files in update the map is unplayable due to lag, yet if I wanted multiple levels, this is the only possible way to do it, I can't load the files in setup because it won't update to the new map. Any ideas on how to successfully do this?   Level.cpp bool Level::LoadFromFile(std::string filename) { TiXmlDocument levelFile(filename.c_str()); if (!levelFile.LoadFile()) { std::cout << "Loading level \"" << filename << "\" failed." << std::endl; return false; } //Map element. This is the root element for the whole file. TiXmlElement *map; map = levelFile.FirstChildElement("map"); //Set up misc map properties. width = atoi(map->Attribute("width")); height = atoi(map->Attribute("height")); tileWidth = atoi(map->Attribute("tilewidth")); tileHeight = atoi(map->Attribute("tileheight")); //Tileset stuff TiXmlElement *tilesetElement; tilesetElement = map->FirstChildElement("tileset"); firstTileID = atoi(tilesetElement->Attribute("firstgid")); //Tileset image TiXmlElement *image; image = tilesetElement->FirstChildElement("image"); std::string imagepath = image->Attribute("source"); if (!tilesetImage.LoadFromFile(imagepath))//Load the tileset image { std::cout << "Failed to load tile sheet." << std::endl; return false; } tilesetImage.CreateMaskFromColor(sf::Color(255, 0, 255)); tilesetImage.SetSmooth(false); //Columns and rows (of tileset image) int columns = tilesetImage.GetWidth() / tileWidth; int rows = tilesetImage.GetHeight() / tileHeight; std::vector <sf::Rect<int> > subRects;//container of subrects (to divide the tilesheet image up) //tiles/subrects are counted from 0, left to right, top to bottom for (int y = 0; y < rows; y++) { for (int x = 0; x < columns; x++) { sf::Rect <int> rect; rect.Top = y * tileHeight; rect.Bottom = y * tileHeight + tileHeight; rect.Left = x * tileWidth; rect.Right = x * tileWidth + tileWidth; subRects.push_back(rect); } } //Layers TiXmlElement *layerElement; layerElement = map->FirstChildElement("layer"); while (layerElement) { Layer layer; if (layerElement->Attribute("opacity") != NULL)//check if opacity attribute exists { float opacity = strtod(layerElement->Attribute("opacity"), NULL);//convert the (string) opacity element to float layer.opacity = 255 * opacity; } else { layer.opacity = 255;//if the attribute doesnt exist, default to full opacity } //Tiles TiXmlElement *layerDataElement; layerDataElement = layerElement->FirstChildElement("data"); if (layerDataElement == NULL) { std::cout << "Bad map. No layer information found." << std::endl; } TiXmlElement *tileElement; tileElement = layerDataElement->FirstChildElement("tile"); if (tileElement == NULL) { std::cout << "Bad map. No tile information found." << std::endl; return false; } int x = 0; int y = 0; while (tileElement) { int tileGID = atoi(tileElement->Attribute("gid")); int subRectToUse = tileGID - firstTileID;//Work out the subrect ID to 'chop up' the tilesheet image. if (subRectToUse >= 0)//we only need to (and only can) create a sprite/tile if there is one to display { sf::Sprite sprite;//sprite for the tile sprite.SetImage(tilesetImage); sprite.SetSubRect(subRects[subRectToUse]); sprite.SetPosition(x * tileWidth, y * tileHeight); sprite.SetColor(sf::Color(255, 255, 255, layer.opacity));//Set opacity of the tile. //add tile to layer layer.tiles.push_back(sprite); } tileElement = tileElement->NextSiblingElement("tile"); //increment x, y x++; if (x >= width)//if x has "hit" the end (right) of the map, reset it to the start (left) { x = 0; y++; if (y >= height) { y = 0; } } } layers.push_back(layer); layerElement = layerElement->NextSiblingElement("layer"); } //Objects TiXmlElement *objectGroupElement; if (map->FirstChildElement("objectgroup") != NULL)//Check that there is atleast one object layer { objectGroupElement = map->FirstChildElement("objectgroup"); while (objectGroupElement)//loop through object layers { TiXmlElement *objectElement; objectElement = objectGroupElement->FirstChildElement("object"); while (objectElement)//loop through objects { std::string objectType; if (objectElement->Attribute("type") != NULL) { objectType = objectElement->Attribute("type"); } std::string objectName; if (objectElement->Attribute("name") != NULL) { objectName = objectElement->Attribute("name"); } int x = atoi(objectElement->Attribute("x")); int y = atoi(objectElement->Attribute("y")); int width = atoi(objectElement->Attribute("width")); int height = atoi(objectElement->Attribute("height")); Object object; object.name = objectName; object.type = objectType; sf::Rect <int> objectRect; objectRect.Top = y; objectRect.Left = x; objectRect.Bottom = y + height; objectRect.Right = x + width; if (objectType == "solid") { solidObjects.push_back(objectRect); } object.rect = objectRect; TiXmlElement *properties; properties = objectElement->FirstChildElement("properties"); if (properties != NULL) { TiXmlElement *prop; prop = properties->FirstChildElement("property"); if (prop != NULL) { while(prop) { std::string propertyName = prop->Attribute("name"); std::string propertyValue = prop->Attribute("value"); object.properties[propertyName] = propertyValue; prop = prop->NextSiblingElement("property"); } } } objects.push_back(object); objectElement = objectElement->NextSiblingElement("object"); } objectGroupElement = objectGroupElement->NextSiblingElement("objectgroup"); } } else { std::cout << "No object layers found..." << std::endl; } return true; }
  12. Elit3d

    Tinyxml C++ Grabbing Id

      Oh so by using the map structure, it will allow me to print out the text along with the next id to make a simple id handler in a way? Thanks I will try this and maybe will paste my code here if anyone else stumbles on this post!
  13. So I am using tinyxml with c++ and its my first time really parsing with it and xml. Is it possible to, when the text element is pressed, grab the goto attribute and set the dialog id to that attribute. Can't seem to figure out a way to grab the id when the text element is clicked. <dialogtree> <dialog id='0'> <name>Boss</name> <message name="Boss" text="Testing the dialog system!"></message> <responses> <response condition="True" goto='1'> response text </response> <response condition="False" goto='2'> response text </response> <response condition="False" goto='2'> response text </response> </responses> </dialog> <dialog id='1'> <name>Player</name> <message name="Boss" text="Next message"></message> <responses> <response condition="True" goto='3'> response text </response> <response condition="False" goto='4'> response text </response> <response condition="False" goto='4'> response text </response> </responses> </dialog> </dialogtree>
  14. Elit3d

    Xml Dialog Tree For C++

    My main issue I am having right now is figuring out how to do tree traversal stuff with my XML, eg. nextDialogue='2' should send me to <dialog id='2'>   any ideas on this?
  15. Elit3d

    Xml Dialog Tree For C++

      People seem to be recommending TinyXML in there which is what I am currently using.     I am not sure, I have seen a few dialogs done in XML however, it isnt very common, I see more LUA stuff with dialog which could be a second option
  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!