~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/editor/EditorView.cc

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Goulais
  • Date: 2004-08-09 10:26:00 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040809102600-lg2q9lfars0q1p42
Tags: 0.6.0-8
Applied patch from Andreas Jochens (Closes: #263992)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//  $Id: EditorView.cc,v 1.10 2002/01/15 10:48:51 grumbel Exp $
2
 
//
3
 
//  Pingus - A free Lemmings clone
4
 
//  Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
5
 
//
6
 
//  This program is free software; you can redistribute it and/or
7
 
//  modify it under the terms of the GNU General Public License
8
 
//  as published by the Free Software Foundation; either version 2
9
 
//  of the License, or (at your option) any later version.
10
 
//
11
 
//  This program is distributed in the hope that it will be useful,
12
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
//  GNU General Public License for more details.
15
 
//
16
 
//  You should have received a copy of the GNU General Public License
17
 
//  along with this program; if not, write to the Free Software
18
 
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
 
 
20
 
#include "../Sprite.hh"
21
 
#include "EditorView.hh"
22
 
 
23
 
EditorView::EditorView (int x1, int y1, int x2, int y2, 
24
 
                        int x_offset, int y_offset)
25
 
  : x1 (x1), y1 (y1), x2 (x2), y2 (y2), offset (-(x2 - x1)/2.0f, -(y2-x1)/2.0f, 1.0f)
26
 
{
27
 
  center = CL_Vector ((x2 - x1)/2.0f + x1,
28
 
                      (y2 - y1)/2.0f + y1);
29
 
  std::cout << "View: " << x1 << ", " << y1 << ", " << x2 << ", " << y2 
30
 
            << std::endl;
31
 
}
32
 
 
33
 
EditorView::~EditorView ()
34
 
{
35
 
}
36
 
 
37
 
CL_Vector
38
 
EditorView::get_offset ()
39
 
{
40
 
  return offset;
41
 
}
42
 
 
43
 
float
44
 
EditorView::get_zoom ()
45
 
{
46
 
  return offset.z;
47
 
}
48
 
 
49
 
void
50
 
EditorView::set_zoom (float new_zoom)
51
 
{
52
 
  offset.z = new_zoom;
53
 
  //std::cout << "Zoom: " << offset.z << std::endl;
54
 
}
55
 
 
56
 
void 
57
 
EditorView::zoom_to (CL_Rect arg_rect)
58
 
{
59
 
  CL_Rect rect;
60
 
 
61
 
  rect.x1 = min (arg_rect.x1, arg_rect.x2);
62
 
  rect.x2 = max (arg_rect.x1, arg_rect.x2);
63
 
  rect.y1 = min (arg_rect.y1, arg_rect.y2);
64
 
  rect.y2 = max (arg_rect.y1, arg_rect.y2);
65
 
  
66
 
  CL_Vector pos1 = screen_to_world (CL_Vector(rect.x1, rect.y1));
67
 
  CL_Vector pos2 = screen_to_world (CL_Vector(rect.x2, rect.y2));
68
 
 
69
 
  CL_Vector center = (pos2 + pos1) * 0.5f;
70
 
  offset = -center;
71
 
 
72
 
  float width  = pos2.x - pos1.x;
73
 
  float height = pos2.y - pos1.y;
74
 
 
75
 
  if (width < 10 && height < 10)
76
 
    return ;
77
 
 
78
 
  float screen_relation = get_width () / get_height ();
79
 
  float rect_reation = width / height;
80
 
  
81
 
  if (rect_reation > screen_relation)
82
 
    {
83
 
      set_zoom (get_width () / (pos2.x - pos1.x));
84
 
    }
85
 
  else
86
 
    {
87
 
      set_zoom (get_height () / (pos2.y - pos1.y));
88
 
    }
89
 
}
90
 
 
91
 
int 
92
 
EditorView::get_width ()
93
 
{
94
 
  return x2 - x1;
95
 
}
96
 
 
97
 
int
98
 
EditorView::get_height ()
99
 
{
100
 
  return y2 - y1;
101
 
}
102
 
 
103
 
void 
104
 
EditorView::move (CL_Vector delta)
105
 
{
106
 
  offset += delta;
107
 
}
108
 
 
109
 
CL_Vector
110
 
EditorView::screen_to_world (CL_Vector pos)
111
 
{
112
 
  return ((pos - center) * (1.0f/offset.z)) - offset;
113
 
}
114
 
 
115
 
CL_Vector
116
 
EditorView::world_to_screen (CL_Vector pos)
117
 
{
118
 
  return ((pos + offset) * offset.z) + center;
119
 
}
120
 
 
121
 
float 
122
 
EditorView::get_x_offset ()
123
 
{
124
 
  return offset.x;
125
 
}
126
 
 
127
 
float 
128
 
EditorView::get_y_offset ()
129
 
{
130
 
  return offset.y;
131
 
}
132
 
 
133
 
void 
134
 
EditorView::draw (Sprite& sprite, const CL_Vector& pos)
135
 
{
136
 
  CL_Surface sur (sprite.get_surface ());
137
 
  draw (sur, 
138
 
        (int) pos.x + sprite.get_x_align (),
139
 
        (int) pos.y + sprite.get_y_align ());
140
 
}
141
 
 
142
 
void 
143
 
EditorView::draw (Sprite& sprite, const CL_Vector& pos, int frame)
144
 
{
145
 
  CL_Surface sur (sprite.get_surface ());
146
 
  draw (sur, 
147
 
        (int) pos.x + sprite.get_x_align (),
148
 
        (int) pos.y + sprite.get_y_align (), 
149
 
        frame);
150
 
}
151
 
 
152
 
void 
153
 
EditorView::draw (CL_Surface& sur, const CL_Vector& pos)
154
 
{
155
 
  if (offset.z == 1.0)
156
 
    {   
157
 
      sur.put_screen (int(pos.x + get_x_offset () + center.x),
158
 
                      int(pos.y + get_y_offset () + center.y));
159
 
    }
160
 
  else
161
 
    {
162
 
      sur.put_screen (int((pos.x + get_x_offset ()) * offset.z + center.x),
163
 
                      int((pos.y + get_y_offset ()) * offset.z + center.y),
164
 
                      offset.z, offset.z);
165
 
    }
166
 
  //CL_Display::draw_line (x1, y1, x2, y2, 1.0, 1.0, 0.0);
167
 
  //CL_Display::draw_line (x1, y2, x2, y1, 1.0, 1.0, 0.0);
168
 
}
169
 
 
170
 
void 
171
 
EditorView::draw (CL_Surface& sur, const CL_Vector& pos, int frame)
172
 
{
173
 
  draw (sur, int(pos.x), int(pos.y), frame);
174
 
}
175
 
 
176
 
void 
177
 
EditorView::draw (CL_Surface& sur, int x_pos, int y_pos)
178
 
{
179
 
  if (offset.z == 1.0)
180
 
    {
181
 
      sur.put_screen (int(x_pos + get_x_offset () + center.x),
182
 
                      int(y_pos + get_y_offset () + center.y));
183
 
    }
184
 
  else
185
 
    {
186
 
      sur.put_screen (int((x_pos + get_x_offset ()) * offset.z + center.x),
187
 
                      int((y_pos + get_y_offset ()) * offset.z + center.y),
188
 
                      offset.z, offset.z);
189
 
    }
190
 
}
191
 
 
192
 
void 
193
 
EditorView::draw (CL_Surface& sur, int x_pos, int y_pos, int frame)
194
 
{
195
 
  if (offset.z == 1.0)
196
 
    {
197
 
      sur.put_screen (int(x_pos + get_x_offset () + center.x),
198
 
                      int(y_pos + get_y_offset () + center.y),
199
 
                      frame);  
200
 
    }
201
 
  else
202
 
    {
203
 
      sur.put_screen (int((x_pos + get_x_offset ()) * offset.z + center.x),
204
 
                      int((y_pos + get_y_offset ()) * offset.z + center.y),
205
 
                      offset.z, offset.z,
206
 
                      frame);  
207
 
    }
208
 
}
209
 
 
210
 
void 
211
 
EditorView::draw (CL_Surface& sur, int x_pos, int y_pos, 
212
 
            float size_x, float size_y, int frame)
213
 
{
214
 
  sur.put_screen (int(x_pos + get_x_offset () + center.x),
215
 
                  int(y_pos + get_y_offset () + center.y),
216
 
                  size_x * offset.z, 
217
 
                  size_y * offset.z, frame); 
218
 
}
219
 
 
220
 
void 
221
 
EditorView::draw_line (const CL_Vector& pos1, const CL_Vector& pos2,
222
 
                       float r, float g, float b, float a)
223
 
{
224
 
  draw_line (int(pos1.x), int(pos1.y), int(pos2.x), int(pos2.y), r, g, b, a);
225
 
}
226
 
 
227
 
void 
228
 
EditorView::draw_line (int x1, int y1, int x2, int y2, 
229
 
                       float r, float g, float b, float a)
230
 
{
231
 
  CL_Display::draw_line (int((x1 + get_x_offset ()) * offset.z + center.x),
232
 
                         int((y1 + get_y_offset ()) * offset.z + center.y),
233
 
                         int((x2 + get_x_offset ()) * offset.z + center.x),
234
 
                         int((y2 + get_y_offset ()) * offset.z + center.y),
235
 
                         r, g, b, a);
236
 
}
237
 
 
238
 
void 
239
 
EditorView::draw_fillrect (int x1, int y1, int x2, int y2, 
240
 
                           float r, float g, float b, float a)
241
 
{
242
 
  CL_Display::fill_rect (int((x1 + get_x_offset ()) * offset.z + center.x),
243
 
                         int((y1 + get_y_offset ()) * offset.z + center.y), 
244
 
                         int((x2 + get_x_offset ()) * offset.z + center.x),
245
 
                         int((y2 + get_y_offset ()) * offset.z + center.y),
246
 
                         r, g, b, a);
247
 
}
248
 
 
249
 
void 
250
 
EditorView::draw_rect (int x1, int y1, int x2, int y2, 
251
 
                 float r, float g, float b, float a)
252
 
{
253
 
  CL_Display::draw_rect (int((x1 + get_x_offset ()) * offset.z + center.x),
254
 
                         int((y1 + get_y_offset ()) * offset.z + center.y), 
255
 
                         int((x2 + get_x_offset ()) * offset.z + center.x),
256
 
                         int((y2 + get_y_offset ()) * offset.z + center.y),
257
 
                         r, g, b, a);
258
 
}
259
 
 
260
 
void 
261
 
EditorView::draw_pixel (int x_pos, int y_pos, 
262
 
                   float r, float g, float b, float a)
263
 
{
264
 
  //CL_Display::put_pixel (x1 + get_x_offset (),
265
 
  //                     y1 + get_y_offset (), r, g, b, a);
266
 
  std::cout << "View::draw_pixel () not implemented" << std::endl;
267
 
}
268
 
 
269
 
void 
270
 
EditorView::draw_circle (int x_pos, int y_pos, int radius,
271
 
                   float r, float g, float b, float a)
272
 
{
273
 
  // FIXME: Probally not the fast circle draw algo on this world...
274
 
  const float pi = 3.1415927f * 2.0f;
275
 
  const float steps = 8;
276
 
  CL_Vector current (radius, 0);
277
 
  CL_Vector next = current.rotate (pi/steps, CL_Vector (0, 0, 1.0f));
278
 
 
279
 
  for (int i = 0; i < steps; ++i)
280
 
    {
281
 
      draw_line (int(x_pos + current.x), int(y_pos + current.y),
282
 
                 int(x_pos + next.x), int(y_pos + next.y),
283
 
                 r, g, b, a);
284
 
      current = next;
285
 
      next = next.rotate (pi/8, CL_Vector (0, 0, 1.0f));
286
 
    }
287
 
}
288
 
 
289
 
/* EOF */