Initial commit

This commit is contained in:
Jan Potocki
2020-05-04 15:56:02 +02:00
commit a8382b30e1
27 changed files with 1282 additions and 0 deletions
+158
View File
@@ -0,0 +1,158 @@
#include <iostream>
#include <mutex>
#include "Entry.h"
bool Entry::initialized = false;
int Entry::xMax;
int Entry::yMax;
bool full;
std::mutex state;
std::mutex ready1;
std::mutex ready2;
std::mutex service;
Entry::Entry(int speedRate)
{
//ctor
this->xPosition = -1;
this->yPosition = yMax / 2;
this->speedRate = speedRate;
this->symbol = "*";
if(initialized == false)
{
std::cout << "WARNING: Scene size not initialized!" << std::endl;
}
}
Entry::~Entry()
{
//dtor
}
void Entry::initScene(int xRes, int yRes)
{
xMax = xRes;
yMax = yRes;
initialized = true;
}
int Entry::getxPosition()
{
return xPosition;
}
int Entry::getyPosition()
{
return yPosition;
}
std::string Entry::getSymbol()
{
return symbol;
}
void Entry::run()
{
if(initialized)
{
while(xPosition < xMax / 2)
{
xPosition++;
usleep(100000 - 10000 * speedRate);
}
bool goAround;
state.lock();
if(full == true)
{
goAround = true;
}
else
{
goAround = false;
full = true;
ready2.lock();
}
state.unlock();
while(goAround)
{
while(yPosition < 3 * yMax / 4)
{
yPosition++;
usleep(100000 - 10000 * speedRate);
}
while(xPosition < 3 * xMax / 4)
{
xPosition++;
usleep(100000 - 10000 * speedRate);
}
while(yPosition > yMax / 4)
{
yPosition--;
usleep(100000 - 10000 * speedRate);
}
while(xPosition > xMax / 2)
{
xPosition--;
usleep(100000 - 10000 * speedRate);
}
while(yPosition < yMax / 2)
{
yPosition++;
usleep(100000 - 10000 * speedRate);
}
state.lock();
if(full == false)
{
goAround = false;
full = true;
ready2.lock();
}
state.unlock();
}
xPosition++;
symbol = "2";
ready1.lock();
state.lock();
full = false;
state.unlock();
ready2.unlock();
xPosition++;
symbol = "1";
service.lock();
ready1.unlock();
xPosition++;
symbol = "#";
sleep(5);
service.unlock();
symbol = "*";
while(xPosition < xMax)
{
xPosition++;
usleep(100000 - 10000 * speedRate);
}
}
else
{
std::cout << "ERROR: Scene size not initialized!" << std::endl;
}
}
std::thread Entry::runThread()
{
return std::thread(&Entry::run, this);
}
+30
View File
@@ -0,0 +1,30 @@
#ifndef ENTRY_H
#define ENTRY_H
#include <string>
#include <thread>
#include <unistd.h>
class Entry
{
public:
Entry(int speedRate);
~Entry();
static void initScene(int xRes, int yRes);
int getxPosition();
int getyPosition();
std::string getSymbol();
std::thread runThread();
protected:
private:
static bool initialized;
static int xMax;
static int yMax;
int xPosition;
int yPosition;
int speedRate;
std::string symbol;
void run();
};
#endif // ENTRY_H
Binary file not shown.
+15
View File
@@ -0,0 +1,15 @@
CXXFLAGS = -Wall -std=c++11
OBJS = so2-center.o Entry.o
LIBS = -pthread -lncurses
TARGET = so2-center
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
+91
View File
@@ -0,0 +1,91 @@
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <ncurses.h>
#include <unistd.h>
#include "Entry.h"
bool running = true;
std::vector<Entry> entries;
std::mutex display;
// "Monitor" function
void refreshScreen()
{
while(running == true)
{
clear();
for(int i = 0; i < entries.size(); i++)
{
mvprintw(entries[i].getyPosition(), entries[i].getxPosition(), entries[i].getSymbol().c_str());
}
refresh();
// Refresh every 0.01 s
usleep(10000);
}
}
int main()
{
int xMax, yMax, fallingBricks = 0;
std::vector<std::thread> entriesThreads;
srand(time(0));
// Initialize ncurses
initscr();
curs_set(0);
getmaxyx(stdscr, yMax, xMax);
// Initialize scene
Entry::initScene(xMax, yMax);
// Start monitor
std::thread monitor(refreshScreen);
for(int i = 0; i < 15; i++)
{
entries.push_back(*(new Entry(rand() % 6)));
}
for(int i = 0; i < entries.size(); i++)
{
unsigned randTime = rand() % 3 + 1;
sleep(1 * randTime);
entriesThreads.push_back(entries[i].runThread());
}
// Wait for all entries
for(int i = 0; i < entriesThreads.size(); i++)
{
entriesThreads.at(i).join();
}
// Stop monitor
sleep(1);
running = false;
monitor.join();
// Close ncurses
endwin();
std::cout << "SYncsys Next Caban SYStem v1.1" << std::endl;
std::cout << "Jan Potocki 2018" << std::endl;
std::cout << "(beerware)" << std::endl;
std::cout << std::endl;
std::cout << '"' << "...And now the times have changed" << std::endl;
std::cout << "Repos on the web, git," << std::endl;
std::cout << "Now githubs everywhere." << std::endl;
std::cout << "Not like the winter of '95..." << '"' << std::endl;
return 0;
}
Binary file not shown.