Skip to content

Commit

Permalink
change water style
Browse files Browse the repository at this point in the history
  • Loading branch information
averrin committed Oct 6, 2017
1 parent 9fb61d5 commit c4d42c2
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ set (mapgen_VERSION_PATCH 1)
file(GLOB SOURCE "src/*.cpp" "include/mapgen/*.h")
file(COPY "font.ttf" DESTINATION "bin")
file(COPY "images" DESTINATION "bin")
file(COPY "images" DESTINATION "bin")
file(COPY "src/blur.frag" DESTINATION "bin")

configure_file (
"${PROJECT_SOURCE_DIR}/MapgenConfig.h.in"
Expand Down
Binary file modified mapgen_screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 78 additions & 9 deletions src/Painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "mapgen/Walker.hpp"
#include "rang.hpp"

#include <cmath>

// TODO: move ints to utils.cpp
template <typename T> using filterFunc = std::function<bool(T *)>;
template <typename T> using sortFunc = std::function<bool(T *, T *)>;
Expand Down Expand Up @@ -52,6 +54,12 @@ class Painter {
bgColor.g = static_cast<sf::Uint8>(color[1] * 255.f);
bgColor.b = static_cast<sf::Uint8>(color[2] * 255.f);
window->clear(bgColor);

char spath[100];
sprintf(spath, "%s/blur.frag", dir.c_str());

shader_blur.loadFromFile(spath, sf::Shader::Type::Fragment);
shader_blur.setUniform("blur_radius", 0.004f);
};

sf::Font sffont;
Expand All @@ -69,6 +77,7 @@ class Painter {
bool needUpdate = true;
sf::Clock clock;
std::vector<Walker*> walkers;
sf::Shader shader_blur;

std::string get_selfpath() {
char buff[PATH_MAX];
Expand Down Expand Up @@ -113,6 +122,7 @@ class Painter {

public:
std::vector<sf::ConvexShape> polygons;
std::vector<sf::ConvexShape> waterPolygons;
std::vector<sf::ConvexShape> infoPolygons;
std::vector<sf::CircleShape> poi;
std::vector<sf::Sprite> sprites;
Expand All @@ -131,6 +141,8 @@ class Painter {
bool states = true;
bool areas = false;
bool showWalkers = true;
bool lables = true;
bool blur = true;

bool isIncreasing{true};

Expand Down Expand Up @@ -170,7 +182,7 @@ class Painter {
if (mapgen->map != nullptr) {
sf::Text operation(mapgen->map->status, sffont);
operation.setCharacterSize(20);
operation.setColor(sf::Color::White);
operation.setFillColor(sf::Color::White);

auto middle = (sf::Vector2f(window->getSize())) / 2.f;
operation.setPosition(sf::Vector2f(
Expand Down Expand Up @@ -325,8 +337,61 @@ class Painter {
}
}

void drawLables() {
for (auto c : mapgen->map->cities) {

sf::RectangleShape bg;
bg.setFillColor(sf::Color(50,30,22, 200));
if (c->isCapital) {
bg.setOutlineColor(c->region->state->color);
} else {
bg.setOutlineColor(sf::Color(200,200,180, 180));
}
bg.setOutlineThickness(1);

sf::Text label(c->name, sffont);
label.setCharacterSize(10);
label.setFillColor(sf::Color(255,255,220));
label.setPosition(sf::Vector2f(c->region->site->x, c->region->site->y + 10));

bg.setSize(sf::Vector2f(label.getGlobalBounds().width + 8, 18));
bg.setPosition(sf::Vector2f(c->region->site->x - 4, c->region->site->y + 7));

window->draw(bg);
window->draw(label);
}
}

void drawMap() {
if (needUpdate) {
sf::Vector2u windowSize = window->getSize();
for (auto p : waterPolygons) {
window->draw(p);
}

if (blur) {
for (auto p : polygons) {
window->draw(p);
}
cachedMap.create(windowSize.x, windowSize.y);
cachedMap.update(*window);

sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(window->getSize().x, window->getSize().y));
rectangle.setPosition(0, 0);
rectangle.setTexture(&cachedMap);

window->draw(rectangle, &shader_blur);

for (auto p : polygons) {
auto p2 = sf::ConvexShape(p);
p2.setFillColor(bgColor);
p2.setOutlineColor(sf::Color(50,20,0));
p2.setOutlineThickness(2);
window->draw(p2);
}
}

for (auto p : polygons) {
window->draw(p);
}
Expand All @@ -339,11 +404,10 @@ class Painter {
window->draw(sprite);
}

// if (info) {
// for (auto p : poi) {
// window->draw(p);
// }
// }
if (lables) {
drawLables();
}

if (states) {
drawBorders();
}
Expand All @@ -368,7 +432,6 @@ class Painter {
}
drawMark();

sf::Vector2u windowSize = window->getSize();
cachedMap.create(windowSize.x, windowSize.y);
cachedMap.update(*window);
needUpdate = false;
Expand All @@ -377,6 +440,7 @@ class Painter {
rectangle.setSize(sf::Vector2f(window->getSize().x, window->getSize().y));
rectangle.setPosition(0, 0);
rectangle.setTexture(&cachedMap);

window->draw(rectangle);
}
}
Expand Down Expand Up @@ -469,7 +533,7 @@ class Painter {
sprintf(mt, "Mapgen [%s] by Averrin", VERSION.c_str());
sf::Text mark(mt, sffont);
mark.setCharacterSize(15);
mark.setColor(sf::Color::White);
mark.setFillColor(sf::Color::White);
mark.setPosition(sf::Vector2f(windowSize.x - 240, windowSize.y - 25));
window->draw(mark);
}
Expand All @@ -483,6 +547,7 @@ class Painter {
void update() {
infoPolygons.clear();
polygons.clear();
waterPolygons.clear();
poi.clear();
sprites.clear();
walkers.clear();
Expand Down Expand Up @@ -610,7 +675,11 @@ class Painter {
polygon.setFillColor(col);
}
}
polygons.push_back(polygon);
if (region->megaCluster->isLand) {
polygons.push_back(polygon);
} else {
waterPolygons.push_back(polygon);
}
}

needUpdate = true;
Expand Down
4 changes: 3 additions & 1 deletion src/Walker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Walker::Walker(City *c, MapGenerator *m) : mapgen(m) {
// road = *mapgen->select_randomly(s->roads.begin(), s->roads.end());
reverse = rand() % 2 == 0;
shape = new sf::CircleShape(3);
shape->setFillColor(sf::Color::Red);
shape->setFillColor(sf::Color(225,225,190));
shape->setOutlineColor(sf::Color::Black);
shape->setOutlineThickness(1);
road = *select_randomly(c->roads.begin(), c->roads.end());
reverse = road->regions.back()->city == c;
if (reverse) {
Expand Down
23 changes: 18 additions & 5 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Application {
void initMapGen() {
seed = std::chrono::system_clock::now().time_since_epoch().count();
mapgen = new MapGenerator(window->getSize().x, window->getSize().y);
//mapgen->setSeed(28167073);
// mapgen->setSeed(28167073);
octaves = mapgen->getOctaveCount();
freq = mapgen->getFrequency();
nPoints = mapgen->getPointCount();
Expand Down Expand Up @@ -164,7 +164,15 @@ class Application {
getScreenshot = true;
break;
case sf::Keyboard::W:
painter->showWalkers = ! painter->showWalkers;
painter->showWalkers = !painter->showWalkers;
break;
case sf::Keyboard::N:
painter->lables = !painter->lables;
painter->invalidate();
break;
case sf::Keyboard::B:
painter->blur = !painter->blur;
painter->invalidate();
break;
}
break;
Expand All @@ -190,7 +198,7 @@ class Application {

ImGui::DrawTabsBackground();

//TODO: move to separate file
// TODO: move to separate file
if (ImGui::AddTab("Generation")) {
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
Expand Down Expand Up @@ -274,6 +282,7 @@ class Application {
painter->update();
}
ImGui::Checkbox("Walkers*", &painter->showWalkers);
ImGui::Checkbox("Lables", &painter->lables);

ImGui::TreePop();
}
Expand All @@ -287,7 +296,11 @@ class Application {
"\n[ESC] for exit\n[S] for save screenshot\n[R] for random "
"map\n[U] toggle ui\n[H] toggle humidity\n[I] toggle info\n[P] "
"toggle pathes\n[RCLICK] toggle selection lock\n"
"[M] for distance ruler");
"[M] for distance ruler\n"
"[W] toggle walkers\n"
"[B] toggle water blur\n"
"[N] toggle labels\n"
"[A] show state clusters\n");
}

if (ImGui::AddTab("Simulation")) {
Expand Down Expand Up @@ -367,7 +380,7 @@ class Application {
mg::getDistance(rulerRegion->site, currentRegion->site));
sf::Text mark(mt, painter->sffont);
mark.setCharacterSize(15);
mark.setColor(sf::Color::Black);
mark.setFillColor(sf::Color::Black);
mark.setPosition(line[1].position + sf::Vector2f(15.f, 15.f));
window->draw(mark);
}
Expand Down
20 changes: 20 additions & 0 deletions src/blur.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
uniform sampler2D texture;
uniform float blur_radius;

void main()
{
vec2 offx = vec2(blur_radius, 0.0);
vec2 offy = vec2(0.0, blur_radius);

vec4 pixel = texture2D(texture, gl_TexCoord[0].xy) * 4.0 +
texture2D(texture, gl_TexCoord[0].xy - offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy - offx + offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx + offy) * 1.0;

gl_FragColor = gl_Color * (pixel / 16.0);
}
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "application.cpp"

std::string VERSION = "0.5.3";
std::string VERSION = "0.6.1";

int main()
{
Expand Down

0 comments on commit c4d42c2

Please sign in to comment.