From 8ffb27ab09ff567a159e5be5a243fd3967084977 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 3 Feb 2019 16:10:44 -0500 Subject: Very basic ECS --- src/consts.h | 10 +++++ src/entity.h | 31 +++++++++++++ src/level.h | 52 ++++++++++++++++++++++ src/main.cpp | 60 +++++++++++++++++++++++++ src/renderer.cpp | 97 +++++++++++++++++++++++++++++++++++++++++ src/renderer.h | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/simulation.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ src/simulation.h | 57 ++++++++++++++++++++++++ src/tileset.h | 13 ++++++ src/vector.h | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 680 insertions(+) create mode 100644 src/consts.h create mode 100644 src/entity.h create mode 100644 src/level.h create mode 100644 src/main.cpp create mode 100644 src/renderer.cpp create mode 100644 src/renderer.h create mode 100644 src/simulation.cpp create mode 100644 src/simulation.h create mode 100644 src/tileset.h create mode 100644 src/vector.h (limited to 'src') diff --git a/src/consts.h b/src/consts.h new file mode 100644 index 0000000..f3a073c --- /dev/null +++ b/src/consts.h @@ -0,0 +1,10 @@ +#ifndef CONSTS_H_8019F1B3 +#define CONSTS_H_8019F1B3 + +#include "vector.h" + +constexpr vec2s TILE_SIZE { 32, 32 }; +constexpr vec2s LEVEL_SIZE { 16, 16 }; +constexpr vec2s WINDOW_SIZE = TILE_SIZE * LEVEL_SIZE; + +#endif /* end of include guard: CONSTS_H_8019F1B3 */ diff --git a/src/entity.h b/src/entity.h new file mode 100644 index 0000000..d99680f --- /dev/null +++ b/src/entity.h @@ -0,0 +1,31 @@ +#ifndef ENTITY_H_0D6CB29A +#define ENTITY_H_0D6CB29A + +#include "vector.h" + +class Entity { +public: + + // Transform + vec2s pos; + vec2s size; + + // Grid placement + vec2s gridPos; + + // Movement + bool moving = false; + vec2s destPos; + double movementTween = 0.0; + double speed = 0.0; // Tiles per second + + // Player + bool player = false; + + // Collision + bool playerCanPush = false; + bool trainCanPush = false; + +}; + +#endif /* end of include guard: ENTITY_H_0D6CB29A */ diff --git a/src/level.h b/src/level.h new file mode 100644 index 0000000..3d6fb87 --- /dev/null +++ b/src/level.h @@ -0,0 +1,52 @@ +#ifndef LEVEL_H_678CFCCF +#define LEVEL_H_678CFCCF + +#include "consts.h" +#include "tileset.h" + +class Level { +public: + + Level() + { + size_ = LEVEL_SIZE; + + tiles_.resize(size_.w() * size_.h()); + + for (size_t y = 0; y < size_.h(); y++) + { + for (size_t x = 0; x < size_.w(); x++) + { + tiles_[x+y*size_.w()] = rand() % 10; + } + } + } + + const vec2s& getSize() const + { + return size_; + } + + size_t at(vec2s pos) const + { + return at(pos.x(), pos.y()); + } + + size_t at(size_t x, size_t y) const + { + return tiles_.at(x + size_.w() * y); + } + + const Tileset& getTileset() const + { + return tileset_; + } + +private: + + vec2s size_; + std::vector tiles_; + Tileset tileset_; +}; + +#endif /* end of include guard: LEVEL_H_678CFCCF */ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..1b74143 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,60 @@ +#include "renderer.h" + +#include +#include "simulation.h" +#include "level.h" + +int main(int, char**) +{ + std::random_device randomEngine; + std::mt19937 rng(randomEngine()); + + Renderer renderer; + Level level; + Simulation sim(level); + + Simulation::id_type player = sim.emplaceEntity(); + Entity& entity = sim.getEntity(player); + entity.gridPos.x() = 1; + entity.gridPos.y() = 5; + entity.size = TILE_SIZE; + entity.speed = 3.0; + entity.player = true; + + bool quit = false; + + SDL_Event e; + size_t lastTime = SDL_GetTicks(); + + while (!quit) + { + size_t currentTime = SDL_GetTicks(); + size_t frameTime = currentTime - lastTime; + lastTime = currentTime; + + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) + { + quit = true; + } else if (e.type == SDL_KEYDOWN) + { + switch (e.key.keysym.sym) + { + case SDLK_ESCAPE: + { + quit = true; + + break; + } + } + } + } + + const Uint8* state = SDL_GetKeyboardState(NULL); + + sim.tick(frameTime / 1000.0, state); + + renderer.render(sim); + } +} diff --git a/src/renderer.cpp b/src/renderer.cpp new file mode 100644 index 0000000..2916dd6 --- /dev/null +++ b/src/renderer.cpp @@ -0,0 +1,97 @@ +#include "renderer.h" +#include "simulation.h" +#include "consts.h" +#include "level.h" + +Renderer::Renderer() +{ + win_ = window_ptr( + SDL_CreateWindow( + "dispatcher", + 100, + 100, + WINDOW_SIZE.w(), + WINDOW_SIZE.h(), + SDL_WINDOW_SHOWN)); + + if (!win_) + { + throw sdl_error(); + } + + ren_ = renderer_ptr( + SDL_CreateRenderer( + win_.get(), + -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); + + if (!ren_) + { + throw sdl_error(); + } + + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD); + SDL_SetRenderDrawColor(ren_.get(), 255, 150, 255, 255); + SDL_RenderFillRect(ren_.get(), nullptr); +} + +void Renderer::render(const Simulation& sim) +{ + texture_ptr canvas( + SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + WINDOW_SIZE.w(), + WINDOW_SIZE.h())); + + if (!canvas) + { + throw sdl_error(); + } + + SDL_SetRenderTarget(ren_.get(), nullptr); + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); + SDL_SetRenderDrawColor(ren_.get(), 255, 150, rand() % 255, 255); + SDL_RenderClear(ren_.get()); + + const Level& level = sim.getLevel(); + + for (size_t y = 0; y < level.getSize().h(); y++) + { + for (size_t x = 0; x < level.getSize().w(); x++) + { + int val = level.at(x, y) * 10; + + SDL_SetRenderDrawColor(ren_.get(), val, val, 0, 255); + + SDL_Rect rect { + static_cast(x * TILE_SIZE.w()), + static_cast(y * TILE_SIZE.h()), + TILE_SIZE.w(), + TILE_SIZE.h() + }; + + SDL_RenderFillRect(ren_.get(), &rect); + } + } + + for (Simulation::id_type id : sim.getActive()) + { + const Entity& entity = sim.getEntity(id); + + SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, 255); + + SDL_Rect rect { + static_cast(entity.pos.x()), + static_cast(entity.pos.y()), + static_cast(entity.size.w()), + static_cast(entity.size.h()) + }; + + SDL_RenderFillRect(ren_.get(), &rect); + } + + //SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); + SDL_RenderPresent(ren_.get()); +} diff --git a/src/renderer.h b/src/renderer.h new file mode 100644 index 0000000..dd3498f --- /dev/null +++ b/src/renderer.h @@ -0,0 +1,126 @@ +#ifndef RENDERER_H_6A58EC30 +#define RENDERER_H_6A58EC30 + +#include +#include +#include +#include + +class Simulation; + +class sdl_error : public std::logic_error { +public: + + sdl_error() : std::logic_error(SDL_GetError()) + { + } +}; + +class img_error : public std::logic_error { +public: + + img_error() : std::logic_error(IMG_GetError()) + { + } +}; + +class sdl_wrapper { +public: + + sdl_wrapper() + { + if (SDL_Init(SDL_INIT_VIDEO) != 0) + { + sdl_error ex; + SDL_Quit(); + + throw ex; + } + } + + ~sdl_wrapper() + { + SDL_Quit(); + } +}; + +class img_wrapper { +public: + + img_wrapper() + { + if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) + { + img_error ex; + IMG_Quit(); + + throw ex; + } + } + + ~img_wrapper() + { + IMG_Quit(); + } +}; + +class window_deleter { +public: + + void operator()(SDL_Window* ptr) + { + SDL_DestroyWindow(ptr); + } +}; + +using window_ptr = std::unique_ptr; + +class renderer_deleter { +public: + + void operator()(SDL_Renderer* ptr) + { + SDL_DestroyRenderer(ptr); + } +}; + +using renderer_ptr = std::unique_ptr; + +class surface_deleter { +public: + + void operator()(SDL_Surface* ptr) + { + SDL_FreeSurface(ptr); + } +}; + +using surface_ptr = std::unique_ptr; + +class texture_deleter { +public: + + void operator()(SDL_Texture* ptr) + { + SDL_DestroyTexture(ptr); + } +}; + +using texture_ptr = std::unique_ptr; + +class Renderer { +public: + + Renderer(); + + void render(const Simulation& game); + +private: + + sdl_wrapper sdl_; + img_wrapper img_; + window_ptr win_; + renderer_ptr ren_; +}; + +#endif /* end of include guard: RENDERER_H_6A58EC30 */ diff --git a/src/simulation.cpp b/src/simulation.cpp new file mode 100644 index 0000000..3079f56 --- /dev/null +++ b/src/simulation.cpp @@ -0,0 +1,115 @@ +#include "simulation.h" + +#include "consts.h" +#include "level.h" + +void Simulation::tick( + double dt, + const Uint8* keystate) +{ + for (id_type id : active_) + { + Entity& entity = entities_.at(id); + + // Control + if (entity.player && + !entity.moving) + { + if (keystate[SDL_SCANCODE_LEFT] && + entity.gridPos.x() > 0 && + level_.getTileset().canPlayerMoveTo( + level_.at(entity.gridPos - vec2s { 1, 0 }))) + { + entity.moving = true; + entity.destPos = entity.gridPos - vec2s { 1, 0 }; + } + else if (keystate[SDL_SCANCODE_UP] && + entity.gridPos.y() > 0 && + level_.getTileset().canPlayerMoveTo( + level_.at(entity.gridPos - vec2s { 0, 1 }))) + { + entity.moving = true; + entity.destPos = entity.gridPos - vec2s { 0, 1 }; + } else if (keystate[SDL_SCANCODE_RIGHT] && + entity.gridPos.x() < (level_.getSize().w() - 1) && + level_.getTileset().canPlayerMoveTo( + level_.at(entity.gridPos + vec2s { 1, 0 }))) + { + entity.moving = true; + entity.destPos = entity.gridPos + vec2s { 1, 0 }; + } + else if (keystate[SDL_SCANCODE_DOWN] && + entity.gridPos.y() < (level_.getSize().h() - 1) && + level_.getTileset().canPlayerMoveTo( + level_.at(entity.gridPos + vec2s { 0, 1 }))) + { + entity.moving = true; + entity.destPos = entity.gridPos + vec2s { 0, 1 }; + } + + if (entity.moving) + { + entity.movementTween = 0.0; + } + } + + + + + // Collision + + + + + // Movement + if (entity.moving) + { + entity.movementTween += entity.speed * dt; + + if (entity.movementTween >= 1.0) + { + entity.moving = false; + entity.gridPos = entity.destPos; + } + } + + if (entity.moving) + { + entity.pos.x() = + TILE_SIZE.x() * entity.destPos.x() * entity.movementTween + + TILE_SIZE.x() * entity.gridPos.x() * (1.0 - entity.movementTween); + + entity.pos.y() = + TILE_SIZE.y() * entity.destPos.y() * entity.movementTween + + TILE_SIZE.y() * entity.gridPos.y() * (1.0 - entity.movementTween); + } else { + entity.pos = TILE_SIZE * entity.gridPos; + } + } +} + +Simulation::id_type Simulation::emplaceEntity() +{ + id_type nextId; + + if (!available_.empty()) + { + nextId = available_.front(); + available_.pop_front(); + + entities_.at(nextId) = Entity(); + } else { + nextId = entities_.size(); + entities_.emplace_back(); + } + + active_.insert(nextId); + + return nextId; +} + +void Simulation::deleteEntity(id_type id) +{ + available_.push_back(id); + active_.erase(id); +} diff --git a/src/simulation.h b/src/simulation.h new file mode 100644 index 0000000..bc47642 --- /dev/null +++ b/src/simulation.h @@ -0,0 +1,57 @@ +#ifndef SIMULATION_H_7BF6EEA4 +#define SIMULATION_H_7BF6EEA4 + +#include "entity.h" +#include "renderer.h" +#include +#include +#include + +class Level; + +class Simulation { +public: + + using id_type = std::vector::size_type; + + // Constructor + explicit Simulation(const Level& level) : level_(level) {} + + void tick( + double dt, + const Uint8* keystate); + + id_type emplaceEntity(); + + void deleteEntity(id_type id); + + Entity& getEntity(id_type id) + { + return entities_.at(id); + } + + const Entity& getEntity(id_type id) const + { + return entities_.at(id); + } + + const std::set& getActive() const + { + return active_; + } + + const Level& getLevel() const + { + return level_; + } + +private: + + const Level& level_; + + std::vector entities_; + std::deque available_; + std::set active_; +}; + +#endif /* end of include guard: SIMULATION_H_7BF6EEA4 */ diff --git a/src/tileset.h b/src/tileset.h new file mode 100644 index 0000000..552b2f8 --- /dev/null +++ b/src/tileset.h @@ -0,0 +1,13 @@ +#ifndef TILESET_H_B89AE7A1 +#define TILESET_H_B89AE7A1 + +class Tileset { +public: + + bool canPlayerMoveTo(size_t tile) const + { + return (tile % 2 == 1); + } +}; + +#endif /* end of include guard: TILESET_H_B89AE7A1 */ diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..10fe4da --- /dev/null +++ b/src/vector.h @@ -0,0 +1,119 @@ +#ifndef COORDINATES_H_A45D34FB +#define COORDINATES_H_A45D34FB + +#include + +template +class vec2 { +public: + + T coords[2]; + + constexpr vec2() : coords{0, 0} + { + } + + constexpr vec2(T x, T y) : coords{x, y} + { + } + + inline T& x() + { + return coords[0]; + } + + constexpr inline const T& x() const + { + return coords[0]; + } + + inline T& w() + { + return coords[0]; + } + + constexpr inline const T& w() const + { + return coords[0]; + } + + inline T& y() + { + return coords[1]; + } + + constexpr inline const T& y() const + { + return coords[1]; + } + + inline T& h() + { + return coords[1]; + } + + constexpr inline const T& h() const + { + return coords[1]; + } + + template + constexpr operator vec2() const + { + return vec2(x(), y()); + } + + constexpr vec2 operator+(const vec2& other) const + { + return vec2(x() + other.x(), y() + other.y()); + } + + vec2& operator+=(const vec2& other) + { + x() += other.x(); + y() += other.y(); + + return *this; + } + + constexpr vec2 operator-(const vec2& other) const + { + return vec2(x() - other.x(), y() - other.y()); + } + + vec2 operator-=(const vec2& other) + { + x() -= other.x(); + y() -= other.y(); + + return *this; + } + + constexpr vec2 operator-() const + { + return vec2(-x(), -y()); + } + + constexpr vec2 operator*(T s) const + { + return vec2(x() * s, y() * s); + } + + vec2& operator*=(T s) + { + x() *= s; + y() *= s; + + return *this; + } + + constexpr vec2 operator*(const vec2& other) const + { + return vec2(x() * other.x(), y() * other.y()); + } + +}; + +using vec2s = vec2; + +#endif /* end of include guard: COORDINATES_H_A45D34FB */ -- cgit 1.4.1