Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <string>
- #include <cmath>
- #include <ncurses.h>
- #include <iostream>
- #include <thread>
- #include <mutex>
- #include <chrono>
- #include <ctime>
- typedef std::chrono::time_point<std::chrono::high_resolution_clock> timestamp;
- //std::chrono::high_resolution_clock clock;
- WINDOW * window; //obiekt okan ncurses
- int screenH, screenW; //wielkosc okna ncurses
- double gravity = 9.8 / 5.0; //przyspieszenie grawitacyjne [m/s2]
- double energy_loss = 0.5; //wspolczynnik zachowanej energii po kolicji
- std::mutex window_mutex; //mutex chroniacy operacje na oknie
- //Pomocnicza klasa - wrapper chrono::high_resolution_clock
- class Timer{
- public:
- static timestamp now(){
- return std::chrono::high_resolution_clock::now();
- }
- static double periodToSec(timestamp begin, timestamp end){
- //std::chrono::duration<double, std::chrono::seconds> seconds = end - begin;
- return std::chrono::duration_cast<std::chrono::seconds>(end - begin).count();
- }
- };
- void spawnBall(double vy, double vx)
- {
- vy /= 5.0; vx /= 5.0;
- std::string ball = "O";
- std::string eraser = " ";
- double py = screenH / 2.0;
- double px = screenW / 2.0;
- double oldPy, oldPx;
- window_mutex.lock();
- mvwprintw(window, py, px, ball.c_str());
- window_mutex.unlock();
- timestamp p_begin = Timer::now();
- timestamp p_end;
- double timePassed = 0.0;
- while(1)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
- //Pozyskaj czas od ostatnich obliczen
- p_end = Timer::now();
- timePassed = Timer::periodToSec(p_begin, p_end);
- //mvwprintw(window, 1, 0, std::to_string(
- // std::ctime((std::chrono::high_resolution_clock::to_time_t(p_begin)))
- //).c_str())
- //mvwprintw(window, 2, 0, std::to_string(p_end).c_str());
- p_begin = p_end;
- //Zachowaj poprzednia pozycje pilki
- oldPy = py; oldPx = px;
- //Przemiesc pilke zgodnie z wektorem predkosci
- py -= vy * timePassed;
- ///-------------------ODBICIA----------------------------
- //Jezeli kolizja z sufitem
- if(py < 1.0){
- //Skoryguj odbicie
- py = 1-py;
- //Zmien kierunek pionowej skladowej wektra predkosi
- vy = -vy;
- }
- else if(py > screenH-1.0){
- //Skoryguj odbicie + wprowadz utrate energii
- py = (screenH-1.0) - ((py-(screenH-1.0)) * energy_loss);
- //Zmien kierunek pionowej skladowej wektra predkosi
- //+ wprowadz utrate energii
- vy = -vy * energy_loss;
- }
- vy -= gravity * timePassed;
- if(vy > 10) vy = 10;
- if(vy < -10) vy = -10;
- mvwprintw(window, 0, 0, std::to_string(vy).c_str());
- //mvwprintw(window, 1, 0, std::to_string(py).c_str());
- px += vx * timePassed;
- if(px >= screenW-1 || px <= 0){vx = -vx; px += 2*vx;}
- //Narysuj pilke w nowym miejsu
- window_mutex.lock();
- mvwprintw(window, oldPy, oldPx, eraser.c_str());
- mvwprintw(window, py, px, ball.c_str());
- window_mutex.unlock();
- }
- }
- void ballDispenser(int spawnTimeGap)
- {
- int counter = 0;
- while(1)
- {
- // mvwprintw(window, screenH/2, screenW/2, std::to_string(counter).c_str());
- //std::cout << "ball spawned!" <<std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(spawnTimeGap));
- ++counter;
- }
- }
- void setFPS(int fps)
- {
- int period = 100.0 / fps;
- while(1)
- {
- window_mutex.lock();
- wrefresh(window);
- window_mutex.unlock();
- std::this_thread::sleep_for(std::chrono::milliseconds(period));
- }
- }
- int main(int argc, char ** argv)
- {
- int timeGap = 1200;
- screenH = 15;
- screenW = 40;
- if(argc > 1)
- timeGap = std::atoi(argv[1]);
- if(argc > 3)
- {
- screenH = atoi(argv[2]);
- screenW = atoi(argv[3]);
- }
- initscr();
- window = newwin(screenH, screenW, 0, 0);
- curs_set(0);
- refresh();
- box(window, 0, 0);
- wrefresh(window);
- std::thread refresh_thread(setFPS, 15);
- refresh_thread.detach();
- //std::thread dispenser(ballDispenser, timeGap);
- //dispenser.detach();
- std::thread releaseBall(spawnBall, 25, 10);
- releaseBall.detach();
- //std::cout << std::chrono::high_resolution_clock::period::den <<std::endl;
- //std::cout<<"main: On it!"<<std::endl;
- std::getchar();
- endwin();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement