Permalink
Browse files

Added random number generator and something that uses it as a test.

  • Loading branch information...
1 parent e427cb7 commit 9767ebb09f16df9884684df9b86cd5f2b02f73ed @wilkie committed May 19, 2009
Showing with 244 additions and 16 deletions.
  1. +12 −12 core/graphics.d
  2. +91 −0 core/random.d
  3. +2 −0 djehuty.d
  4. +6 −1 hello.lua
  5. +1 −1 makefile
  6. 0 scripting/bindings/perl.d
  7. +22 −0 scripting/lua.d
  8. +110 −2 winsamp.d
View
@@ -243,7 +243,7 @@ class Graphics
// x: The x coordinate to place the top-left corner of the image.
// y: The y coordinate to place the top-left corner of the image.
// view: The view in which to draw.
- void drawView(int x, int y, ref View view)
+ void drawView(int x, int y, View view)
{
view.lockDisplay();
@@ -260,7 +260,7 @@ class Graphics
// view: The view in which to draw.
// viewX: The x coordinate to crop the image. Everything from this X to X + viewWidth of the view will be drawn.
// viewY: The y coordinate to crop the image. Everything from this Y to Y + viewHeight of the view will be drawn.
- void drawView(int x, int y, ref View view, int viewX, int viewY)
+ void drawView(int x, int y, View view, int viewX, int viewY)
{
view.lockDisplay();
@@ -279,7 +279,7 @@ class Graphics
// viewY: The y coordinate to crop the image. Everything from this Y to Y + viewHeight of the view will be drawn.
// viewWidth: The width of the region to crop from the source image.
// viewHeight: The height of the region to crop from the source image.
- void drawView(int x, int y, ref View view, int viewX, int viewY, int viewWidth, int viewHeight)
+ void drawView(int x, int y, View view, int viewX, int viewY, int viewWidth, int viewHeight)
{
view.lockDisplay();
@@ -295,7 +295,7 @@ class Graphics
// y: The y coordinate to place the top-left corner of the image.
// view: The view in which to draw.
// opacity: The opacity. 1.0 is full opacity. 0.0 will result in no image, as this is full transparency.
- void drawView(int x, int y, ref View view, double opacity)
+ void drawView(int x, int y, View view, double opacity)
{
view.lockDisplay();
@@ -313,7 +313,7 @@ class Graphics
// viewX: The x coordinate to crop the image. Everything from this X to X + viewWidth of the view will be drawn.
// viewY: The y coordinate to crop the image. Everything from this Y to Y + viewHeight of the view will be drawn.
// opacity: The opacity. 1.0 is full opacity. 0.0 will result in no image, as this is full transparency.
- void drawView(int x, int y, ref View view, int viewX, int viewY, double opacity)
+ void drawView(int x, int y, View view, int viewX, int viewY, double opacity)
{
view.lockDisplay();
@@ -333,7 +333,7 @@ class Graphics
// viewWidth: The width of the region to crop from the source image.
// viewHeight: The height of the region to crop from the source image.
// opacity: The opacity. 1.0 is full opacity. 0.0 will result in no image, as this is full transparency.
- void drawView(int x, int y, ref View view, int viewX, int viewY, int viewWidth, int viewHeight, double opacity)
+ void drawView(int x, int y, View view, int viewX, int viewY, int viewWidth, int viewHeight, double opacity)
{
view.lockDisplay();
@@ -350,7 +350,7 @@ class Graphics
// x: The x coordinate to place the top-left corner of the image.
// y: The y coordinate to place the top-left corner of the image.
// image: The image to draw.
- void drawImage(int x, int y, ref Image image)
+ void drawImage(int x, int y, Image image)
{
ImageLock(image);
View v = image.getView();
@@ -367,7 +367,7 @@ class Graphics
// image: The image to draw.
// srcX: The x coordinate to crop the image. The top-left corner of the render will correspond to the top-left corner of this cropped region.
// srcY: The y coordinate to crop the image. The top-left corner of the render will correspond to the top-left corner of this cropped region.
- void drawImage(int x, int y, ref Image image, int srcX, int srcY)
+ void drawImage(int x, int y, Image image, int srcX, int srcY)
{
ImageLock(image);
View v = image.getView();
@@ -386,7 +386,7 @@ class Graphics
// srcY: The y coordinate to crop the image. The top-left corner of the render will correspond to the top-left corner of this cropped region.
// srcW: The width of the cropped region.
// srcH: The height of the cropped region.
- void drawImage(int x, int y, ref Image image, int srcX, int srcY, int srcW, int srcH)
+ void drawImage(int x, int y, Image image, int srcX, int srcY, int srcW, int srcH)
{
ImageLock(image);
View v = image.getView();
@@ -402,7 +402,7 @@ class Graphics
// y: The y coordinate to place the top-left corner of the image.
// image: The image to draw.
// opacity: The opacity. 1.0 is full opacity. 0.0 will result in no image, as this is full transparency.
- void drawImage(int x, int y, ref Image image, double opacity)
+ void drawImage(int x, int y, Image image, double opacity)
{
ImageLock(image);
View v = image.getView();
@@ -420,7 +420,7 @@ class Graphics
// srcX: The x coordinate to crop the image. The top-left corner of the render will correspond to the top-left corner of this cropped region.
// srcY: The y coordinate to crop the image. The top-left corner of the render will correspond to the top-left corner of this cropped region.
// opacity: The opacity. 1.0 is full opacity. 0.0 will result in no image, as this is full transparency.
- void drawImage(int x, int y, ref Image image, int srcX, int srcY, double opacity)
+ void drawImage(int x, int y, Image image, int srcX, int srcY, double opacity)
{
ImageLock(image);
View v = image.getView();
@@ -440,7 +440,7 @@ class Graphics
// srcW: The width of the cropped region.
// srcH: The height of the cropped region.
// opacity: The opacity. 1.0 is full opacity. 0.0 will result in no image, as this is full transparency.
- void drawImage(int x, int y, ref Image image, int srcX, int srcY, int srcW, int srcH, double opacity)
+ void drawImage(int x, int y, Image image, int srcX, int srcY, int srcW, int srcH, double opacity)
{
ImageLock(image);
View v = image.getView();
View
@@ -0,0 +1,91 @@
+/*
+ * random.d
+ *
+ * This module implements a random number generator.
+ *
+ * It is based upon the implementation by Steve Park and Dave Geyer. That is,
+ * it is based upon a Lehmer random number generator. It returns a random
+ * number distributed uniformly between 0.0 and 1.0.
+ *
+ * Author: Dave Wilkinson
+ * Reference: Steve Park, Dave Geyer
+ * Originated: May 18, 2009
+ *
+ */
+
+module core.random;
+
+import platform.imports;
+mixin(PlatformGenericImport!("definitions"));
+mixin(PlatformScaffoldImport!());
+
+// Description: This class represents a Random number generator.
+class Random {
+
+ // Description: This will set up a new random number generator and will seed it with the given seed.
+ // seed: The seed to use with the generator.
+ this(long seed = -1) {
+ setSeed(seed);
+ }
+
+ // Description: This will reseed the random number generator.
+ // seed: The seem to use with the generator.
+ void setSeed(long seed) {
+ if (seed < 0) {
+ seed = Scaffold.TimeGet();
+ }
+ state = seed;
+ }
+
+ // Description: This will retrieve the current state of the generator.
+ // Returns: The state of the generator. (Reseed with this value to continue from the same position)
+ long getSeed() {
+ return state;
+ }
+
+ double nextDouble() {
+ mutateState();
+ return cast(double)state / cast(double)MODULUS;
+ }
+
+ long next() {
+ mutateState();
+ return state;
+ }
+
+ long next(long max) {
+ if (max < 0) { return 0; }
+ return next() % max;
+ }
+
+ long next(long min, long max) {
+ if (max < 0) { return 0; }
+ if (min < 0) { return 0; }
+ if (min > max) { max = min; }
+
+ return (next() % (max - min)) + min;
+ }
+
+protected:
+ const auto MODULUS = 2147483647;
+ const auto MULTIPLIER = 48271;
+ const auto CHECK = 399268537;
+ const auto A256 = 22925;
+ const auto DEFAULT = 123456789;
+
+ long state = DEFAULT;
+
+ void mutateState() {
+ const long Q = MODULUS / MULTIPLIER;
+ const long R = MODULUS % MULTIPLIER;
+ long t;
+
+ t = MULTIPLIER * (state % Q) - R * (state / Q);
+ if (t > 0) {
+ state = t;
+ }
+ else {
+ state = t + MODULUS;
+ }
+ }
+}
View
@@ -13,6 +13,7 @@ public import core.definitions;
// import main classes
public import core.string ;
+public import core.random : Random;
public import core.window : Window;
public import core.stream : Stream;
public import core.stream : StreamData;
@@ -31,6 +32,7 @@ public import core.semaphore : Semaphore;
public import core.sound : Sound, SoundState;
public import core.audio : Audio;
public import core.time : Time;
+public import core.view : View;
public import core.color;
public import core.directory;
public import core.filesystem;
View
@@ -1 +1,6 @@
-print "Hello from Lua"
+print "Hello from Lua"
+print "Start"
+for i=1,10 do
+ print(i)
+end
+print "End"
View
@@ -24,7 +24,7 @@ DFILES_PLATFORM_UNIX = platform/unix/cairo/cairo.d platform/unix/scaffolds/threa
DFILES_PLATFORM_WIN = platform/win/main.d platform/win/common.d platform/win/scaffold.d platform/win/vars.d platform/win/console.d platform/win/definitions.d platform/win/scaffolds/wave.d platform/win/scaffolds/directory.d platform/win/scaffolds/graphics.d platform/win/scaffolds/thread.d platform/win/scaffolds/menu.d platform/win/scaffolds/window.d platform/win/scaffolds/view.d platform/win/scaffolds/color.d platform/win/scaffolds/file.d platform/win/scaffolds/socket.d platform/win/scaffolds/app.d platform/win/controls/oslistfield.d platform/win/controls/osprogressbar.d platform/win/controls/oslistbox.d platform/win/controls/oshscrollbar.d platform/win/controls/osvscrollbar.d platform/win/controls/osbutton.d platform/win/controls/ostextfield.d platform/win/controls/ostogglefield.d platform/win/controls/ostrackbar.d platform/win/scaffolds/time.d platform/win/oscontrolinterface.d platform/win/scaffolds/opengl.d
DFILES_PLATFORM_XOMB = platform/xomb/main.d platform/xomb/common.d platform/xomb/scaffold.d platform/xomb/vars.d platform/xomb/console.d platform/xomb/definitions.d platform/xomb/scaffolds/wave.d platform/xomb/scaffolds/graphics.d platform/xomb/scaffolds/thread.d platform/xomb/scaffolds/menu.d platform/xomb/scaffolds/window.d platform/xomb/scaffolds/view.d platform/xomb/scaffolds/color.d platform/xomb/scaffolds/file.d platform/xomb/scaffolds/socket.d platform/xomb/scaffolds/app.d platform/xomb/scaffolds/time.d platform/xomb/oscontrolinterface.d
-DFILES_CORE = core/regex.d core/debugger.d core/arguments.d core/filesystem.d core/directory.d core/definitions.d core/stringliteral.d core/format.d core/wavelet.d core/time.d core/audio.d core/mutex.d core/sound.d core/unicode.d core/semaphore.d core/thread.d core/graphics.d core/script.d core/resource.d core/menu.d core/timer.d core/socket.d core/endian.d core/image.d core/file.d core/stream.d core/string.d core/window.d core/main.d core/view.d core/control.d core/color.d core/basewindow.d core/windowedcontrol.d
+DFILES_CORE = core/random.d core/regex.d core/debugger.d core/arguments.d core/filesystem.d core/directory.d core/definitions.d core/stringliteral.d core/format.d core/wavelet.d core/time.d core/audio.d core/mutex.d core/sound.d core/unicode.d core/semaphore.d core/thread.d core/graphics.d core/script.d core/resource.d core/menu.d core/timer.d core/socket.d core/endian.d core/image.d core/file.d core/stream.d core/string.d core/window.d core/main.d core/view.d core/control.d core/color.d core/basewindow.d core/windowedcontrol.d
DFILES_CONTROLS = controls/container.d controls/trackbar.d controls/radiogroup.d controls/progressbar.d controls/togglefield.d controls/oscontrol.d controls/listfield.d controls/listbox.d controls/vscrollbar.d controls/hscrollbar.d controls/button.d controls/textfield.d
DFILES_UTILS = utils/stack.d utils/arraylist.d utils/linkedlist.d
DFILES_PARSERS = parsers/lexer.d parsers/cfg.d
No changes.
View
@@ -24,6 +24,8 @@ import console.main;
// A Helper class
class LuaScript {
+ alias lua_CFunction Callback;
+
this() {
initialize();
}
@@ -53,6 +55,24 @@ class LuaScript {
eval(new String(code));
}
+ // Description: This function will map a function of the type int(LuaBindings.lua_State) to be called whenever the function specified by the second parameter is called within a Lua script.
+ // func: The callback function to execute.
+ // functionName: The name of the function within the Lua script to map.
+ void registerFunction(Callback func, String functionName) {
+ char[] funcStr = functionName.toUtf8() ~ "\0";
+ //lua_pushcfunction(L, func);
+ //lua_setglobal(L, funcStr.ptr);
+ }
+
+ // Description: This function will map a function of the type int(LuaBindings.lua_State) to be called whenever the function specified by the second parameter is called within a Lua script.
+ // func: The callback function to execute.
+ // functionName: The name of the function within the Lua script to map.
+ void registerFunction(Callback func, StringLiteral functionName) {
+ registerFunction(func, new String(functionName));
+ }
+
+ // Description: This function will evaluate the Lua script located at the path provided.
+ // filename: A Lua script to evaluate.
void evalFile(String filename) {
char[] chrs = Unicode.toUtf8(filename.array) ~ "\0";
@@ -74,6 +94,8 @@ class LuaScript {
}
}
+ // Description: This function will evaluate the Lua script located at the path provided.
+ // filename: A Lua script to evaluate.
void evalFile(StringLiteral filename) {
evalFile(new String(filename));
}
Oops, something went wrong.

0 comments on commit 9767ebb

Please sign in to comment.