Bladeren bron

animation stuff, scaling, etc

opengl-renderer-broken
Martin Dørum 4 jaren geleden
bovenliggende
commit
647cded803

BIN
core.mod/assets-src/entities/player-still.xcf Bestand weergeven


BIN
core.mod/assets/entities/player-still.png Bestand weergeven


+ 14
- 0
core.mod/src/entities/EntPlayer.cc Bestand weergeven

@@ -1,7 +1,19 @@
#include "EntPlayer.h"

EntPlayer::EntPlayer(const Swan::Vec2 &pos):
body_(pos, SIZE, MASS) {
texture_.create(animation_still_.width_, animation_still_.height_);
sprite_ = sf::Sprite(texture_);
}

void EntPlayer::draw(Swan::Win &win) {
body_.outline(win);

if (animation_still_.fill(texture_))
sprite_.setTexture(texture_);

win.setPos(body_.pos_);
win.draw(sprite_);
}

void EntPlayer::update(Swan::WorldPlane &plane, float dt) {
@@ -16,4 +28,6 @@ void EntPlayer::update(Swan::WorldPlane &plane, float dt) {
body_.gravity();
body_.update(dt);
body_.collide(plane);

animation_still_.tick(dt);
}

+ 7
- 4
core.mod/src/entities/EntPlayer.h Bestand weergeven

@@ -9,10 +9,7 @@ public:
Swan::Entity *create(const Swan::Vec2 &pos) override { return new EntPlayer(pos); }
};

Swan::Body body_;

EntPlayer(Swan::Vec2 pos):
body_(pos, SIZE, MASS) {}
EntPlayer(const Swan::Vec2 &pos);

const Swan::Vec2 &getPos() override { return body_.pos_; }

@@ -25,4 +22,10 @@ private:
static constexpr float MASS = 80;
static constexpr Swan::Vec2 FRICTION = Swan::Vec2(400, 0);
static constexpr Swan::Vec2 SIZE = Swan::Vec2(1, 2);

Swan::Animation animation_still_ = Swan::Animation(
32, 64, 1.3, "core.mod/assets/entities/player-still.png");
sf::Texture texture_;
sf::Sprite sprite_;
Swan::Body body_;
};

+ 1
- 0
libswan/CMakeLists.txt Bestand weergeven

@@ -1,4 +1,5 @@
add_library(libswan SHARED
src/Animation.cc
src/Body.cc
src/Chunk.cc
src/Game.cc

+ 27
- 0
libswan/include/swan/Animation.h Bestand weergeven

@@ -0,0 +1,27 @@
#pragma once

#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Texture.hpp>

namespace Swan {

class Animation {
public:
Animation(int w, int h, double freq, const sf::Image &img);
Animation(int w, int h, double freq, const std::string &path);

void tick(double dt);
bool fill(sf::Texture &tex, bool force = false);

int width_, height_;

private:
sf::Image img_;
int fcount_;
int frame_ = 0;
bool dirty_ = true;
double interval_;
double time_ = 0;
};

}

+ 4
- 0
libswan/include/swan/Vector2.h Bestand weergeven

@@ -18,6 +18,10 @@ public:
return std::pair<T, T>(x_, y_);
}

operator Vector2<float>() const {
return Vector2<float>(x_, y_);
}

bool operator==(const Vector2<T> &vec) const {
return x_ == vec.x_ && y_ == vec.y_;
}

+ 9
- 1
libswan/include/swan/common.h Bestand weergeven

@@ -20,16 +20,24 @@ public:
sf::RenderWindow *window_;
sf::Transform transform_;
Vec2 cam_;
double scale_ = 2;

Win(sf::RenderWindow *win): window_(win) {}

void setPos(const Vec2 &pos) {
transform_ = sf::Transform().translate(pos - cam_);
transform_ = sf::Transform()
.scale(scale_, scale_)
.translate((pos - cam_) * TILE_SIZE);
}

void draw(const sf::Drawable &drawable) {
window_->draw(drawable, transform_);
}

Vec2 getSize() {
sf::Vector2 v = window_->getSize();
return Vec2(v.x, v.y) / (TILE_SIZE * scale_);
}
};

}

+ 1
- 0
libswan/include/swan/swan.h Bestand weergeven

@@ -1,5 +1,6 @@
#pragma once

#include <swan/Animation.h>
#include <swan/Body.h>
#include <swan/Chunk.h>
#include <swan/Entity.h>

+ 40
- 0
libswan/src/Animation.cc Bestand weergeven

@@ -0,0 +1,40 @@
#include "Animation.h"

namespace Swan {

Animation::Animation(int w, int h, double freq, const sf::Image &img):
width_(w), height_(h), img_(img) {
fcount_ = img_.getSize().y / height_;
interval_ = 1.0 / freq;
}

Animation::Animation(int w, int h, double freq, const std::string &path):
width_(w), height_(h) {
img_.loadFromFile(path);
fcount_ = img_.getSize().y / height_;
interval_ = 1.0 / freq;
}

void Animation::tick(double dt) {
if (time_ > interval_) {
dirty_ = true;
frame_ += 1;
time_ = 0;
if (frame_ >= fcount_)
frame_ = 0;
}

time_ += dt;
}

bool Animation::fill(sf::Texture &tex, bool force) {
if (!force && !dirty_)
return false;

const sf::Uint8 *data = img_.getPixelsPtr() + 4 * width_ * height_ * frame_;
tex.update(data);
dirty_ = false;
return true;
}

}

+ 1
- 1
libswan/src/Body.cc Bestand weergeven

@@ -28,7 +28,7 @@ void Body::collide(WorldPlane &plane) {
}

void Body::outline(Win &win) {
win.setPos(pos_ * TILE_SIZE);
win.setPos(pos_);
sf::RectangleShape rect(size_ * TILE_SIZE);
rect.setFillColor(sf::Color::Transparent);
rect.setOutlineColor(sf::Color(128, 128, 128));

+ 1
- 1
libswan/src/Chunk.cc Bestand weergeven

@@ -34,7 +34,7 @@ void Chunk::draw(Win &win) {
dirty_ = false;
}

win.setPos(Vec2(pos_.x_ * CHUNK_WIDTH * TILE_SIZE, pos_.y_ * CHUNK_HEIGHT * TILE_SIZE));
win.setPos(pos_ * Vec2i(CHUNK_WIDTH, CHUNK_HEIGHT));
win.draw(sprite_);
}


+ 1
- 6
libswan/src/World.cc Bestand weergeven

@@ -67,12 +67,7 @@ WorldPlane &World::addPlane(std::string gen) {
}

void World::draw(Win &win) {
auto size = win.window_->getSize();
const Vec2 &pos = player_->getPos();
win.cam_ = Vec2(
pos.x_ * TILE_SIZE - size.x / 2,
pos.y_ * TILE_SIZE - size.y / 2);

win.cam_ = player_->getPos() - (win.getSize() / 2) + 0.5;
planes_[current_plane_].draw(win);
}


Laden…
Annuleren
Opslaan