Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F102522
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/enfucraft.pro b/enfucraft.pro
new file mode 100644
index 0000000..354d98b
--- /dev/null
+++ b/enfucraft.pro
@@ -0,0 +1,17 @@
+TEMPLATE = app
+CONFIG += console
+CONFIG -= app_bundle
+CONFIG -= qt
+CONFIG += c++11
+SOURCES += main.cpp \
+ game.cpp \
+ tilemap.cpp
+
+LIBS += -lsfml-window -lsfml-system -lsfml-graphics
+
+HEADERS += \
+ game.h \
+ tilemap.h
+
+OTHER_FILES += \
+ assets/img/zw-tilesets/_MAP.png
diff --git a/game.cpp b/game.cpp
new file mode 100644
index 0000000..24337d6
--- /dev/null
+++ b/game.cpp
@@ -0,0 +1,60 @@
+#include <stdlib.h>
+#include "game.h"
+namespace efc {
+Game::Game()
+{
+ sf::RenderWindow window(sf::VideoMode(512, 512), "Tilemap");
+ // Grass tile starts at 342 and has 11 tiles
+ int level[256];
+ /* =
+ {
+ 342, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 80, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
+ 1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
+ 0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
+ 0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
+ 0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
+ 2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
+ 0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
+ 342, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 80, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
+ 1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
+ 0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
+ 0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
+ 0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
+ 2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
+ 0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
+ };*/
+
+ // Fill the array
+ for (int i=0;i<256;i++)
+ {
+ int grass = rand() % 11;
+ level[i] = 342 + grass;
+ }
+
+ // create the tilemap from the level definition
+ TileMap map;
+ if (!map.load("/home/bluszcz/repo/enfucraft/assets/img/zw-tilesets/_MAP.png", sf::Vector2u(25, 25), level, 16, 16))
+ std::exit(1);
+
+
+ // run the main loop
+ while (window.isOpen())
+ {
+ // handle events
+ sf::Event event;
+ while (window.pollEvent(event))
+ {
+ if(event.type == sf::Event::Closed)
+ window.close();
+ }
+
+ // draw the map
+ window.clear();
+ window.draw(map);
+ window.display();
+ }
+
+}
+}
diff --git a/game.h b/game.h
new file mode 100644
index 0000000..fb3e6a5
--- /dev/null
+++ b/game.h
@@ -0,0 +1,15 @@
+#ifndef GAME_H
+#define GAME_H
+#include <SFML/Window.hpp>
+#include <SFML/Graphics.hpp>
+#include "tilemap.h"
+
+namespace efc {
+class Game
+{
+public:
+ Game();
+ sf::RenderWindow window;
+};
+}
+#endif // GAME_H
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..425a908
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+#include "game.h"
+using namespace std;
+
+int main()
+{
+ cout << "Hello World!" << endl;
+
+ efc::Game game;
+ return 0;
+
+
+}
+
diff --git a/tilemap.cpp b/tilemap.cpp
new file mode 100644
index 0000000..90ba1ae
--- /dev/null
+++ b/tilemap.cpp
@@ -0,0 +1,62 @@
+#include "tilemap.h"
+
+TileMap::TileMap()
+{
+}
+
+ bool TileMap::load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
+ {
+ // load the tileset texture
+ if (!m_tileset.loadFromFile(tileset))
+ return false;
+
+ // resize the vertex array to fit the level size
+ m_vertices.setPrimitiveType(sf::Quads);
+ m_vertices.resize(width * height * 4);
+
+ // populate the vertex array, with one quad per tile
+ for (unsigned int i = 0; i < width; ++i)
+ for (unsigned int j = 0; j < height; ++j)
+ {
+ // get the current tile number
+ int tileNumber = tiles[i + j * width];
+
+ // find its position in the tileset texture
+ int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
+ int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
+
+ // get a pointer to the current tile's quad
+ sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
+
+ // define its 4 corners
+ quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
+ quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
+ quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
+ quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
+
+ // define its 4 texture coordinates
+ quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
+ quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
+ quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
+ quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
+ }
+
+ return true;
+ }
+
+
+
+ void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
+ {
+ // apply the transform
+ states.transform *= getTransform();
+
+ // apply the tileset texture
+ states.texture = &m_tileset;
+
+ // draw the vertex array
+ target.draw(m_vertices, states);
+ }
+
+ sf::VertexArray m_vertices;
+ sf::Texture m_tileset;
diff --git a/tilemap.h b/tilemap.h
new file mode 100644
index 0000000..9a9d119
--- /dev/null
+++ b/tilemap.h
@@ -0,0 +1,20 @@
+#ifndef TILEMAP_H
+#define TILEMAP_H
+#include <SFML/Window.hpp>
+#include <SFML/Graphics.hpp>
+#include <SFML/System.hpp>
+
+class TileMap : public sf::Drawable, public sf::Transformable
+{
+public:
+ bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);
+ TileMap();
+
+private:
+ virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
+ sf::VertexArray m_vertices;
+ sf::Texture m_tileset;
+
+};
+
+#endif // TILEMAP_H
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Feb 2, 9:13 PM (1 d, 19 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55593
Default Alt Text
(6 KB)
Attached To
Mode
R82 deerportal
Attached
Detach File
Event Timeline
Log In to Comment