Re: Best way to learn 2d games with D?

2020-03-15 Thread Mike Parker via Digitalmars-d-learn

On Monday, 16 March 2020 at 05:45:52 UTC, bauss wrote:



Please don't recommend Derelict to anyone :-) bindbc-sdl is 
what folks should be using now. I'm not maintaining Derelict 
anymore.


Haven't even heard of that!

Does it work in similar fashion?


Yes. The loader is @nog and betterC compatible, and I've taken a 
different approach to handling multiple library versions. Other 
than that, basically the same.


https://github.com/BindBC/bindbc-sdl


Re: Best way to learn 2d games with D?

2020-03-15 Thread bauss via Digitalmars-d-learn

On Monday, 16 March 2020 at 02:52:56 UTC, Mike Parker wrote:

On Sunday, 15 March 2020 at 18:14:44 UTC, bauss wrote:



I would recommend using Derelict and SDL with D since it's the 
most mature.




Please don't recommend Derelict to anyone :-) bindbc-sdl is 
what folks should be using now. I'm not maintaining Derelict 
anymore.


Haven't even heard of that!

Does it work in similar fashion?


Re: Best way to learn 2d games with D?

2020-03-15 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 15 March 2020 at 21:33:29 UTC, Arine wrote:



I wouldn't use SDL2 for rendering. It is really just there for 
legacy. The only thing people use SDL2 is for setting up a 
window and creating a render context for OpenGL/Vulkan/Directx, 
along with handling input/events.




There's no need for someone just learning 2D games to implement 
their own OGL/Vulkan/DX renderer. The SDL2 rendering API is 
plenty fine.


And it isn't just for "legacy". It was supposed to be an 
enhancement over SDL's old SDL_Surface API. When they first 
implemented it, they restricted it to the features they 
implemented cross-platform in software. It also didn't do any 
batching for the hardware rendering. As of SDL 2.10, they have 
batching for the hardware renderers, which makes it even more 
usable.


The only time I would recommend against the SDL2 renderer for a 
2D game is when you A) already have your own renderer implemented 
anyway, or B) need some fancy shader effect. I've seen people get 
some decent F/X with the SDL2 renderer, but it's limited in that 
regard. Still no need to implement a custom renderer, though, as 
SDL_gpu opens the doors to shaders.


Re: Best way to learn 2d games with D?

2020-03-15 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 15 March 2020 at 18:14:44 UTC, bauss wrote:



I would recommend using Derelict and SDL with D since it's the 
most mature.




Please don't recommend Derelict to anyone :-) bindbc-sdl is what 
folks should be using now. I'm not maintaining Derelict anymore.




Re: Best way to learn 2d games with D?

2020-03-15 Thread dwdv via Digitalmars-d-learn

On 2020-03-15 18:58, Steven Schveighoffer via Digitalmars-d-learn wrote:

I'd prefer to do it with D.


How about raylib in conjunction with thin d bindings? I prefer it over sdl, 
sfml and the like.

https://www.raylib.com/examples.html (make sure to enable js for embedded 
examples)
https://code.dlang.org/packages/raylib-d
https://github.com/raysan5/raylib/blob/master/BINDINGS.md

Should I just start with another language and then migrate to D later? 


If you can stomach lua(jit), check out love2d. Docs and provided game tutorials are super easy to 
follow:


https://love2d.org/
https://love2d.org/wiki/Category:Tutorials

Lastly, if you're more interested in the gamedev aspect, there's also the option to pick your poison 
from this curated mixed list of slim and fat 2d game engines:


https://thomasgervraud.com/best-2d-game-engine/


Re: Best way to learn 2d games with D?

2020-03-15 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Sunday, 15 March 2020 at 17:58:58 UTC, Steven Schveighoffer 
wrote:
I want to try and learn how to write 2d games. I'd prefer to do 
it with D.


No resources but I remember the serpent framework mentioned on 
this forum here recently. It looks pretty decent.


https://github.com/lispysnake/serpent



Re: What is the wrong with my C++ interfacing

2020-03-15 Thread Arine via Digitalmars-d-learn

On Sunday, 15 March 2020 at 21:27:32 UTC, Ferhat Kurtulmuş wrote:

On Sunday, 15 March 2020 at 21:16:43 UTC, Arine wrote:

On Sunday, 15 March 2020 at 20:53:49 UTC, Ferhat Kurtulmuş


I wouldn't use a class on the D side, unless your C++ type 
uses virtual functions. Classes in D are different from 
structs, it is not the same as C++ where they are basically 
the same thing.


I know their differences, classes are reference types and 
structs are value types in D.


That's not the only difference. It's why your code doesn't work.

I actually don't need this code. I've already wrapped many c 
code, but have no so much experience with interfacing C++ code. 
I am trying to understand capabilities of D in interfacing with 
C++ and its limits. I would like to know why I can interface a 
template-free class, but not a class template.


You can, you just aren't representing the C++ code properly in D. 
You don't want class, it means something different in D than in 
C++.


This works for me, as it is using struct that corresponds to the 
C++ type. You have to use extern(C++, class), as it is using 
"class" on the C++ side, which produces a different mangling.


extern(C++, cv){
extern(C++, class) struct Size_(_Tp){
@disable this();
~this() { }

final _Tp area() const;
final double aspectRatio() const;
final bool empty() const;

_Tp width; //!< the width
_Tp height; //!< the height
}
}

extern(C++){
cv.Size_!int* createSizeIntWH(int w, int h);
}

void main()
{
Size_!int* sz = createSizeIntWH(200, 100);
writeln(sz.width);
}



Re: Get symbols (and/or UDAs) of subclass from superclass

2020-03-15 Thread Basile B. via Digitalmars-d-learn

On Sunday, 15 March 2020 at 20:18:03 UTC, James Blachly wrote:
I would like to programmatically retrieve members of a subclass 
to create a self-documenting interface. I am afraid that my 
approach is not possible due to need for compile time __traits 
/ std.traits, and runtime typeinfo. My proposed approach is as 
follows:


class Base
{
string whatever;

string toString()
{
// loop over __traits(allMembers, typeof(this)) or 
getSymbolsByUDA, etc.

}
}

/// There may be a dozen Derived classes
class Derived1 : Base
{
@Config("a name", "a description")
float probability;
}
class Derived2 : Base
{
@Config("another", "more description")
int replicates;
}
...

Unfortunately, I am afraid this doesn't look possible because 
of the need for compile-time UDAs and runtime TypeInfo. Is 
there a way to do this without re-implementing toString in 
every single derived class? I expect there to be many derived 
classes.


Other ideas welcomed, as I usually write C-style D and am only 
recently taking a stab at OO Inheritance features.


Thanks in advance.
James


A few years ago I've been faced to a similar problem and I ended 
up by using an array, defined in the base class which was filled 
by "hybdrid" runtime/compile-time reflection (explanations about 
this come later).


In each derived class the constructor performed reflection, using 
__traits(derivedMembers) + UDA filtering and the array from the 
base class received the new stuff...


More concrectly:

---
class Base
{
Stuff[] stuff;
}

/// There may be a dozen Derived classes
class Derived1 : Base
{
@Config("a name", "a description")
float probability;

this()
{
foreach(dm; __traits(derivedMembers, this))
{ /* UDA filtering, then add to stuff*/ }
}

}
class Derived2 : Base
{
@Config("another", "more description")
int replicates;

this()
{
foreach(dm; __traits(derivedMembers, this))
{ /* UDA filtering, then add to stuff*/ }
}
}
---

This worked and would work for you but you'll be faced to several 
issues, for example the most obvious is what is the type of 
Stuff...


Another, less immediate, is that because of overridden methods 
some already existing entries in the array of Stuff may have to 
be replaced.


Oterwise the thing to get here is that altough you use 
compile-time reflection, the reflection code is only executed at 
runtime, to fill the array of Stuff.


Re: Best way to learn 2d games with D?

2020-03-15 Thread Arine via Digitalmars-d-learn
On Sunday, 15 March 2020 at 20:19:17 UTC, Steven Schveighoffer 
wrote:

On 3/15/20 4:12 PM, Jordan Wilson wrote:
On Sunday, 15 March 2020 at 17:58:58 UTC, Steven Schveighoffer 
wrote:
I want to try and learn how to write 2d games. I'd prefer to 
do it with D.


I've found a ton of tutorials on learning 2d gaming with 
other languages. Is there a place to look that uses D for 
learning? Should I just start with another language and then 
migrate to D later? Anyone recommend any specific 
tutorial/book?




I'm on a similar journey myself, I'll list my findings, maybe 
it could be useful for you.


I decided on a game programming library. I mostly looked at 
SFML and Allegro, and found both to have good bindings 
available in D, and good documentation, and got minimal 
examples working with both. I went with SFML, simply because 
there was a book written specifically about writing a game in 
SFML. I didn't see any such books for Allegro (although there 
are plenty of tutorials/articles).


I learnt about the "game loop". Bauss touched on it in his 
post, and I'm sure there are a lot of tutorials on it. I 
specifically learnt about it from the first few chapters of 
the SFML Game Development book.


I learnt about game design. In doing so, I came across 
Entity-Component-System design pattern. I decided to use this 
pattern, for no other reason than to try something other than 
OOP.

I found these links useful:
https://medium.com/ingeniouslysimple/entities-components-and-systems-89c31464240d

https://gameprogrammingpatterns.com/component.html
https://www.richardlord.net/blog/ecs/what-is-an-entity-framework.html

I started writing a game, using the derelict-sfml2 as my game 
library (again, I found the allegro library to be good too), 
and entitysysd to provide the ECS framework (there are a few 
ECS written in D available). Are they the best choices? Is 
SFML technically limited? Will I cope with ECS beyound the toy 
game example? No idea. But I'm having a lot of fun, which I 
think for a hobby project, is a fairly good measure of success 
;-)




OK, I will take a look there. I am running through some basic 
SDL game video tutorials right now.


Thanks everyone for the pointers.

-Steve


I wouldn't use SDL2 for rendering. It is really just there for 
legacy. The only thing people use SDL2 is for setting up a window 
and creating a render context for OpenGL/Vulkan/Directx, along 
with handling input/events.


Do you want to create games, or do you want to create game 
engines? If you want to make games, then you are better off using 
something like Unity or UE4. If you want to learn how game 
engines work, then you are better off going the 
OpenGL/Vulkan/Directx route. But it'll be a lot of work and you 
basically have to create everything from the ground up yourself. 
If you want to try and lighten your load, it can be difficult to 
find separate libraries as everything is quite interconnected, 
and if they weren't built to communicate with one another you are 
just going to have a difficult time getting everything to work.




Re: What is the wrong with my C++ interfacing

2020-03-15 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Sunday, 15 March 2020 at 21:16:43 UTC, Arine wrote:

On Sunday, 15 March 2020 at 20:53:49 UTC, Ferhat Kurtulmuş


I wouldn't use a class on the D side, unless your C++ type uses 
virtual functions. Classes in D are different from structs, it 
is not the same as C++ where they are basically the same thing.


I know their differences, classes are reference types and structs 
are value types in D. I actually don't need this code. I've 
already wrapped many c code, but have no so much experience with 
interfacing C++ code. I am trying to understand capabilities of D 
in interfacing with C++ and its limits. I would like to know why 
I can interface a template-free class, but not a class template.


Re: Get symbols (and/or UDAs) of subclass from superclass

2020-03-15 Thread Simen Kjærås via Digitalmars-d-learn

On Sunday, 15 March 2020 at 20:18:03 UTC, James Blachly wrote:
I would like to programmatically retrieve members of a subclass 
to create a self-documenting interface. I am afraid that my 
approach is not possible due to need for compile time __traits 
/ std.traits, and runtime typeinfo. My proposed approach is as 
follows:


class Base
{
string whatever;

string toString()
{
// loop over __traits(allMembers, typeof(this)) or 
getSymbolsByUDA, etc.

}
}

/// There may be a dozen Derived classes
class Derived1 : Base
{
@Config("a name", "a description")
float probability;
}
class Derived2 : Base
{
@Config("another", "more description")
int replicates;
}
...


There's no built-in way to do this - toString() doesn't know what 
derived classes exist, and more could be added from other modules 
and even dynamically loaded libraries. Now, there are ways to do 
something somewhat like it. I would suggest creating an abstract 
method in the base class, that derived class would have to 
override, and use a template this argument to get the correct 
type for __traits(allMembers) to operate on:


class Base {
override string toString() {
return toStringImpl();
}
abstract string toStringImpl();
string toStringBase(this That)() {
// foreach (e; __traits(allMembers, That) {...}
}
}

class Derived : Base {
override string toStringImpl() {
return this.toStringBase();
}
}

Since toStringImpl will always call toStringBase, this could 
perhaps better be modeled with a template mixin:


mixin template DerivedToString() {
override string toStringImpl() {
return this.toStringBase();
}
}

class Derived2 : Base {
mixin DerivedToString!();
}

This way, you can have all the logic of toString in the base 
class, and the only thing a subclass will have to include is one 
line for the mixin. In addition, since toStringImpl is abstract, 
the implementer of a subclass will get a compile-time error if he 
or she forgets to do either the mixin or override toStringImpl 
themselves.


--
  Simen


Re: What is the wrong with my C++ interfacing

2020-03-15 Thread Arine via Digitalmars-d-learn

On Sunday, 15 March 2020 at 20:53:49 UTC, Ferhat Kurtulmuş wrote:

On Sunday, 15 March 2020 at 20:46:14 UTC, drug wrote:

15.03.2020 23:25, Ferhat Kurtulmuş пишет:

On Sunday, 15 March 2020 at 20:21:57 UTC, drug wrote:

15.03.2020 22:39, Ferhat Kurtulmuş пишет:




What is the D version of `createSizeIntWH`? In C++ it 
returns a pointer but in D version it returns an instance.


extern(C++){
     cv.Size_!int createSizeInt();
     cv.Size_!int createSizeIntWH(int w, int h);
     void deleteSizeInt(ref cv.Size_!int sz);
}



createSizeIntWH returns a pointer to instance, not an 
instance. Try:

cv.Size_!int* createSizeIntWH(int w, int h);


I doubt it because in 
https://dlang.org/spec/cpp_interface.html#using_cpp_classes_from_d


cpp code:
Derived *createInstance(int i)

d code:
extern (C++){
...
Derived createInstance(int i);
}


I wouldn't use a class on the D side, unless your C++ type uses 
virtual functions. Classes in D are different from structs, it is 
not the same as C++ where they are basically the same thing.


Re: What is the wrong with my C++ interfacing

2020-03-15 Thread drug via Digitalmars-d-learn

15.03.2020 23:53, Ferhat Kurtulmuş пишет:


I doubt it because in 
https://dlang.org/spec/cpp_interface.html#using_cpp_classes_from_d


cpp code:
Derived *createInstance(int i)

d code:
extern (C++){
     ...
     Derived createInstance(int i);
}



Ah, really, you use classes on D side.




Re: What is the wrong with my C++ interfacing

2020-03-15 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Sunday, 15 March 2020 at 20:46:14 UTC, drug wrote:

15.03.2020 23:25, Ferhat Kurtulmuş пишет:

On Sunday, 15 March 2020 at 20:21:57 UTC, drug wrote:

15.03.2020 22:39, Ferhat Kurtulmuş пишет:




What is the D version of `createSizeIntWH`? In C++ it returns 
a pointer but in D version it returns an instance.


extern(C++){
     cv.Size_!int createSizeInt();
     cv.Size_!int createSizeIntWH(int w, int h);
     void deleteSizeInt(ref cv.Size_!int sz);
}



createSizeIntWH returns a pointer to instance, not an instance. 
Try:

cv.Size_!int* createSizeIntWH(int w, int h);


I doubt it because in 
https://dlang.org/spec/cpp_interface.html#using_cpp_classes_from_d


cpp code:
Derived *createInstance(int i)

d code:
extern (C++){
...
Derived createInstance(int i);
}




Re: What is the wrong with my C++ interfacing

2020-03-15 Thread drug via Digitalmars-d-learn

15.03.2020 23:25, Ferhat Kurtulmuş пишет:

On Sunday, 15 March 2020 at 20:21:57 UTC, drug wrote:

15.03.2020 22:39, Ferhat Kurtulmuş пишет:




What is the D version of `createSizeIntWH`? In C++ it returns a 
pointer but in D version it returns an instance.


extern(C++){
     cv.Size_!int createSizeInt();
     cv.Size_!int createSizeIntWH(int w, int h);
     void deleteSizeInt(ref cv.Size_!int sz);
}



createSizeIntWH returns a pointer to instance, not an instance. Try:
cv.Size_!int* createSizeIntWH(int w, int h);


Re: What is the wrong with my C++ interfacing

2020-03-15 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Sunday, 15 March 2020 at 20:21:57 UTC, drug wrote:

15.03.2020 22:39, Ferhat Kurtulmuş пишет:




What is the D version of `createSizeIntWH`? In C++ it returns a 
pointer but in D version it returns an instance.


extern(C++){
cv.Size_!int createSizeInt();
cv.Size_!int createSizeIntWH(int w, int h);
void deleteSizeInt(ref cv.Size_!int sz);
}



Re: What is the wrong with my C++ interfacing

2020-03-15 Thread drug via Digitalmars-d-learn

15.03.2020 22:39, Ferhat Kurtulmuş пишет:


The original C++ class
https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types.hpp#L315: 


template class Size_
{
public:
     typedef _Tp value_type;

     //! default constructor
     Size_();
     Size_(_Tp _width, _Tp _height);
     Size_(const Size_& sz);
     Size_(Size_&& sz) CV_NOEXCEPT;
     Size_(const Point_<_Tp>& pt);

     Size_& operator = (const Size_& sz);
     Size_& operator = (Size_&& sz) CV_NOEXCEPT;
     //! the area (width*height)
     _Tp area() const;
     //! aspect ratio (width/height)
     double aspectRatio() const;
     //! true if empty
     bool empty() const;

     //! conversion of another data type.
     template operator Size_<_Tp2>() const;

     _Tp width; //!< the width
     _Tp height; //!< the height
};

// my auxiliary cpp code:
cv::Size_* createSizeIntWH(int _width, int _height){
     return new cv::Size_(_width, _height);
}

void deleteSizeInt(cv::Size_ *&sz){
     delete sz;
}

// d code:
extern(C++, cv){
class Size_(_Tp){
     @disable this();

     final _Tp area() const;
     final double aspectRatio() const;
     final bool empty() const;

     _Tp width; //!< the width
     _Tp height; //!< the height
}
}

// my test code that fails:
Size_!int sz = createSizeIntWH(200, 100);
writeln(sz.width);


What is the D version of `createSizeIntWH`? In C++ it returns a pointer 
but in D version it returns an instance.




One of the problems is that sz.width is not printed as 200, but a random 
int.
Other problem is that if I try to call one of area, aspectRatio, and 
empty, it does not compile yielding a linker error:
error LNK2019: unresolved external symbol "public: int __cdecl 
cv::Size_::area(void)const " (?area@?$Size_@H@cv@@QEBAHXZ) 
referenced in function _Dmain

Somehow linker cannot locate that symbol. Is this a name mangling issue?
In the same library I could successfully interface cv::Mat which is a 
template-free definition. I suspect if D allows interfacing C++ class 
templates since docs do not cover this, but only struct templates?




Re: Best way to learn 2d games with D?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/15/20 4:12 PM, Jordan Wilson wrote:

On Sunday, 15 March 2020 at 17:58:58 UTC, Steven Schveighoffer wrote:
I want to try and learn how to write 2d games. I'd prefer to do it 
with D.


I've found a ton of tutorials on learning 2d gaming with other 
languages. Is there a place to look that uses D for learning? Should I 
just start with another language and then migrate to D later? Anyone 
recommend any specific tutorial/book?




I'm on a similar journey myself, I'll list my findings, maybe it could 
be useful for you.


I decided on a game programming library. I mostly looked at SFML and 
Allegro, and found both to have good bindings available in D, and good 
documentation, and got minimal examples working with both. I went with 
SFML, simply because there was a book written specifically about writing 
a game in SFML. I didn't see any such books for Allegro (although there 
are plenty of tutorials/articles).


I learnt about the "game loop". Bauss touched on it in his post, and I'm 
sure there are a lot of tutorials on it. I specifically learnt about it 
from the first few chapters of the SFML Game Development book.


I learnt about game design. In doing so, I came across 
Entity-Component-System design pattern. I decided to use this pattern, 
for no other reason than to try something other than OOP.

I found these links useful:
https://medium.com/ingeniouslysimple/entities-components-and-systems-89c31464240d 


https://gameprogrammingpatterns.com/component.html
https://www.richardlord.net/blog/ecs/what-is-an-entity-framework.html

I started writing a game, using the derelict-sfml2 as my game library 
(again, I found the allegro library to be good too), and entitysysd to 
provide the ECS framework (there are a few ECS written in D available). 
Are they the best choices? Is SFML technically limited? Will I cope with 
ECS beyound the toy game example? No idea. But I'm having a lot of fun, 
which I think for a hobby project, is a fairly good measure of success ;-)




OK, I will take a look there. I am running through some basic SDL game 
video tutorials right now.


Thanks everyone for the pointers.

-Steve


Get symbols (and/or UDAs) of subclass from superclass

2020-03-15 Thread James Blachly via Digitalmars-d-learn
I would like to programmatically retrieve members of a subclass to 
create a self-documenting interface. I am afraid that my approach is not 
possible due to need for compile time __traits / std.traits, and runtime 
typeinfo. My proposed approach is as follows:


class Base
{
string whatever;

string toString()
{
// loop over __traits(allMembers, typeof(this)) or 
getSymbolsByUDA, etc.

}
}

/// There may be a dozen Derived classes
class Derived1 : Base
{
@Config("a name", "a description")
float probability;
}
class Derived2 : Base
{
@Config("another", "more description")
int replicates;
}
...

Unfortunately, I am afraid this doesn't look possible because of the 
need for compile-time UDAs and runtime TypeInfo. Is there a way to do 
this without re-implementing toString in every single derived class? I 
expect there to be many derived classes.


Other ideas welcomed, as I usually write C-style D and am only recently 
taking a stab at OO Inheritance features.


Thanks in advance.
James


Re: Best way to learn 2d games with D?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/15/20 3:03 PM, Ferhat Kurtulmuş wrote:

On Sunday, 15 March 2020 at 19:02:11 UTC, Ferhat Kurtulmuş wrote:

On Sunday, 15 March 2020 at 18:45:14 UTC, Steven Schveighoffer wrote:

[...]


I refer this ugly site a lot for basics of SDL. Since one could easily 
convert c code to d, transition is very fast.


My favorite 2d game engine is cocos2dx but it has no support for d. I 
consider Godot-d offers a similar experience. However, it has almost 
zero documentation.


https://lazyfoo.net/tutorials/SDL/index.php


Thanks, looks useful.

-Steve


Re: Best way to learn 2d games with D?

2020-03-15 Thread Jordan Wilson via Digitalmars-d-learn
On Sunday, 15 March 2020 at 17:58:58 UTC, Steven Schveighoffer 
wrote:
I want to try and learn how to write 2d games. I'd prefer to do 
it with D.


I've found a ton of tutorials on learning 2d gaming with other 
languages. Is there a place to look that uses D for learning? 
Should I just start with another language and then migrate to D 
later? Anyone recommend any specific tutorial/book?


-Steve


I'm on a similar journey myself, I'll list my findings, maybe it 
could be useful for you.


I decided on a game programming library. I mostly looked at SFML 
and Allegro, and found both to have good bindings available in D, 
and good documentation, and got minimal examples working with 
both. I went with SFML, simply because there was a book written 
specifically about writing a game in SFML. I didn't see any such 
books for Allegro (although there are plenty of 
tutorials/articles).


I learnt about the "game loop". Bauss touched on it in his post, 
and I'm sure there are a lot of tutorials on it. I specifically 
learnt about it from the first few chapters of the SFML Game 
Development book.


I learnt about game design. In doing so, I came across 
Entity-Component-System design pattern. I decided to use this 
pattern, for no other reason than to try something other than OOP.

I found these links useful:
https://medium.com/ingeniouslysimple/entities-components-and-systems-89c31464240d
https://gameprogrammingpatterns.com/component.html
https://www.richardlord.net/blog/ecs/what-is-an-entity-framework.html

I started writing a game, using the derelict-sfml2 as my game 
library (again, I found the allegro library to be good too), and 
entitysysd to provide the ECS framework (there are a few ECS 
written in D available). Are they the best choices? Is SFML 
technically limited? Will I cope with ECS beyound the toy game 
example? No idea. But I'm having a lot of fun, which I think for 
a hobby project, is a fairly good measure of success ;-)


Jordan



Re: Integer without restrictions

2020-03-15 Thread Dennis via Digitalmars-d-learn

On Sunday, 15 March 2020 at 19:07:05 UTC, ... wrote:
And if I need to create very large value, how to use GMP 
library (It isn't described in that post)?


You can use C bindings [1], look up examples in C and do the same 
in D, or use a high-level wrapper [2].


[1] https://code.dlang.org/packages/libgmp
[2] https://code.dlang.org/packages/gmp-d


What is the wrong with my C++ interfacing

2020-03-15 Thread Ferhat Kurtulmuş via Digitalmars-d-learn



The original C++ class
https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types.hpp#L315:
template class Size_
{
public:
typedef _Tp value_type;

//! default constructor
Size_();
Size_(_Tp _width, _Tp _height);
Size_(const Size_& sz);
Size_(Size_&& sz) CV_NOEXCEPT;
Size_(const Point_<_Tp>& pt);

Size_& operator = (const Size_& sz);
Size_& operator = (Size_&& sz) CV_NOEXCEPT;
//! the area (width*height)
_Tp area() const;
//! aspect ratio (width/height)
double aspectRatio() const;
//! true if empty
bool empty() const;

//! conversion of another data type.
template operator Size_<_Tp2>() const;

_Tp width; //!< the width
_Tp height; //!< the height
};

// my auxiliary cpp code:
cv::Size_* createSizeIntWH(int _width, int _height){
return new cv::Size_(_width, _height);
}

void deleteSizeInt(cv::Size_ *&sz){
delete sz;
}

// d code:
extern(C++, cv){
class Size_(_Tp){
@disable this();

final _Tp area() const;
final double aspectRatio() const;
final bool empty() const;

_Tp width; //!< the width
_Tp height; //!< the height
}
}

// my test code that fails:
Size_!int sz = createSizeIntWH(200, 100);
writeln(sz.width);

One of the problems is that sz.width is not printed as 200, but a 
random int.
Other problem is that if I try to call one of area, aspectRatio, 
and empty, it does not compile yielding a linker error:
error LNK2019: unresolved external symbol "public: int __cdecl 
cv::Size_::area(void)const " (?area@?$Size_@H@cv@@QEBAHXZ) 
referenced in function _Dmain
Somehow linker cannot locate that symbol. Is this a name mangling 
issue?
In the same library I could successfully interface cv::Mat which 
is a template-free definition. I suspect if D allows interfacing 
C++ class templates since docs do not cover this, but only struct 
templates?




Re: Integer without restrictions

2020-03-15 Thread ... via Digitalmars-d-learn

On Sunday, 15 March 2020 at 19:03:54 UTC, Paul Backus wrote:

On Sunday, 15 March 2020 at 19:02:00 UTC, ... wrote:
How to create an integer without restrictions of size? Do I 
need to create a new class or there is a type of integer that 
doesn't have any restrictions in size?


http://dpldocs.info/experimental-docs/std.bigint.html


And if I need to create very large value, how to use GMP library 
(It isn't described in that post)?


Re: Best way to learn 2d games with D?

2020-03-15 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Sunday, 15 March 2020 at 19:02:11 UTC, Ferhat Kurtulmuş wrote:
On Sunday, 15 March 2020 at 18:45:14 UTC, Steven Schveighoffer 
wrote:

[...]


I refer this ugly site a lot for basics of SDL. Since one could 
easily convert c code to d, transition is very fast.


My favorite 2d game engine is cocos2dx but it has no support 
for d. I consider Godot-d offers a similar experience. However, 
it has almost zero documentation.


https://lazyfoo.net/tutorials/SDL/index.php


Re: Best way to learn 2d games with D?

2020-03-15 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Sunday, 15 March 2020 at 18:45:14 UTC, Steven Schveighoffer 
wrote:

On 3/15/20 2:14 PM, bauss wrote:

[...]


[snip]


[...]


Thanks, I'm pretty much starting from zero as I have very 
little experience in what is necessary to actually do the 
drawing parts, or how to store/manipulate sprites etc. I've got 
a lot of experience in GUI design, but getting the rendering to 
work, making sure it's fast enough, etc. seems like something 
I'd rather leave to someone else (i.e. library/framework/etc).


I was looking at the lispysnake stuff, and realizing I have no 
idea what any of the blogs are talking about. The D gaming 
libraries seem to come from the perspective of "Oh, you know 
how to write games, here's how you do it in D". I kind of need 
a "here's how you write 2d games" which uses D as a way to show 
it.


I think I'll probably just use a straight tutorial with another 
language and then move on to D. But thank you for the offer, I 
might take you up on it later.


-Steve


I refer this ugly site a lot for basics of SDL. Since one could 
easily convert c code to d, transition is very fast.


My favorite 2d game engine is cocos2dx but it has no support for 
d. I consider Godot-d offers a similar experience. However, it 
has almost zero documentation.


Integer without restrictions

2020-03-15 Thread ... via Digitalmars-d-learn
How to create an integer without restrictions of size? Do I need 
to create a new class or there is a type of integer that doesn't 
have any restrictions in size?


Re: Integer without restrictions

2020-03-15 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 15 March 2020 at 19:02:00 UTC, ... wrote:
How to create an integer without restrictions of size? Do I 
need to create a new class or there is a type of integer that 
doesn't have any restrictions in size?


http://dpldocs.info/experimental-docs/std.bigint.html


Re: Pattern matching via switch?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/15/20 2:22 PM, 12345swordy wrote:

On Sunday, 15 March 2020 at 17:55:59 UTC, Steven Schveighoffer wrote:

On 3/14/20 3:04 PM, 12345swordy wrote:

I.E.

switch (object)
 case Type1 t1:
 case Type2 t2:
 case Type3 t3:



Is this a class object and you are trying to determine at runtime 
which derived type it is and perform an action based on that? Or are 
you trying to switch on the type of a concrete item?


It should technically be possible to use the fully qualified name to 
switch on, but I don't think typeid(Type1).name is usable as a switch 
label.




https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching

It is an example from c#.
object is the top type in that language.
https://en.wikipedia.org/wiki/Top_type


D doesn't support this natively. The closest you can get is something 
akin to what aliak wrote (you would need to write something, not sure if 
Phobos or some package has implemented the feature), or use cascaded if 
statements:


if(auto t1 = cast(Type1)object)
// use t1 as Type1
else if(auto t2 = cast(Type2)object)
// use t2 as Type2
...

Taking care of course to look for most derived types first.

a switch based on static or runtime type would be really cool for D to 
support.


-Steve


Re: Best way to learn 2d games with D?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/15/20 2:14 PM, bauss wrote:

On Sunday, 15 March 2020 at 17:58:58 UTC, Steven Schveighoffer wrote:
I want to try and learn how to write 2d games. I'd prefer to do it 
with D.


I've found a ton of tutorials on learning 2d gaming with other 
languages. Is there a place to look that uses D for learning? Should I 
just start with another language and then migrate to D later? Anyone 
recommend any specific tutorial/book?




The theory is the same in any language ex. if you can write a 2d game in 
C++ chances are you can do it in D as well but you could probably apply 
the same theory to C, Java, Python etc.


So for D what you really need to be familiar with, is just the library 
you're using.


The theory for a 2d game is rather simple though.

You have a loop on the main thread (or ui thread) and within that you 
handle events, clear the screen, draw the graphics and repeat pretty much.




[snip]


I would recommend using Derelict and SDL with D since it's the most mature.
If you can get a SDL application running and at the very least showing a 
window then I won't mind helping you in the right direction.


It should be trivial using the guides available at the derelict docs.


Thanks, I'm pretty much starting from zero as I have very little 
experience in what is necessary to actually do the drawing parts, or how 
to store/manipulate sprites etc. I've got a lot of experience in GUI 
design, but getting the rendering to work, making sure it's fast enough, 
etc. seems like something I'd rather leave to someone else (i.e. 
library/framework/etc).


I was looking at the lispysnake stuff, and realizing I have no idea what 
any of the blogs are talking about. The D gaming libraries seem to come 
from the perspective of "Oh, you know how to write games, here's how you 
do it in D". I kind of need a "here's how you write 2d games" which uses 
D as a way to show it.


I think I'll probably just use a straight tutorial with another language 
and then move on to D. But thank you for the offer, I might take you up 
on it later.


-Steve


Re: Pattern matching via switch?

2020-03-15 Thread 12345swordy via Digitalmars-d-learn
On Sunday, 15 March 2020 at 17:55:59 UTC, Steven Schveighoffer 
wrote:

On 3/14/20 3:04 PM, 12345swordy wrote:

I.E.

switch (object)
     case Type1 t1:
     case Type2 t2:
     case Type3 t3:



Is this a class object and you are trying to determine at 
runtime which derived type it is and perform an action based on 
that? Or are you trying to switch on the type of a concrete 
item?


It should technically be possible to use the fully qualified 
name to switch on, but I don't think typeid(Type1).name is 
usable as a switch label.


-Steve


https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching

It is an example from c#.
object is the top type in that language.
https://en.wikipedia.org/wiki/Top_type


Re: Best way to learn 2d games with D?

2020-03-15 Thread bauss via Digitalmars-d-learn
On Sunday, 15 March 2020 at 17:58:58 UTC, Steven Schveighoffer 
wrote:
I want to try and learn how to write 2d games. I'd prefer to do 
it with D.


I've found a ton of tutorials on learning 2d gaming with other 
languages. Is there a place to look that uses D for learning? 
Should I just start with another language and then migrate to D 
later? Anyone recommend any specific tutorial/book?


-Steve


The theory is the same in any language ex. if you can write a 2d 
game in C++ chances are you can do it in D as well but you could 
probably apply the same theory to C, Java, Python etc.


So for D what you really need to be familiar with, is just the 
library you're using.


The theory for a 2d game is rather simple though.

You have a loop on the main thread (or ui thread) and within that 
you handle events, clear the screen, draw the graphics and repeat 
pretty much.


I would recommend using Derelict and SDL with D since it's the 
most mature.


There's DSFML but it's dead and doesn't support the latest 
version of SFML.


You could also try SFML through Derelict but SDL just seems more 
stable tbh.


TBH. if you have any experience with GUI engines then applying 
that to writing a game is pretty much the same.


A player, monster, NPC etc. is just a sprite (which is pretty 
much just an image.) it has a size, position etc. and you just 
manipulate that based on the events you receive in your main loop 
etc.


I don't think there's currently any good or complete tutorials on 
game development in D, much less 2d game development. There's a 
lot of "dead" libraries etc.


If you can get a SDL application running and at the very least 
showing a window then I won't mind helping you in the right 
direction.


It should be trivial using the guides available at the derelict 
docs.





Re: Pattern matching via switch?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/14/20 3:04 PM, 12345swordy wrote:

I.E.

switch (object)
     case Type1 t1:
     case Type2 t2:
     case Type3 t3:



Is this a class object and you are trying to determine at runtime which 
derived type it is and perform an action based on that? Or are you 
trying to switch on the type of a concrete item?


It should technically be possible to use the fully qualified name to 
switch on, but I don't think typeid(Type1).name is usable as a switch label.


-Steve


Best way to learn 2d games with D?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn

I want to try and learn how to write 2d games. I'd prefer to do it with D.

I've found a ton of tutorials on learning 2d gaming with other 
languages. Is there a place to look that uses D for learning? Should I 
just start with another language and then migrate to D later? Anyone 
recommend any specific tutorial/book?


-Steve


Re: exporting function from betterc to windows dll

2020-03-15 Thread Ahmet Sait via Digitalmars-d-learn

On Saturday, 14 March 2020 at 20:53:45 UTC, Abby wrote:
I would like to export some functions from my bettec dll for 
dotnet core application in windows.



You can check my library documentation to understand how to do 
some fancy interop between C# and D: 
https://github.com/ahmetsait/IDL/wiki


Should probably write a detailed blog post about that some time :/


Re: Does anyone have gdb pretty printers for buildin types like associative arrays?

2020-03-15 Thread Dennis via Digitalmars-d-learn

On Sunday, 15 March 2020 at 16:42:52 UTC, Panke wrote:
Should this just work and by box is not correctly configured or 
do I need some pretty printers? If so, has someone already made 
them?


Take a look at:

https://forum.dlang.org/post/ztyhmmxalpiysgjkv...@forum.dlang.org




Re: Is it possible to dynamically load a @safe function from a shared library ?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/13/20 5:06 PM, H. S. Teoh wrote:

On Fri, Mar 13, 2020 at 04:31:16PM -0400, Steven Schveighoffer via 
Digitalmars-d-learn wrote:
[...]

I would expect that something could be written to turn a signature
string into a mangling and also provide the correct type upon return.
Something like:

auto f = getFunction!(@safe void function(int))("package.module.symbol");

and have it properly mangle the expected function name and pull it
from the dynamic library.

[...]

This would still have to be @trusted, of course, since there's no
telling what's actually inside the object file.


Of course it has to be trusted. The underlying function uses void *.

But not because you can't trust the mangling.


But this sort of facility totally should be in Phobos, or at least in
some dub package somewhere.  It will make working with dynamically
loaded libraries in D so much more convenient.


I'm not really motivated to make such a package, as I don't do any 
software with runtime loading of libraries. But I bet it would be either 
accepted into Phobos and/or wouldn't be that hard to add for a library.


-Steve


Re: Is it possible to dynamically load a @safe function from a shared library ?

2020-03-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/14/20 6:06 AM, wjoe wrote:

On Friday, 13 March 2020 at 20:31:16 UTC, Steven Schveighoffer wrote:

On 3/13/20 4:22 PM, wjoe wrote:
I would expect that something could be written to turn a signature 
string into a mangling and also provide the correct type upon return. 
Something like:


auto f = getFunction!(@safe void function(int))("package.module.symbol");

and have it properly mangle the expected function name and pull it 
from the dynamic library.




Thanks for your reply.

core.demangle: mangle; comes to mind. And in fact, because the exports 
weren't extern(C), that's how I imported the symbols; prior to reading 
H. S. Teoh's reply. That's when I realized that I've got a problem.


Even though I know that the function being exported are all compiled 
@safe, that doesn't mean I can count on the fact.
Since a plugin is a separate file it can be swapped out with a version 
that exports all the same (forged) symbols names but with a @system 
implementation.
Forging these symbol names is as easy as something like mangle!(int 
function(int))("a.b")); (=_D1a1bPFiZi) and copy/pasting that into 
pragma(mangle, "_D1a1bPFiZi") and voila, the loader can nothing but 
trust that this is true.


Forging is maybe too hard a word but my vocabulary lacks a better one.


My original point is that forging is possible without using a dynamic 
library. Anyone who has pragma(mangle)'d the symbol can "forge" anything 
they want, it's not that hard.


Even without using pragma(mangle, it's possible):

mod1.d:

__gshared someGlobal;
extern(C) void realImpl() // no mangling
{
   someGlobal = 5; // yay side effects!
   *(cast(ubyte*)0xdeadbeef) = 5; // yay no safety!
   throw new Exception("yay, I can throw!");
}

mod2.d:

extern(C) void realImpl() @safe pure nothrow;

void foo() @safe pure nothrow // mangled properly
{
   realImpl();
}

So why worry about the dynamic library case? I would say if you obey the 
mangling rules, the compiler should trust the mangling.


-Steve


Does anyone have gdb pretty printers for buildin types like associative arrays?

2020-03-15 Thread Panke via Digitalmars-d-learn
At least on my installation they are printed as just a pointer. 
Should this just work and by box is not correctly configured or 
do I need some pretty printers? If so, has someone already made 
them?