Skip to content
Snippets Groups Projects
Commit 95a847c8 authored by Filip Strömbäck's avatar Filip Strömbäck
Browse files

Updated introduction a bit for 2022.

parent d5367753
No related branches found
No related tags found
No related merge requests found
Pipeline #82361 passed
basic/player.png

2.69 KiB

...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
* Fundera på: * Fundera på:
* - Vad är skillnaden här jämfört med förra? * - Vad är skillnaden här jämfört med förra?
* - Hur påverkas hastigheten av olika hastighet på programmet? * - Hur påverkas hastigheten av olika hastighet på programmet?
* - Hur fungerar detta med avseende på om programmet har fokus eller ej?
*/ */
......
...@@ -57,7 +57,7 @@ int main() { ...@@ -57,7 +57,7 @@ int main() {
auto delta = clock.restart(); auto delta = clock.restart();
{ {
float distance = 250.0f * (delta.asMicroseconds() / 1000000.0f); float distance = 250.0f * delta.asSeconds();
location += direction * distance; location += direction * distance;
} }
......
#include <SFML/Graphics.hpp>
#include "point.h"
#include "standard.h"
/**
* Steg 7: Alternativ hantering av tangentbordsinmatning.
*
* Fundera på:
* - Vad är fördelar och nackdelar gentemot förra lösningen?
* - Finns det problem med den här lösningen?
*/
const size_t width = 1024;
const size_t height = 768;
class Key_State {
public:
Key_State()
: up{false},
down{false},
left{false},
right{false}
{}
void onKey(sf::Keyboard::Key key, bool pressed) {
switch (key) {
case sf::Keyboard::W:
case sf::Keyboard::Up:
up = pressed;
break;
case sf::Keyboard::S:
case sf::Keyboard::Down:
down = pressed;
break;
case sf::Keyboard::A:
case sf::Keyboard::Left:
left = pressed;
break;
case sf::Keyboard::D:
case sf::Keyboard::Right:
right = pressed;
break;
default:
break;
}
}
sf::Vector2f direction() const {
sf::Vector2f result;
if (up)
result.y -= 1;
if (down)
result.y += 1;
if (left)
result.x -= 1;
if (right)
result.x += 1;
return normalize(result);
}
private:
bool up;
bool down;
bool left;
bool right;
};
int main() {
sf::RenderWindow window{sf::VideoMode{width, height}, "Demo"};
window.setKeyRepeatEnabled(false);
window.setVerticalSyncEnabled(true);
sf::CircleShape circle{40};
sf::Vector2f location{300, 300};
sf::Clock clock;
Key_State keys;
bool quit = false;
while (!quit) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
quit = true;
break;
case sf::Event::KeyPressed:
keys.onKey(event.key.code, true);
break;
case sf::Event::KeyReleased:
keys.onKey(event.key.code, false);
break;
default:
break;
}
}
if (quit)
break;
sf::Vector2f direction = keys.direction();
auto delta = clock.restart();
{
float distance = 250.0f * delta.asSeconds();
location += direction * distance;
}
window.clear();
circle.setPosition(location);
window.draw(circle);
window.display();
}
return 0;
}
#include <SFML/Graphics.hpp>
#include "point.h"
#include "standard.h"
/**
* Steg 8: Använda texturer.
*/
const size_t width = 1024;
const size_t height = 768;
sf::Vector2f find_direction() {
sf::Vector2f direction;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
direction.y -= 1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
direction.y += 1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
direction.x -= 1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
direction.x += 1;
return normalize(direction);
}
int main() {
sf::RenderWindow window{sf::VideoMode{width, height}, "Demo"};
window.setKeyRepeatEnabled(false);
window.setVerticalSyncEnabled(true);
sf::Texture texture;
texture.loadFromFile("player.png");
sf::Vector2f textureSize{texture.getSize()};
sf::RectangleShape player{textureSize};
player.setTexture(&texture);
player.setOrigin(textureSize / 2.0f);
sf::Vector2f location{300, 300};
sf::Clock clock;
bool quit = false;
while (!quit) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
quit = true;
break;
default:
break;
}
}
if (quit)
break;
sf::Vector2f direction = find_direction();
auto delta = clock.restart();
{
float distance = 250.0f * delta.asSeconds();
location += direction * distance;
}
window.clear();
player.setPosition(location);
window.draw(player);
window.display();
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment