Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Merge pull request #16 from SaplingStudios/experimental
Browse files Browse the repository at this point in the history
Version 1.0 Release
  • Loading branch information
ElykDeer authored Jul 19, 2017
2 parents c70b35e + 5b15c4f commit dd668ca
Show file tree
Hide file tree
Showing 14 changed files with 338 additions and 111 deletions.
5 changes: 3 additions & 2 deletions Compiler/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ bool verifyPlugin(const fs::directory_entry& entry)
//Just a stack of if statements that need to be true
if(fs::is_directory(entry.symlink_status())) //Makes sure it's actually a folder
if((string(entry.path()).substr(8) != "Enviornment")) //Ignores the enviornment
return true;
if((string(entry.path()).substr(8) != "Graphics")) //Ignores the enviornment
return true;
return false;
}

Expand Down Expand Up @@ -243,7 +244,7 @@ void compile(const unordered_set<string>& plugins)
dependencies += getDependencies("Plugins/" + dep);

string gccOptions = "-Wall -Wextra -pedantic -std=c++1y ";
string links = "-pthread"; //Linker dependencies
string links = "-pthread -lsfml-graphics -lsfml-window -lsfml-audio -lsfml-system"; //Linker dependencies

string command =
"g++ " + gccOptions + dependencies + "-o mainP " + links;
Expand Down
118 changes: 118 additions & 0 deletions Main/Graphics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "Graphics.h"
using namespace std;

GraphicsInternals::GraphicsInternals(Bin* const bin, ThreadManager* const manager)
: bin(bin), manager(manager) {}

GraphicsInternals::~GraphicsInternals() {}

void GraphicsInternals::openWindow(const string& name)
{
window.create(sf::VideoMode(bin->getWidth(), bin->getHeight()), name, sf::Style::Default);
}

void GraphicsInternals::drawMap()
{
sf::CircleShape hexa((*bin->getAllHexes()).getRadius(), 6); //Each hex will be a circle with 6 sides
hexa.rotate(30);
hexa.setOutlineThickness(0.8);
hexa.setOutlineColor(sf::Color::Black);
hexa.setFillColor(sf::Color::White);
for (const Bin::Hex& hex : bin->getAllHexes())
{
hexa.setPosition(hex.getX(), hex.getY());
window.draw(hexa);
}
}

void GraphicsInternals::drawEntities()
{
//Every ent will be a red ring with a red dot at it's center

sf::CircleShape circularEnt(2, 30); //Each ent will be a circle of radius x
circularEnt.setOrigin(2, 2);
circularEnt.setOutlineThickness(0.8);
circularEnt.setOutlineColor(sf::Color::Red);
circularEnt.setFillColor(sf::Color::Transparent);

sf::CircleShape entCenter(0.2, 30); //Each ent will be a circle of radius
entCenter.setOrigin(0.2, 0.2);
entCenter.setFillColor(sf::Color::Red);

for (const Entity& entity : bin->getAll())
{
//Draw circle for entitiy
circularEnt.setPosition(entity.getX(), entity.getY());
window.draw(circularEnt);

//Draw point for center
entCenter.setPosition(entity.getX(), entity.getY());
window.draw(entCenter);
}
}

void GraphicsInternals::manageEvents()
{
//check window events
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
{
manager->kill();
window.close();
}

// catch the resize events
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}

//zoom
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
{
// create a view half the size of the default view
sf::View view = window.getView();
if (event.mouseWheelScroll.delta <= -1)
view.zoom(1.1);
else if (event.mouseWheelScroll.delta >= 1)
view.zoom(0.9);
window.setView(view);
}
}
}

void GraphicsInternals::input()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
sf::View view = window.getView();
view.move(-moveSensitivity, 0);
window.setView(view);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
sf::View view = window.getView();
view.move(moveSensitivity, 0);
window.setView(view);
}

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sf::View view = window.getView();
view.move(0, -moveSensitivity);
window.setView(view);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
sf::View view = window.getView();
view.move(0, moveSensitivity);
window.setView(view);
}
}

#include "../Plugins/Graphics/Graphics.cpp"
40 changes: 40 additions & 0 deletions Main/Graphics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef SIM_GRAPHICAL_API
#define SIM_GRAPHICAL_API 1
#define SFML_STATIC

#include "dataStructure.h"
#include "ThreadManager.h"
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <string>

class GraphicsInternals
{
public:
GraphicsInternals(Bin* const bin, ThreadManager* const manager);
virtual ~GraphicsInternals();

virtual void openWindow(const std::string& name);

virtual void spin() = 0; //Perhaps remove this later...

virtual void drawMap();
virtual void drawEntities();

//These two should probably be built by the compiler based on some rules I'll think up later
virtual void manageEvents(); //Window resizing, closing, scrolling
virtual void input(); //Keyboard, mouse, etc

protected:
Bin* const bin;
ThreadManager* const manager;

sf::RenderWindow window;

private:
int moveSensitivity = 2;
};

#include "../Plugins/Graphics/Graphics.h"

#endif
103 changes: 53 additions & 50 deletions Main/ThreadManager.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
#include "ThreadManager.h"

#include <iostream>

using namespace std;
using namespace std::chrono;

typedef std::chrono::high_resolution_clock Clock;

ThreadManager::ThreadManager(Bin& bin) : bin(&bin) {}

void ThreadManager::startGraphics( void (*graphics)(const Bin* const, const ThreadManager* const) )
void ThreadManager::startGraphics( void (*graphics)(Bin* const, ThreadManager* const) )
{
//Just let the graphics do their thing
graphicsThread = new thread(graphics, bin, this);
}

unsigned int ThreadManager::getResolution() const
void ThreadManager::startUpdatingMap()
{
return resolution;
//continueUpdatingMap(); // - private function
//Alternativly, if I want more in main:
//Start thread, let it go, get back to main
updateMapThread = new thread(&ThreadManager::continueUpdatingMap, this);
}

unsigned int ThreadManager::getTick() const
void ThreadManager::waitForThreadsEnd()
{
return tick;
if (updateMapThread)
updateMapThread->join();
if (graphicsThread)
graphicsThread->join();
}

void ThreadManager::sleep(unsigned long long int nanosecs) const
{
this_thread::sleep_for(nanoseconds(nanosecs));
}

void ThreadManager::pause()
{
paused = true;
}

void ThreadManager::resume()
{
paused = false;
}

void ThreadManager::setSpeed(const unsigned int newSpeed)
Expand All @@ -36,32 +54,23 @@ unsigned int ThreadManager::getSpeed() const
return speed;
}

void ThreadManager::startUpdatingMap()
unsigned int ThreadManager::getResolution() const
{
//continueUpdatingMap(); // - private function
//Alternativly, if I want more in main:
//Start thread, let it go, get back to main
updateMapThread = new thread(&ThreadManager::continueUpdatingMap, this);
return resolution;
}

void ThreadManager::wait()
unsigned int ThreadManager::getTick() const
{
if (updateMapThread)
updateMapThread->join();
if (graphicsThread)
graphicsThread->join();
return tick;
}

void ThreadManager::continueUpdatingMap()
{
//lock my lock - own it - doesn't actually matter?
originalLock.lock();

//Spawn two threads for maps and entities
thread mapThread(&ThreadManager::map, this);
thread entThread(&ThreadManager::entities, this);

while (1)
while (running)
{
//Sleep == 0; means paused. Sleep for 1/20th a second and try again
if (paused)
Expand All @@ -71,19 +80,25 @@ void ThreadManager::continueUpdatingMap()
}

//Start Threads
ready = true;
sync.notify_all();
originalLock.unlock();
nextLoop = true;

//Start timing
t1 = Clock::now();
while (!mapBool || !entBool) {}
while (!mapBool || !entBool)
{
if (!running) //If while we were waiting, the game closes...
{
mapThread.join();
entThread.join();

return;
}
}
t2 = Clock::now();

//Reset things
mapBool = entBool = false;
ready = false;
originalLock.lock();
nextLoop = false;

lasTimeeee = timeeee;
timeeee = duration_cast<duration<double>>(t2 - t1);
Expand Down Expand Up @@ -116,13 +131,12 @@ void ThreadManager::continueUpdatingMap()

void ThreadManager::map()
{
while(1)
while(running)
{
//Wait to be notified to continue
unique_lock<std::mutex> lock(originalLock);
while (!ready)
sync.wait(lock);
sync.notify_all(); //Just incase
while (!nextLoop || mapBool)
if (!running)
return;

bin->updateHexes(resolution);

Expand All @@ -133,13 +147,12 @@ void ThreadManager::map()

void ThreadManager::entities()
{
while(1)
while(running)
{
//Wait to be notified to continue
unique_lock<std::mutex> lock(originalLock);
while (!ready)
sync.wait(lock);
sync.notify_all(); //Just incase
while (!nextLoop || entBool)
if (!running)
return;

bin->updateEntities(resolution);

Expand All @@ -148,17 +161,7 @@ void ThreadManager::entities()
}
}

void ThreadManager::sleep(unsigned long long int nanosecs) const
{
this_thread::sleep_for(nanoseconds(nanosecs));
}

void ThreadManager::pause()
void ThreadManager::kill()
{
paused = true;
}

void ThreadManager::resume()
{
paused = false;
running = false;
}
Loading

0 comments on commit dd668ca

Please sign in to comment.