[Add] Added that darn fixed point class.

This commit is contained in:
Rtch90 2017-12-22 23:34:48 +00:00
parent 3a2fa73f2d
commit 405b42d4b9
4 changed files with 69 additions and 3 deletions

View File

@ -16,5 +16,5 @@ include_HEADERS = body.h frame.h generic_system_view.h glfreetype.h gui_button.h
gui_radio_group.h gui_screen.h gui_toggle_button.h gui_widget.h libs.h matrix4x4.h mtrand.h objimport.h l3d.h \
planet.h player.h dynamic_body.h sector.h sector_view.h ship_cpanel.h ship.h space.h star.h star_system.h system_info_view.h \
system_view.h vector3.h view.h world_view.h date.h space_station.h space_station_view.h model_body.h gui_iselectable.h \
ship_type.h object.h info_view.h model_coll_mesh_data.h object_viewer_view.h
ship_type.h object.h info_view.h model_coll_mesh_data.h object_viewer_view.h fixed.h

56
src/fixed.h Normal file
View File

@ -0,0 +1,56 @@
#pragma once
#include <SDL_stdinc.h>
class fixed {
public:
enum { FRAC = 16 };
fixed(void) : v(0) {}
fixed(int raw) : v(raw) {}
fixed(int num, int denom) : v(((Sint64)num<<FRAC) / (Sint64)denom) {}
friend fixed operator+(const fixed a, const int b) { return a+fixed(b<<FRAC); }
friend fixed operator-(const fixed a, const int b) { return a-fixed(b<<FRAC); }
friend fixed operator*(const fixed a, const int b) { return a*fixed(b<<FRAC); }
friend fixed operator/(const fixed a, const int b) { return a/fixed(b<<FRAC); }
friend fixed operator+(const int a, const fixed b) { return fixed(a<<FRAC)+b; }
friend fixed operator-(const int a, const fixed b) { return fixed(a<<FRAC)-b; }
friend fixed operator*(const int a, const fixed b) { return fixed(a<<FRAC)*b; }
friend fixed operator/(const int a, const fixed b) { return fixed(a<<FRAC)/b; }
friend bool operator==(const fixed a, const int b) { return a == fixed(b<<FRAC); }
friend bool operator==(const int a, const fixed b) { return b == fixed(a<<FRAC); }
friend bool operator>=(const fixed a, const int b) { return a >= fixed(b<<FRAC); }
friend bool operator>=(const int a, const fixed b) { return b >= fixed(a<<FRAC); }
friend bool operator<=(const fixed a, const int b) { return a <= fixed(b<<FRAC); }
friend bool operator<=(const int a, const fixed b) { return b <= fixed(a<<FRAC); }
friend bool operator>(const fixed a, const int b) { return a > fixed(b<<FRAC); }
friend bool operator>(const int a, const fixed b) { return b > fixed(a<<FRAC); }
friend bool operator<(const fixed a, const int b) { return a < fixed(b<<FRAC); }
friend bool operator<(const int a, const fixed b) { return b < fixed(a<<FRAC); }
fixed &operator*=(const fixed a) { (*this) = (*this)*a; return (*this); }
fixed &operator*=(const int a) { (*this) = (*this)*a; return (*this); }
fixed &operator/=(const fixed a) { (*this) = (*this)/a; return (*this); }
fixed &operator/=(const int a) { (*this) = (*this)/a; return (*this); }
fixed &operator+=(const fixed a) { (*this) = (*this)+a; return (*this); }
fixed &operator+=(const int a) { (*this) = (*this)+a; return (*this); }
fixed &operator-=(const fixed a) { (*this) = (*this)-a; return (*this); }
fixed &operator-=(const int a) { (*this) = (*this)-a; return (*this); }
friend fixed operator+(const fixed a, const fixed b) { return fixed(a.v+b.v); }
friend fixed operator-(const fixed a, const fixed b) { return fixed(a.v-b.v); }
friend fixed operator*(const fixed a, const fixed b) { return fixed(((Sint64)a.v*(Sint64)b.v)>>FRAC); }
friend fixed operator/(const fixed a, const fixed b) { return fixed(((Sint64)a.v<<FRAC)/(Sint64)b.v); }
friend bool operator==(const fixed a, const fixed b) { return a.v == b.v; }
friend bool operator>(const fixed a, const fixed b) { return a.v > b.v; }
friend bool operator<(const fixed a, const fixed b) { return a.v < b.v; }
friend bool operator>=(const fixed a, const fixed b) { return a.v >= b.v; }
friend bool operator<=(const fixed a, const fixed b) { return a.v <= b.v; }
operator float(void) { return v/(float)(1<<FRAC); }
operator double(void) { return v/(double)(1<<FRAC); }
private:
int v;
};

View File

@ -15,6 +15,7 @@
#define alloca _alloca
#endif
#include "fixed.h"
#include "vector3.h"
#include "matrix4x4.h"
#include "mtrand.h"

View File

@ -1,5 +1,5 @@
#pragma once
#include "fixed.h"
/* Mersenne Twister rng. */
class MTRand_int32 {
public:
@ -61,7 +61,7 @@ public:
double NDouble(int p) {
double o = Double(1.0);
while(--p) o *= Double(1.0);
while(--p > 0) o *= Double(1.0);
return o;
}
@ -98,6 +98,15 @@ public:
return (static_cast<double>(rand_int32() >> 5) * 67108864. +
static_cast<double>(rand_int32() >> 6)) * (1./9007199254740992.);
}
/* [0,1] */
fixed Fixed(void) {
return fixed(rand_int32() >> fixed::FRAC);
}
fixed NFixed(int p) {
fixed o(1 << fixed::FRAC);
while(--p > 0) o*= Fixed();
return o;
}
/* [min,max] */
unsigned int Int32(int min, int max) {
return (rand_int32()%(1+max-min))+min;