Hello everyone, I've recently started learning C++ game programming with a book called:
"Beginning C++ game programming", which uses SFML. I've followed the book's instructions and am currently in page 229, in the ZombieArena project.
For some reason, the code that they give in the book doesn't work. First, in page 221, they instruct to create a ZombieArena.h file which includes only these lines:
#pragma once
using namespace sf;
int createBackground(VertexArray& rVA, IntRect arena);
Then for the next few pages they instruct how to code the createBackground function in a 'CreateBackground.cpp' file.
After that, in page 227, they instruct to return to the main file, the ZombieArena.cpp file, and make use of the createBackground function, with these few lines, first at the top of the file:
// Create the backgroundVertexArray background;
// Load the texture for our background vertex array
Texture textureBackground;
textureBackground.loadFromFile("graphics/background_sheet.png");
And then inside the game loop:
// Pass the vertex array by reference
// to the createBackground function
int tileSize = createBackground(background, arena);
and:
// Draw the background
window.draw(background, &textureBackground);
However, I'm received several errors:
First, they declare a Texture called textureBackground but try to use one called background, I've fixed this by changing background to textureBackground.
Then, in the header file, visual studio doesn't recognize VertexArray, I've fixed it by adding an #include statement for SFML/graphics.
Then, it seems that they suggest sending a Texture into the createBackground object, even though it accepts only a VertexArray reference. I've looked online a bit and following the example from here:
https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php
Found that I can eliminate the errors by adding these lines of code:
VertexArray vertices;
RenderStates states;
states.texture = &textureBackground;
After declaring the textureBackground Texture, and then calling createBackground like so:
int tileSize = createBackground(vertices, arena);
and window.draw like so:
window.draw(vertices, states);
However, after running the game, I still don't see any background, only the player. I can't figure out how to fix this issue, and the book doesn't help at all with it, nor can I find any useful information on the internet. Any help will be appreciated, thanks in advance!
After checking the book's errata section, I saw that they've reported the error, and the fix is as follows:
It is: // Create the backgroundVertexArray background;
Should be:
// Create the background
VertexArray background;
After applying the necessary changes (and removing the no longer needed 'vertices' and 'states' variables), I manage to get a no-error code, but when I start the game, I still don't see any background, only a black screen with my character on it. I'm going to go through all of the code I've written so far and check if I've made any mistake.
Oh turns out I actually have a digital copy of the book and thus also access to the code.
My point is, that if the texture image isn't in the right location and loadFromFile fails, then the code provided by the book won't detect that.
Best to wrap the function call into an if, something like:
if(!textureBackground.loadFromFile("graphics/background_sheet.png"))
{
return -1;
}
If the application now closes, it means that the texture failed to load, which probably means that the texture is in the wrong place.