Re: raylib-d Gamepad Detection Fails

2022-11-30 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/30/22 8:49 PM, jwatson-CO-edu wrote:



Yes, following your instructions I have raylib 4.2.0 and raylib-d 4.2.4 
in the project directory.  I am now using the latest version of 
raylib-d, but this did not resolve the gamepad issue. I can ask around 
on the raylib channels.


Oh, I think I know the issue -- you need to check inside the loop. The 
way raylib works is that it polls events from the glfw library. So 
unless you are actually calling `BeginDrawing` and `EndDrawing`, those 
flags won't change.


-Steve


Re: raylib-d Gamepad Detection Fails

2022-11-30 Thread jwatson-CO-edu via Digitalmars-d-learn
On Thursday, 1 December 2022 at 01:12:17 UTC, Steven 
Schveighoffer wrote:

On 11/30/22 7:28 PM, jwatson-CO-edu wrote:

On Wednesday, 30 November 2022 at 23:18:33 UTC, ryuukk_ wrote:
On Wednesday, 30 November 2022 at 22:46:52 UTC, 
jwatson-CO-edu wrote:

Hello,

I have this small [gamepad input test 
program](https://github.com/jwatson-CO-edu/nanoverse/blob/main/d/raylib/04_jsInput/source/app.d).

Which OS and wich controllers are you using?

You might want to open an issue on the raylib-d repo: 
https://github.com/schveiguy/raylib-d/issues


I tested on Ubuntu 22.04 and Linux Mint 20 with an older 
GameSir brand wired XBox controller.  Built with Raylib 2.4.0 
and raylib-d 2.4.1.


I know that the author monitors the forum.  So, I will open an 
issue if he does not point out something obvious that I missed.





Do you have that version correct? I'm assuming you mean raylib 
4.2.0 and raylib-d 4.2.1?


Highly recommend to update to raylib-d 4.2.4, and use the the 
Linux raylib-d:install as that now works (and will properly 
grab the correct raylib library from the repository for you).






-Steve


Yes, following your instructions I have raylib 4.2.0 and raylib-d 
4.2.4 in the project directory.  I am now using the latest 
version of raylib-d, but this did not resolve the gamepad issue.  
I can ask around on the raylib channels.


Re: raylib-d Create Light Sources

2022-11-30 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/30/22 7:56 PM, jwatson-CO-edu wrote:


uint MAX_LIGHTS = 4;


This needs to be an `enum`.


//...
Light[MAX_LIGHTS] lights; // Error: undefined identifier `Light`


The rlights header file is not part of the raylib library, but is in the 
examples directory, you will need to port it. It's not very big, you 
probably can do it in a half hour.


https://github.com/raysan5/raylib/blob/387c06000618ef0aa3b15c5e46d1c525ba194c50/examples/shaders/rlights.h

-Steve


Re: raylib-d Gamepad Detection Fails

2022-11-30 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/30/22 7:28 PM, jwatson-CO-edu wrote:

On Wednesday, 30 November 2022 at 23:18:33 UTC, ryuukk_ wrote:

On Wednesday, 30 November 2022 at 22:46:52 UTC, jwatson-CO-edu wrote:

Hello,

I have this small [gamepad input test 
program](https://github.com/jwatson-CO-edu/nanoverse/blob/main/d/raylib/04_jsInput/source/app.d).

Which OS and wich controllers are you using?

You might want to open an issue on the raylib-d repo: 
https://github.com/schveiguy/raylib-d/issues


I tested on Ubuntu 22.04 and Linux Mint 20 with an older GameSir brand 
wired XBox controller.  Built with Raylib 2.4.0 and raylib-d 2.4.1.


I know that the author monitors the forum.  So, I will open an issue if 
he does not point out something obvious that I missed.





Do you have that version correct? I'm assuming you mean raylib 4.2.0 and 
raylib-d 4.2.1?


Highly recommend to update to raylib-d 4.2.4, and use the the Linux 
raylib-d:install as that now works (and will properly grab the correct 
raylib library from the repository for you).


For your specific problem, I would say that if it's not working, it's 
not a problem specifically with raylib-d, but with raylib. Such 
functions are just prototypes in raylib-d, the C library does the 
implementation.


This would probably be best actually to discuss either on the raylib 
discord (there's a raylib-d channel) or open an issue in my repository. 
Raylib does a lot of logging things, you can probably see if your 
gamepad is detected.


-Steve


Re: Syntax Sugar for Initializing a Fixed float Array as void*?

2022-11-30 Thread Ali Çehreli via Digitalmars-d-learn

On 11/30/22 16:48, Ali Çehreli wrote:


Functions are syntax sugar. :)


And I remembered std.array.staticArray. One its overloads should be useful:

import std;

void main() {
auto v3 = staticArray!(0.1f.repeat(5));
auto v4 = staticArray!5(0.1f.repeat);

writeln(v3);
writeln(v4);
}

Ali



Re: Syntax Sugar for Initializing a Fixed float Array as void*?

2022-11-30 Thread jwatson-CO-edu via Digitalmars-d-learn

On Thursday, 1 December 2022 at 00:47:18 UTC, Adam D Ruppe wrote:
On Thursday, 1 December 2022 at 00:39:21 UTC, jwatson-CO-edu 
wrote:
Is there a way to write a single statement that creates a void 
pointer that points to an initialized float array?



float[] f = [1,1,1];

some_function_taking_void(f.ptr);

and it just works.


Thank you, that was just the magic I needed!

```d
float[4] arr = [0.1f, 0.1f, 0.1f, 1.0f]; // Init array with values

SetShaderValue(
shader, ambientLoc, arr.ptr, 
ShaderUniformDataType.SHADER_UNIFORM_VEC4

); //   ^^^---void* here
```




raylib-d Create Light Sources

2022-11-30 Thread jwatson-CO-edu via Digitalmars-d-learn
I am attempting to recreate the [Raylib flat shading 
tutorial](https://www.raylib.com/examples/shaders/loader.html?name=shaders_basic_lighting) with raylib-d.  I have been able to find most of the necessary wrappers except the one the creates light sources. How do I create light sources in raylib-d?


```d
uint MAX_LIGHTS = 4;
//...
Light[MAX_LIGHTS] lights; // Error: undefined identifier `Light`
lights[0] = CreateLight(
LIGHT_POINT, Vector3( -2, 1, -2 ), Vector3Zero(), 
Colors.YELLOW, shader

);
lights[1] = CreateLight(
LIGHT_POINT, Vector3( 2, 1, 2 ), Vector3Zero(), Colors.RED, 
shader

);
lights[2] = CreateLight(
LIGHT_POINT, Vector3( -2, 1, 2 ), Vector3Zero(), 
Colors.GREEN, shader

);
lights[3] = CreateLight(
LIGHT_POINT, Vector3( 2, 1, -2 ), Vector3Zero(), Colors.BLUE, 
shader

);
```


Re: Syntax Sugar for Initializing a Fixed float Array as void*?

2022-11-30 Thread Ali Çehreli via Digitalmars-d-learn

On 11/30/22 16:39, jwatson-CO-edu wrote:
Is there a way to write a single statement that creates a void pointer 
that points to an initialized float array?  See below:

```d
float* arr = cast(float*) new float[4];
arr[0] = 0.1;
arr[1] = 0.1;
arr[2] = 0.1;
arr[3] = 0.1;
void* value = cast(void*) arr;
```


Functions are syntax sugar. :)

import std;

void* inittedArray(T)(T value, size_t count) {
auto arr = new T[count];
arr[] = value;
return arr.ptr;
}

void main() {
auto v = inittedArray(0.1f, 5);

// or something a little crazy:
auto v2 = 0.1f.repeat(5).array.ptr.to!(void*);
}

Ali



Re: Syntax Sugar for Initializing a Fixed float Array as void*?

2022-11-30 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 1 December 2022 at 00:39:21 UTC, jwatson-CO-edu 
wrote:
Is there a way to write a single statement that creates a void 
pointer that points to an initialized float array?


void* f = [1.0f, 1.0f, 1.0f].ptr;

Though I'd recommend keeping it typed as float[] until the last 
possible moment. If you are passing it a function, remmeber 
pointers convert to void* automatically, so you can do like:


float[] f = [1,1,1];

some_function_taking_void(f.ptr);

and it just works.


Syntax Sugar for Initializing a Fixed float Array as void*?

2022-11-30 Thread jwatson-CO-edu via Digitalmars-d-learn
Is there a way to write a single statement that creates a void 
pointer that points to an initialized float array?  See below:

```d
float* arr = cast(float*) new float[4];
arr[0] = 0.1;
arr[1] = 0.1;
arr[2] = 0.1;
arr[3] = 0.1;
void* value = cast(void*) arr;
```


Re: raylib-d Gamepad Detection Fails

2022-11-30 Thread jwatson-CO-edu via Digitalmars-d-learn

On Wednesday, 30 November 2022 at 23:18:33 UTC, ryuukk_ wrote:
On Wednesday, 30 November 2022 at 22:46:52 UTC, jwatson-CO-edu 
wrote:

Hello,

I have this small [gamepad input test 
program](https://github.com/jwatson-CO-edu/nanoverse/blob/main/d/raylib/04_jsInput/source/app.d).

Which OS and wich controllers are you using?

You might want to open an issue on the raylib-d repo: 
https://github.com/schveiguy/raylib-d/issues


I tested on Ubuntu 22.04 and Linux Mint 20 with an older GameSir 
brand wired XBox controller.  Built with Raylib 2.4.0 and 
raylib-d 2.4.1.


I know that the author monitors the forum.  So, I will open an 
issue if he does not point out something obvious that I missed.





Re: raylib-d Gamepad Detection Fails

2022-11-30 Thread ryuukk_ via Digitalmars-d-learn
On Wednesday, 30 November 2022 at 22:46:52 UTC, jwatson-CO-edu 
wrote:

Hello,

I have this small [gamepad input test 
program](https://github.com/jwatson-CO-edu/nanoverse/blob/main/d/raylib/04_jsInput/source/app.d).  In the program, I poll the first 3 gamepad IDs, but all detection attempts return `false`. I know that my gamepad is an Xinput model.  I have tried starting the program before and after the gamepad is plugged in, but there is no change.


Sample:
```d
bool GP0_found = IsGamepadAvailable(0); // false
bool GP1_found = IsGamepadAvailable(1); // false
bool GP2_found = IsGamepadAvailable(2); // false
```

Has anyone else experienced this and know the fix?


Which OS and wich controllers are you using?

You might want to open an issue on the raylib-d repo: 
https://github.com/schveiguy/raylib-d/issues


raylib-d Gamepad Detection Fails

2022-11-30 Thread jwatson-CO-edu via Digitalmars-d-learn

Hello,

I have this small [gamepad input test 
program](https://github.com/jwatson-CO-edu/nanoverse/blob/main/d/raylib/04_jsInput/source/app.d).  In the program, I poll the first 3 gamepad IDs, but all detection attempts return `false`. I know that my gamepad is an Xinput model.  I have tried starting the program before and after the gamepad is plugged in, but there is no change.


Sample:
```d
bool GP0_found = IsGamepadAvailable(0); // false
bool GP1_found = IsGamepadAvailable(1); // false
bool GP2_found = IsGamepadAvailable(2); // false
```

Has anyone else experienced this and know the fix?


Re: Is there a formula for overflow?

2022-11-30 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Nov 30, 2022 at 03:07:44AM +, thebluepandabear via 
Digitalmars-d-learn wrote:
> I am reading through Ali's book about D, and he gives the following
> examples for an overflow:
[...]
> The result overflows and is 1705032704.
> 
> Also for the second example, it overflows and comes up with the value
> of 4294967286:
[...]
> Also a fun one, the following produces 4294967295:
[...]
> But the book doesn't talk about why the D compiler came up with these
> results (and it also goes for division/multiplication) for the
> overflow (or maybe it did further down ?), as in it didn't talk about
> the formula it used for calculating this value.
> 
> I am curious as to what formula the D compiler uses for calculating
> 'overflowed' values, if such thing exists? :)
[...]

First, you have to understand that it's not the D compiler that imposes
some arbitrary maximum after which an integer value will overflow. To
overflow rules are more-or-less a description of how the hardware
behaves under the hood, rather than something the compiler deliberately
imposes.

The value 4294967296 is actually 2^32. Why 2^32? Because that's the
number of distinct values a 32-bit machine register can hold. In fact,
the maximum value that a 32-bit register can hold is (2^32 - 1), i.e.,
4294967295, because 0 is one of the values represented.  Now, (2^32 - 1)
is the maximum for uint, but for int, one bit is reserved for indicating
the sign of the value, so the actual maximum is (2^31 - 1), i.e.,
2147483647, which, incidentally, is the value of int.max.

As for the actual overflow behaviour, it's a simple consequence of the
2's complement representation of integers. I.e., -1 is represented not
as:
0b1000________0001

but rather as:

0b________ (i.e. 0x_)

The most negative value that can be represented in 32-bit 2's complement
is:

0b1000________ (0x8000_)

which is -2^31 == -2147483648, which, incidentally, is int.min.

Why 2's complement?  Well, because that's what the machine implements...
but why did the engineers choose to implement it that way?  Because 2's
complement representation has the interesting property that addition and
subtraction can be done exactly as if the values were unsigned, and
you'd get the correct results back when you reinterpret them as 2's
complement.  I.e., if you add 1 to 0x_, interpreted as an
unsigned integer, you get 0x_ (there's a leading 1 on the far
left but it's in the 33rd bit, which doesn't fit in the machine
register, so it gets discarded).  If you reinterpret 0x_ in 2's
complement as -1, then you have the nice result that (-1) + 1 == 0 (the
+ here is unsigned addition).

One consequence of this is that negation is implemented as:

-x == ~(x-1)

As a consequence of the 2's-complement representation of integers, and
the way arithmetic operations are implemented, the overflow rules that
you read about just fall out of the rules as natural consequences:

0x7FFF_ + 1 == 0x8000_

i.e., interpreted as 2's complement:

2147483647 + 1 == -2147483648

Negating an unsigned number is equivalent to doing ~(x-1), so:

cast(uint) -1 == 0x_ == 2^32 - 1 == 4294967295

Adding 3_000_000_000 to 3_000_000_000 (in hexadecimal, that's
0xB2D05E00) gives you:

0xB2D05E00 + 0xB2D05E00 == 1_65A0BC00

But that leading 1 is in the 33rd bit, which doesn't fit into a 32-bit
register (i.e., it overflows). If you discard it, you get 0x65A0BC00,
which in decimal is 1705032704.

So you see, none of this is D's own idiosyncratic rules; it's merely a
reflection of how the machine implements 32-bit integer values.
(Analogous results can be said for 64-bit values.)


T

-- 
If you want to solve a problem, you need to address its root cause, not just 
its symptoms. Otherwise it's like treating cancer with Tylenol...


Re: Thinking about the difference between fixed and 'dynamic' arrays.

2022-11-30 Thread Ali Çehreli via Digitalmars-d-learn

On 11/29/22 15:25, DLearner wrote:

> 'dynamic array' is
> not a reasonable description for a construct that behaves like
> VarArr2[3] becoming 40.

I agree with you: It has always bothered me to call the following a 
dynamic array:


  int[] arr;

'arr' is not a dynamic array but the slice interface. Dynamic arrays are 
owned by the D runtime and are always nameless.


Ali



Re: Is there a formula for overflow?

2022-11-30 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 30 November 2022 at 11:40:36 UTC, thebluepandabear 
wrote:

then the narrower value is converted to the wider type
**C.** If the signed type is wider than the unsigned type, 
then the unsigned value is converted to the signed type
**D.** Otherwise the signed type is converted to the unsigned 
type


SDB@79


I didn't  ask about casting...


You cannot fully grasp the topics without fully knowing the above 
items.


SDB@79


Re: Is there a formula for overflow?

2022-11-30 Thread Ali Çehreli via Digitalmars-d-learn

On 11/29/22 19:07, thebluepandabear wrote:

> But the book doesn't talk about why the D compiler came up with these
> results

The compiler doesn't do anything special. It's all about the lack of 
bits to store the value. If the result needs 33 bits but the type has 
only 32 bits, the contribution of the highest bit is simply lost.


Ali



Re: Thinking about the difference between fixed and 'dynamic' arrays.

2022-11-30 Thread Basile.B via Digitalmars-d-learn

On Wednesday, 30 November 2022 at 03:04:47 UTC, Basile B. wrote:
Essentially slices are only useful to be consumed locally, 
typically


```d
while mySlice.length do
{
   slice = slice[1..$];
}
```


sorry I cant force push, it was obviously meant to be written as

```d
while mySlice.length do
{
   mySlice = mySlice[1..$];
}
```




Re: Is there a formula for overflow?

2022-11-30 Thread thebluepandabear via Digitalmars-d-learn

then the narrower value is converted to the wider type
**C.** If the signed type is wider than the unsigned type, then 
the unsigned value is converted to the signed type
**D.** Otherwise the signed type is converted to the unsigned 
type


SDB@79


I didn't  ask about casting...