Search code examples
c++cfiletexturessfml

How to apply a texture from a function in sfml


I am trying to apply a texture from a function but it doesn't seem to be working so how do I apply a texture to a function in sfml? I also get a error when I try to apply a texture from a function which is: button.h|18|error: no matching function for call to 'sf::RectangleShape::setTexture(sf::Texture&)'|. You can solve this problem by finding a way to apply a texture from a function or by fixing the error and it applies the texture. I have 2 files, one called main.cpp and another one called button.h. Here is their code.

main.cpp

#include <SFML/Graphics.hpp>
#include <iostream>
#include <windows.h>
#include "button.h"
using namespace std;
using namespace sf;
int main(){
    bool isStartOpen = false;
    RenderWindow window(VideoMode(500, 500), "Operating System", Style::Close | Style::Resize);
    RectangleShape player(Vector2f(500.0f, 30.0f));
    RectangleShape startMenu(Vector2f(300.0f, 400.0f));
    Texture startTexture;
    if(!startTexture.loadFromFile("startButton.png")){
        MessageBox(NULL, "Error loading image file: startButton.png in the system", "Image File Error", MB_OK | MB_ICONERROR);
    }
    startMenu.setFillColor(Color(0, 0, 0, 200));
    startMenu.setPosition(0.0f, 500.0f);
    player.setFillColor(Color::Black);
    player.setPosition(0.0f, 473.0f);
    Font font;
    if(!font.loadFromFile("Calibri Regular.ttf")){
       MessageBox(NULL, "Error loading font file: buttonFont.ttf in the system", "Font File Error", MB_OK | MB_ICONERROR);
    }
    Button openNotepad("Notepad", {90, 90}, 20, Color::Green, startTetxure);
    openNotepad.setPosition({100, 300});
    openNotepad.setFont(font);
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            switch(event.type){
                case Event::Closed:
                    window.close();
                    cout << "Window has been removed" << endl;
                    break;
                case Event::MouseMoved:
                    if(openNotepad.isMouseOver(window)){
                        openNotepad.setBackColor(Color(42,150,83));
                        openNotepad.setOutThick(1.5);
                        openNotepad.setOutColor(Color(255, 255, 255, 100));
                    }else{
                        openNotepad.setBackColor(Color(0,110,51));
                        openNotepad.setOutThick(0);
                    }
                    break;
                case Event::MouseButtonPressed:
                    if(openNotepad.isMouseOver(window)){
                        cout << "You clicked the button" << endl;
                    }
                    break;
            }
        if(Keyboard::isKeyPressed(Keyboard::Key::S) && !isStartOpen){
            startMenu.setPosition(0.0f, 75.0f);
            isStartOpen = true;
        }else{
            startMenu.setPosition(0.0f, 500.0f);
            isStartOpen = false;
        }
        window.draw(startMenu);
        window.draw(player);
        openNotepad.drawTo(window);
        window.display();
        window.clear(Color(1, 159, 255));
    }
}
}

button.h

#pragma once
#include <iostream>
#include <SFML/graphics.hpp>
using namespace sf;
using namespace std;
class Button{
    public:
        Button(){

        }
        Button(string t, Vector2f size, int charSize, Color bgColor, Texture &texture){
            text.setString(t);
            text.setColor(Color::White);
            text.setCharacterSize(charSize);
            text.setScale(0.7f, 0.7f);
            button.setSize(size);
            button.setFillColor(bgColor);
            button.setTexture(texture);
        }
        void setFont(Font &font){
            text.setFont(font);
        }
        void setBackColor(Color color){
            button.setFillColor(color);
        }
        void setTextColor(Color color){
            text.setColor(color);
        }
        void setPosition(Vector2f pos){
            button.setPosition(pos);
            float xPos = (pos.x + button.getLocalBounds().width / 30) - (text.getLocalBounds().width / 2);
            float yPos = (pos.y + button.getLocalBounds().height / 1.3) - (text.getLocalBounds().height / 2);
            text.setPosition({xPos, yPos});
        }
        void drawTo(RenderWindow &window){
            window.draw(button);
            window.draw(text);
        }
        void setOutColor(Color outColor){
            button.setOutlineColor(outColor);
        }
        void setOutThick(float outThick){
            button.setOutlineThickness(outThick);
        }
        bool isMouseOver(RenderWindow &window){
            float mouseX = Mouse::getPosition(window).x;
            float mouseY = Mouse::getPosition(window).y;
            float btnPosX = button.getPosition().x;
            float btnPosY = button.getPosition().y;
            float btnxPosWidth = button.getPosition().x + button.getLocalBounds().width;
            float btnyPosWidth = button.getPosition().y + button.getLocalBounds().height;
            if(mouseX < btnxPosWidth && mouseX > btnPosX && mouseY < btnyPosWidth && mouseY > btnPosY){
                return true;
            }
            return false;
        }
    private:
       RectangleShape button;
       Text text;
};

Solution

  • Let us dissect the error.

    button.h|18|error: no matching function for call to 'sf::RectangleShape::setTexture(sf::Texture&)'

    This means that you are trying to call a function that does not exist.

    What function are we trying to call? It looks like we are trying to call the setTexture function inside of sf::RectangleShape.

    Let's look at the documentation for the function of the same name in sf::RectangleShape.

    This function takes const Texture* texture as its first parameter. Let's look at the end of the error message again. It is trying to find a function that takes sf::Texture& as a parameter. The type it expects and the type you supply are different. sf::Texture& does not equal const Texture*, and there are no overloaded functions that take sf::Texture& as a parameter.

    The solution is to get a const Texture* from a sf::Texture&.

    The former is known as a pointer, (specifically a const pointer), and the latter is a reference (specifically a non-const reference).
    You can get a pointer from a reference by using the address-of operator (&). So by calling &texture you can get the non-const pointer of the texture. The reverse can be done with the indirection operator (*).
    A non-const pointer/reference can be implicitly changed to a const pointer/reference. This is one way and the reverse is not true.