2020-03-08 22:25:13 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* rotozoomer.cpp - Rotozoomer effect demo *
|
|
|
|
* *
|
|
|
|
* This file is part of the Final Cut widget toolkit *
|
|
|
|
* *
|
|
|
|
* Copyright 2020 Markus Gans *
|
|
|
|
* *
|
|
|
|
* The Final Cut is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 3 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* The Final Cut is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this program. If not, see *
|
|
|
|
* <http://www.gnu.org/licenses/>. *
|
|
|
|
***********************************************************************/
|
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2020-03-08 22:25:13 +01:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include <final/final.h>
|
|
|
|
|
|
|
|
namespace fc = finalcut::fc;
|
2020-03-22 21:53:27 +01:00
|
|
|
using namespace std::chrono;
|
2020-03-08 22:25:13 +01:00
|
|
|
using finalcut::FPoint;
|
|
|
|
using finalcut::FSize;
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
// This rotozoomer demo is based on the code of Robert Dörfler
|
|
|
|
// http://bloerp.de/code/tempel/roto.c
|
|
|
|
// http://bloerp.de/code/tempel/tempel-des-codes.html
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class RotoZoomer
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2020-04-14 23:46:42 +02:00
|
|
|
class RotoZoomer final : public finalcut::FDialog
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Constructor
|
2020-03-22 21:53:27 +01:00
|
|
|
explicit RotoZoomer (finalcut::FWidget* = nullptr, bool = false, int = 314);
|
2020-03-08 22:25:13 +01:00
|
|
|
|
|
|
|
// Destructor
|
|
|
|
~RotoZoomer() override;
|
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
// Accessors
|
|
|
|
finalcut::FString getReport();
|
|
|
|
|
2020-03-08 22:25:13 +01:00
|
|
|
// Event handlers
|
|
|
|
void onShow (finalcut::FShowEvent*) override;
|
|
|
|
void onTimer (finalcut::FTimerEvent*) override;
|
|
|
|
void onKeyPress (finalcut::FKeyEvent*) override;
|
|
|
|
void onClose (finalcut::FCloseEvent*) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Methods
|
|
|
|
void draw() override;
|
|
|
|
void rotozoomer (double, double, double, double);
|
2020-03-22 21:53:27 +01:00
|
|
|
void generateReport();
|
2020-03-08 22:25:13 +01:00
|
|
|
void adjustSize() override;
|
|
|
|
|
|
|
|
// Data member
|
2020-03-22 21:53:27 +01:00
|
|
|
bool benchmark{false};
|
|
|
|
int loops{0};
|
|
|
|
int path{0};
|
|
|
|
wchar_t data[256]{};
|
2020-04-09 12:38:35 +02:00
|
|
|
finalcut::FString report{};
|
2020-03-22 21:53:27 +01:00
|
|
|
time_point<system_clock> start{};
|
|
|
|
time_point<system_clock> end{};
|
2020-03-08 22:25:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2020-04-02 09:59:34 +02:00
|
|
|
RotoZoomer::RotoZoomer (finalcut::FWidget* parent, bool b, int l)
|
2020-03-08 22:25:13 +01:00
|
|
|
: finalcut::FDialog(parent)
|
2020-03-22 21:53:27 +01:00
|
|
|
, benchmark(b)
|
2020-04-02 09:59:34 +02:00
|
|
|
, loops(l)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
2020-04-19 20:38:52 +02:00
|
|
|
FDialog::setText ("Rotozoomer effect");
|
2020-03-08 22:25:13 +01:00
|
|
|
|
|
|
|
int h{0};
|
|
|
|
|
|
|
|
for (int j{0}; j < 8; j++)
|
|
|
|
{
|
|
|
|
for (int i{0}; i < 8; i++)
|
|
|
|
{
|
|
|
|
data[h++] = L' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i{0}; i < 8; i++)
|
|
|
|
{
|
|
|
|
data[h++] = L'+';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j{0}; j < 8; j++)
|
|
|
|
{
|
|
|
|
for (int i{0}; i < 8; i++)
|
|
|
|
{
|
|
|
|
data[h++] = L'x';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i{0}; i < 8; i++)
|
|
|
|
{
|
|
|
|
data[h++] = L' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
RotoZoomer::~RotoZoomer()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::draw()
|
|
|
|
{
|
2020-03-22 21:53:27 +01:00
|
|
|
if ( benchmark && start == time_point<system_clock>() )
|
|
|
|
start = system_clock::now();
|
|
|
|
|
2020-03-08 22:25:13 +01:00
|
|
|
finalcut::FDialog::draw();
|
|
|
|
double cx = double(80.0 / 2.0 + (80.0 / 2.0 * std::sin(double(path) / 50.0)));
|
|
|
|
double cy = double(23.0 + (23.0 * std::cos(double(path) / 50.0)));
|
|
|
|
double r = double(128.0 + 96.0 * std::cos(double(path) / 10.0));
|
|
|
|
double a = double(path) / 50.0;
|
|
|
|
rotozoomer (cx, cy, r, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::rotozoomer (double cx, double cy, double r, double a)
|
|
|
|
{
|
|
|
|
const int Cols = int(getClientWidth());
|
|
|
|
const int Lines = int(getClientHeight());
|
|
|
|
int Ax = int(4096.0 * (cx + r * std::cos(a)));
|
|
|
|
int Ay = int(4096.0 * (cy + r * std::sin(a)));
|
|
|
|
int Bx = int(4096.0 * (cx + r * std::cos(a + 2.02358)));
|
|
|
|
int By = int(4096.0 * (cy + r * std::sin(a + 2.02358)));
|
|
|
|
int Cx = int(4096.0 * (cx + r * std::cos(a - 1.11701)));
|
|
|
|
int Cy = int(4096.0 * (cy + r * std::sin(a - 1.11701)));
|
|
|
|
int dxdx = (Bx - Ax) / 80;
|
|
|
|
int dydx = (By - Ay) / 80;
|
|
|
|
int dxdy = (Cx - Ax) / 23;
|
|
|
|
int dydy = (Cy - Ay) / 23;
|
|
|
|
|
|
|
|
for (int y = 0; y < Lines; y++)
|
|
|
|
{
|
|
|
|
Cx = Ax;
|
|
|
|
Cy = Ay;
|
2020-05-02 00:07:35 +02:00
|
|
|
print() << FPoint{2, 3 + y};
|
2020-03-08 22:25:13 +01:00
|
|
|
|
|
|
|
for (int x = 0; x < Cols; x++)
|
|
|
|
{
|
|
|
|
wchar_t ch = data[((Cy >> 14) & 0xf) + ((Cx >> 10) & 0xf0)];
|
|
|
|
|
|
|
|
if ( ch == '+' )
|
2020-05-02 00:07:35 +02:00
|
|
|
print() << finalcut::FColorPair{fc::Black, fc::Red};
|
2020-03-08 22:25:13 +01:00
|
|
|
else if ( ch == 'x' )
|
2020-05-02 00:07:35 +02:00
|
|
|
print() << finalcut::FColorPair{fc::Black, fc::Cyan};
|
2020-03-08 22:25:13 +01:00
|
|
|
else
|
2020-05-02 00:07:35 +02:00
|
|
|
print() << finalcut::FColorPair{fc::Black, fc::White};
|
2020-03-08 22:25:13 +01:00
|
|
|
|
|
|
|
print() << ch;
|
|
|
|
Cx += dxdx;
|
|
|
|
Cy += dydx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ax += dxdy;
|
|
|
|
Ay += dydy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::generateReport()
|
|
|
|
{
|
2020-05-16 22:24:36 +02:00
|
|
|
finalcut::FString term_type = finalcut::FTerm::getTermType();
|
2020-03-22 21:53:27 +01:00
|
|
|
finalcut::FString dimension_str{};
|
|
|
|
finalcut::FString time_str{};
|
|
|
|
finalcut::FString fps_str{};
|
|
|
|
std::wostringstream rep;
|
|
|
|
dimension_str << getDesktopWidth()
|
|
|
|
<< "x" << getDesktopHeight();
|
2020-04-02 09:59:34 +02:00
|
|
|
int elapsed_ms = int(duration_cast<milliseconds>(end - start).count());
|
2020-03-22 21:53:27 +01:00
|
|
|
time_str << double(elapsed_ms) / 1000 << "ms";
|
|
|
|
fps_str << double(loops) * 1000.0 / double(elapsed_ms);
|
|
|
|
|
2020-05-02 00:07:35 +02:00
|
|
|
rep << finalcut::FString{55, '-'} << "\n"
|
2020-03-22 21:53:27 +01:00
|
|
|
<< "Terminal Size Time Loops Frame rate\n"
|
2020-05-02 00:07:35 +02:00
|
|
|
<< finalcut::FString{55, '-'} << "\n"
|
2020-03-22 21:53:27 +01:00
|
|
|
<< std::left << std::setw(20) << term_type
|
|
|
|
<< std::setw(8) << dimension_str
|
|
|
|
<< std::setw(10) << time_str
|
|
|
|
<< std::setw(7) << loops
|
|
|
|
<< std::setw(7) << fps_str.left(7) << "fps\n";
|
|
|
|
report << rep.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline finalcut::FString RotoZoomer::getReport()
|
|
|
|
{
|
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
2020-03-08 22:25:13 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::onShow (finalcut::FShowEvent*)
|
|
|
|
{
|
2020-03-22 21:53:27 +01:00
|
|
|
if ( ! benchmark )
|
2020-04-04 20:58:47 +02:00
|
|
|
addTimer(33); // Starts the timer every 33 milliseconds
|
2020-03-22 21:53:27 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (path = 1; path < loops; path++)
|
|
|
|
{
|
|
|
|
redraw();
|
|
|
|
updateTerminal();
|
|
|
|
}
|
|
|
|
|
|
|
|
end = system_clock::now();
|
|
|
|
generateReport();
|
|
|
|
flush();
|
|
|
|
quit();
|
|
|
|
}
|
2020-03-08 22:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::onTimer (finalcut::FTimerEvent*)
|
|
|
|
{
|
2020-03-22 21:53:27 +01:00
|
|
|
if ( path >= 314 ) // More than 360 degrees
|
2020-03-08 22:25:13 +01:00
|
|
|
path = 0;
|
|
|
|
else
|
|
|
|
path++;
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
updateTerminal();
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::onKeyPress (finalcut::FKeyEvent* ev)
|
|
|
|
{
|
|
|
|
if ( ! ev )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( ev->key() == 'q' )
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
ev->accept();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
finalcut::FDialog::onKeyPress(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::onClose (finalcut::FCloseEvent* ev)
|
|
|
|
{
|
2020-03-22 21:53:27 +01:00
|
|
|
if ( ! benchmark )
|
|
|
|
finalcut::FApplication::closeConfirmationDialog (this, ev);
|
2020-03-08 22:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::adjustSize()
|
|
|
|
{
|
2020-03-22 21:53:27 +01:00
|
|
|
if ( ! benchmark )
|
|
|
|
{
|
|
|
|
std::size_t h = getDesktopHeight();
|
|
|
|
std::size_t w = getDesktopWidth();
|
2020-03-08 22:25:13 +01:00
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
if ( h > 1 )
|
|
|
|
h--;
|
2020-03-08 22:25:13 +01:00
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
if ( w > 8 )
|
|
|
|
w -= 8;
|
|
|
|
|
2020-05-02 00:07:35 +02:00
|
|
|
setGeometry(FPoint{5, 1}, FSize{w, h}, false);
|
2020-03-22 21:53:27 +01:00
|
|
|
}
|
2020-03-08 22:25:13 +01:00
|
|
|
|
|
|
|
finalcut::FDialog::adjustSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// main part
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int main (int argc, char* argv[])
|
2020-05-02 00:07:35 +02:00
|
|
|
{
|
2020-03-22 21:53:27 +01:00
|
|
|
bool benchmark{false};
|
|
|
|
finalcut::FString report{};
|
|
|
|
int quit_code{0};
|
|
|
|
|
|
|
|
if ( argv[1] && ( strcmp(argv[1], "--help") == 0
|
2020-05-02 00:07:35 +02:00
|
|
|
|| strcmp(argv[1], "-h") == 0 ) )
|
2020-03-22 21:53:27 +01:00
|
|
|
{
|
|
|
|
std::cout << "RotoZoomer options:\n"
|
|
|
|
<< " -b, --benchmark "
|
|
|
|
<< "Starting a benchmark run\n\n";
|
|
|
|
}
|
|
|
|
else if ( argv[1] && ( strcmp(argv[1], "--benchmark") == 0
|
|
|
|
|| strcmp(argv[1], "-b") == 0 ) )
|
|
|
|
{
|
|
|
|
benchmark = true;
|
|
|
|
}
|
|
|
|
|
2020-04-13 12:40:11 +02:00
|
|
|
{ // Create the application object in this scope
|
2020-05-02 00:07:35 +02:00
|
|
|
finalcut::FApplication app{argc, argv};
|
2020-03-22 21:53:27 +01:00
|
|
|
app.setNonBlockingRead();
|
2020-03-08 22:25:13 +01:00
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
// Create a simple dialog box
|
2020-04-26 20:45:57 +02:00
|
|
|
constexpr int iterations = 314;
|
2020-05-02 00:07:35 +02:00
|
|
|
RotoZoomer roto{&app, benchmark, iterations};
|
2020-03-08 22:25:13 +01:00
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
if ( benchmark )
|
2020-05-02 00:07:35 +02:00
|
|
|
roto.setGeometry (FPoint{1, 1}, FSize{80, 24});
|
2020-03-22 21:53:27 +01:00
|
|
|
else
|
2020-05-02 00:07:35 +02:00
|
|
|
roto.setGeometry (FPoint{5, 1}, FSize{72, 23});
|
2020-03-22 21:53:27 +01:00
|
|
|
|
|
|
|
roto.setShadow();
|
|
|
|
|
|
|
|
// Set the RotoZoomer object as main widget
|
2020-04-13 12:40:11 +02:00
|
|
|
finalcut::FWidget::setMainWidget(&roto);
|
2020-03-22 21:53:27 +01:00
|
|
|
|
|
|
|
// Show and start the application
|
|
|
|
roto.show();
|
|
|
|
quit_code = app.exec();
|
|
|
|
|
|
|
|
if ( benchmark )
|
|
|
|
report = roto.getReport();
|
2020-04-13 12:40:11 +02:00
|
|
|
} // Hide and destroy the application object
|
2020-03-22 21:53:27 +01:00
|
|
|
|
|
|
|
if ( benchmark )
|
|
|
|
{
|
|
|
|
std::cout << "Benchmark:\n" << report;
|
|
|
|
}
|
2020-03-08 22:25:13 +01:00
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
return quit_code;
|
2020-03-08 22:25:13 +01:00
|
|
|
}
|