# SwEng/cleanup

- deleted most old libraries/headers in codepit to avoid confusion (they
are now in SVN anyway). updated required-libraries-linux.txt accordingly
- moved rand() into separate file, out of lib.cpp
- removed CGUIScrollBarStyle.cpp to avoid empty-file warning
- wxwidgets.h: remove redundant #pragma lib and include wxw PCH
- move openal-specific stuff to external_libraries/openal.h
- cpu, bsd: macosx is-a bsd, so only test OS_BSD

This was SVN commit r5082.
This commit is contained in:
janwas 2007-05-18 00:14:26 +00:00
parent 7d4af5979b
commit 9d2acce9d8
23 changed files with 169 additions and 152 deletions

View File

@ -24,8 +24,7 @@ $GL := directory containing OpenGL headers.
$binaries := directory containing system/ and data/
- OpenGL extensions:
download http://www.wildfiregames.com/~code/libraries/glext.h
... and put it in $GL.
copy libraries/opengl/include/GL/glext.h to $GL.
- SpiderMonkey [javascript]
The source is *not* compatible with the mozjs libraries included in mozilla

View File

@ -9,6 +9,7 @@
#include "ps/XML/Xeromyces.h"
#include "ps/CLogger.h"
#include "lib/timer.h"
#include "lib/rand.h"
#include "maths/MathUtil.h"
#define LOG_CATEGORY "graphics"

View File

@ -17,6 +17,7 @@
#include "ps/XML/XMLWriter.h"
#include "lib/res/file/vfs.h"
#include "lib/rand.h"
#include <sstream>

View File

@ -1,8 +0,0 @@
/*
SGUIScrollBarStyle
*/
#include "precompiled.h"
#include "GUI.h"
using namespace std;

View File

@ -0,0 +1,26 @@
/**
* =========================================================================
* File : openal.h
* Project : 0 A.D.
* Description : bring in OpenAL header+library, with compatibility fixes
* =========================================================================
*/
// license: GPL; see lib/license.txt
#ifndef INCLUDED_OPENAL
#define INCLUDED_OPENAL
#ifdef __APPLE__
# include <OpenAL/al.h>
# include <OpenAL/alc.h>
#else
# include <AL/al.h>
# include <AL/alc.h>
#endif
#if MSC_VERSION
# pragma comment(lib, "openal32.lib")
#endif
#endif // #ifndef INCLUDED_OPENAL

View File

@ -23,55 +23,19 @@ struct HINSTANCE__
typedef struct HINSTANCE__* HINSTANCE; // definition as if STRICT were #defined
#include "wx/wx.h"
#include "wx/wxprec.h"
#include "wx/file.h"
#include "wx/ffile.h"
#include "wx/filename.h"
#include "wx/mimetype.h"
#include "wx/statline.h"
#include "wx/debugrpt.h"
#ifdef __WXMSW__
#include "wx/evtloop.h" // for SetCriticalWindow()
#endif // __WXMSW__
// automatically link against the required library
#if MSC_VERSION
# ifdef NDEBUG
# else
# pragma comment(lib, "wxmsw28ud_core.lib")
# pragma comment(lib, "wxmsw28ud_qa.lib")
# pragma comment(lib, "wxbase28ud.lib")
# pragma comment(lib, "wxbase28ud_xml.lib")
//# pragma comment(lib, "wxbase28ud_net.lib")
//# pragma comment(lib, "wxbase28ud_odbc.lib")
//# pragma comment(lib, "wxmsw28ud_adv.lib")
//# pragma comment(lib, "wxmsw28ud_aui.lib")
//# pragma comment(lib, "wxmsw28ud_dbgrid.lib")
//# pragma comment(lib, "wxmsw28ud_gl.lib")
//# pragma comment(lib, "wxmsw28ud_html.lib")
//# pragma comment(lib, "wxmsw28ud_media.lib")
//# pragma comment(lib, "wxmsw28ud_richtext.lib")
//# pragma comment(lib, "wxmsw28ud_xrc.lib")
//# pragma comment(lib, "wxexpatd.lib")
//# pragma comment(lib, "wxpngd.lib")
//# pragma comment(lib, "wxjpegd.lib")
//# pragma comment(lib, "wxtiffd.lib")
//# pragma comment(lib, "wxzlibd.lib")
//# pragma comment(lib, "wxregexd.lib")
# pragma comment(lib, "Rpcrt4.lib") // Uuid
# pragma comment(lib, "comctl32.lib") // ImageList_*
# endif // NDEBUG
#endif // MSC_VERSION
// note: wxWidgets already does #pragma comment(lib) to add link targets.
#endif // #ifndef INCLUDED_WXWIDGETS

View File

@ -35,57 +35,6 @@ u16 subusw(u16 x, u16 y)
//-----------------------------------------------------------------------------
// rand
// return random integer in [min, max).
// avoids several common pitfalls; see discussion at
// http://www.azillionmonkeys.com/qed/random.html
// rand() is poorly implemented (e.g. in VC7) and only returns < 16 bits;
// double that amount by concatenating 2 random numbers.
// this is not to fix poor rand() randomness - the number returned will be
// folded down to a much smaller interval anyway. instead, a larger XRAND_MAX
// decreases the probability of having to repeat the loop.
#if RAND_MAX < 65536
static const uint XRAND_MAX = (RAND_MAX+1)*(RAND_MAX+1) - 1;
static uint xrand()
{
return rand()*(RAND_MAX+1) + rand();
}
// rand() is already ok; no need to do anything.
#else
static const uint XRAND_MAX = RAND_MAX;
static uint xrand()
{
return rand();
}
#endif
uint rand(uint min_inclusive, uint max_exclusive)
{
const uint range = (max_exclusive-min_inclusive);
// huge interval or min >= max
if(range == 0 || range > XRAND_MAX)
{
WARN_ERR(ERR::INVALID_PARAM);
return 0;
}
const uint inv_range = XRAND_MAX / range;
// generate random number in [0, range)
// idea: avoid skewed distributions when <range> doesn't evenly divide
// XRAND_MAX by simply discarding values in the "remainder".
// not expected to run often since XRAND_MAX is large.
uint x;
do
x = xrand();
while(x >= range * inv_range);
x /= inv_range;
x += min_inclusive;
debug_assert(x < max_exclusive);
return x;
}
//-----------------------------------------------------------------------------
// type conversion

View File

@ -207,14 +207,6 @@ inline bool feqf(float f1, float f2, float epsilon = 0.001f)
}
/**
* return random integer in [min, max).
* avoids several common pitfalls; see discussion at
* http://www.azillionmonkeys.com/qed/random.html
**/
extern uint rand(uint min_inclusive, uint max_exclusive);
//-----------------------------------------------------------------------------
// type conversion

62
source/lib/rand.cpp Normal file
View File

@ -0,0 +1,62 @@
/**
* =========================================================================
* File : rand.cpp
* Project : 0 A.D.
* Description : pseudorandom number generator
* =========================================================================
*/
// license: GPL; see lib/license.txt
#include "precompiled.h"
#include "rand.h"
// avoids several common pitfalls; see discussion at
// http://www.azillionmonkeys.com/qed/random.html
// rand() is poorly implemented (e.g. in VC7) and only returns < 16 bits;
// double that amount by concatenating 2 random numbers.
// this is not to fix poor rand() randomness - the number returned will be
// folded down to a much smaller interval anyway. instead, a larger XRAND_MAX
// decreases the probability of having to repeat the loop.
#if RAND_MAX < 65536
static const uint XRAND_MAX = (RAND_MAX+1)*(RAND_MAX+1) - 1;
static uint xrand()
{
return rand()*(RAND_MAX+1) + rand();
}
// rand() is already ok; no need to do anything.
#else
static const uint XRAND_MAX = RAND_MAX;
static uint xrand()
{
return rand();
}
#endif
uint rand(uint min_inclusive, uint max_exclusive)
{
const uint range = (max_exclusive-min_inclusive);
// huge interval or min >= max
if(range == 0 || range > XRAND_MAX)
{
WARN_ERR(ERR::INVALID_PARAM);
return 0;
}
const uint inv_range = XRAND_MAX / range;
// generate random number in [0, range)
// idea: avoid skewed distributions when <range> doesn't evenly divide
// XRAND_MAX by simply discarding values in the "remainder".
// not expected to run often since XRAND_MAX is large.
uint x;
do
x = xrand();
while(x >= range * inv_range);
x /= inv_range;
x += min_inclusive;
debug_assert(x < max_exclusive);
return x;
}

21
source/lib/rand.h Normal file
View File

@ -0,0 +1,21 @@
/**
* =========================================================================
* File : rand.h
* Project : 0 A.D.
* Description : pseudorandom number generator
* =========================================================================
*/
// license: GPL; see lib/license.txt
#ifndef INCLUDED_RAND
#define INCLUDED_RAND
/**
* return random integer in [min, max).
* avoids several common pitfalls; see discussion at
* http://www.azillionmonkeys.com/qed/random.html
**/
extern uint rand(uint min_inclusive, uint max_exclusive);
#endif // #ifndef INCLUDED_RAND

View File

@ -15,6 +15,7 @@
#include "lib/posix/posix_filesystem.h"
#include "lib/adts.h"
#include "lib/rand.h"
#include "lib/allocators.h"
#include "lib/sysdep/sysdep.h"
#include "file_internal.h"

View File

@ -9,6 +9,7 @@
#include "lib/res/file/archive_builder.h"
#include "lib/res/h_mgr.h"
#include "lib/res/mem.h"
#include "lib/rand.h"
class TestArchiveBuilder : public CxxTest::TestSuite
{

View File

@ -1,6 +1,7 @@
#include "lib/self_test.h"
#include "lib/res/file/file_cache.h"
#include "lib/rand.h"
class TestFileCache : public CxxTest::TestSuite
{

View File

@ -19,15 +19,7 @@
#include <deque>
#include <math.h>
#include "maths/MathUtil.h"
#ifdef __APPLE__
# include <OpenAL/al.h>
# include <OpenAL/alc.h>
#else
# include <AL/al.h>
# include <AL/alc.h>
#endif
#include "maths/MathUtil.h" // PI
// for DLL-load hack in alc_init
#if OS_WIN
@ -37,15 +29,11 @@
#include "lib/res/res.h"
#include "lib/timer.h"
#include "lib/app_hooks.h"
#include "lib/external_libraries/openal.h"
#define OGG_HACK
#include "ogghack.h"
#if MSC_VERSION
# pragma comment(lib, "openal32.lib")
#endif
// HACK: OpenAL loads and unloads certain DLLs several times on Windows.
// that looks unnecessary and wastes 100..400 ms on startup.
// we hold a reference to prevent the actual unload. everything works ATM;

View File

@ -17,7 +17,7 @@
# include "lib/sysdep/ia32/ia32.h"
# include "lib/sysdep/ia32/ia32_memcpy.h"
#endif
#if OS_BSD || OS_MACOSX
#if OS_BSD
# include "lib/sysdep/unix/bsd.h"
#endif
#if OS_WIN

View File

@ -1,7 +1,7 @@
#include "precompiled.h"
#include "bsd.h"
#if OS_BSD || OS_MACOSX
#if OS_BSD
#include <sys/sysctl.h>

View File

@ -1,6 +1,7 @@
#include "lib/self_test.h"
#include "lib/adts.h"
#include "lib/rand.h"
class TestRingbuf : public CxxTest::TestSuite
{

View File

@ -1,6 +1,7 @@
#include "lib/self_test.h"
#include "lib/cache_adt.h"
#include "lib/rand.h"
class TestCache: public CxxTest::TestSuite
{

View File

@ -43,33 +43,4 @@ public:
}
// fp_to_u?? already validate the result.
void test_rand()
{
// complain if huge interval or min > max
debug_skip_next_err(ERR::INVALID_PARAM);
TS_ASSERT_EQUALS(rand(1, 0), 0);
debug_skip_next_err(ERR::INVALID_PARAM);
TS_ASSERT_EQUALS(rand(2, ~0u), 0);
// returned number must be in [min, max)
for(int i = 0; i < 100; i++)
{
uint min = rand(), max = min+rand();
uint x = rand(min, max);
TS_ASSERT(min <= x && x < max);
}
// make sure both possible values are hit
uint ones = 0, twos = 0;
for(int i = 0; i < 100; i++)
{
uint x = rand(1, 3);
// paranoia: don't use array (x might not be 1 or 2 - checked below)
if(x == 1) ones++;
if(x == 2) twos++;
}
TS_ASSERT_EQUALS(ones+twos, 100);
TS_ASSERT(ones > 10 && twos > 10);
}
};

View File

@ -3,6 +3,7 @@
#include "lib/lockfree.h"
#include "lib/sysdep/cpu.h" // atomic_add
#include "lib/timer.h"
#include "lib/rand.h"
// make sure the data structures work at all; doesn't test thread-safety.
class TestLockfreeBasic : public CxxTest::TestSuite

View File

@ -0,0 +1,42 @@
#include "lib/self_test.h"
#include "lib/rand.h"
class TestRand : public CxxTest::TestSuite
{
public:
// complain if huge interval or min > max
void TestParam()
{
debug_skip_next_err(ERR::INVALID_PARAM);
TS_ASSERT_EQUALS(rand(1, 0), 0);
debug_skip_next_err(ERR::INVALID_PARAM);
TS_ASSERT_EQUALS(rand(2, ~0u), 0);
}
// returned number must be in [min, max)
void TestReturnedRange()
{
for(int i = 0; i < 100; i++)
{
uint min = rand(), max = min+rand();
uint x = rand(min, max);
TS_ASSERT(min <= x && x < max);
}
}
// make sure both possible values are hit
void TestTwoValues()
{
uint ones = 0, twos = 0;
for(int i = 0; i < 100; i++)
{
uint x = rand(1, 3);
// paranoia: don't use array (x might not be 1 or 2 - checked below)
if(x == 1) ones++;
if(x == 2) twos++;
}
TS_ASSERT_EQUALS(ones+twos, 100);
TS_ASSERT(ones > 10 && twos > 10);
}
};

View File

@ -21,6 +21,8 @@
#include "ps/Game.h"
#include "ps/World.h"
#include "lib/rand.h"
enum EGotoSituation
{
NORMAL = 0,

View File

@ -16,6 +16,7 @@
#include "ps/XML/Xeromyces.h"
#include "ps/CLogger.h"
#include "lib/rand.h"
#define LOG_CATEGORY "audio"