Ver código fonte

some restructuring, and break blocks

opengl-renderer-broken
Martin Dørum 4 anos atrás
pai
commit
45870840db

+ 10
- 0
core.mod/src/entities/EntPlayer.cc Ver arquivo

@@ -23,12 +23,21 @@ void EntPlayer::update(Swan::Game &game, Swan::WorldPlane &plane, float dt) {

mouse_tile_ = game.getMouseTile();
plane.debugBox(mouse_tile_);
break_timer_.tick(dt);

// Break block
if (game.isMousePressed() && break_timer_.periodic(0.25)) {
fprintf(stderr, "button pressed\n");
plane.setTile(mouse_tile_, "core::air");
}

// Move left
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
body_.force_ += Swan::Vec2(-FORCE, 0);
state_ = State::RUNNING_L;
}

// Move right
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
body_.force_ += Swan::Vec2(FORCE, 0);
if (state_ == State::RUNNING_L)
@@ -37,6 +46,7 @@ void EntPlayer::update(Swan::Game &game, Swan::WorldPlane &plane, float dt) {
state_ = State::RUNNING_R;
}

// Jump
if (body_.on_ground_ && sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
body_.vel_.y_ = -JUMP_FORCE;
}

+ 1
- 0
core.mod/src/entities/EntPlayer.h Ver arquivo

@@ -35,6 +35,7 @@ private:
State state_ = State::IDLE;
Swan::Animation anims_[(int)State::COUNT];

Swan::Timer break_timer_;
Swan::TilePos mouse_tile_;

Swan::Body body_;

+ 2
- 1
libswan/include/swan/Animation.h Ver arquivo

@@ -4,6 +4,7 @@

#include "common.h"
#include "Asset.h"
#include "Timer.h"

namespace Swan {

@@ -32,7 +33,7 @@ private:
int fcount_;
int frame_ = 0;
bool dirty_ = true;
double time_ = 0;
Timer timer_;
sf::Sprite sprite_;
};


+ 1
- 0
libswan/include/swan/Game.h Ver arquivo

@@ -18,6 +18,7 @@ public:
void createWorld(std::string worldgen);

TilePos getMouseTile();
bool isMousePressed();

void draw();
void update(float dt);

+ 6
- 6
libswan/include/swan/Timer.h Ver arquivo

@@ -1,17 +1,17 @@
#pragma once

#include <string>

namespace Swan {

class Timer {
public:
Timer &start();
Timer &print(const std::string &str);

static double now();
void tick(float dt) { time_ += dt; }
void reset() { time_ = 0; }
bool periodic(float secs);

private:
std::string name_;
double start_;
float time_ = 0;
};

}

+ 1
- 0
libswan/include/swan/WorldPlane.h Ver arquivo

@@ -26,6 +26,7 @@ public:
bool hasChunk(ChunkPos pos);
Chunk &getChunk(ChunkPos pos);
void setTileID(TilePos pos, Tile::ID id);
void setTile(TilePos pos, const std::string &name);
Tile &getTile(TilePos pos);

Entity &spawnPlayer();

+ 3
- 5
libswan/src/Animation.cc Ver arquivo

@@ -18,17 +18,15 @@ void Animation::init(int w, int h, double interval, const Asset &asset, int flag
}

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

sprite_.setTextureRect(sf::IntRect(0, height_ * frame_, width_, height_));
}

time_ += dt;
}

void Animation::draw(Win &win) {
@@ -36,7 +34,7 @@ void Animation::draw(Win &win) {
}

void Animation::reset() {
time_ = 0;
timer_.reset();
frame_ = 0;
dirty_ = true;
}

+ 4
- 0
libswan/src/Game.cc Ver arquivo

@@ -56,6 +56,10 @@ TilePos Game::getMouseTile() {
(int)floor(win_.cam_.y_ + mousePos.y / (Swan::TILE_SIZE * win_.scale_)));
}

bool Game::isMousePressed() {
return win_.window_->hasFocus() && sf::Mouse::isButtonPressed(sf::Mouse::Button::Left);
}

void Game::draw() {
world_->draw(win_);
}

+ 6
- 20
libswan/src/Timer.cc Ver arquivo

@@ -4,27 +4,13 @@

namespace Swan {

Timer &Timer::start() {
start_ = now();
return *this;
}

Timer &Timer::print(const std::string &str) {
double t = now() - start_;
if (t > 1)
fprintf(stderr, "%s: %.2fs\n", str.c_str(), t);
else if (t > 0.001)
fprintf(stderr, "%s: %.2fms\n", str.c_str(), t * 1000);
else
fprintf(stderr, "%s: %.2fμ\n", str.c_str(), t * 1000000);

return *this;
}
bool Timer::periodic(float secs) {
if (time_ >= secs) {
time_ = 0;
return true;
}

double Timer::now() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + ((double)ts.tv_nsec / 1000000000.0);
return false;
}

}

+ 4
- 0
libswan/src/WorldPlane.cc Ver arquivo

@@ -54,6 +54,10 @@ void WorldPlane::setTileID(TilePos pos, Tile::ID id) {
getChunk(chunkPos(pos)).setTileID(*world_, relPos(pos), id);
}

void WorldPlane::setTile(TilePos pos, const std::string &name) {
setTileID(pos, world_->getTileID(name));
}

Tile &WorldPlane::getTile(TilePos pos) {
return getChunk(chunkPos(pos)).getTile(*world_, relPos(pos));
}

+ 7
- 6
src/main.cc Ver arquivo

@@ -8,6 +8,8 @@
#include <swan/Game.h>
#include <swan/Timer.h>

#include <SFML/System/Clock.hpp>

using namespace Swan;

int main() {
@@ -29,9 +31,9 @@ int main() {

game.createWorld("core::default");

double prevtime = Timer::now();
double fpsAcc = 0;
double tickAcc = 0;
sf::Clock clock;
float fpsAcc = 0;
float tickAcc = 0;
int fcount = 0;
int slowFrames = 0;

@@ -46,10 +48,9 @@ int main() {
}
}

float dt = clock.restart().asSeconds();

// Display FPS
double now = Timer::now();
double dt = now - prevtime;
prevtime = now;
fpsAcc += dt;
fcount += 1;
if (fpsAcc >= 4) {

Carregando…
Cancelar
Salvar