Re: Need some technical help an object oriented wrapper I am creating for bindbc.sfml

2023-01-23 Thread Christian Köstlin via Digitalmars-d-learn

On 24.01.23 04:59, thebluepandabear wrote:

Regards,
thebluepandabear


Btw I understand this question is extremely complex, don't want to 
pressure anyone to help me because of that... but any sort of assistance 
or leads would be greatly... greatly apprecaited...
I do not know anything about sfml, but could you try a simpler shape, 
e.g. circle (that is a primitive on the native side). perhaps the 
winding order of your vertices is wrong?


Kind regards,
Christian



Re: Need some technical help an object oriented wrapper I am creating for bindbc.sfml

2023-01-23 Thread thebluepandabear via Digitalmars-d-learn

Regards,
thebluepandabear


Btw I understand this question is extremely complex, don't want 
to pressure anyone to help me because of that... but any sort of 
assistance or leads would be greatly... greatly apprecaited...


Need some technical help an object oriented wrapper I am creating for bindbc.sfml

2023-01-23 Thread thebluepandabear via Digitalmars-d-learn

Hello everyone , hope everyone is having a good day.

Hopefully I am allowed to ask technical questions here, if not 
please tell me and I will remove this.


I am trying to create object oriented wrappers around 
`bindbc.sfml`, this is because I don't like the C-style syntax of 
CSFML.


The C-style syntax is not right -- in my opinion -- for an object 
oriented language. Dealing with pointers all the time is also 
unsafe.


This is not to say that CSFML isn't good -- it's great, and I've 
made some apps using `bindbc-sfml`. I just want to extend it to 
my liking with object oriented wrappers that can more closely 
match the C++ SFML syntax.


For the wrappers, I created a `Shape` class. This `Shape` class 
is seen in the original C++ SFML implementation:


```D
class Shape : Transformable, Drawable {
void setTexture(sfTexture* texture, bool resetRect) {
ptr.sfShape_setTexture(texture, resetRect);
}

void setTextureRect(IntRect rect) {
ptr.sfShape_setTextureRect(rect.to_sfIntRect());
}

void setFillColor(Color color) {
ptr.sfShape_setFillColor(color.to_sfColor());
}

void setOutlineColor(Color color) {
ptr.sfShape_setOutlineColor(color.to_sfColor());
}

void setOutlineThickness(float thickness) {
ptr.sfShape_setOutlineThickness(thickness);
}

const(sfTexture)* getTexture() {
return ptr.sfShape_getTexture();
}

IntRect getTextureRect() {
return ptr.sfShape_getTextureRect().toIntRect();
}

Color getFillColor() {
return ptr.sfShape_getFillColor().toColor();
}

Color getOutlineColor() {
return ptr.sfShape_getOutlineColor().toColor();
}

float getOutlineThickness() {
return ptr.sfShape_getOutlineThickness();
}

size_t getPointCount() nothrow {
return ptr.sfShape_getPointCount();
}

Vector2f getPoint(size_t index) nothrow {
return ptr.sfShape_getPoint(index).toVector2f_noThrow();
}

FloatRect getLocalBounds() {
return ptr.sfShape_getLocalBounds().toFloatRect();
}

FloatRect getGlobalBounds() {
return ptr.sfShape_getGlobalBounds().toFloatRect();
}

private sfShape* ptr;
}
```

The `sfShape` pointer isn't currently initialized, I'll get to 
that issue soon.


As you can see, `Shape` extends the `Transformable` class and the 
`Drawable` interface. This again roughly matches what's seen in 
SFML. SFML.NET also did a similar wrapper for their CSFML C# 
bindings. What's great about SFML.NET is that you don't even know 
that you're using CSFML, this is because it feels just like C++ 
SFML.


Now, I will create a `RectangleShape` which will be a subclass of 
the `Shape` class:


(Btw I took a lot of inspiration from SFML.NET when it comes to 
these wrappers.)


```D
class RectangleShape : Shape {
this(Vector2f size) {
_size = size;
setSize(_size);
}

Vector2f getSize() {
return _size;
}

void setSize(Vector2f size) {
_size = size;
}

override {
size_t getPointCount() {
return 4;
}

Vector2f getPoint(size_t index) {
final switch (index) {
case 0:
return Vector2f(0, 0);
case 1:
return Vector2f(_size.x, 0);
case 2:
return Vector2f(_size.x, _size.y);
case 3:
return Vector2f(0, _size.y);
}
}
}

private Vector2f _size;
}
```

As you can see, the `Rectangle` class only overrides the 
`getPointCount` and `getPoint` methods.


**These are the methods that the superclass - `Shape` - will use 
to construct the shape object for it to actually be drawable.**


Now, let us add the following code to the `Shape` class so that 
we can construct a `Shape` via these two methods, which we assume 
that the child provides us a good implementation for:


```D
class Shape : Transformable, Drawable {
this() {
ptr = sfShape_create(, , 
cast(void*)this);

}

extern(C) private static ulong getPointCount(void* data) 
nothrow {

return (cast(Shape)data).getPointCount();
}

extern(C) private static sfVector2f getPoint(size_t index, 
void* data) nothrow {
return 
(cast(Shape)data).getPoint(index).to_sfVector2f_noThrow();

}
```

I hear you asking, what's going on here?

We are providing two callbacks to the `getPointCount` and 
`getPoint` methods via function pointers, and we're passing in 
the current object to the `data` `void*` pointer. It's kind of 
hard to understand, but if you read through it carefully you 
should get a rough idea of what's going on.


Now, when we create a new instance of `Rectangle`, I will assume 
that the constructor will be called, the `sf_shape` ptr will be 
initialized correctly (as it will be utilizing the crucial 
`getPoint` and `getPointCount` methods) and everything 

Re: Where I download Digital Mars C Preprocessor sppn.exe?

2023-01-23 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Jan 23, 2023 at 08:06:28PM +, Alain De Vos via Digitalmars-d-learn 
wrote:
> Mixing D with C or C++ or Python is looking for problems.
> Better write something in D.
> And write something in C/C++/Python.
> And have some form of communication between both.

I don't know about Python, but I regularly write D code that interacts
with external C libraries and have not encountered any major problems.
You just have to put `extern(C)` in the right places and make sure you
link the right objects / libraries, and you're good to go.

So far I haven't actually tried integrating non-trivial C++ libraries
with D yet, but I expect it will be similar unless you're dealing with
C++ templates (which are not compatible with D templates) or multiple
inheritance, which D doesn't support.


T

-- 
Right now I'm having amnesia and deja vu at the same time. I think I've 
forgotten this before.


Re: Where I download Digital Mars C Preprocessor sppn.exe?

2023-01-23 Thread Alain De Vos via Digitalmars-d-learn

Mixing D with C or C++ or Python is looking for problems.
Better write something in D.
And write something in C/C++/Python.
And have some form of communication between both.


Re: Where I download Digital Mars C Preprocessor sppn.exe?

2023-01-23 Thread Nick Treleaven via Digitalmars-d-learn

On Monday, 23 January 2023 at 17:15:30 UTC, Nick Treleaven wrote:

On Saturday, 2 April 2022 at 21:57:02 UTC, Marcone wrote:
Where I download Digital Mars C Preprocessor sppn.exe? I need 
it to use ImportC


Found this thread by googling `dlang sppn.exe`. For the record, 
it can be obtained from sppn.zip here:

http://ftp.digitalmars.com/

I didn't have it for some reason even though latest dmc is 
installed.


I then got some weird errors building druntime:

std::array not supported by DMC
std::basic_string_view not supported by DMC

I figured that dmc was not correctly installed (not sure why). So 
I downloaded dmc.zip which fixed it.


Re: Where I download Digital Mars C Preprocessor sppn.exe?

2023-01-23 Thread Nick Treleaven via Digitalmars-d-learn

On Saturday, 2 April 2022 at 21:57:02 UTC, Marcone wrote:
Where I download Digital Mars C Preprocessor sppn.exe? I need 
it to use ImportC


Found this thread by googling `dlang sppn.exe`. For the record, 
it can be obtained from sppn.zip here:

http://ftp.digitalmars.com/

I didn't have it for some reason even though latest dmc is 
installed.


Re: How to use @safe when a C library integration needed

2023-01-23 Thread Dom DiSc via Digitalmars-d-learn

On Monday, 23 January 2023 at 16:36:21 UTC, Leonardo wrote:

Hello. How to use @safe when a C library integration needed?

Everything need a system function...


```d
@safe fn()
{
   // lot of safe stuff

   () @trusted {
   // in this block[*] @system function like extern C can be 
called.

   // you need to make sure the API is used correct
   @assert(/*C_Fun is safe to be used with param1*/);
   @assert(/*C_Fun is safe to be used with param2*/);
   C_Fun(param1, param2);
   }();

   // more safe stuff

}
```

[*] in fact, this is a lambda function that is directly called, 
because real trusted blocks are not allowed (yet).


How to use @safe when a C library integration needed

2023-01-23 Thread Leonardo via Digitalmars-d-learn

Hello. How to use @safe when a C library integration needed?

Everything need a system function...