add support to SDLBackend for rendering 24bit data
[openc2e.git] / caosVM_vectors.cpp
blob9db70b3d659e926c5b6c6dfced590a2b590f6ce9
1 /*
2 * caosVM_variables.cpp
3 * openc2e
5 * Created by Bryan Donlan on Sun Jul 23 2006
6 * Copyright (c) 2006 Bryan Donlan. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "caosVM.h"
21 #include <cmath>
22 #include "caosVar.h"
23 #include "physics.h"
25 #define PI (atanf(1)*4)
27 /**
28 * VEC: MAKE (vector) x (float) y (float)
29 * %status maybe
31 * Creates and returns a vector with the given components.
33 * Openc2e-only command
36 void caosVM::v_VEC_MAKE() {
37 VM_PARAM_FLOAT(y)
38 VM_PARAM_FLOAT(x)
40 result.setVector(Vector<float>(x, y));
43 /**
44 * VEC: GETC (command) vec (vector) x (variable) y (variable)
45 * %status maybe
47 * Extracts the components of vector vec and places them in x and y.
49 * Openc2e-only command
52 void caosVM::c_VEC_GETC() {
53 VM_PARAM_VARIABLE(y)
54 VM_PARAM_VARIABLE(x)
55 VM_PARAM_VECTOR(vec)
57 x->setFloat(vec.x);
58 y->setFloat(vec.y);
61 /**
62 * VEC: ANGL (float) vec (vector)
63 * %status maybe
65 * Find and return the angle from the X-axis of the given vector.
66 * This is computed using atan(y/x) if X is nonzero, and a hard-coded
67 * return if X is nonzero. If the input vector is the null vector (0,0),
68 * zero will be returned.
70 * The returned angle is in degrees, and in the range -180 to 180 degrees.
72 * Openc2e-only command.
75 void caosVM::v_VEC_ANGL() {
76 float ret = 0;
77 VM_PARAM_VECTOR(vec)
79 if (vec.x != 0) {
80 ret = atanf(fabsf(vec.y/vec.x))*180/PI;
81 if (vec.x < 0)
82 ret = 180 - ret;
83 if (vec.y < 0)
84 ret = -ret;
86 else if (vec.y > 0)
87 ret = 90;
88 else if (vec.y < 0)
89 ret = -90;
90 else if (vec.y == 0)
91 ret = 0;
92 if (ret == -180)
93 ret = 180; // hacky -_-;;
95 result.setFloat(ret);
98 /**
99 * VEC: SUBV (command) vec1 (variable) vec2 (vector)
100 * %status maybe
102 * Subtracts vec2 from vec1 and stores the result in vec1.
104 * Openc2e-only command.
107 void caosVM::c_VEC_SUBV() {
108 VM_PARAM_VECTOR(vec2)
109 VM_PARAM_VARIABLE(vec1)
111 if (!vec1->hasVector())
112 throw badParamException();
114 vec1->setVector(vec1->getVector() - vec2);
118 * VEC: ADDV (command) vec1 (variable) vec2 (vector)
119 * %status maybe
121 * Adds vec1 to vec2 and stores the result in vec1.
123 * Openc2e-only command.
126 void caosVM::c_VEC_ADDV() {
127 VM_PARAM_VECTOR(vec2)
128 VM_PARAM_VARIABLE(vec1)
130 if (!vec1->hasVector())
131 throw badParamException();
133 vec1->setVector(vec1->getVector() + vec2);
137 * VEC: MULV (command) vec (variable) mag (decimal)
138 * %status maybe
140 * Multiplies the magnitude of the vector vec by mag, and stores the result
141 * in vec.
143 * Openc2e-only command
146 void caosVM::c_VEC_MULV() {
147 VM_PARAM_FLOAT(mag)
148 VM_PARAM_VARIABLE(vec)
150 if (!vec->hasVector())
151 throw badParamException();
152 vec->setVector(vec->getVector().scale(mag));
156 * VEC: UNIT (vector) angle (decimal)
157 * %status maybe
159 * Constructs and returns a unit vector with angle angle.
161 * Openc2e-only command,
164 void caosVM::v_VEC_UNIT() {
165 VM_PARAM_FLOAT(angle)
167 result.setVector(Vector<float>::unitVector(angle * PI / 180));
171 * VEC: NULL (vector)
172 * %status maybe
174 * Returns the nullary (0,0) vector.
176 * Openc2e-only command
179 void caosVM::v_VEC_NULL() {
180 result.setVector(Vector<float>(0,0));
184 * VEC: MAGN (float) vec (vector)
185 * %status maybe
187 * Returns the magnitude of the passed vector.
189 * Openc2e-only command.
191 void caosVM::v_VEC_MAGN() {
192 VM_PARAM_VECTOR(vec)
194 result.setFloat(vec.getMagnitude());
198 * VEC: SETV (command) dest (variable) src (vector)
199 * %status maybe
201 * Sets the variable passed in dest to the vector in src
203 * Openc2e-only command
205 void caosVM::c_VEC_SETV() {
206 VM_PARAM_VECTOR(src)
207 VM_PARAM_VARIABLE(dest)
209 dest->setVector(src);
212 /* vim: set noet: */