Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Error regarding VertexArray  (Read 2790 times)

0 Members and 2 Guests are viewing this topic.

BurnEmDown

  • Newbie
  • *
  • Posts: 5
    • View Profile
Error regarding VertexArray
« on: January 20, 2019, 01:29:01 pm »
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!

BurnEmDown

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error regarding VertexArray
« Reply #1 on: January 21, 2019, 11:32:49 am »
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Error regarding VertexArray
« Reply #2 on: January 21, 2019, 12:46:27 pm »
Is the texture loaded correctly? As you're not checking the return value of loadFromFile, the texture my not be loading.

Generally hard to say what the problem is, without a complete and minimal code example.
Is the books source code publicly available?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BurnEmDown

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error regarding VertexArray
« Reply #3 on: January 21, 2019, 04:43:29 pm »
I believe the book's source code is publicly available, but only to packt's registered users (the registration is free). After you register, you can click on "Code files" here: https://www.packtpub.com/game-development/beginning-c-game-programming and download the files.
I've gone through it and everything I've written seems to be matching what they have in the source code files.
I'm not sure how can I test if the texture is loaded correctly. Would it be of any help if I upload all of my code files here?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Error regarding VertexArray
« Reply #4 on: January 21, 2019, 05:25:57 pm »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BurnEmDown

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error regarding VertexArray
« Reply #5 on: January 21, 2019, 05:33:21 pm »
Thanks, I've tried what you suggested, and the application didn't exit without me pressing the escape key. The game loaded as usual, and as soon as I pressed any of the keys, only my character was visible on the screen, with a black background as before.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Error regarding VertexArray
« Reply #6 on: January 22, 2019, 01:22:09 pm »
Attach a debugger and find out if all the values are set to what you expect them to be. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BurnEmDown

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error regarding VertexArray
« Reply #7 on: January 22, 2019, 07:47:49 pm »
Thanks, it worked!! I've never used the debugger tool with visual studio before, so it was a little hard to understand what was going on, but then I found out that I did make a mistake in the main ZombieArena.cpp file, which I didn't notice before  ::), it was assigning arena.width = 500 twice instead of assigning it once and arena.height = 500 once  :o
Thanks again for all the help!  ;D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Error regarding VertexArray
« Reply #8 on: January 22, 2019, 08:10:14 pm »
I glad you could figure it out and learned a bit how to use a debugger. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything