Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9c6353f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+.idea
+cmake-build-debug
+cmake-build-release
+include
+lib
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..c11b660
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,7 @@
+cmake_minimum_required(VERSION 3.14)
+project(SDL_Game)
+set(CMAKE_CXX_STANDARD 14)
+include_directories(include)
+link_directories(lib)
+link_libraries(mingw32 SDL2main SDL2 SDL2_image)
+add_executable(SDL_Game src/main.cpp src/Game.cpp src/Game.h src/Entity.cpp src/Entity.h src/Component.cpp src/Component.h src/System.cpp src/System.h src/systems/RenderSystem.cpp src/systems/RenderSystem.h src/components/Visibility.cpp src/components/Visibility.h)
diff --git a/src/Component.cpp b/src/Component.cpp
new file mode 100644
index 0000000..22d4c59
--- /dev/null
+++ b/src/Component.cpp
@@ -0,0 +1,6 @@
+//
+// Created by Ido Mozes on 20/06/2019.
+//
+
+#include "Component.h"
+
diff --git a/src/Component.h b/src/Component.h
new file mode 100644
index 0000000..289f0b4
--- /dev/null
+++ b/src/Component.h
@@ -0,0 +1,28 @@
+//
+// Created by Ido Mozes on 20/06/2019.
+//
+
+#ifndef SDL2_GAME_COMPONENT_H
+#define SDL2_GAME_COMPONENT_H
+
+
+class Entity;
+
+enum ComponentType {
+ POSITION, VISIBILITY,
+ LENGTH
+
+};
+
+class Component {
+ Entity *entity;
+public:
+ explicit Component(Entity *entity) : entity(entity) {};
+
+ ~Component() = default;
+
+ Entity *getEntity() { return entity; }
+
+};
+
+#endif //SDL2_GAME_COMPONENT_H
diff --git a/src/Entity.cpp b/src/Entity.cpp
new file mode 100644
index 0000000..5358b18
--- /dev/null
+++ b/src/Entity.cpp
@@ -0,0 +1,10 @@
+//
+// Created by Ido Mozes on 20/06/2019.
+//
+
+#include "Entity.h"
+
+Entity::Entity():componentsMask() ,components(){
+ id = getNewId();
+}
+
diff --git a/src/Entity.h b/src/Entity.h
new file mode 100644
index 0000000..1f989c6
--- /dev/null
+++ b/src/Entity.h
@@ -0,0 +1,52 @@
+//
+// Created by Ido Mozes on 20/06/2019.
+//
+
+#ifndef SDL2_GAME_ENTITY_H
+#define SDL2_GAME_ENTITY_H
+
+#include <bitset>
+#include <vector>
+#include <memory>
+#include "Component.h"
+
+constexpr int maxComponents = 64;
+typedef std::size_t EntityId;
+
+
+class Entity {
+ static EntityId getNewId() {
+ static EntityId id_counter = 0;
+ return id_counter++;
+ }
+
+ bool active = true;
+ EntityId id;
+ std::bitset<ComponentType::LENGTH> componentsMask;
+ std::array<Component *, ComponentType::LENGTH> components;
+public:
+ Entity();
+
+ ~Entity() = default;
+
+ EntityId getId() { return id; }
+
+ bool isActive() { return active; }
+
+ void destroy() { active = false; }
+
+ bool hasComponents(uint64_t mask) { return (componentsMask.to_ullong() & mask) == mask; }
+
+ template<class T>
+ T &getComponent() { return *(T *) (components[T::getComponentType()]); }
+
+ template<typename T, typename ... Targs>
+ Component &addComponent(Targs &&... args){
+ T *c = new T(this, std::forward<Targs>(args)...);
+ components[T::getComponentType()] = c;
+ componentsMask[T::getComponentType()] = true;
+ return *c;
+ }
+};
+
+#endif //SDL2_GAME_ENTITY_H
diff --git a/src/Game.cpp b/src/Game.cpp
new file mode 100644
index 0000000..ae05be7
--- /dev/null
+++ b/src/Game.cpp
@@ -0,0 +1,42 @@
+//
+// Created by Ido Mozes on 18/06/2019.
+//
+
+#include <array>
+#include "Game.h"
+
+
+
+Game::Game(bool fullscreen):renderSystem(fullscreen),isRunning(true) {
+ auto player = new Entity();
+ player->addComponent<Visibility>(renderSystem.getRenderer(),"../assets/knight.png",new SDL_Rect{0,0,128,128});
+ entities.emplace_back(player);
+ auto player2 = new Entity();
+ player2->addComponent<Visibility>(renderSystem.getRenderer(),"../assets/knight.png",new SDL_Rect{0,150,128,128});
+ entities.emplace_back(player2);
+// systems = {
+//
+// };
+}
+
+Game::~Game() {
+ SDL_Quit();
+}
+
+void Game::handleEvents() {
+ SDL_Event event;
+ SDL_PollEvent(&event);
+ switch (event.type) {
+ case SDL_QUIT:
+ isRunning = false;
+ break;
+ default:
+ break;
+ }
+}
+
+void Game::update() {
+ renderSystem.update(entities);
+}
+
+
diff --git a/src/Game.h b/src/Game.h
new file mode 100644
index 0000000..0c34399
--- /dev/null
+++ b/src/Game.h
@@ -0,0 +1,39 @@
+//
+// Created by Ido Mozes on 18/06/2019.
+//
+
+#ifndef SDL2_GAME_GAME_H
+#define SDL2_GAME_GAME_H
+
+#include <iostream>
+#include <memory>
+#include <vector>
+
+#include "SDL.h"
+#include "SDL_image.h"
+
+#include "Entity.h"
+#include "System.h"
+#include "systems/RenderSystem.h"
+#include "components/Visibility.h"
+
+class Game {
+ bool isRunning;
+ std::vector<std::unique_ptr<System>> systems;
+ std::vector<std::unique_ptr<Entity>> entities;
+ RenderSystem renderSystem;
+public:
+ explicit Game(bool fullscreen);
+
+ ~Game();
+
+ void handleEvents();
+
+ void update();
+
+
+ bool running() { return isRunning; }
+
+};
+
+#endif //SDL2_GAME_GAME_H
diff --git a/src/System.cpp b/src/System.cpp
new file mode 100644
index 0000000..2c9b5b8
--- /dev/null
+++ b/src/System.cpp
@@ -0,0 +1,6 @@
+//
+// Created by Ido Mozes on 23/06/2019.
+//
+
+#include "System.h"
+
diff --git a/src/System.h b/src/System.h
new file mode 100644
index 0000000..c5949d4
--- /dev/null
+++ b/src/System.h
@@ -0,0 +1,23 @@
+//
+// Created by Ido Mozes on 23/06/2019.
+//
+
+#ifndef SDL2_GAME_SYSTEM_H
+#define SDL2_GAME_SYSTEM_H
+#include <initializer_list>
+#include <vector>
+#include <cmath>
+#include "Entity.h"
+
+constexpr uint64_t createMask( std::initializer_list<int> types) noexcept {
+ uint64_t mask = 0;
+ for (int bit : types) {
+ mask |= (uint64_t) pow(2, bit);
+ }
+ return mask;
+}
+class System{
+public:
+ virtual void update(std::vector<std::unique_ptr<Entity>> &entities)=0;
+};
+#endif //SDL2_GAME_SYSTEM_H
diff --git a/src/components/Visibility.cpp b/src/components/Visibility.cpp
new file mode 100644
index 0000000..f668823
--- /dev/null
+++ b/src/components/Visibility.cpp
@@ -0,0 +1,23 @@
+//
+// Created by Ido Mozes on 23/06/2019.
+//
+
+#include "Visibility.h"
+SDL_Texture *loadTexture(SDL_Renderer * renderer, const char *path) {
+ SDL_Surface *tmpSurface = IMG_Load(path);
+ SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, tmpSurface);
+ SDL_FreeSurface(tmpSurface);
+ return tex;
+}
+Visibility::Visibility(Entity *entity, SDL_Renderer *renderer, const char *path, SDL_Rect *dstR, SDL_Rect *srcR)
+ : Component(entity) {
+ texture = loadTexture(renderer, path);
+ setDstRect(dstR);
+ setSrcRect(srcR);
+}
+
+Visibility::~Visibility() {
+ delete dstRect;
+ delete srcRect;
+ SDL_DestroyTexture(texture);
+}
diff --git a/src/components/Visibility.h b/src/components/Visibility.h
new file mode 100644
index 0000000..97bfdb8
--- /dev/null
+++ b/src/components/Visibility.h
@@ -0,0 +1,46 @@
+//
+// Created by Ido Mozes on 23/06/2019.
+//
+
+#ifndef SDL2_GAME_VISIBILITY_H
+#define SDL2_GAME_VISIBILITY_H
+#include "SDL.h"
+#include "SDL_image.h"
+
+
+#include "../Component.h"
+
+SDL_Texture *loadTexture(SDL_Renderer *renderer, const char *path);
+
+class Visibility : public Component {
+ SDL_Texture *texture;
+ SDL_Rect *srcRect;
+ SDL_Rect *dstRect;
+
+public:
+ static ComponentType getComponentType() { return ComponentType::VISIBILITY; };
+
+ Visibility(Entity *entity, SDL_Renderer *renderer, const char *path, SDL_Rect *dstR = nullptr,
+ SDL_Rect *srcR = nullptr);
+
+ ~Visibility();
+
+ SDL_Texture *getTexture() { return texture; }
+
+ SDL_Rect *getSrcRect() { return srcRect; }
+
+ SDL_Rect *getDstRect() { return dstRect; }
+
+ void setSrcRect(SDL_Rect *srcR) {
+ srcRect = srcR ? new SDL_Rect(*srcR) : nullptr;
+ delete srcR;
+ }
+
+ void setDstRect(SDL_Rect *dstR) {
+ dstRect = dstR ? new SDL_Rect(*dstR) : nullptr;
+ delete dstR;
+ }
+
+};
+
+#endif //SDL2_GAME_VISIBILITY_H
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..f8b6eb7
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,21 @@
+//#include <iostream>
+#include "SDL.h"
+#include "Game.h"
+
+
+int main(int argc, char *argv[]) {
+ const int FPS = 60, frameDelay = 1000 / FPS;
+ Uint32 frameStart;
+ int frameTime;
+ Game game(false);
+ while (game.running()) {
+ frameStart = SDL_GetTicks();
+ game.handleEvents();
+ game.update();
+ frameTime = SDL_GetTicks() - frameStart;
+ if (frameDelay > frameTime) {
+ SDL_Delay(frameDelay - frameTime);
+ }
+ }
+ return 0;
+}
\ No newline at end of file
diff --git a/src/systems/RenderSystem.cpp b/src/systems/RenderSystem.cpp
new file mode 100644
index 0000000..2e84616
--- /dev/null
+++ b/src/systems/RenderSystem.cpp
@@ -0,0 +1,35 @@
+//
+// Created by Ido Mozes on 23/06/2019.
+//
+
+#include "RenderSystem.h"
+#include "../components/Visibility.h"
+
+
+uint64_t RenderSystem::mask = createMask({ComponentType::VISIBILITY});
+
+RenderSystem::RenderSystem(bool fullscreen) {
+ SDL_Init(SDL_INIT_EVERYTHING);
+ window = SDL_CreateWindow("BloonsTD", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720,
+ fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
+ renderer = SDL_CreateRenderer(window, -1, 0);
+ SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
+}
+
+RenderSystem::~RenderSystem() {
+ SDL_DestroyWindow(window);
+ SDL_DestroyRenderer(renderer);
+}
+
+void RenderSystem::update(std::vector<std::unique_ptr<Entity>>& entities) {
+ SDL_RenderClear(renderer);
+ for (auto &entity: entities) {
+ if (entity->hasComponents(mask)) {
+ auto &v = entity->getComponent<Visibility>();
+ SDL_RenderCopy(renderer, v.getTexture(), v.getSrcRect(), v.getDstRect());
+ }
+ }
+ SDL_RenderPresent(renderer);
+}
+
+
diff --git a/src/systems/RenderSystem.h b/src/systems/RenderSystem.h
new file mode 100644
index 0000000..3da3e88
--- /dev/null
+++ b/src/systems/RenderSystem.h
@@ -0,0 +1,26 @@
+//
+// Created by Ido Mozes on 23/06/2019.
+//
+
+#ifndef SDL2_GAME_RENDERSYSTEM_H
+#define SDL2_GAME_RENDERSYSTEM_H
+
+#include <iostream>
+#include "../components/Visibility.h"
+#include "../System.h"
+
+class RenderSystem : public System {
+ static uint64_t mask;
+ SDL_Window *window;
+ SDL_Renderer *renderer;
+public:
+ explicit RenderSystem(bool fullscreen);
+
+ ~RenderSystem();
+
+ SDL_Renderer *getRenderer() { return renderer; }
+
+ void update(std::vector<std::unique_ptr<Entity>> &entities) override;
+};
+
+#endif //SDL2_GAME_RENDERSYSTEM_H

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jun 16, 12:20 AM (2 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
70771
Default Alt Text
(10 KB)

Event Timeline