~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/geometry.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-2004 by the Wide Lands Development Team
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#ifndef GEOMETRY_H
 
21
#define GEOMETRY_H
 
22
 
 
23
#include <cmath>
 
24
 
 
25
struct Point
 
26
{
 
27
   int x;
 
28
   int y;
 
29
 
 
30
   Point() { }
 
31
   Point(int px, int py) : x(px), y(py) { }
 
32
};
 
33
 
 
34
inline bool operator==(Point a, Point b) { return (a.x == b.x) && (a.y == b.y); }
 
35
inline bool operator!=(Point a, Point b) { return (a.x != b.x) || (a.y != b.y); }
 
36
// TODO adding points doesn't make sense
 
37
inline Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); }
 
38
inline Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }
 
39
 
 
40
struct Rect : public Point {
 
41
        int w;
 
42
        int h;
 
43
 
 
44
        Rect() { }
 
45
        Rect(int px, int py, int pw, int ph) : Point(px, py), w(pw), h(ph) { }
 
46
};
 
47
 
 
48
/** class Point_with_bright
 
49
 * this class is like a point, but with additional bright factor
 
50
 * bright is an int to make it possible to directly save shifted values (8.8 fixed or so)
 
51
 */
 
52
struct Point_with_bright : public Point {
 
53
   int b;
 
54
   Point_with_bright() : Point(0,0) { b=0; }
 
55
   Point_with_bright(int px, int py, int pb) : Point(px, py) { b=pb; }
 
56
};
 
57
 
 
58
/** struct Vertex
 
59
 *
 
60
 * This replaces Point_with_bright for use with the new texture mapping renderer.
 
61
 *
 
62
 * This struct is like a point, but with an additional bright factor and texture coordinates.
 
63
 */
 
64
struct Vertex:public Point {
 
65
        int b,tx,ty;
 
66
        Vertex (): Point (0,0) { b=tx=ty=0; }
 
67
        Vertex (int vx,int vy,int vb,int vtx,int vty): Point (vx,vy)
 
68
        { b=vb; tx=vtx; ty=vty; }
 
69
};
 
70
 
 
71
// hm, floats...
 
72
// tried to be faster with fixed point arithmetics
 
73
// it was, but i'll try to find other opts first
 
74
class Vector
 
75
{
 
76
   public:
 
77
      float x;
 
78
      float y;
 
79
      float z;
 
80
      Vector()
 
81
      {
 
82
         x = y = z = 0;
 
83
      }
 
84
      Vector(float px, float py, float pz)
 
85
      {
 
86
         x = px; y = py; z = pz;
 
87
      }
 
88
      void normalize()
 
89
      {
 
90
         float f = (float)sqrt(x*x + y*y + z*z);
 
91
         if (f == 0)
 
92
            return;
 
93
         x /= f;
 
94
         y /= f;
 
95
         z /= f;
 
96
      }
 
97
};
 
98
 
 
99
// vector addition
 
100
inline Vector operator + (const Vector& a, const Vector& b)
 
101
{
 
102
   return Vector(a.x + b.x, a.y + b.y, a.z + b.z);
 
103
}
 
104
 
 
105
// inner product
 
106
inline float operator * (const Vector& a, const Vector& b)
 
107
{
 
108
   return a.x * b.x + a.y * b.y + a.z * b.z;
 
109
}
 
110
 
 
111
 
 
112
// Structure used to store map coordinates
 
113
struct Coords {
 
114
   int x;
 
115
   int y;
 
116
 
 
117
        inline Coords() { }
 
118
        inline Coords(int nx, int ny) : x(nx), y(ny) { }
 
119
};
 
120
 
 
121
inline bool operator==(const Coords& c1, const Coords& c2) { return (c1.x == c2.x) && (c1.y == c2.y); }
 
122
inline bool operator!=(const Coords& c1, const Coords& c2) { return !(c1 == c2); }
 
123
 
 
124
class Field;
 
125
 
 
126
struct FCoords : public Coords {
 
127
        Field           *field;
 
128
 
 
129
        inline FCoords() { }
 
130
        inline FCoords(Coords nc, Field *nf) : Coords(nc), field(nf) { }
 
131
        inline FCoords(int nx, int ny, Field *nf) : Coords(nx, ny), field(nf) { }
 
132
};
 
133
 
 
134
#endif /* GEOMETRY_H */