I have a spritesheet animation drawn using a vertexarray. As an example let's say I have an arrow pointing to the right with a simple animation. I would like to be able to flip the texture on the x axis so that the arrow can also point left without the need to double the size of my spritesheet.
I know that this is possible with sprites by using a negative scale factor, but in cases where I have to use vertexarray I would like to know how this is possible. I tried giving the texcoords a negative width but it did not work. Here's my example code that isolates the issue as much as possible:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
// CREATE THE WINDOW
sf::RenderWindow window(sf::VideoMode(200, 200), "Arrow");
window.setFramerateLimit(20);
// CREATE GRAPHICS
sf::Texture texArrow;
texArrow.loadFromFile("Arrow.png");
sf::VertexArray vaArrow;
sf::RenderStates rsArrow;
rsArrow.texture = &texArrow;
vaArrow.setPrimitiveType(sf::Quads);
vaArrow.resize(4);
vaArrow[0].position = sf::Vector2f(0, 0);
vaArrow[1].position = sf::Vector2f(100, 0);
vaArrow[2].position = sf::Vector2f(100, 100);
vaArrow[3].position = sf::Vector2f(0, 100);
vaArrow[0].texCoords = sf::Vector2f(0, 0);
vaArrow[1].texCoords = sf::Vector2f(100, 0);
vaArrow[2].texCoords = sf::Vector2f(100, 100);
vaArrow[3].texCoords = sf::Vector2f(0, 100);
// ARROW DIRECTION STATES
enum Directions { right, left};
Directions dir = right;
float AnimationFrame = 0;
// GAME LOOP
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) { window.close(); }
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { dir = right; }
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { dir = left; }
}
// UPDATE
switch (dir)
{
case right: // NORMAL
std::cout << "Arrow is now pointing RIGHT" << std::endl;
vaArrow[0].texCoords = sf::Vector2f(AnimationFrame * 100 , 0);
vaArrow[1].texCoords = sf::Vector2f(AnimationFrame * 100 + 100 , 0);
vaArrow[2].texCoords = sf::Vector2f(AnimationFrame * 100 + 100 , 100);
vaArrow[3].texCoords = sf::Vector2f(AnimationFrame * 100 , 100);
break;
case left: // FLIPPED
std::cout << "Arrow is now pointing LEFT" << std::endl;
// THIS IS WHAT I HAVE TRIED BUT IT DOESN'T WORK:
vaArrow[0].texCoords = sf::Vector2f((AnimationFrame * 100) * -1 , 0);
vaArrow[1].texCoords = sf::Vector2f((AnimationFrame * 100 + 100) * -1 , 0);
vaArrow[2].texCoords = sf::Vector2f((AnimationFrame * 100 + 100) * -1 , 100);
vaArrow[3].texCoords = sf::Vector2f((AnimationFrame * 100) * -1 , 100);
break;
}
AnimationFrame++;
if (AnimationFrame == 5) { AnimationFrame = 0; } // Always loop 0 1 2 3 4
// RENDER
window.clear();
window.draw(vaArrow, rsArrow);
window.display();
}
return 0;
}
Spritesheet for the example:
There are many days to solve this, but your actual idea isn't that far off. Did you notice that you're basically stretching one end of the arrow over your whole quad?
The solution is pretty simple: By default. Textures aren't repeated, they're clamped (meaning that the pixels on the edges are repeated infinitely), which causes the glitch you've noticed.
So all you have to do to get your approach working is to set the texture to be repeated:
texArrow.setRepeated(true);
Related
I tried to make a cube that moves side to side and bounces off the floor.
It bounced a couple times and then fell through the floor.
I tried making the floor higher.
I tried adding extra vertical velocity.
I have tried everything i can think of.
I would like to get the cube to not fall through the floor.
how do I do that?
#include <SFML/Graphics.hpp> <iostream>
int main(){
sf::RenderWindow window(sf::VideoMode(1000, 700), "project");
window.setFramerateLimit(60);
sf::RectangleShape rect;
int w = 100;
int h = 100;
rect.setSize(sf::Vector2f(w, h));
sf::Vector2f rectangle_position(500 - (w/2), 300 - (h/2));
rect.setPosition(rectangle_position);
float x_velocity = 3;
float y_velocity = 3;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
}
if (rectangle_position.x > 1000 - w) {
x_velocity = x_velocity * -1;
}
if (rectangle_position.x < 1 ) {
x_velocity = x_velocity * -1;
}
if (rectangle_position.y > 700 - h) {
y_velocity = y_velocity * -1;
}
if (rectangle_position.y < 50) {
y_velocity = y_velocity * -1;
}
y_velocity = y_velocity + 3;
rectangle_position.x = rectangle_position.x + x_velocity;
rectangle_position.y = rectangle_position.y + y_velocity;
rect.setPosition(rectangle_position);
window.clear();
window.draw(rect);
window.display();
}
}
In your implementation, once the bottom of the rectangle goes below the ground, you just reverse the velocity but never update the position of the rectangle. This causes the rectangle to sink below the ground.
You should make sure that the bottom of the rectangle never goes below the ground. This can be done by adding the following condition:
if (rectangle_position.y > 700 - h) {
// make sure that rectangle never goes below the ground
rectangle_position.y -= 3;
y_velocity = y_velocity * -1;
}
And the result is:
Hope it helps.
I am working on a recreation of minesweeper using cpp and SFML. I have coded the board and the tiles switch to their revealed states when you left click on them, however, I am running into a strange issue with the right click.
Since I want to be able to flag and unflag tiles, I created a texture called tile_foreground that stores a texture the same size as the tiles but is completely transparent. The idea is that the first click will flag the tile and draw the flag sprite and a 2nd click would unflag the tile and change the sprite texture to the empty png so that only the base of the tile shows, with the cycle continuing on further clicks.
When you right click, the first click wont register and it will take at least a 2nd click to place down a flag on the tile. Sometimes it takes more than 2 clicks and clicking on a tile, moving your mouse to a different tile and then back to the same tile without clicking on a different tile will not flag the tile. It seems that the program doesnt register the first click as a click and waits for another one from the same tile to register it.
I suspect this comes from the way I handle the clicking events since before I added the statement below, holding down the right button would make the sprites alternate quickly and infinitely.
event.type == sf::Event::MouseButtonReleased
Any input would be appreciated since I dont really know how I would fix this.
Below is my main.cpp file
#include <SFML/Graphics.hpp>
#include <random>
#include <iostream>
#include <fstream>
#include <string>
#include "Board.h"
#include "TextureManager.h"
void testFunc() {
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();
}
}
void loadMSImages() {
string files[21] = {"debug", "digits", "face_happy", "face_lose", "face_win", "flag", "mine",
"number_1", "number_2", "number_3" , "number_4" , "number_5" , "number_6",
"number_7" , "number_8", "test_1", "test_2", "test_3", "tile_hidden", "tile_revealed", "tile_foreground"};
for (int i = 0; i < 21; i += 1) {
TextureManager::LoadTexture(files[i]);
}
}
int main()
{
loadMSImages();
//testFunc(); // Test function to see if program is still working properly
// Reads config file to determine window size
int colNum;
int rowNum;
int mineNum;
std::fstream reader("boards\\config.cfg");
string readString;
if (!reader.is_open())
{
std::cout << "File could not be opened" << std::endl;
return 0;
}
// Reads col #, row #, and mine # in that order
std::getline(reader, readString);
colNum = stoi(readString);
std::getline(reader, readString);
rowNum = stoi(readString);
std::getline(reader, readString);
mineNum = stoi(readString);
reader.close();
// Provided test boards have a size of 25 x 16
sf::RenderWindow window;
window.create(sf::VideoMode((colNum * 32), (rowNum * 32) + 100), "Minesweeper"); // Width, Height
window.setKeyRepeatEnabled(true);
window.setVerticalSyncEnabled(true);
Board testBoard(rowNum, colNum, mineNum);
testBoard.printBoard();
// Tile Testing Code
// Creating sprites for buttons on bottom of game
sf::Sprite testBoardOne;
sf::Sprite testBoardTwo;
sf::Sprite testBoardThree;
sf::Sprite debugButton;
sf::Sprite statusFace;
sf::Sprite timerDigitOne;
sf::Sprite timerDigitTwo;
sf::Sprite timerDigitThree;
// Setting the positions of these sprites
testBoardOne.setPosition(sf::Vector2f(((32 * colNum) - 64), ((32 * rowNum))));
testBoardTwo.setPosition(sf::Vector2f(((32 * colNum) -128), ((32 * rowNum))));
testBoardThree.setPosition(sf::Vector2f(((32 * colNum) - 192), ((32 * rowNum))));
debugButton.setPosition(sf::Vector2f(((32 * colNum) - 256), ((32 * rowNum))));
statusFace.setPosition(sf::Vector2f((((32 * colNum)/2)-32), ((32 * rowNum))));
// Loading the textures onto the sprites
testBoardOne.setTexture(TextureManager::GetTexture("test_1"));
testBoardTwo.setTexture(TextureManager::GetTexture("test_2"));
testBoardThree.setTexture(TextureManager::GetTexture("test_3"));
debugButton.setTexture(TextureManager::GetTexture("debug"));
statusFace.setTexture(TextureManager::GetTexture("face_happy"));
// 2D vector of tiles
std::vector<std::vector<Tile*>> tiles;
//Holding variables for creating Tile objects
Tile* holdingTile;
sf::Sprite* holdingSprite;
for (int i = 0; i < 25; i += 1)
{
std::vector<Tile*> holdingVec;
tiles.push_back(holdingVec);
for (int j = 0; j < 16; j += 1)
{
holdingTile = new Tile(0, i, j);
holdingSprite = new sf::Sprite();
holdingSprite->setTexture(TextureManager::GetTexture("tile_hidden"));
holdingSprite->setPosition(sf::Vector2f((32 * i), (32 * j)));
holdingTile->setBackgroundSprite(holdingSprite);
holdingSprite = new sf::Sprite();
//holdingSprite->setTexture(TextureManager::GetTexture("tile_foreground"));
holdingSprite->setPosition(sf::Vector2f((32 * i), (32 * j)));
holdingTile->setForegroundSprite(holdingSprite);
tiles.at(i).push_back(holdingTile);
}
}
//============================ GAME LOOP ======================================
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// Clears previous frame, draws new frame, and displays it
window.clear();
// Draws tiles on board
for (unsigned int i = 0; i < tiles.size(); i += 1)
{
for (unsigned int j = 0; j < tiles.at(i).size(); j += 1)
{
window.draw(*(tiles.at(i).at(j)->getBackgroundSprite()));
window.draw(*(tiles.at(i).at(j)->getForegroundSprite()));
}
}
//Draws "Constant" Buttons, such as debug mode and boards
window.draw(testBoardOne);
window.draw(testBoardTwo);
window.draw(testBoardThree);
window.draw(debugButton);
window.draw(statusFace);
window.display();
// Clicking handlers
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
// transform the mouse position from window coordinates to world coordinates
sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));
// retrieve the bounding box of the sprite
sf::FloatRect bounds;
for (unsigned int i = 0; i < tiles.size(); i += 1)
{
for (unsigned int j = 0; j < tiles.at(i).size(); j += 1)
{
bounds = tiles.at(i).at(j)->getBackgroundSprite()->getGlobalBounds();
if (bounds.contains(mouse))
{
//std::cout << "Sprite pressed" << std::endl;
tiles.at(i).at(j)->getBackgroundSprite()->setTexture(TextureManager::GetTexture("tile_revealed"));
}
}
}
}
//if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) // WIP - Rapid oscillation of textures
if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && event.type == sf::Event::MouseButtonReleased)
{
// transform the mouse position from window coordinates to world coordinates
sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));
// retrieve the bounding box of the sprite
sf::FloatRect bounds;
for (unsigned int i = 0; i < tiles.size(); i += 1)
{
for (unsigned int j = 0; j < tiles.at(i).size(); j += 1)
{
bounds = tiles.at(i).at(j)->getBackgroundSprite()->getGlobalBounds();
if (bounds.contains(mouse))
{
std::cout << "Tile right clicked" << std::endl;
if (tiles.at(i).at(j)->isFlagged)
{
tiles.at(i).at(j)->unflagTile();
tiles.at(i).at(j)->getForegroundSprite()->setTexture(TextureManager::GetTexture("tile_foreground"));
}
else
{
tiles.at(i).at(j)->flagTile();
tiles.at(i).at(j)->getForegroundSprite()->setTexture(TextureManager::GetTexture("flag"));
}
}
}
}
}
}
// Make sure to clear the textures at the end of the program to avoid errors
TextureManager::Clear();
return 0;
}
The problem is that you use the sf::Event event variable outside the while (window.pollEvent(event)) loop, so when you use the event, the last event type is assigned to it, which does not necessarily have to be MouseButtonReleased but can be one of these:
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Event.php#af41fa9ed45c02449030699f671331d4a
Solution:
Move this condition:
if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && event.type == sf::Event::MouseButtonReleased)
to the while (window.pollEvent(event)) loop and change it to:
if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Right)
So i'm trying to make a basic gui with sfml and need a scroll bar to scroll through a loop of drawables. After research I learned that view manipulation would be the way to do it. I have a rectangle for an outline that i'm trying to use as the view and have set up the bar itself i just need it to change the view. My issue is that I isn't drawing anything I put inside the view. If it won't draw anything in the view its hard to scroll through it. All help is appreciated thanks.
my deliration
projectsBox = sf::RectangleShape(sf::Vector2f((400 * scale) - (2 * (10 * scale)), 225 * scale));
projectsBox.setOrigin(sf::Vector2f(-10 * scale, -130 * scale));
projectsBox.setOutlineColor(sf::Color::Black);
projectsBox.setOutlineThickness(10 * scale);
projectsBox.setFillColor(sf::Color::Transparent);
projectsVeiw.setViewport(projectsBox.getGlobalBounds());
void sfmlWindow::drawProjects() {
sf::CircleShape base;
base.setRadius(30);
base.setScale(0, userProjects->projects[1].name.length());
base.setFillColor(sf::Color::Blue/*sf::Color(33, 33, 33, 270)*/);
base.setOrigin(0, 0);
sf::Text text;
textInit(&text, userProjects->projects[1].name, 96);
text.setOrigin(0, 0);
projectsDrawables.emplace_back(std::make_unique<sf::CircleShape>(base));
projectsDrawables.emplace_back(std::make_unique<sf::Text>(text));
}
My Loop
while (mainWindow.isOpen()) {
eventLoop();
mainWindow.clear(sf::Color::Transparent);
//Do project veiw
mainWindow.setView(projectsVeiw);
drawProjects();
for (std::unique_ptr<sf::Drawable>& i : projectsDrawables) {
mainWindow.draw(*i);
}
//draw drawbles
mainWindow.setView(mainWindow.getDefaultView());
for (std::unique_ptr<sf::Drawable>& i : allDrawables) {
mainWindow.draw(*i);
}
mainWindow.draw(projectsBox);
projectsSlider.setSize(sf::Vector2f(15, slidersize()));
mainWindow.draw(projectsSlider);
//render
mainWindow.display();
}
I've adapted your code to something that compiles and actually draws stuff on the screen. It's not how you want it to look but it show drawing through both views working. You should be able to experiment with this to progress.
projectDrawables are located around 1000,1000 in your world where as allDrawables are positioned around 0,0. To shrink you projectBox to just be in the corner you should increase the size of the projectView.
#include <iostream>
#include <SFML/Graphics.hpp>
const int BOARD_SIZE = 40;
const float TILE_SIZE = 20.0f;
std::vector<std::unique_ptr<sf::Drawable> > projectsDrawables;
std::vector<std::unique_ptr<sf::Drawable> > allDrawables;
sf::Font font;
void drawProjects() {
sf::CircleShape base;
base.setRadius(30);
base.setFillColor(sf::Color::Blue);
base.setPosition(1000, 1000);
sf::Text text;
text.setFont(font);
text.setFillColor(sf::Color::White);
text.setString("hello");
text.setCharacterSize(96);
text.setOrigin(1100, 1000);
projectsDrawables.emplace_back(std::make_unique<sf::Text>(text));
projectsDrawables.emplace_back(std::make_unique<sf::CircleShape>(base));
sf::CircleShape base2;
base2.setRadius(50);
base2.setFillColor(sf::Color::Red);
base2.setPosition(30, 80);
sf::Text text2;
text2.setFont(font);
text2.setFillColor(sf::Color::Green);
text2.setString("Cheese");
text2.setCharacterSize(48);
text2.setPosition(30, 30);
allDrawables.emplace_back(std::make_unique<sf::Text>(text2));
allDrawables.emplace_back(std::make_unique<sf::CircleShape>(base2));
}
int main()
{
sf::RenderWindow mainWindow(sf::VideoMode((2+BOARD_SIZE) * (int)TILE_SIZE, (2+BOARD_SIZE) * (int)TILE_SIZE), "Snake");
if (!font.loadFromFile("Instruction.ttf") ) {
std::cerr << "Font error." << std::endl;
exit( -1 );
}
sf::Clock clock;
auto scale = 1;
auto projectsBox = sf::RectangleShape(sf::Vector2f(400, 225));
projectsBox.setPosition(1000,1000);
projectsBox.setOutlineColor(sf::Color::Green);
projectsBox.setOutlineThickness(10 * scale);
projectsBox.setFillColor(sf::Color::Transparent);
sf::View projectsView( projectsBox.getGlobalBounds());
while (mainWindow.isOpen()) {
sf::Time elapsed = clock.getElapsedTime();
if (elapsed.asSeconds() > 0.2f) {
clock.restart();
}
sf::Event event;
while (mainWindow.pollEvent(event)) {
if (event.type == sf::Event::Closed)
mainWindow.close();
// Respond to key pressed events
if (event.type == sf::Event::KeyPressed){
if (event.key.code == sf::Keyboard::Escape){
return 0;
}
}
}
mainWindow.clear(sf::Color::Black);
mainWindow.setView(projectsView);
mainWindow.draw(projectsBox);
//Do project veiw
drawProjects();
for (std::unique_ptr<sf::Drawable>& i : projectsDrawables) {
mainWindow.draw(*i);
}
//draw drawbles
mainWindow.setView(mainWindow.getDefaultView());
for (std::unique_ptr<sf::Drawable>& i : allDrawables) {
mainWindow.draw(*i);
}
//projectsSlider.setSize(sf::Vector2f(15, slidersize()));
//mainWindow.draw(projectsSlider);
//render
mainWindow.display();
}
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to create a scribble clone for a uni project.
My lines are basically a lot of dots. If I draw too quick, the line breaks and I have single dots.
Also, I can't figure out how to draw lines ONLY when a mouse button is pressed.
I tried to put mouseMoved into a while loop until mouseButton is released but that didn't seem to work. I ended up in an infinite loop.
Here is my code so far:
while (window.isOpen())
{
while (window.pollEvent(event))
{
int mouseButtonX = event.mouseButton.x;
int mouseButtonY = event.mouseButton.y;
int mouseMoveX = event.mouseMove.x;
int mouseMoveY = event.mouseMove.y;
setBrushSize(5);
brush.setRadius(brushSize);
brush.setPosition(mouseMoveX - brushSize, mouseMoveY - brushSize);
brush.setFillColor(sf::Color::Transparent);
brush.setOutlineColor(sf::Color::Green);
brush.setOutlineThickness(2);
switch (event.type) {
case (sf::Event::Closed):
window.close();
break;
case (sf::Event::KeyPressed):
if (event.key.control && event.key.code == sf::Keyboard::X) {
cout << "closing";
window.close();
}
if (event.key.code == sf::Keyboard::R) {
cout << "printed";
brushColor = setBrushColor(255, 0, 0);
}
if (event.key.code == sf::Keyboard::G) {
brushColor = setBrushColor(0, 255, 0);
}
if (event.key.code == sf::Keyboard::B) {
brushColor = setBrushColor(0, 0, 255);
}
if (event.key.code == sf::Keyboard::C) {
for (int i = 0; i < points.size(); i++) {
points.clear();
}
it = 0;
}
break;
case(sf::Event::MouseButtonPressed):
points.push_back(point);
points[it].setRadius(brushSize);
points[it].setFillColor(brushColor);
points[it].setPosition(mouseButtonX - brushSize, mouseButtonY - brushSize);
it++;
cout << "drawPoint: Pressed X = " << mouseButtonX << " Y = " << mouseButtonY << endl;
break;
case(sf::Event::MouseMoved):
points.push_back(point);
points[it].setRadius(brushSize);
points[it].setFillColor(brushColor);
points[it].setPosition(mouseMoveX - brushSize, mouseMoveY - brushSize);
it++;
cout << "drawPoint: Moved X = " << mouseMoveX << " Y = " << mouseMoveY << endl;
break;
}
}
window.clear(sf::Color(255, 247, 204));
window.draw(SkechyT);
window.draw(close);
window.draw(brush);
window.draw(color);
window.draw(clear);
for (int i = 0; i < points.size(); i++) {
window.draw(points[i]);
}
//window.draw(point);
window.display();
}
}
int getBrushSize() {
return brushSize;
}
void setBrushSize(int num) {
brushSize = num;
}
sf::Color setBrushColor(int r, int g, int b) {
return sf::Color(r, g, b);
}
~Visualizer();
};
While you can modify a sf::VertexArray on the fly (basically building a vector drawing app), you can also use a sf::RenderTexture as an actual drawing canvas.
Considering you tried drawing lots of small points, I'd assume you're aiming for the latter. What's important here is the fact that you don't necessarily have to clear render textures between drawing calls and can therefore preserve whatever has been drawn before.
Combined with the original goal – drawing – this becomes very easy.
All you have to do is draw the changes (e.g. when moving the cursor), finalize the render texture (by calling display()), and then presenting it using any drawable (like sf::Sprite.
I've quickly scribbled together the following example, which should show the concept rather well (and you won't run into an endless loop other than the actual main loop):
#include <SFML/Graphics.hpp>
#include <vector>
int main(int argc, char **argv) {
sf::RenderWindow window(sf::VideoMode(800, 600), L"SFML Drawing – C to clear, PageUp/PageDown to pick colors", sf::Style::Default);
// Set a specific frame rate, since we don't want to
// worry about vsync or the time between drawing iterations
window.setVerticalSyncEnabled(false);
window.setFramerateLimit(100);
// First we'll use a canvas to basically store our image
sf::RenderTexture canvas;
canvas.create(800, 600);
canvas.clear(sf::Color::White);
// Next we'll need a sprite as a helper to draw our canvas
sf::Sprite sprite;
sprite.setTexture(canvas.getTexture(), true);
// Define some colors to use
// These are all with very low alpha so we
// can (over-)draw based on how fast we move the cursor
const std::vector<sf::Color> colors = {
sf::Color(255, 0, 0, 8),
sf::Color(255, 255, 0, 8),
sf::Color(0, 255, 0, 8),
sf::Color(0, 255, 255, 8),
sf::Color(0, 0, 255, 8),
sf::Color(255, 0, 255, 8)
};
// We'll need something to actually draw
// For simplicity, I'm just drawing a circle shape
// but you could also draw a line, rectangle, or something more complex
const float brush_size = 25;
sf::CircleShape brush(brush_size, 24);
brush.setOrigin(brush_size, brush_size); // Center on the circle's center
sf::Vector2f lastPos;
bool isDrawing = false;
unsigned int color = 0;
// Apply some default color
brush.setFillColor(colors[color]);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch (event.key.code) {
case sf::Keyboard::C:
// Clear our canvas
canvas.clear(sf::Color::White);
canvas.display();
break;
case sf::Keyboard::PageUp:
// Get next color
color = (color + 1) % colors.size();
// Apply it
brush.setFillColor(colors[color]);
break;
case sf::Keyboard::PageDown:
// Get previous color
color = (color - 1) % colors.size();
// Apply it
brush.setFillColor(colors[color]);
break;
}
break;
case sf::Event::Resized:
{
// Window got resized, update the view to the new size
sf::View view(window.getView());
const sf::Vector2f size(window.getSize().x, window.getSize().y);
view.setSize(size); // Set the size
view.setCenter(size / 2.f); // Set the center, moving our drawing to the top left
window.setView(view); // Apply the view
break;
}
case sf::Event::MouseButtonPressed:
// Only care for the left button
if (event.mouseButton.button == sf::Mouse::Left) {
isDrawing = true;
// Store the cursor position relative to the canvas
lastPos = window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y});
// Now let's draw our brush once, so we can
// draw dots without actually draging the mouse
brush.setPosition(lastPos);
// Draw our "brush"
canvas.draw(brush);
// Finalize the texture
canvas.display();
}
break;
case sf::Event::MouseButtonReleased:
// Only care for the left button
if (event.mouseButton.button == sf::Mouse::Left)
isDrawing = false;
break;
case sf::Event::MouseMoved:
if (isDrawing)
{
// Calculate the cursor position relative to the canvas
const sf::Vector2f newPos(window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y)));
// I'm only using the new position here
// but you could also use `lastPos` to draw a
// line or rectangle instead
brush.setPosition(newPos);
// Draw our "brush"
canvas.draw(brush);
// Finalize the texture
canvas.display();
break;
}
}
}
// Clear the window
window.clear(sf::Color(64, 64, 64));
// Draw our canvas
window.draw(sprite);
// Show the window
window.display();
}
return 0;
}
Once running, you can start drawing using the left mouse button. C will clear the canvas and Page Up and Page Down allow you to pick a different color:
Edit: And just to mention it, in the example above, rather than drawing a circle, you can just draw a sf::VertexArray with sf::Lines and two vertices: lastPos and newPos. This way you'll always draw a continuous line. (But you'd obviously have to save/update lastPos with the value of newPos once you're done.)
I am trying to make a game and am stuck on gravity..... In the following code a rectangle stands for a player and when I press up key it moves in y-axis but when I activate gravity on it (i.e resetting its previous position) it does not animate (i.e. It does not jumps) instead it just stays in its position. I am using SFML library of C++ and that's a game development tool. Please Help!
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Gravity");
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(100, 100));
rectangle.setFillColor(sf::Color::Black);
rectangle.setPosition(sf::Vector2f(10, 350));
while(window.isOpen())
{
sf::Event Event;
while(window.pollEvent(Event))
{
if(Event.type == sf::Event::Closed)
{
window.close();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
rectangle.move(0, -1);
}
if(rectangle.getPosition().y >= 350-1)
{
rectangle.setPosition(0, 350);
}
window.display();
window.clear(sf::Color::Cyan);
window.draw(rectangle);
}
}
Theoretically your code would work, but there's one significant problem:
Your initial position is 350.
Now your "jumping code" (which will allow the player to fly indefinitely!) triggers and your position is changed to 349.
However, your code keeping the player from dropping off the screen (y >= 350-1) essentially resolves to the check y >= 349, which will be true, so your position is permanently reset to 350.
To solve this, just remove the -1 or replace the >= operator with >.
While your approach should be working (once the fix above is applied), you should rethink your strategy and store a velocity in addition to a position. I've recently written the following example code. It's far from being perfect, but it should teach you a few basics for a jump and run game (not necessarily the only way to do such things):
Allow the player to jump.
Apply gravity.
Allow the player to determine jump height based on how long he holds down a key.
#include <SFML/Graphics.hpp>
int main(int argc, char **argv) {
sf::RenderWindow window;
sf::Event event;
sf::RectangleShape box(sf::Vector2f(32, 32));
box.setFillColor(sf::Color::White);
box.setOrigin(16, 32);
box.setPosition(320, 240);
window.create(sf::VideoMode(640, 480), "Jumping Box [cursor keys + space]");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(false);
// player position
sf::Vector2f pos(320, 240);
// player velocity (per frame)
sf::Vector2f vel(0, 0);
// gravity (per frame)
sf::Vector2f gravity(0, .5f);
// max fall velocity
const float maxfall = 5;
// run acceleration
const float runacc = .25f;
// max run velocity
const float maxrun = 2.5f;
// jump acceleration
const float jumpacc = -1;
// number of frames to accelerate in
const unsigned char jumpframes = 10;
// counts the number of frames where you can still accelerate
unsigned char jumpcounter = 0;
// inputs
bool left = false;
bool right = false;
bool jump = false;
while (window.isOpen()) {
while (window.pollEvent(event)) {
switch(event.type) {
case sf::Event::KeyPressed:
case sf::Event::KeyReleased:
switch (event.key.code) {
case sf::Keyboard::Escape:
window.close();
break;
case sf::Keyboard::Left:
left = event.type == sf::Event::KeyPressed;
break;
case sf::Keyboard::Right:
right = event.type == sf::Event::KeyPressed;
break;
case sf::Keyboard::Space:
jump = event.type == sf::Event::KeyPressed;
break;
}
break;
case sf::Event::Closed:
window.close();
break;
}
}
// logic update start
// first, apply velocities
pos += vel;
// determine whether the player is on the ground
const bool onground = pos.y >= 480;
// now update the velocity by...
// ...updating gravity
vel += gravity;
// ...capping gravity
if (vel.y > maxfall)
vel.y = maxfall;
if (left) { // running to the left
vel.x -= runacc;
}
else if (right) { // running to the right
vel.x += runacc;
}
else { // not running anymore; slowing down each frame
vel.x *= 0.9;
}
// jumping
if (jump) {
if (onground) { // on the ground
vel.y += jumpacc * 2;
jumpcounter = jumpframes;
}
else if (jumpcounter > 0) { // first few frames in the air
vel.y += jumpacc;
jumpcounter--;
}
}
else { // jump key released, stop acceleration
jumpcounter = 0;
}
// check for collision with the ground
if (pos.y > 480) {
vel.y = 0;
pos.y = 480;
}
// check for collision with the left border
if (pos.x < 16) {
vel.x = 0;
pos.x = 16;
}
else if (pos.x > 624) {
vel.x = 0;
pos.x = 624;
}
// logic update end
// update the position
box.setPosition(pos);
window.clear();
window.draw(box);
window.display();
}
return 0;
}