I have been trying to do this for 2 days now and I just cant seem to get it to work. I know how it should be done in my head, I just have a very hard time translating that to code. I need the mouse to get the current sprite it's over and then set the texture to a different one, but I cannot figure out how to do it, I looked all through the documentation, the forums here, google I just cant find anything that helps me. The code below is taken from my game project, but I made it so that it will work correctly if you just copy and paste it into your IDE
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
using std::cout;
using std::endl;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
sf::Mouse mouse;
while(window.isOpen())
{
window.clear(sf::Color::White);
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
{
window.close();
}break;
case sf::Event::Resized:
{
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}break;
}
}
//Set Tiles to Grid Test
sf::Texture setGridTexture;
sf::Sprite setGridSprite;
setGridSprite.setTexture(setGridTexture);
setGridSprite.setColor(sf::Color::Black);
sf::Vector2i Grid(15,15);
sf::Texture tempGridTexture;
sf::Sprite tempGridSprite;
tempGridSprite.setTexture(tempGridTexture);
tempGridSprite.setColor(sf::Color::Green);
sf::Vector2i setTilesOnGridX(mouse.getPosition(window).x, Grid.x);
sf::Vector2i setTilesOnGridY(mouse.getPosition(window).y, Grid.y);
//Draw Grid
for(int i = 0; i < Grid.x; i++)
{
for(int j = 0; j < Grid.y; j++)
{
tempGridSprite.setPosition(j * 32, i * 32);
tempGridSprite.setTextureRect(sf::IntRect(Grid.x * 32, Grid.y * 32, 32, 32));
window.draw(tempGridSprite);
}
}
//Place Tiles
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
cout << "Clicked" << endl;
window.draw(setGridSprite);
setGridSprite.setPosition(setTilesOnGridX.x * 32, setTilesOnGridY.y * 32);
setGridSprite.setTextureRect(sf::IntRect(Grid.x * 32, Grid.y * 32, 32, 32));
}
else
{
cout << "Let go" << endl;
}
//End set tiles to Grid Test.
window.display();
}
}
Also I cannot figure out collision either. I looked at the documents about the intersecting function, but that did nothing. Here is the code for the current collision I have.
#include <iostream>
#include <math.h>
#include <vector>
#include <SFML/Graphics.hpp>
using namespace std;
int main()
{
sf::Vector2f position;
sf::Vector2f velocity;
float maxspeed = 4.0f;
float accel = 4.5f;
float decel = 0.01f;
bool vSyncEnabled = true;
sf::FloatRect bottom, left, right, top;
sf::Vector2f dsize;
sf::Color color;
sf::RectangleShape rect;
//Setup Window and Framerate
sf::RenderWindow window(sf::VideoMode(800, 600), "Bounding Box (Collision)");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(vSyncEnabled);
//Load Texture
sf::Texture character;
if(!character.loadFromFile("Resources/Textures/triangle.png"))
{
cout << "Error loading resource 'triangle.png'" << endl;
}
else
{
cout << "triangle.png texture loaded" << endl;
}
//Set Sprite for Character Object
sf::Sprite characterSprite;
characterSprite.setTexture(character);
//characterSprite.setOrigin(sf::Vector2f(0, 0));dsa
characterSprite.setPosition(400, 300);
//Load Red Block Texture
sf::Texture redBlock;
if(!redBlock.loadFromFile("Resources/Textures/RedBlock.png"))
{
cout << "Error loading 'RedBlock.png" << endl;
}
else
{
cout << "RedBlock.png texture loaded" << endl;
}
//Set Sprite for Red Block Object
sf::Sprite redBlockSprite;
redBlockSprite.setTexture(redBlock);
redBlockSprite.setOrigin(sf::Vector2f(0, 0));
redBlockSprite.setPosition(200, 200);
sf::Vector2f RBSposition;
while(window.isOpen())
{
window.clear(sf::Color::White);
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
{
window.close();
}
break;
//Dont stretch the window contents when its resized
case sf::Event::Resized:
{
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}
break;
}
}
rect.setPosition(position);
rect.setSize(dsize);
rect.setFillColor(color);
//WASD Movement
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
velocity.x -= accel;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
velocity.x += accel;
}
else
{
velocity.x *= decel;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
velocity.y -= accel;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
velocity.y += accel;
}
else
{
velocity.y *= decel;
}
//Make sure the sprite isn't going too fast
if(velocity.x < -maxspeed) velocity.x = -maxspeed;
if(velocity.x > maxspeed) velocity.x = maxspeed;
if(velocity.y < -maxspeed) velocity.y = -maxspeed;
if(velocity.y > maxspeed) velocity.y = maxspeed;
//Update sprite position and move sprite
position += velocity;
//Get the window's size
sf::Vector2u currWSize = window.getSize();
if(position.x <= 0)
{
position.x = 0;
}
if(position.y <= 0)
{
position.y = 0;
}
if(position.x >= currWSize.x - characterSprite.getGlobalBounds().width)
{
position.x = currWSize.x - characterSprite.getGlobalBounds().width;
}
if(position.y >= currWSize.y - characterSprite.getGlobalBounds().height)
{
position.y = currWSize.y - characterSprite.getGlobalBounds().height;
}
float x, y, BoundsX, BoundsY;
float CharX, CharY, CharBoundsY, CharBoundsX;
x = redBlockSprite.getPosition().x;
y = redBlockSprite.getPosition().y;
BoundsX = redBlockSprite.getGlobalBounds().width;
BoundsY = redBlockSprite.getGlobalBounds().height;
CharX = characterSprite.getPosition().x;
CharY = characterSprite.getPosition().y;
//Collision
if(characterSprite.getGlobalBounds().intersects(redBlockSprite.getGlobalBounds()))
{
//If player hits right side of block, go right
if(CharX <= x)
{
position.x = x - BoundsX;
}
else if(CharX >= x)
{
position.x = x + BoundsX;
}
else if(CharY <= y)
{
position.x = y - BoundsY;
}
else if(CharY >= y)
{
position.x = y + BoundsY;
}
}
sf::FloatRect rectToMove(position, {characterSprite.getGlobalBounds().width, characterSprite.getGlobalBounds().height});
characterSprite.setPosition(position);
velocity *= decel;
window.draw(characterSprite);
window.draw(redBlockSprite);
window.display();
}
return 0;
}
If I can at least get these 2 things figured out, then I can finally get somewhere with my project.