summaryrefslogtreecommitdiff
path: root/thirdparty/misc/open-simplex-noise-no-allocate.patch
blob: fc3abe7d007d1701bc9e58618053100884cdab97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
diff -u orig/open-simplex-noise.c misc/open-simplex-noise.c
--- orig/open-simplex-noise.c	2018-09-14 11:11:40.049810000 +0200
+++ misc/open-simplex-noise.c	2018-09-14 11:09:39.726457000 +0200
@@ -13,6 +13,11 @@
  *   of any particular randomization library, so results
  *   will be the same when ported to other languages.
  */
+
+// -- GODOT start --
+// Modified to work without allocating memory, also removed some unused function. 
+// -- GODOT end --
+
 #include <math.h>
 #include <stdlib.h>
 #include <stdint.h>
@@ -34,11 +39,12 @@
 	
 #define DEFAULT_SEED (0LL)
 
-struct osn_context {
+// -- GODOT start --
+/*struct osn_context {
 	int16_t *perm;
 	int16_t *permGradIndex3D;
-};
-
+};*/
+// -- GODOT end --
 #define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0]))
 
 /* 
@@ -126,7 +132,9 @@
 	int xi = (int) x;
 	return x < xi ? xi - 1 : xi;
 }
-	
+
+// -- GODOT start --
+/*
 static int allocate_perm(struct osn_context *ctx, int nperm, int ngrad)
 {
 	if (ctx->perm)
@@ -154,18 +162,21 @@
 	memcpy(ctx->perm, p, sizeof(*ctx->perm) * nelements);
 		
 	for (i = 0; i < 256; i++) {
-		/* Since 3D has 24 gradients, simple bitmask won't work, so precompute modulo array. */
+		// Since 3D has 24 gradients, simple bitmask won't work, so precompute modulo array.
 		ctx->permGradIndex3D[i] = (int16_t)((ctx->perm[i] % (ARRAYSIZE(gradients3D) / 3)) * 3);
 	}
 	return 0;
 }
+*/
+// -- GODOT end --
 
 /*	
  * Initializes using a permutation array generated from a 64-bit seed.
  * Generates a proper permutation (i.e. doesn't merely perform N successive pair
  * swaps on a base array).  Uses a simple 64-bit LCG.
  */
-int open_simplex_noise(int64_t seed, struct osn_context **ctx)
+// -- GODOT start --
+int open_simplex_noise(int64_t seed, struct osn_context *ctx)
 {
 	int rc;
 	int16_t source[256];
@@ -174,20 +185,9 @@
 	int16_t *permGradIndex3D;
 	int r;
 
-	*ctx = (struct osn_context *) malloc(sizeof(**ctx));
-	if (!(*ctx))
-		return -ENOMEM;
-	(*ctx)->perm = NULL;
-	(*ctx)->permGradIndex3D = NULL;
-
-	rc = allocate_perm(*ctx, 256, 256);
-	if (rc) {
-		free(*ctx);
-		return rc;
-	}
-
-	perm = (*ctx)->perm;
-	permGradIndex3D = (*ctx)->permGradIndex3D;
+	perm = ctx->perm;
+	permGradIndex3D = ctx->permGradIndex3D;
+// -- GODOT end --
 
 	for (i = 0; i < 256; i++)
 		source[i] = (int16_t) i;
@@ -206,6 +206,8 @@
 	return 0;
 }
 
+// -- GODOT start --
+/*
 void open_simplex_noise_free(struct osn_context *ctx)
 {
 	if (!ctx)
@@ -220,6 +222,8 @@
 	}
 	free(ctx);
 }
+*/
+// -- GODOT end --
 	
 /* 2D OpenSimplex (Simplectic) Noise. */
 double open_simplex_noise2(struct osn_context *ctx, double x, double y) 
diff -u orig/open-simplex-noise.h misc/open-simplex-noise.h
--- orig/open-simplex-noise.h	2018-09-14 11:11:19.659807000 +0200
+++ misc/open-simplex-noise.h	2018-09-14 11:10:05.006460000 +0200
@@ -35,11 +35,18 @@
 	extern "C" {
 #endif
 
-struct osn_context;
+// -- GODOT start --
+// Modified to work without allocating memory, also removed some unused function. 
 
-int open_simplex_noise(int64_t seed, struct osn_context **ctx);
+struct osn_context {
+	int16_t perm[256];
+	int16_t permGradIndex3D[256];
+};
+
+int open_simplex_noise(int64_t seed, struct osn_context *ctx);
+//int open_simplex_noise_init_perm(struct osn_context *ctx, int16_t p[], int nelements);
+// -- GODOT end --
 void open_simplex_noise_free(struct osn_context *ctx);
-int open_simplex_noise_init_perm(struct osn_context *ctx, int16_t p[], int nelements);
 double open_simplex_noise2(struct osn_context *ctx, double x, double y);
 double open_simplex_noise3(struct osn_context *ctx, double x, double y, double z);
 double open_simplex_noise4(struct osn_context *ctx, double x, double y, double z, double w);