Procházet zdrojové kódy

move Win into its own thing, make vector stuff constexpr

opengl-renderer-broken
Martin Dørum před 4 roky
rodič
revize
3902a2d652

+ 1
- 1
CMakeLists.txt Zobrazit soubor

@@ -4,7 +4,7 @@ project(swan)
find_package(SFML 2.5 COMPONENTS graphics system window REQUIRED)

set(CMAKE_CXX_CLANG_TIDY
clang-tidy;
clang-tidy
-header-filter=.*
-checks=-*,-checks=bugprone-*,cert-*,performance-*,clang-analyzer-*,-cert-dcl16-c,-cert-err58-cpp,-clang-analyzer-optin.cplusplus.VirtualCall)


+ 19
- 19
libswan/include/swan/Vector2.h Zobrazit soubor

@@ -12,85 +12,85 @@ struct Vector2 {

constexpr Vector2(T x = 0, T y = 0): x(x), y(y) {}

Vector2<T> &set(T x, T y) {
constexpr Vector2<T> &set(T x, T y) {
this->x = x;
this->y = y;
return *this;
}

operator sf::Vector2<T>() const {
constexpr operator sf::Vector2<T>() const {
return sf::Vector2<T>(x, y);
}

operator std::pair<T, T>() const {
constexpr operator std::pair<T, T>() const {
return std::pair<T, T>(x, y);
}

operator Vector2<float>() const {
constexpr operator Vector2<float>() const {
return Vector2<float>(x, y);
}

bool operator==(const Vector2<T> &vec) const {
constexpr bool operator==(const Vector2<T> &vec) const {
return x == vec.x && y == vec.y;
}

bool operator!=(const Vector2<T> &vec) const {
constexpr bool operator!=(const Vector2<T> &vec) const {
return !(*this == vec);
}

Vector2<T> operator-() const {
constexpr Vector2<T> operator-() const {
return Vector2<T>(-x, -y);
}

Vector2<T> operator+(const Vector2<T> &vec) const {
constexpr Vector2<T> operator+(const Vector2<T> &vec) const {
return Vector2<T>(x + vec.x, y + vec.y);
}
Vector2<T> &operator+=(const Vector2<T> &vec) {
constexpr Vector2<T> &operator+=(const Vector2<T> &vec) {
x += vec.x;
y += vec.y;
return *this;
}

Vector2<T> operator-(const Vector2<T> &vec) const {
constexpr Vector2<T> operator-(const Vector2<T> &vec) const {
return Vector2<T>(x - vec.x, y - vec.y);
}
Vector2<T> &operator-=(const Vector2<T> &vec) {
constexpr Vector2<T> &operator-=(const Vector2<T> &vec) {
x -= vec.x;
y -= vec.y;
return *this;
}

Vector2<T> operator*(const Vector2<T> &vec) const {
constexpr Vector2<T> operator*(const Vector2<T> &vec) const {
return Vector2<T>(x * vec.x, y * vec.y);
}
Vector2<T> &operator*=(const Vector2<T> &vec) {
constexpr Vector2<T> &operator*=(const Vector2<T> &vec) {
x *= vec.x;
y *= vec.y;
return *this;
}

Vector2<T> operator*(T num) const {
constexpr Vector2<T> operator*(T num) const {
return Vector2<T>(x * num, y * num);
}
Vector2<T> operator*=(T num) {
constexpr Vector2<T> operator*=(T num) {
x *= num;
y *= num;
return *this;
}

Vector2<T> operator/(const Vector2<T> &vec) const {
constexpr Vector2<T> operator/(const Vector2<T> &vec) const {
return Vector2<T>(x / vec.x, y / vec.y);
}
Vector2<T> &operator/=(const Vector2<T> &vec) {
constexpr Vector2<T> &operator/=(const Vector2<T> &vec) {
x /= vec.x;
y /= vec.y;
return *this;
}

Vector2<T> operator/(T num) const {
constexpr Vector2<T> operator/(T num) const {
return Vector2<T>(x / num, y / num);
}
Vector2<T> operator/=(T num) {
constexpr Vector2<T> operator/=(T num) {
x /= num;
y /= num;
return *this;

+ 34
- 0
libswan/include/swan/Win.h Zobrazit soubor

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

#include "common.h"

namespace Swan {

class Win {
public:
float scale_ = 2;
Vec2 cam_;

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

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

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

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

private:
sf::RenderWindow *window_;
sf::Transform transform_;
};

}

+ 1
- 24
libswan/include/swan/common.h Zobrazit soubor

@@ -18,6 +18,7 @@ using ChunkPos = Vec2i;
class Game;
class World;
class WorldPlane;
class Win;

struct Context {
Game &game;
@@ -25,28 +26,4 @@ struct Context {
WorldPlane &plane;
};

struct Win {
sf::RenderWindow *window_;
sf::Transform transform_;
Vec2 cam_;
float scale_ = 2;

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

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

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

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

}

+ 1
- 0
libswan/include/swan/swan.h Zobrazit soubor

@@ -12,6 +12,7 @@
#include <swan/Tile.h>
#include <swan/Timer.h>
#include <swan/Vector2.h>
#include <swan/Win.h>
#include <swan/World.h>
#include <swan/WorldGen.h>
#include <swan/WorldPlane.h>

+ 2
- 0
libswan/src/Animation.cc Zobrazit soubor

@@ -1,5 +1,7 @@
#include "Animation.h"

#include "Win.h"

namespace Swan {

void Animation::init(int w, int h, float interval, const Asset &asset, int flags) {

+ 1
- 0
libswan/src/Body.cc Zobrazit soubor

@@ -3,6 +3,7 @@
#include <math.h>

#include "WorldPlane.h"
#include "Win.h"

namespace Swan {


+ 1
- 0
libswan/src/Chunk.cc Zobrazit soubor

@@ -5,6 +5,7 @@

#include "World.h"
#include "Game.h"
#include "Win.h"

namespace Swan {


+ 1
- 0
libswan/src/Game.cc Zobrazit soubor

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

#include "Tile.h"
#include "Asset.h"
#include "Win.h"

namespace Swan {


+ 1
- 0
libswan/src/World.cc Zobrazit soubor

@@ -3,6 +3,7 @@
#include <SFML/System/Clock.hpp>

#include "Game.h"
#include "Win.h"

namespace Swan {


+ 1
- 0
libswan/src/WorldPlane.cc Zobrazit soubor

@@ -7,6 +7,7 @@
#include "World.h"
#include "Game.h"
#include "Timer.h"
#include "Win.h"

namespace Swan {


+ 1
- 0
src/main.cc Zobrazit soubor

@@ -7,6 +7,7 @@
#include <swan/World.h>
#include <swan/Game.h>
#include <swan/Timer.h>
#include <swan/Win.h>

#include <SFML/System/Clock.hpp>
#include <SFML/Audio.hpp>

Načítá se…
Zrušit
Uložit