From 7eb635401f3c2628f8aab4aa3c5bad10fddfb95a Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 22 Dec 2017 23:34:48 +0000 Subject: [PATCH] [Add] Added that darn fixed point class. --- src/Makefile.am | 2 +- src/fixed.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ src/libs.h | 1 + src/mtrand.h | 13 ++++++++++-- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/fixed.h diff --git a/src/Makefile.am b/src/Makefile.am index 646a1dc..b7abfaa 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/fixed.h b/src/fixed.h new file mode 100644 index 0000000..98b8e02 --- /dev/null +++ b/src/fixed.h @@ -0,0 +1,56 @@ +#pragma once +#include + +class fixed { +public: + enum { FRAC = 16 }; + fixed(void) : v(0) {} + fixed(int raw) : v(raw) {} + fixed(int num, int denom) : v(((Sint64)num<=(const fixed a, const int b) { return a >= fixed(b<=(const int a, const fixed b) { return b >= fixed(a<(const fixed a, const int b) { return a > fixed(b<(const int a, const fixed b) { return b > fixed(a<>FRAC); } + friend fixed operator/(const fixed a, const fixed b) { return fixed(((Sint64)a.v<(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< 0) o *= Double(1.0); return o; } @@ -98,6 +98,15 @@ public: return (static_cast(rand_int32() >> 5) * 67108864. + static_cast(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;