2020-03-08 22:25:13 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* rotozoomer.cpp - Rotozoomer effect demo *
|
|
|
|
* *
|
2020-07-08 21:32:47 +02:00
|
|
|
* This file is part of the FINAL CUT widget toolkit *
|
2020-03-08 22:25:13 +01:00
|
|
|
* *
|
2021-02-09 22:01:21 +01:00
|
|
|
* Copyright 2020-2021 Markus Gans *
|
2020-03-08 22:25:13 +01:00
|
|
|
* *
|
2020-07-08 21:32:47 +02:00
|
|
|
* 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 *
|
2020-03-08 22:25:13 +01:00
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
2020-07-08 21:32:47 +02:00
|
|
|
* FINAL CUT is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
2020-03-08 22:25:13 +01:00
|
|
|
* 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-10-08 05:55:32 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <iomanip>
|
2020-10-08 05:55:32 +02:00
|
|
|
#include <string>
|
2020-03-08 22:25:13 +01:00
|
|
|
|
|
|
|
#include <final/final.h>
|
|
|
|
|
2020-08-23 00:32:41 +02:00
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
using std::chrono::system_clock;
|
|
|
|
using std::chrono::time_point;
|
2020-03-08 22:25:13 +01:00
|
|
|
using finalcut::FPoint;
|
|
|
|
using finalcut::FSize;
|
2020-12-31 20:45:10 +01:00
|
|
|
using finalcut::FColor;
|
2020-03-08 22:25:13 +01:00
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
// 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
|
2020-11-24 21:06:39 +01:00
|
|
|
~RotoZoomer() override = default;
|
2020-03-08 22:25:13 +01:00
|
|
|
|
2020-03-22 21:53:27 +01:00
|
|
|
// Accessors
|
2020-07-12 15:25:21 +02:00
|
|
|
finalcut::FString getReport() const;
|
2020-03-22 21:53:27 +01:00
|
|
|
|
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};
|
2020-10-08 05:55:32 +02:00
|
|
|
std::wstring data{std::wstring(256, L'\0')};
|
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-05-24 23:55:08 +02:00
|
|
|
: finalcut::FDialog{parent}
|
|
|
|
, benchmark{b}
|
|
|
|
, 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
|
|
|
|
2020-10-08 05:55:32 +02:00
|
|
|
std::size_t h{0};
|
2020-03-08 22:25:13 +01:00
|
|
|
|
2020-10-08 05:55:32 +02:00
|
|
|
for (std::size_t j{0}; j < 8; j++)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
2020-10-08 05:55:32 +02:00
|
|
|
for (std::size_t i{0}; i < 8; i++)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
2020-10-08 12:09:48 +02:00
|
|
|
data[h] = L' ';
|
|
|
|
h++;
|
2020-03-08 22:25:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 05:55:32 +02:00
|
|
|
for (std::size_t i{0}; i < 8; i++)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
2020-10-08 12:09:48 +02:00
|
|
|
data[h] = L'+';
|
|
|
|
h++;
|
2020-03-08 22:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 05:55:32 +02:00
|
|
|
for (std::size_t j{0}; j < 8; j++)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
2020-10-08 05:55:32 +02:00
|
|
|
for (std::size_t i{0}; i < 8; i++)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
2020-10-08 12:09:48 +02:00
|
|
|
data[h] = L'x';
|
|
|
|
h++;
|
2020-03-08 22:25:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 05:55:32 +02:00
|
|
|
for (std::size_t i{0}; i < 8; i++)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
2020-10-08 12:09:48 +02:00
|
|
|
data[h] = L' ';
|
|
|
|
h++;
|
2020-03-08 22:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
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();
|
2020-10-04 02:55:15 +02:00
|
|
|
auto cx = double(80.0 / 2.0 + (80.0 / 2.0 * std::sin(double(path) / 50.0)));
|
|
|
|
auto cy = double(23.0 + (23.0 * std::cos(double(path) / 50.0)));
|
|
|
|
auto r = double(128.0 + 96.0 * std::cos(double(path) / 10.0));
|
|
|
|
auto a = double(path) / 50.0;
|
2020-03-08 22:25:13 +01:00
|
|
|
rotozoomer (cx, cy, r, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::rotozoomer (double cx, double cy, double r, double a)
|
|
|
|
{
|
2020-10-05 04:24:14 +02:00
|
|
|
const auto Cols = int(getClientWidth());
|
|
|
|
const auto Lines = int(getClientHeight());
|
2020-10-04 02:55:15 +02:00
|
|
|
auto Ax = int(4096.0 * (cx + r * std::cos(a)));
|
|
|
|
auto Ay = int(4096.0 * (cy + r * std::sin(a)));
|
|
|
|
auto Bx = int(4096.0 * (cx + r * std::cos(a + 2.02358)));
|
|
|
|
auto By = int(4096.0 * (cy + r * std::sin(a + 2.02358)));
|
|
|
|
auto Cx = int(4096.0 * (cx + r * std::cos(a - 1.11701)));
|
|
|
|
auto Cy = int(4096.0 * (cy + r * std::sin(a - 1.11701)));
|
2020-03-08 22:25:13 +01:00
|
|
|
int dxdx = (Bx - Ax) / 80;
|
|
|
|
int dydx = (By - Ay) / 80;
|
|
|
|
int dxdy = (Cx - Ax) / 23;
|
|
|
|
int dydy = (Cy - Ay) / 23;
|
|
|
|
|
2020-10-25 01:21:45 +02:00
|
|
|
for (auto y = 0; y < Lines; y++)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-10-25 01:21:45 +02:00
|
|
|
for (auto x = 0; x < Cols; x++)
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
2020-10-08 05:55:32 +02:00
|
|
|
auto ch = data[((Cy >> 14) & 0xf) + ((Cx >> 10) & 0xf0)];
|
2020-03-08 22:25:13 +01:00
|
|
|
|
|
|
|
if ( ch == '+' )
|
2020-12-31 20:45:10 +01:00
|
|
|
print() << finalcut::FColorPair{FColor::Black, FColor::Red};
|
2020-03-08 22:25:13 +01:00
|
|
|
else if ( ch == 'x' )
|
2020-12-31 20:45:10 +01:00
|
|
|
print() << finalcut::FColorPair{FColor::Black, FColor::Cyan};
|
2020-03-08 22:25:13 +01:00
|
|
|
else
|
2020-12-31 20:45:10 +01:00
|
|
|
print() << finalcut::FColorPair{FColor::Black, FColor::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{};
|
2020-05-24 02:15:43 +02:00
|
|
|
finalcut::FStringStream rep;
|
2020-03-22 21:53:27 +01:00
|
|
|
dimension_str << getDesktopWidth()
|
|
|
|
<< "x" << getDesktopHeight();
|
2020-04-02 09:59:34 +02:00
|
|
|
int elapsed_ms = int(duration_cast<milliseconds>(end - start).count());
|
2020-09-08 23:22:52 +02:00
|
|
|
time_str << double(elapsed_ms) / 1000 << "s";
|
2020-03-22 21:53:27 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2020-07-12 15:25:21 +02:00
|
|
|
inline finalcut::FString RotoZoomer::getReport() const
|
2020-03-22 21:53:27 +01:00
|
|
|
{
|
|
|
|
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();
|
2020-11-14 20:59:26 +01:00
|
|
|
forceTerminalUpdate();
|
2020-03-22 21:53:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end = system_clock::now();
|
|
|
|
generateReport();
|
|
|
|
flush();
|
2020-10-04 00:59:21 +02:00
|
|
|
close();
|
2020-03-22 21:53:27 +01:00
|
|
|
}
|
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();
|
2020-11-14 20:59:26 +01:00
|
|
|
forceTerminalUpdate();
|
2020-03-08 22:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::onKeyPress (finalcut::FKeyEvent* ev)
|
|
|
|
{
|
|
|
|
if ( ! ev )
|
|
|
|
return;
|
|
|
|
|
2020-12-31 20:45:10 +01:00
|
|
|
if ( ev->key() == finalcut::FKey('q') )
|
2020-03-08 22:25:13 +01:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
ev->accept();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
finalcut::FDialog::onKeyPress(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void RotoZoomer::onClose (finalcut::FCloseEvent* ev)
|
|
|
|
{
|
2020-10-04 00:59:21 +02:00
|
|
|
if ( benchmark )
|
|
|
|
ev->accept();
|
|
|
|
else
|
2020-03-22 21:53:27 +01:00
|
|
|
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-10-04 00:59:21 +02:00
|
|
|
// Disable terminal data requests
|
|
|
|
auto& start_options = finalcut::FStartOptions::getFStartOptions();
|
|
|
|
start_options.terminal_data_request = false;
|
2020-03-22 21:53:27 +01:00
|
|
|
}
|
|
|
|
|
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};
|
2021-03-10 10:07:32 +01:00
|
|
|
finalcut::FVTerm::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
|
|
|
|
2020-10-05 04:24:14 +02:00
|
|
|
roto.setShadow(); // Instead of the transparent window shadow
|
2020-03-22 21:53:27 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|