Compile error in SFML 2.5.1 sample code in Linux - c++

Compile error
I was looking at a configuration video in yt for linux mint but it doesn't give me the same results in arch.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}

As the error message indicate, there is no constructor for sf::VideoMode which takes two int input for width and height. The constructor expect an argument of type Vector2u.
Create the Window as follows:
sf::RenderWindow window(sf::VideoMode({200, 200}), "SFML works!");

Related

SDL_FINGERDOWN not registering correctly

I don't why SDL_FINGERDOWN records
even simple touch event. It should register on Sliding finger down as name says, right? I use android N for testing.
Here is my code
#include "SDL.h"
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
int r, g, b;
r = g = b = 0;
SDL_Window *window;
window = SDL_CreateWindow("S", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 1320, SDL_WINDOW_RESIZABLE);
SDL_Surface *screen = SDL_GetWindowSurface(window);
Uint32 white = SDL_MapRGB(screen->format, 255, 255, 255);
SDL_FillRect(screen, NULL, white);
SDL_UpdateWindowSurface(window);
SDL_Event event;
bool running = true;
while (running)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_FINGERDOWN)
{
running = false;
break;
}
//end if
} //end inner while
}
SDL_DestroyWindow(window);
SDL_Quit();
}
You have misunderstood meaning of SDL_FINGERDOWN and SDL_FINGERUP.
When you land your finger on touch screen then that's event of type SDL_FINGERDOWN.When you lift up your finger from touch screen then that's event of type SDL_FINGERUP.

Strange behaviour when drawing convex shape figure in SFML

I'm drawing a convex shape figure using SFML library:
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(400, 400), "SFML window");
ConvexShape convex;
convex.setPointCount(8);
convex.setPoint(0, sf::Vector2f(0, 0));
convex.setPoint(1, sf::Vector2f(180, 0));
convex.setPoint(2, sf::Vector2f(180, 90));
convex.setPoint(3, sf::Vector2f(100, 90));
convex.setPoint(4, sf::Vector2f(100, 180));
convex.setPoint(5, sf::Vector2f(30, 180));
convex.setPoint(6, sf::Vector2f(30, 90));
convex.setPoint(7, sf::Vector2f(0, 90));
convex.setPosition(100, 100);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(convex);
window.display();
}
return EXIT_SUCCESS;
}
The points defined in clockwise order and everything is ok!
convex1
But when I change two coordinates a little bit I'm getting something that I really don't expect:
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(400, 400), "SFML window");
ConvexShape convex;
convex.setPointCount(8);
convex.setPoint(0, sf::Vector2f(0, 0));
convex.setPoint(1, sf::Vector2f(180, 0));
convex.setPoint(2, sf::Vector2f(180, 90));
convex.setPoint(3, sf::Vector2f(100, 90));
convex.setPoint(4, sf::Vector2f(100, 200)); // CHANGED 180 to 200
convex.setPoint(5, sf::Vector2f(30, 200)); // CHANGED 180 to 200
convex.setPoint(6, sf::Vector2f(30, 90));
convex.setPoint(7, sf::Vector2f(0, 90));
convex.setPosition(50, 50);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(convex);
window.display();
}
return EXIT_SUCCESS;
}
I'm getting the following window:
convex2
Why this situation take place?
These coordinates are related to the points in your Convex shape, you should use those only to change the format of the object. In order to move your entire object you should use this:
convex.move(x Position, y Position);

Rotate an axis aligned rectangle by a 90 degrees

I have rectangle object (sf::IntRect) , with attributes: left, top, width and height on a 2D plane. I want to rotate it by a multiple of 90 degrees (that is 90, 180 or 270), around point (0,0). Thus I am writing a function like this:
void rotateRect(sf::IntRect& rect, int rot)
{
//code I need
}
the rotation is either 0 (0), 1 (90), 2 (180) or 3 (270).
How can I achieve this as simply as possible.
Here is a basic solution using sf::Tranform on sf::FloatRect:
constexpr auto rotationAngle(int rot) { return rot * 90.f; }
void rotateRect(sf::IntRect& rect, int rot)
{
auto deg = rotationAngle(rot);
auto transform = sf::Transform();
transform.rotate(deg);
// Would be better if rect was a FloatRect...
auto rectf = sf::FloatRect(rect);
rectf = transform.transformRect(rectf);
rect = static_cast<sf::IntRect>(rectf);
}
However, I personally would slightly change the signature of your function to use float rects and a more compact notation:
sf::FloatRect rotateRect(sf::FloatRect const& rect, int rot)
{
return sf::Transform().rotate(rotationAngle(rot)).transformRect(rect);
}
And below a full example showing how it behaves.
#include <SFML/Graphics.hpp>
constexpr auto rotationAngle(int rot) { return rot * 90.f; }
sf::FloatRect rotateRect(sf::FloatRect const& rect, int rot)
{
return sf::Transform().rotate(rotationAngle(rot)).transformRect(rect);
}
void updateShape(sf::RectangleShape& shape, sf::FloatRect const& rect)
{
shape.setPosition(rect.left, rect.top);
shape.setSize({ static_cast<float>(rect.width), static_cast<float>(rect.height) });
}
int main(int, char const**)
{
sf::RenderWindow window(sf::VideoMode(500, 500), "rotate");
auto rect = sf::FloatRect(0, 0, 100, 50);
auto shape = sf::RectangleShape();
shape.setFillColor(sf::Color::Red);
updateShape(shape, rect);
auto view = window.getView();
view.move({ -250, -250 });
window.setView(view);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::R)
{
rect = rotateRect(rect, 1);
updateShape(shape, rect);
}
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::N)
{
rect = sf::FloatRect(50, 50, 100, 50);
updateShape(shape, rect);
}
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::M)
{
rect = sf::FloatRect(0, 0, 100, 50);
updateShape(shape, rect);
}
}
window.clear();
window.draw(shape);
window.display();
}
return EXIT_SUCCESS;
}
NB: I used a few trick from C++14 but I'm sure you can convert that code to C++11/C++98 if you need to.

Repeating texture to fit certain size in SFML

I have a file that contains textures. I load it to sf::Texture and split into sprites with setTextureRect.
Now lets say one sprite contains part of texture that is 20 pixels wide. How can I render it to fit width of e.g. 213 pixels. The only way I can think about is to render it to sf::RenderTexture and crop it with another sprite.
Is there a better way to do this?
You can use sf::Texture::setRepeated to do that.
However, you'll need to copy that part of your bigger image into an independant texture.
Here is an example:
#include <SFML/Graphics.hpp>
int main(int, char const**)
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
sf::Image img;
img.create(20, 20);
for (auto i = 0; i < 20; ++i) {
for (auto j = 0; j < 20; ++j) {
img.setPixel(i, j, sf::Color(255 * i / 20, 255 * j / 20, 255 * i / 20 * j / 20));
}
}
sf::Texture texture;
texture.loadFromImage(img);
texture.setRepeated(true);
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect({ 0, 0, 800, 600 });
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
window.clear();
window.draw(sprite);
window.display();
}
return EXIT_SUCCESS;
}

Image doesn't load to window

Solution:
Works when writing the whole image adress instead of only the name
I know I asked a similar question recently but I haven't got a solution yet. I'm trying to load a simple bmp picture to a window in c++ and SDL. This is my code:
int main(int argc, char *argv[])
{
//init
SDL_Init( SDL_INIT_EVERYTHING );
//screen window
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_SWSURFACE);
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
//image
SDL_Surface *background = IMG_Load("images.png");
SDL_Event event;
bool gameRunning = true;
//loop
while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
}
SDL_BlitSurface(background, NULL, screen, NULL);
//update screen
SDL_Flip(screen);
}
//quit
SDL_Quit();
return 0;
}
Is there anything wrong with my code or can be a problem with my computer? I know I have placed the image in the project folder and spelt it the right way.