-
I recently decided to dive into game development using SFML, and thought that the Arkanoid clone tutorial would be a good place to start. However, I keep on getting errors in the code, and even downloading the source gives me these same errors.
The code as I have it is:
#ifdef SFML_STATIC
#pragma comment(lib, "glew.lib")
#pragma comment(lib, "freetype.lib")
#pragma comment(lib, "jpeg.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")
#endif // SFML_STATIC
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
const int windowWidth{ 800 }, windowHeight{ 600 };
int main()
{
RenderWindow window{ { windowWidth, windowHeight }, "Arkanoid - 1" };
window.setFramerateLimit(60);
while (true)
{
window.clear(Color::Black);
if (Keyboard::isKeyPressed(Keyboard::Key::Escape)) break;
window.display();
}
return 0;
}
The errors I received from the VS2013 are :
- 1: Line 16 - int should be preceded by a ;
- 2: Line 16 - missing type specifier, int assumed
- 3: Line 20 - Element '1' - conversion from 'initializer-list' to 'sf::VideoMode' requires a narrowing conversion
- 4: Line 20 - same, but from 'int' to 'unsigned int'
- 5: Line 20 - same as 4, but applying to Element '2'
I was able to fix the two errors for line sixteen (the const expr) by changing it to simply a const. I would like to know how to keep from having to do this, however.
Line 20 in my code (in case it doesn't show up) is the RenderWindow line, in case that helps.
Thank you!
-
Try creating the window using it's constructor rather than an initializer list.
This code is from the tutorial:
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
-
That works, as does simply replacing the windowWidth and windowHeight with 800 and 600 respectively in the original line. I still am not sure why using the const values causes an error on my machine, though.
-
Have you tried using that line from the tutorial but used your constants for the dimensions? (i.e. use the constructor and use the sf::VideoMode part)
-
Do you mean like this?
RenderWindow window{ VideoMode{ windowWidth, windowHeight }, "Arkanoid - 1" };
If so, yes, and it didn't work. It gave the same errors.
-
No, no. I mean using the constructor (parentheses), not initializer lists (braces).
Does this work?
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Arkanoid - 1");
-
Thank you! That made it work. Tutorial still confuses me, but as long as the code is working, I guess.
-
I just played around with your constants/window creation code and it turns out that the only thing that you would need to change in it would be to make your constants the right type :p (i.e. unsigned int instead of int)
const unsigned int windowWidth{ 800 }, windowHeight{ 600 };
Also, you're welcome :)
EDIT:
This works too and looks tidier ;)
using namespace sf;
const VideoMode windowSize{ 800, 600 };
// ...
RenderWindow window{ windowSize, "Arkanoid - 1" };