Hey All,
I'm a game dev student that is working with SFML(2.4.2) for the first time.
Context:
I'm making a simple map editor for a terrible frogger clone, that runs off of a 2D array of a class I created, where SFML is drawing a bunch of sprites to fill the screen. I've made another sprite of a crocodile that I need to layer on the top of my water sprites.
So my program is actually working fine logic wise. I have no errors or anything and the crocodile sprites are being drawn in the right location on the right tiles only and everything, but they're layered behind my first set of sprites, so normally they are invisible to the user.
I've made my other layer of sprites partially transparent to make sure of this. I've messed around with changing the draw order of my sprites among other things with no success.
Any advice on how I can get my crocodile sprite to draw over the top of my tile map? I'm open to everything (please be gentle though I only started working with SFML/c++ about 6 weeks ago I'm not too good at this yet)
If you want to take a look at the whole C++ project heres a google drive link (no viruses i promise, though google may disagree):
https://drive.google.com/file/d/1ovWeXVrxQS5gxUiUWnbqWVc-DFLZZ3fn/view?usp=sharingCode below:
int main()
{
// Creating Textures
Texture asphaltTile;
if (!asphaltTile.loadFromFile("AsphaltTile.png")) { }
Texture grassTile;
if (!grassTile.loadFromFile("GrassTile.png")) { }
Texture waterTile;
if (!waterTile.loadFromFile("WaterTile.png")) { }
Texture crocTile;
if (!crocTile.loadFromFile("CrocTile.png")) { }
// Colours for showing/hiding croc
Color invisCroc(0, 0, 0, 0);
Color visCroc(255, 255, 255, 255);
RenderWindow window(VideoMode(1024, 576), "Legally Distinct Frogger Level Editor"); // making render window
Grid theMap = Grid(); // declaring and constructing grid + filling with tiles
theMap.PrintLayout(theMap);
bool isLeftMouseClicked = false; // bool used for mouse clicking
bool isRightMouseClicked = false; // bool used for mouse clicking
// run the program as long as the window is open
while (window.isOpen())
{
if (!Mouse::isButtonPressed(Mouse::Left)) { // if mouse is released allow mouse to be clicked again
isLeftMouseClicked = false;
}
if (!Mouse::isButtonPressed(Mouse::Right)) { // if mouse is released allow mouse to be clicked again
isRightMouseClicked = false;
}
// check all the window's events that were triggered since the last iteration of the loop
Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == Event::Closed)
window.close();
}
for (int i = 0; i < 18; i++) { // drawing sprites
for (int j = 0; j < 32; j++) {
Sprite newSprite; // declaring sprite to draw
newSprite.setPosition(j * 32, i * 32);
Sprite crocSprite; // declaring sprite to draw
crocSprite.setPosition(j * 32, i * 32);
// Changing texture based on which tile type is selected for current tile
switch (theMap.map
[j].myType) {
case TileType::Asphalt:
newSprite.setTexture(asphaltTile);
newSprite.setColor(Color(255, 255, 255, 128)); // transparent tiles for testing
break;
case TileType::Grass:
newSprite.setTexture(grassTile);
newSprite.setColor(Color(255, 255, 255, 128));
break;
case TileType::Water:
newSprite.setTexture(waterTile);
newSprite.setColor(Color(255, 255, 255, 128));
break;
}
// if water tile has croc enabled, draw a croc | otherwise set sprite to be invisible
if (theMap.map[j].hasCroc && theMap.map[j].myType == TileType::Water) {
crocSprite.setTexture(crocTile);
crocSprite.setColor(visCroc);
}
else {
crocSprite.setColor(invisCroc);
}
window.draw(crocSprite);
window.draw(newSprite);
if (Mouse::isButtonPressed(Mouse::Left) && !isLeftMouseClicked) {
// When the mouse is clicked, disables the mouse click with bool to prevent double clicks
// and sets the clicked tile to the next tile type
if (newSprite.getGlobalBounds().contains(window.mapPixelToCoords(Vector2i(event.mouseButton.x, event.mouseButton.y)))) {
cout << "Left Click Detected" << endl;
// changing tile's type to new one when clicked
switch (theMap.map[j].myType) {
case TileType::Asphalt:
theMap.map[j].myType = TileType::Water;
break;
case TileType::Grass:
theMap.map[j].myType = TileType::Asphalt;
break;
case TileType::Water:
theMap.map[j].myType = TileType::Grass;
break;
}
isLeftMouseClicked = true;
}
}
if (Mouse::isButtonPressed(Mouse::Right) && !isRightMouseClicked) {
// When the mouse is clicked, disables the mouse click with bool to prevent double clicks
// and sets a croc to be drawn if a water tile is clicked
if (newSprite.getGlobalBounds().contains(window.mapPixelToCoords(Vector2i(event.mouseButton.x, event.mouseButton.y)))) {
cout << "Right Click Detected" << endl;
if (theMap.map[j].hasCroc) {
theMap.map[j].hasCroc = false;
}
else if (theMap.map[j].myType == TileType::Water) {
theMap.map[j].hasCroc = true;
}
isRightMouseClicked = true;
}
}
}
}
window.display();
}
}