1 /* === S Y N F I G ========================================================= */
2 /*!	\file target.cpp
3 **	\brief Target Class Implementation
4 **
5 **	$Id$
6 **
7 **	\legal
8 **	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **  Copyright (c) 2010 Diego Barrios Romero
10 **
11 **	This package is free software; you can redistribute it and/or
12 **	modify it under the terms of the GNU General Public License as
13 **	published by the Free Software Foundation; either version 2 of
14 **	the License, or (at your option) any later version.
15 **
16 **	This package is distributed in the hope that it will be useful,
17 **	but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **	General Public License for more details.
20 **	\endlegal
21 */
22 /* ========================================================================= */
23 
24 /* === H E A D E R S ======================================================= */
25 
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 
30 #include "target.h"
31 #include "string.h"
32 #include "canvas.h"
33 #include "target_null.h"
34 #include "target_null_tile.h"
35 #include "targetparam.h"
36 
37 using namespace synfig;
38 using namespace etl;
39 using namespace std;
40 
41 synfig::Target::Book* synfig::Target::book_;
42 synfig::Target::ExtBook* synfig::Target::ext_book_;
43 
44 static synfig::Gamma* default_gamma_;
45 
46 /* === P R O C E D U R E S ================================================= */
47 
48 bool
subsys_init()49 Target::subsys_init()
50 {
51 	book_=new synfig::Target::Book();
52 	ext_book_=new synfig::Target::ExtBook();
53 //! \todo Do not hard core gamma to 2.2
54 	default_gamma_=new synfig::Gamma(1.0/2.2);
55 
56 	// At least one target must be available.
57 	book()["null"].factory =
58 		reinterpret_cast<synfig::Target::Factory>(&Target_Null::create);
59 	book()["null"].filename = "null";
60 	book()["null"].target_param = TargetParam();
61 	ext_book()["null"]="null";
62 
63 	book()["null-tile"].factory =
64 		reinterpret_cast<synfig::Target::Factory>(&Target_Null_Tile::create);
65 	book()["null-tile"].filename = "null-tile";
66 	book()["null-tile"].target_param = TargetParam();
67 	ext_book()["null-tile"]="null-tile";
68 
69 	return true;
70 }
71 
72 bool
subsys_stop()73 Target::subsys_stop()
74 {
75 	delete book_;
76 	delete ext_book_;
77 	delete default_gamma_;
78 	return true;
79 }
80 
81 Target::Book&
book()82 Target::book()
83 {
84 	return *book_;
85 }
86 
87 Target::ExtBook&
ext_book()88 Target::ext_book()
89 {
90 	return *ext_book_;
91 }
92 
93 
94 /* === M E T H O D S ======================================================= */
95 
Target()96 Target::Target():
97 	quality_(4),
98 	gamma_(*default_gamma_),
99 	alpha_mode(TARGET_ALPHA_MODE_KEEP),
100 	avoid_time_sync_(false),
101 	curr_frame_(0)
102 {
103 }
104 
105 void
set_canvas(etl::handle<Canvas> c)106 synfig::Target::set_canvas(etl::handle<Canvas> c)
107 {
108 	canvas=c;
109 	RendDesc desc=canvas->rend_desc();
110 	set_rend_desc(&desc);
111 }
112 
113 
114 Target::Handle
create(const String & name,const String & filename,synfig::TargetParam params)115 Target::create(const String &name, const String &filename,
116 			   synfig::TargetParam params)
117 {
118 	if(!book().count(name))
119 		return handle<Target>();
120 
121 	return Target::Handle(book()[name].factory(filename.c_str(), params));
122 }
123 
124 int
next_frame(Time & time)125 Target::next_frame(Time& time)
126 {
127 	int
128 	total_frames(1),
129 	frame_start(0),
130 	frame_end(0);
131 	Time
132 	time_start(0),
133 	time_end(0);
134 
135 	frame_start=desc.get_frame_start();
136 	frame_end=desc.get_frame_end();
137 	time_start=desc.get_time_start();
138 	time_end=desc.get_time_end();
139 	// TODO: Add option to exclude last frame
140 	// If user wants to recover the last buggy behavior then
141 	// expose this option to the interface using the target params.
142 	// At the moment it is set to false.
143 	bool exclude_last_frame(false);
144 	// Calculate the number of frames
145 	total_frames=frame_end-frame_start+(exclude_last_frame?0:1);
146 	if(total_frames<=0)total_frames=1;
147 
148 	if(total_frames == 1)
149 	{
150 		time=time_start;
151 	}
152 	else
153 	{
154 		time=(time_end-time_start)*curr_frame_/(total_frames-(exclude_last_frame?0:1))+time_start;
155 	}
156 
157 //	synfig::info("before curr_frame_: %d",curr_frame_);
158 	curr_frame_++;
159 
160 //	synfig::info("before curr_frame_: %d",curr_frame_);
161 //	synfig::info("total_frames: %d",total_frames);
162 //	synfig::info("time_end: %s",time_end.get_string().c_str());
163 //	synfig::info("time_start: %s",time_start.get_string().c_str());
164 //	synfig::info("time: %s",time.get_string().c_str());
165 //	synfig::info("remaining frames %d", total_frames-curr_frame_);
166 
167 	return total_frames- curr_frame_;
168 }
169 
170