aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-02-11 22:55:22 +0100
committerNao Pross <naopross@thearcway.org>2018-02-11 22:55:22 +0100
commite3fd07e606098dd9cdf0b7ed806340d8466b5572 (patch)
treecbf5cc81afcbbc62784f7c6fc02d4418cf03cee7
parentRemove redundant scenesOffsetX/Y, replaced with panX (diff)
downloadSubconscious-old-e3fd07e606098dd9cdf0b7ed806340d8466b5572.tar.gz
Subconscious-old-e3fd07e606098dd9cdf0b7ed806340d8466b5572.zip
Reimplement in C++ with SFML (mostly)
Diffstat (limited to '')
-rw-r--r--Makefile13
-rw-r--r--build.gradle14
-rw-r--r--src/main/cpp/Actor.cpp27
-rw-r--r--src/main/cpp/Main.cpp10
-rw-r--r--src/main/cpp/Map.cpp28
-rw-r--r--src/main/cpp/Subconscious.cpp60
-rw-r--r--src/main/cpp/WorldScene.cpp28
-rw-r--r--src/main/headers/Actor.hpp40
-rw-r--r--src/main/headers/Map.hpp25
-rw-r--r--src/main/headers/Scene.hpp12
-rw-r--r--src/main/headers/Subconscious.hpp33
-rw-r--r--src/main/headers/Tile.hpp17
-rw-r--r--src/main/headers/WorldScene.hpp33
13 files changed, 340 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index b038e89..cedbfa9 100644
--- a/Makefile
+++ b/Makefile
@@ -1,2 +1,15 @@
all:
+ gradle build
+
+java:
+ gradle compileJava
+
+runjava: java
gradle run
+
+
+cpp:
+ gradle mainExecutable
+
+runcpp: cpp
+ ./build/exe/main/main
diff --git a/build.gradle b/build.gradle
index b57ab80..4d16dd1 100644
--- a/build.gradle
+++ b/build.gradle
@@ -10,3 +10,17 @@ jar {
)
}
}
+
+apply plugin: 'cpp'
+
+
+model {
+ components {
+ main(NativeExecutableSpec) {
+ binaries.all {
+ cppCompiler.args '-std=c++14', '-Wall'
+ linker.args '-lsfml-graphics', '-lsfml-window', '-lsfml-system'
+ }
+ }
+ }
+}
diff --git a/src/main/cpp/Actor.cpp b/src/main/cpp/Actor.cpp
new file mode 100644
index 0000000..219488d
--- /dev/null
+++ b/src/main/cpp/Actor.cpp
@@ -0,0 +1,27 @@
+#include "Actor.hpp"
+
+Actor::Actor(std::string _name, unsigned _maxHp, Type _type) :
+ name(_name), maxHp(_maxHp), type(_type)
+{}
+
+Actor::~Actor()
+{}
+
+void Actor::damage(int amt)
+{
+ _hp -= amt;
+
+ if (_hp < 0) {
+ _hp = 0;
+ _alive = false;
+ }
+}
+
+void Actor::heal(int amt)
+{
+ _hp += amt;
+
+ if (_hp > maxHp) {
+ _hp = maxHp;
+ }
+}
diff --git a/src/main/cpp/Main.cpp b/src/main/cpp/Main.cpp
new file mode 100644
index 0000000..9d46e8d
--- /dev/null
+++ b/src/main/cpp/Main.cpp
@@ -0,0 +1,10 @@
+#include "Subconscious.hpp"
+
+int main(int argc, char *argv[])
+{
+ Subconscious game;
+ // game.run();
+ game.demo();
+
+ return 0;
+}
diff --git a/src/main/cpp/Map.cpp b/src/main/cpp/Map.cpp
new file mode 100644
index 0000000..6d3ee01
--- /dev/null
+++ b/src/main/cpp/Map.cpp
@@ -0,0 +1,28 @@
+#include "Map.hpp"
+
+// TODO remove hardcoded values
+Map::Map() : width(50), height(50)
+{
+ for (unsigned i = 0; i < width; i++) {
+ for (unsigned j = 0; j < height; j++) {
+ // TODO remove hardcoded values (grass)
+ Tile tile(Tile::Type::GRASS, i, j);
+ _tiles.push_back(tile);
+ }
+ }
+}
+
+Tile& Map::tile(unsigned x, unsigned y)
+{
+ return _tiles[y * width + x];
+}
+
+const std::vector<Tile>& Map::tiles() const
+{
+ return _tiles;
+}
+
+bool Map::moveActor(Tile &from, Tile &to)
+{
+ // TODO
+}
diff --git a/src/main/cpp/Subconscious.cpp b/src/main/cpp/Subconscious.cpp
new file mode 100644
index 0000000..942b0b4
--- /dev/null
+++ b/src/main/cpp/Subconscious.cpp
@@ -0,0 +1,60 @@
+#include "Subconscious.hpp"
+#include "WorldScene.hpp"
+
+#include <SFML/Graphics.hpp>
+
+/*** public methods ***/
+
+Subconscious::Subconscious()
+ : _window(sf::VideoMode(800, 600), "Subconscious")
+{
+ _window.setVerticalSyncEnabled(true);
+ _window.setFramerateLimit(90);
+}
+
+Subconscious::~Subconscious()
+{}
+
+void Subconscious::run()
+{
+ _running = true;
+ while (_running && _window.isOpen()) {
+ gameUpdate();
+ gameRender();
+ }
+}
+
+void Subconscious::demo()
+{
+ Scene *demoScene = new WorldScene();
+
+ _currentScene = demoScene;
+ _scenes.push_back(demoScene);
+
+ run();
+}
+
+
+/*** private methods ***/
+
+void Subconscious::gameUpdate()
+{
+ sf::Event event;
+
+ while (_window.pollEvent(event)) {
+ if (event.type == sf::Event::Closed) {
+ _window.close();
+ }
+ }
+}
+
+void Subconscious::gameRender()
+{
+ _window.clear();
+
+ if (_currentScene != nullptr) {
+ _currentScene->render(_window);
+ }
+
+ _window.display();
+}
diff --git a/src/main/cpp/WorldScene.cpp b/src/main/cpp/WorldScene.cpp
new file mode 100644
index 0000000..ab1c4d0
--- /dev/null
+++ b/src/main/cpp/WorldScene.cpp
@@ -0,0 +1,28 @@
+#include "WorldScene.hpp"
+
+WorldScene::WorldScene()
+{
+ _tileSize = DEFAULT_TILE_SIZE_PX;
+}
+
+void WorldScene::render(sf::RenderWindow &window)
+{
+ sf::RectangleShape rect;
+
+ rect.setSize(sf::Vector2f(_tileSize, _tileSize));
+ rect.setFillColor(sf::Color::Green);
+
+ rect.setOutlineThickness(-.5);
+ rect.setOutlineColor(sf::Color::Black);
+
+ for (const Tile &tile : map.tiles()) {
+ rect.setPosition(tile.x * _tileSize, tile.y * _tileSize);
+ window.draw(rect);
+ }
+}
+
+void WorldScene::zoom(int value)
+{}
+
+void WorldScene::pan(int dx, int dy)
+{}
diff --git a/src/main/headers/Actor.hpp b/src/main/headers/Actor.hpp
new file mode 100644
index 0000000..7cc3be5
--- /dev/null
+++ b/src/main/headers/Actor.hpp
@@ -0,0 +1,40 @@
+#ifndef ACTOR_HPP
+#define ACTOR_HPP
+
+#include <string>
+
+class Actor
+{
+public:
+ const enum Type
+ {
+ PLAYER, ENEMY
+ } type;
+
+ const unsigned maxHp;
+ const std::string name;
+
+ Actor() = delete;
+ Actor(std::string _name, unsigned _maxHp, Type _type);
+ virtual ~Actor();
+
+ void damage(int amt);
+ void heal(int amt);
+
+ // TODO shouldnt be done by the map ?
+ // bool move(int x, y);
+
+ /* accessors */
+ bool alive() { return _alive; }
+ unsigned hp() { return _hp; }
+
+ int x() { return _x; }
+ int y() { return _y; }
+
+private:
+ bool _alive;
+ unsigned _hp;
+ int _x, _y;
+};
+
+#endif
diff --git a/src/main/headers/Map.hpp b/src/main/headers/Map.hpp
new file mode 100644
index 0000000..81c456e
--- /dev/null
+++ b/src/main/headers/Map.hpp
@@ -0,0 +1,25 @@
+#ifndef MAP_HPP
+#define MAP_HPP
+
+#include "Tile.hpp"
+
+#include <vector>
+
+class Map
+{
+public:
+ const unsigned width;
+ const unsigned height;
+
+ Map();
+
+ Tile& tile(unsigned x, unsigned y);
+ const std::vector<Tile>& tiles() const;
+
+ bool moveActor(Tile &from, Tile &to);
+
+private:
+ std::vector<Tile> _tiles;
+};
+
+#endif
diff --git a/src/main/headers/Scene.hpp b/src/main/headers/Scene.hpp
new file mode 100644
index 0000000..2b19d31
--- /dev/null
+++ b/src/main/headers/Scene.hpp
@@ -0,0 +1,12 @@
+#ifndef SCENE_HPP
+#define SCENE_HPP
+
+#include <SFML/Graphics.hpp>
+
+class Scene
+{
+public:
+ virtual void render(sf::RenderWindow &window) = 0;
+};
+
+#endif
diff --git a/src/main/headers/Subconscious.hpp b/src/main/headers/Subconscious.hpp
new file mode 100644
index 0000000..8713757
--- /dev/null
+++ b/src/main/headers/Subconscious.hpp
@@ -0,0 +1,33 @@
+#ifndef SUBCONSCIOUS_HPP
+#define SUBCONSCIOUS_HPP
+
+#include "Scene.hpp"
+
+#include <SFML/Graphics.hpp>
+#include <vector>
+
+class Subconscious
+{
+public:
+ Subconscious();
+ ~Subconscious();
+
+ void run();
+ void demo();
+
+private:
+ // game
+ bool _running = false;
+ bool _gameOver = false;
+
+ // graphics
+ sf::RenderWindow _window;
+
+ std::vector<Scene *> _scenes;
+ Scene *_currentScene = nullptr;
+
+ void gameUpdate();
+ void gameRender();
+};
+
+#endif
diff --git a/src/main/headers/Tile.hpp b/src/main/headers/Tile.hpp
new file mode 100644
index 0000000..7292fde
--- /dev/null
+++ b/src/main/headers/Tile.hpp
@@ -0,0 +1,17 @@
+#ifndef TILE_HPP
+#define TILE_HPP
+
+struct Tile
+{
+ enum Type
+ {
+ GRASS, WATER
+ };
+
+ const Type type;
+ const int x, y;
+
+ Tile(Type _type, int _x, int _y) : type(_type), x(_x), y(_y) {}
+};
+
+#endif
diff --git a/src/main/headers/WorldScene.hpp b/src/main/headers/WorldScene.hpp
new file mode 100644
index 0000000..acdda63
--- /dev/null
+++ b/src/main/headers/WorldScene.hpp
@@ -0,0 +1,33 @@
+#ifndef WORLDSCENE_HPP
+#define WORLDSCENE_HPP
+
+#include "Scene.hpp"
+#include "Map.hpp"
+
+#include <SFML/Graphics.hpp>
+
+
+class WorldScene : public Scene
+{
+public:
+ constexpr static int ZOOM_MIN = 1;
+ constexpr static int ZOOM_MAX = 100;
+ constexpr static int DEFAULT_TILE_SIZE_PX = 20;
+
+ WorldScene();
+
+ virtual void render(sf::RenderWindow &window);
+
+ virtual void zoom(int value);
+ virtual void pan(int dx, int dy);
+
+private:
+ Map map;
+
+ unsigned _tileSize;
+
+ int _panX = 0;
+ int _panY = 0;
+};
+
+#endif