Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- int main()
- {
- //Create window, and limit frame rate
- sf::RenderWindow window (sf::VideoMode(800, 600, 32), "Game", sf::Style::Default);
- window.setFramerateLimit(30);
- //Declare image
- sf::Texture texture;
- //Load image
- if(!texture.loadFromFile("Sprites/main.png"))
- {
- return 1;
- }
- //Creates and places the main sprite
- sf::Sprite sprite;
- sprite.setPosition(400, 300);
- //Defines the rects that will be used to switch character view
- sf::IntRect front(2, 2, 17, 22);
- sf::IntRect back (22, 2, 16, 21);
- sf::IntRect left (2, 27, 16, 23);
- sf::IntRect right (23, 27, 16, 23);
- //Starts clock
- sf::Clock clock;
- //Main window loop
- while(window.isOpen())
- {
- sf::Event event;
- sf::Time time = clock.restart();
- while(window.pollEvent(event))
- {
- if(event.type == sf::Event::Closed)
- {
- window.close();
- }
- if(event.key.code == sf::Keyboard::Insert)
- {
- sf::Image screenshot = window.capture();
- screenshot.saveToFile("Screenshot.png");
- }
- }
- //Movement
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- {
- sprite.setTextureRect(front);
- sprite.move(0, -14 * time.asSeconds());
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- {
- sprite.setTextureRect(back);
- sprite.move(0, 14 * time.asSeconds());
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- {
- sprite.setTextureRect(right);
- sprite.move(16 * time.asSeconds(), 0);
- }
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- {
- sprite.setTextureRect(left);
- sprite.move(-16 * time.asSeconds(), 0);
- }
- //Draw sequence
- window.clear(sf::Color(255, 0, 0)); //(Red, Green, Blue, (optional) Alpha) Alpha is transperency
- //Draw....
- window.draw(sprite);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement