Here is my example program using SFML library in C++. I want to create a custom function 'draw_func' which draws something (like a rectangle in this example) at provided coordinates. I set the type of the return variable as an sfml object rectangle (which is what I return and what I draw) but the screen is black.
#include <iostream>
#include <math.h>
#include "SFML/OpenGL.hpp"
#include <SFML/Graphics.hpp>
sf::RectangleShape draw_func(int x, int y)
{
sf::RectangleShape rect(sf::Vector2f(200, 100));
rect.setPosition(x, y);
rect.setFillColor(sf::Color((0, 0, 255)));
return rect;
}
int main()
{
int height = 400;
int length = 400;
int pos_x = 0;
int pos_y = 0;
sf::RenderWindow window(sf::VideoMode(length, height), "My window");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
sf::RectangleShape rectangle = draw_func(pos_x, pos_y);
window.draw(rectangle);
window.display();
}
}
I think the problem is with this statemen here:
rect.setFillColor(sf::Color((0, 0, 255)));
The double parentheses actually resolves to a single value, 0 because:
sf::Color((0, 0, 255))
constructs a sf::Color with the value 0 because
(0, 0, 255)
is not function parameters because of the extra parentheses it is an expression involving the comma operator:
0, 0, 255
The comma operator always has the value of its left most expression. In this case 0.
Now sf::Color has a constructor that takes a single value:
sf::Color(Uint32 color);
You are creating black sf::Color(0).
Related
I have to make Quarto game as a GUI program so firstly I wanted to make buttons in menu, but I don't know why it don't fill color when my mouse is at the button.
Code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include<bits/stdc++.h>
using namespace std;
using namespace sf;
class button{
public:
bool isClicked=false;
int Yposition;
int Xposition;
bool isMouseOn=false;
};
int main(){
//Defining variables
Vector2i mouseWindowPosition;
//rendering window "window"
RenderWindow window(VideoMode(800,600), "Quarto game - menu", Style::Default);
window.display();
//setting vsync
window.setVerticalSyncEnabled(true);
//loading textures
Texture backgroundTexture;
backgroundTexture.loadFromFile("Resources/background.png");
//making sprites
Sprite backgroundSprite;
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setPosition(0,0);
//making buttons and their colors
RectangleShape playButton(Vector2f(200.f,50.f));
playButton.setFillColor(Color(128,128,128));
playButton.setOutlineThickness(5.f);
playButton.setOutlineColor(Color(100,100,100));
button play;
//setting position of buttons
play.Xposition=70;
play.Yposition=200;
//game loop
while(window.isOpen()){
Event event;
playButton.setFillColor(Color(128,128,128));
play.isMouseOn=false;
while(window.pollEvent(event)){
if(event.type==Event::Closed){
window.close();
}
}
//Getting mouse position
mouseWindowPosition=Mouse::getPosition(window);
if(mouseWindowPosition.x<=play.Xposition && mouseWindowPosition.y<=play.Yposition && mouseWindowPosition.x>=play.Xposition+200 && mouseWindowPosition.y>=play.Yposition+50){
play.isMouseOn=true;
}
//Drawing to screen
window.clear();
window.draw(backgroundSprite);
if(play.isClicked==false){
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
}
if(play.isMouseOn==true){
playButton.setFillColor(Color(128,128,128));
}
window.display();
}
}
Is there any better way to make buttons in sfml?
The immediate reason why the button doesn't fill is your if the statement has the signs reversed for checking the box. Using this if statement instead should work:
//Getting mouse position
mouseWindowPosition=Mouse::getPosition(window);
if (mouseWindowPosition.x>=play.Xposition && mouseWindowPosition.y>=play.Yposition &&
mouseWindowPosition.x<=play.Xposition+200 && mouseWindowPosition.y<=play.Yposition+50){
//your code here
}
If you are not already aware, the x and y values are arranged like a table where positive y goes down instead of a cartesian coordinate system where positive y is up.
The other problem is the color you are updating your fill with is the same color of the fill, so changing that color will get you your desired fill.
Also, logic wise you should swap the statement
if (play.isMouseOn == true)
{
}
with the statement
if (play.isClicked == false)
{
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
window.display();
}
Here is a working code that changes the fill from grey to red when you hover above it:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
using namespace std;
using namespace sf;
class button
{
public:
bool isClicked = false;
int Yposition;
int Xposition;
bool isMouseOn = false;
};
int main()
{
//Defining variables
Vector2i mouseWindowPosition;
//rendering window "window"
RenderWindow window(VideoMode(800, 600), "Quarto game - menu", Style::Default);
window.display();
//setting vsync
window.setVerticalSyncEnabled(true);
//loading textures
Texture backgroundTexture;
backgroundTexture.loadFromFile("Resources/background.png");
//making sprites
Sprite backgroundSprite;
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setPosition(0, 0);
//making buttons and their colors
RectangleShape playButton(Vector2f(200.f, 50.f));
playButton.setFillColor(Color(100, 100, 100));
playButton.setOutlineThickness(5.f);
playButton.setOutlineColor(Color(100, 100, 100));
button play;
//setting position of buttons
play.Xposition = 70;
play.Yposition = 200;
//game loop
while (window.isOpen())
{
Event event;
playButton.setFillColor(Color(128, 128, 128));
play.isMouseOn = false;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
}
}
//Getting mouse position
mouseWindowPosition = Mouse::getPosition(window);
if (mouseWindowPosition.x >= play.Xposition && mouseWindowPosition.y >= play.Yposition && mouseWindowPosition.x <= play.Xposition + 200 && mouseWindowPosition.y <= play.Yposition + 50)
{
play.isMouseOn = true;
}
//Drawing to screen
window.clear();
window.draw(backgroundSprite);
if (play.isMouseOn == true)
{
playButton.setFillColor(Color(128, 0, 0));
}
if (play.isClicked == false)
{
playButton.setPosition(Vector2f(play.Xposition, play.Yposition));
window.draw(playButton);
window.display();
}
}
}
I don't want to hardcode the position for the card to be in the middle of the screen and we did a project like this without class. So though would be easy to just put what I did to make the card to be in the center but no matter what I did, the card stays at the top left corner.
I even notice at times if I put the rectSize or something the rectangle proportions changes and look like a square when maximizing the screen.
What am I doing wrong?
This is my background cpp file:
#include "background.h"
background::background() : background(450, 750)
{
}
background::background(float x, float y)
{
sf::RenderWindow window;
sf::RectangleShape rectangle;
sf::RectangleShape::setSize({x, y});
// sf::Vector2f center;
//
// sf::RectangleShape::setPosition({});
}
void setPostioning (sf::RenderWindow &window, sf::RectangleShape &rectangle, float x, float y)
{
sf::Vector2f rectSize ={x,y};
rectangle.setSize(rectSize);
sf::Vector2f center;
rectangle.setPosition({
center.x = window.getSize().x/2 - rectSize.x/2,
center.y = window.getSize().y/2 - rectSize.y/2
});
}
This is my header file of what I have done:
#include <SFML/Graphics.hpp>
class background : public sf::RectangleShape
{
public:
background();
background(float x, float y);
void setPostioning(sf::RenderWindow &window, sf::RectangleShape &rectangle, float x, float y);
};
And now this is my main main file
int main()
{
//set up of the window
sf::VideoMode videoMode(1280,1024,32);
sf::RenderWindow window(videoMode, "SFML Tutorial");//window will display name
window.setFramerateLimit(15);//frame rate
background b;
rank r;
Card Joker;
while(window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
//when window is running they can close it with the close button
if (event.type == sf::Event::Closed)
{
window.close();
}
//this if statement will make our card stay in the same ratio no matter what
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window and keep the center
window.setView(sf::View(window.getView().getCenter(),
sf::Vector2f((float) event.size.width, (float) event.size.height)));
}
}
//invoking and set up to be drawn and display on the window when running
window.clear(sf::Color::Black);
window.draw(Joker);
window.draw(r);
window.display();
}
So yes unsure why the position is not being set up or being taken from the window size or maybe it has to do with the rectSize that I did and being misread. I also think it has to do with the x and y as I set them up already with 450 nd 750.
It is tricky to help you without full code, cause I don't know how exactly did you want to use setPostioning.
After a small workaround, It finally appeared in the center of the screen.
Feel free to comment, if my example still doesn't satisfy your needs.
In the header file I added a reference to sf::RenderWindow, to use it in setPostioning.
Updated background.h:
class background : public sf::RectangleShape
{
public:
background(sf::RenderWindow &window);
background(sf::RenderWindow &window, float x, float y);
void setPostioning(float x, float y);
private:
// Added a refence to original window to have possibility use it in setPositioning
sf::RenderWindow& m_window;
};
In .cpp file I removed some redundant refs to sf::RectangleShape (cause you already inherited from it), and to sf::RenderWindod (cause a referene to it is stored inside class).
Updated background.cpp:
background::background(sf::RenderWindow &window) : background(window, 450, 750)
{
}
background::background(sf::RenderWindow &window, float x, float y) : m_window(window)
{
// sf::RectangleShape rectangle;
sf::RectangleShape::setSize({ x, y });
}
void background::setPostioning(float x, float y)
{
sf::Vector2f rectSize = { x,y };
// don't use rectangle from param, because you already inherited from sf::RectangleShape
//rectangle.setSize(rectSize);
setSize(rectSize);
sf::Vector2f center;
// again, don't use rectangle from param, because you already inherited from sf::RectangleShape
//rectangle.setPosition({
setPosition({
center.x = m_window.getSize().x / 2 - rectSize.x / 2,
center.y = m_window.getSize().y / 2 - rectSize.y / 2
});
}
In main function I called setPostioning before the event loop and added window.draw(b); to render your background.
Updated main function:
int main()
{
//set up of the window
sf::VideoMode videoMode(1280, 1024, 32);
sf::RenderWindow window(videoMode, "SFML Tutorial");//window will display name
window.setFramerateLimit(15);//frame rate
background b(window);
// use setPostioning
b.setPostioning(200.f, 200.f);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
//when window is running they can close it with the close button
if (event.type == sf::Event::Closed)
{
window.close();
}
//this if statement will make our card stay in the same ratio no matter what
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window and keep the center
window.setView(sf::View(window.getView().getCenter(),
sf::Vector2f((float)event.size.width, (float)event.size.height)));
}
}
//invoking and set up to be drawn and display on the window when running
window.clear(sf::Color::Black);
window.draw(b);
window.display();
}
}
I have recently been playing around with the C++ sfml library and i tried using vectors to create multiples of the same shape but i simply do not know how to implement vectors with sfml shapes.
Here is the code i am working with, I am attempting to create more BlueTiles Behind the Blue square everytime it moves, but i don't know how. I would also like to know how to shorten the grid segment.
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
int main()
{
// Window Declarations Start
sf::RenderWindow Window(sf::VideoMode(673,673), "TileEat");
Window.setFramerateLimit(5);
sf::Event event;
// Window Declarataions End
// The Blue Start
sf::RectangleShape Blue(sf::Vector2f(32,32));
Blue.setPosition(0,0);
Blue.setFillColor(sf::Color(0,0,255));
sf::RectangleShape BlueTile(sf::Vector2f(32,32));
BlueTile.setFillColor(sf::Color(0,0,255));
std::vector<sf::RectangleShape>BlueTileVector;
// The Blue End
// The Grid Start
sf::RectangleShape VerticalLine(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine2(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine3(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine4(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine5(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine6(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine7(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine8(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine9(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine10(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine11(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine12(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine13(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine14(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine15(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine16(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine17(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine18(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine19(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine20(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine21(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine22(sf::Vector2f(1,700));
VerticalLine.setPosition(32,0);
VerticalLine2.setPosition(64,0);
VerticalLine3.setPosition(96,0);
VerticalLine4.setPosition(128,0);
VerticalLine5.setPosition(160,0);
VerticalLine6.setPosition(192,0);
VerticalLine8.setPosition(224,0);
VerticalLine9.setPosition(256,0);
VerticalLine10.setPosition(288,0);
VerticalLine11.setPosition(320,0);
VerticalLine12.setPosition(352,0);
VerticalLine13.setPosition(384,0);
VerticalLine14.setPosition(416,0);
VerticalLine15.setPosition(448,0);
VerticalLine16.setPosition(480,0);
VerticalLine17.setPosition(512,0);
VerticalLine18.setPosition(544,0);
VerticalLine19.setPosition(576,0);
VerticalLine20.setPosition(608,0);
VerticalLine21.setPosition(640,0);
VerticalLine22.setPosition(672,0);
sf::RectangleShape HorizontalLine(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine2(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine3(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine4(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine5(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine6(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine7(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine8(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine9(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine10(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine11(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine12(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine13(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine14(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine15(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine16(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine17(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine18(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine19(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine20(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine21(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine22(sf::Vector2f(700,1));
HorizontalLine.setPosition(0,32);
HorizontalLine2.setPosition(0,64);
HorizontalLine3.setPosition(0,96);
HorizontalLine4.setPosition(0,128);
HorizontalLine5.setPosition(0,160);
HorizontalLine6.setPosition(0,192);
HorizontalLine7.setPosition(0,224);
HorizontalLine8.setPosition(0,256);
HorizontalLine9.setPosition(0,288);
HorizontalLine10.setPosition(0,320);
HorizontalLine11.setPosition(0,352);
HorizontalLine12.setPosition(0,384);
HorizontalLine13.setPosition(0,416);
HorizontalLine14.setPosition(0,448);
HorizontalLine15.setPosition(0,480);
HorizontalLine16.setPosition(0,512);
HorizontalLine17.setPosition(0,544);
HorizontalLine18.setPosition(0,576);
HorizontalLine19.setPosition(0,608);
HorizontalLine20.setPosition(0,640);
HorizontalLine21.setPosition(0,672);
// The Grid End
// Game Loop Start
while (Window.isOpen())
{
while (Window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
Window.close();
}
// Blue Movement Start
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){Blue.move(0,32);BlueTileVector.push_back(BlueTile);}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){Blue.move(0,-32);}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){Blue.move(32,0);}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){Blue.move(-32,0);}
if(Blue.getPosition().x >= 672){Blue.setPosition(Blue.getPosition().x-32,Blue.getPosition().y);}
else if(Blue.getPosition().x <= -32){Blue.setPosition(Blue.getPosition().x+32,Blue.getPosition().y);}
if(Blue.getPosition().y >= 672){Blue.setPosition(Blue.getPosition().x,Blue.getPosition().y-32);}
else if(Blue.getPosition().y <= -32){Blue.setPosition(Blue.getPosition().x,Blue.getPosition().y+32);}
// Blue Movement End
// Drawing Table Start
Window.clear();
Window.draw(Blue);
Window.draw(VerticalLine);
Window.draw(VerticalLine2);
Window.draw(VerticalLine3);
Window.draw(VerticalLine4);
Window.draw(VerticalLine5);
Window.draw(VerticalLine6);
Window.draw(VerticalLine7);
Window.draw(VerticalLine8);
Window.draw(VerticalLine9);
Window.draw(VerticalLine10);
Window.draw(VerticalLine11);
Window.draw(VerticalLine12);
Window.draw(VerticalLine13);
Window.draw(VerticalLine14);
Window.draw(VerticalLine15);
Window.draw(VerticalLine16);
Window.draw(VerticalLine17);
Window.draw(VerticalLine18);
Window.draw(VerticalLine19);
Window.draw(VerticalLine20);
Window.draw(VerticalLine21);
Window.draw(VerticalLine22);
Window.draw(HorizontalLine);
Window.draw(HorizontalLine2);
Window.draw(HorizontalLine3);
Window.draw(HorizontalLine4);
Window.draw(HorizontalLine5);
Window.draw(HorizontalLine6);
Window.draw(HorizontalLine7);
Window.draw(HorizontalLine8);
Window.draw(HorizontalLine9);
Window.draw(HorizontalLine10);
Window.draw(HorizontalLine11);
Window.draw(HorizontalLine12);
Window.draw(HorizontalLine13);
Window.draw(HorizontalLine14);
Window.draw(HorizontalLine15);
Window.draw(HorizontalLine16);
Window.draw(HorizontalLine17);
Window.draw(HorizontalLine18);
Window.draw(HorizontalLine19);
Window.draw(HorizontalLine20);
Window.draw(HorizontalLine21);
Window.draw(HorizontalLine22);
Window.display();
// Drawing Table End
}
std::cout << "BlueTileVector Size: " << BlueTileVector.size();
return 0;
// Game Loop End
}
Create a vector to hold your objects:
std::vector<sf::RectangleShape> rectangles;
And add elements to your vector in a loop:
// Create 20 rectangle shapes
for (int i = 0; i != 20; ++i)
rectangles.emplace_back(sf::Vector2f(1, 700));
You can also set their position in the above loop or in it's own loop:
for (auto& i : rectangles)
i.setPosition(200.f, 200.f);
And you can also draw the shapes by using a loop:
for (const auto& rect : rectangles)
window.draw(rect);
FYI. The above examples need -std=c++11 flag to compile. Otherwise regular for loop and push_back(sf::RectangleShape(sf::Vector2f(1, 700))) can be used instead.
You may want to learn about loops and containers. They are really helpful:
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
int main()
{
const size_t numberOfRowsAndColunms = 20;
sf::VideoMode videoMode = sf::VideoMode(600, 600);
sf::RenderWindow Window(videoMode, "TileEat");
Window.setFramerateLimit(30);
sf::Event event;
const sf::Vector2f gridSize = sf::Vector2f(videoMode.width / numberOfRowsAndColunms, videoMode.height / numberOfRowsAndColunms);
// The Blue Start
sf::RectangleShape Blue(gridSize);
Blue.setPosition(0, 0);
Blue.setFillColor(sf::Color(0, 0, 255));
std::vector<sf::RectangleShape> shadows;
// The Blue End
std::vector<sf::RectangleShape> grid;
for (int x = 0; x < numberOfRowsAndColunms; x++)
{
for (int y = 0; y < numberOfRowsAndColunms; y++)
{
sf::RectangleShape cell(gridSize);
cell.setFillColor(sf::Color::Transparent);
cell.setOutlineColor(sf::Color::White);
cell.setOutlineThickness(2.0f);
cell.setPosition(gridSize.x * x, gridSize.y * y);
grid.push_back(cell);
}
}
// Game Loop Start
while (Window.isOpen())
{
while (Window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
Window.close();
}
// Blue Movement Start
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
Blue.move(0, gridSize.y);
shadows.push_back(Blue);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
Blue.move(0, -gridSize.y);
shadows.push_back(Blue);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
Blue.move(gridSize.x, 0);
shadows.push_back(Blue);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
Blue.move(-gridSize.x, 0);
shadows.push_back(Blue);
}
// Blue bounds check
auto position = Blue.getPosition();
position.x = std::min(videoMode.width - gridSize.x, std::max(0.0f, position.x));
position.y = std::min(videoMode.height - gridSize.y, std::max(0.0f, position.y));
Blue.setPosition(position);
// Drawing
Window.clear();
int shadowCount = 0;
for (auto shadow : shadows)
{
shadowCount++;
shadow.setScale(sf::Vector2f(shadowCount / (float)shadows.size(), shadowCount / (float)shadows.size()));
Window.draw(shadow);
}
Window.draw(Blue);
for (auto line : grid)
{
Window.draw(line);
}
Window.display();
}
return 0;
}
Recently, I decided to make an Animal Crossing clone in
C++ and SFML 2.1. But I'm having some issues. The Player won't show up when commanded to be rendered. The program will compile and run just fine but the player just won't show up.
Here's my code:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
RenderWindow window(VideoMode(700, 500), "Animal Crossing: Old oak");
View view(FloatRect(1000, 1000, 300, 200));
class Villager{
public:
int x, y, w, h;
Sprite pl;
string loadDir;
Villager(int x, int y, int w, int h, Color c, string loadDir){
this->x = x;
this->y = y;
this->w = w;
this->h = h;
Image image;
image.loadFromFile(loadDir);
image.createMaskFromColor(Color::Magenta);
Texture tex;
tex.loadFromImage(image);
pl.setTexture(tex);
}
}
};
int main(){
Villager villager(1100, 1000, 100, 100, Color::Blue, "player.png");
view.zoom(5);
Image grasstexloader;
grasstexloader.loadFromFile("grass.png");
Texture grasstex;
grasstex.loadFromImage(grasstexloader);
Sprite grass;
grass.setTexture(grasstex);
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
if(event.type == Event::Closed)
window.close();
if(Keyboard::isKeyPressed(Keyboard::Up))
villager.moveUp();
if(Keyboard::isKeyPressed(Keyboard::Down))
villager.moveDown();
if(Keyboard::isKeyPressed(Keyboard::Left))
villager.moveLeft();
if(Keyboard::isKeyPressed(Keyboard::Right))
villager.moveRight();
if(Keyboard::isKeyPressed(Keyboard::Escape))
window.close();
}
window.setView(view);
window.draw(grass);
window.draw(villager.pl);
window.display();
window.clear();
}
}
I've been staring at this code for an hour now. But I just can't find an error!
Please help!
Edit: I solved the problem with the sprite not being visible, but the sprite is just white instead of the appropriate colors. It proboably has something to do with how I load the file. Please post any suggestions you have on how to fix this new problem!
Your sprite is rendered white because in your Villager constructor, you're giving a local Texture variable to setTexture, which then gets destructed at the end of the constructor scope.
I'm using sfml 2.0 to make a game. The background is going to be a starfield. Rather than use an image, I thought it might be neat to randomly generate a starfield each time the game starts up, so the background looks slightly different each time. Problem is, the way I'm currently doing it slows my game down A LOT. Does anyone know what is causing the speed hit? I've looked through the documentation to see what, but I haven't found anything...
Here's my code:
#include <SFML/Graphics.hpp>
#include "sfml helper/initialization_helpers.h"
#include "sfml helper/cursor_functions.h"
#include "sfml helper/global_event_handler.h"
#include "sfml helper/globals.h"
#include <cstdio>
int main(int argc, char* argv[])
{
try {
// some non-related preparations...
PrepareApplication(argv[0]);
float width = 640, height = 480;
sf::RenderWindow window(sf::VideoMode(width, height), "SFML Test", sf::Style::Close);
window.setFramerateLimit(60);
// Textures is a global object that has an internal std::map<string, sf::Texture>
Textures.add("ball.png", "ball");
sf::Sprite sprite;
sprite.setTexture(Textures.get("ball"));
// This is the snippet that generates the starfield
srand(time(NULL));
sf::Image starsImg;
starsImg.create(width, height, sf::Color::Black);
int numStars = rand() % 20 + 490;
for (int i = 0; i < numStars; i++) {
int x = rand() % (int)width;
int y = rand() % (int)height;
sf::Color color(255, 255, 255, rand() % 75 + 25);
starsImg.setPixel(x, y, color);
}
sf::Texture starsTexture;
starsTexture.loadFromImage(starsImg);
sf::Sprite stars;
stars.setTexture(starsTexture);
// main loop
while (window.isOpen()) {
if (Flags.isActive && Flags.inFocus) {
confineCursorToWindow(window);
} else {
freeCursor();
}
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) {
Flags.isActive = (!Flags.isActive);
}
}
if (event.type == sf::Event::MouseMoved) {
sprite.setPosition(event.mouseMove.x, event.mouseMove.y);
}
// handles default events like sf::Event::WindowClosed
handleDefaultEventsForWindow(event, window);
}
window.clear();
window.draw(stars); // here's where I draw the stars
window.draw(sprite);
window.display();
}
} catch (const char* error) {
printf("%s\n", error);
exit(1);
}
return 0;
}
EDIT
I tried loading an image of a starfield and tried drawing that, the game still runs slow!
Also, by slow, I mean the sprite called "sprite" lags when following the mouse. Is that actually a speed issue?