Re: DlangUI MouseEvent mouse delta

2016-04-26 Thread stunaep via Digitalmars-d-learn

I am currently handling it like this:


current = e.pos();
xdelta = current.x - previous.x;
ydelta = current.y - previous.y;
previous = current;

I'm just wondering if there is a built in solution that I missed.



Re: C header file: tagged enumerations

2016-04-26 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 23:33:08 UTC, Stefan Koch wrote:

static if (win32msi >= 500) .


Won't work here because static if must have a complete 
declaration inside it, and the C pattern only has a few elements 
of the whole inside each #if.


D doesn't handle this C pattern well... you basically have to 
rewrite the whole thing for each version.


Personally, I just decide to support all of them and say the 
older versions with less declarations are less supported...


Re: C header file: tagged enumerations

2016-04-26 Thread Jesse Phillips via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 23:33:08 UTC, Stefan Koch wrote:

On Tuesday, 26 April 2016 at 22:57:36 UTC, Jesse Phillips wrote:
Windows tends to have these in their header files, what is the 
best way to translate them to D?


[...]


eunm win32msi = mixin(_WIN32_MSI);

static if (win32msi >= 500) .


Sorry forgot to mention that static if doesn't allow adding 
partial declarations, always need to be complete. Or to quote the 
compiler:


"Error: basic type expected, not static"


Re: C header file: tagged enumerations

2016-04-26 Thread Stefan Koch via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 22:57:36 UTC, Jesse Phillips wrote:
Windows tends to have these in their header files, what is the 
best way to translate them to D?


[...]


eunm win32msi = mixin(_WIN32_MSI);

static if (win32msi >= 500) .


C header file: tagged enumerations

2016-04-26 Thread Jesse Phillips via Digitalmars-d-learn
Windows tends to have these in their header files, what is the 
best way to translate them to D?


https://msdn.microsoft.com/en-us/library/whbyts4t.aspx

Example below. In general a named enum is close enough to the 
same thing, but this example has additional enumerations added 
based on the preprocessor.


Assuming _WIN32_MSI is defined for compile time, what is 
recommended for creating this enum?


typedef enum tagINSTALLMESSAGE
{
// 12 others ...
INSTALLMESSAGE_INITIALIZE ,
INSTALLMESSAGE_TERMINATE  ,
INSTALLMESSAGE_SHOWDIALOG ,
#if (_WIN32_MSI >= 500)
INSTALLMESSAGE_PERFORMANCE,
#endif // (_WIN32_MSI >= 500)
#if (_WIN32_MSI >= 400)
INSTALLMESSAGE_RMFILESINUSE   ,
#endif // (_WIN32_MSI >= 400)
#if (_WIN32_MSI >= 450)
INSTALLMESSAGE_INSTALLSTART   ,
INSTALLMESSAGE_INSTALLEND ,
#endif // (_WIN32_MSI >= 450)
} INSTALLMESSAGE;



DlangUI MouseEvent mouse delta

2016-04-26 Thread stunaep via Digitalmars-d-learn
I am trying to know how much the mouse moves when clicking down 
on a certain widget, but I can't figure out how. I noticed only a 
mouse wheel delta property and nothing for the mouse pointer x,y 
deltas since the click. I am looking to do something such as


if(e.lbutton().isDown() && !e.rbutton().isDown()) {
pitch -= e.dy();
}


Re: Lazy Generation of Random Sequence

2016-04-26 Thread Seb via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 17:38:33 UTC, Nordlöw wrote:

On Tuesday, 26 April 2016 at 11:09:42 UTC, Seb wrote:
Btw if you do random generation at the moment, you should 
always be aware that it's super-easy to do an implicit copy if 
you pass around the rndGen


So should I pass it by ref or const ref then?


I guess both work fine, but this was just a warning for other 
case.
The example that @WebDrake mentioned on his slides can happen too 
easily - a reason why we should fix it (which is on my agenda).


Re: Lazy Generation of Random Sequence

2016-04-26 Thread Nordlöw via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 11:09:42 UTC, Seb wrote:
Btw if you do random generation at the moment, you should 
always be aware that it's super-easy to do an implicit copy if 
you pass around the rndGen


So should I pass it by ref or const ref then?


Re: Lazy Generation of Random Sequence

2016-04-26 Thread Nordlöw via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 17:38:33 UTC, Nordlöw wrote:

On Tuesday, 26 April 2016 at 11:09:42 UTC, Seb wrote:
Btw if you do random generation at the moment, you should 
always be aware that it's super-easy to do an implicit copy if 
you pass around the rndGen


So should I pass it by ref or const ref then?


Doh. Const ref is of no use... just by ref then, right?


Re: Compare lambda expressions

2016-04-26 Thread Meta via Digitalmars-d-learn
There's no way to do this in D. There have been several proposals 
but nothing implemented as of yet.





Re: Garbage Collector : Ignoring a reference

2016-04-26 Thread Marco Leise via Digitalmars-d-learn
Am Tue, 26 Apr 2016 13:35:37 +
schrieb Begah :

> When the screen switches to another screen ie from menu to the 
> game,
> I want that the "button.png" texture is automaticly destroyed by 
> the gc.

My ideological point of view is that you must not use
non-deterministic garbage collection for resources. Neither
for files, GUI widgets or textures. The garbage collector
cannot look past its own confined environment (heap memory
allocated by the D program and loaded D libraries) and will
not have enough information to tell if the process is running
out of file handles, backing buffer for widgets or texture
memory on the graphics card. It only takes action, when its own
heap is filling up or when you manually call GC.collect().
All the convenient 100% GC languages from Java to Python
require the user to call ".close()" for files,
".dispose()/.Destroy()" for widgets, etc.
Reference counting is the correct approach. It makes using
external resources safe and convenient by removing the need
for manual lifetime management. Cyclic references cannot
exist in the D code in this scenario.

The texture constructor:
- sets ref count to 1
- adds texture to hash map
The copy constructor:
- increments the ref count
The destructor:
- decrements the ref count
- if ref count reaches 0:
  - removes the texture from the hash table
  - destroys the texture data

-- 
Marco



Compare lambda expressions

2016-04-26 Thread Alex via Digitalmars-d-learn

Hi all!

Not sure, if this belongs directly to the D language, or it is 
rather maths, but:

given to delegate generating functions and a main

auto func1(int arg)
{
bool delegate(int x) dg;
dg = (x) => arg * x;
return dg;
}

auto func2(int arg)
{
bool delegate(int x) dg;
dg = (x) => arg * x * x / x;
return dg;
}

void main()
{
auto dgf1 = func1(4);
writeln(dgf1(2));
auto dgf2 = func2(4);
writeln(dgf2(2));
writeln(dgf1 == dgf2);
}

the last line gives false, either in this form, as well with 
.ptr, as well with .funcptr.


The question is, how to say, whether two lambda expressions are 
the same?
What if the lambda expressions are predicates? This case would be 
more important for me...


I understand, that there is only a little hope to get answer 
other then "impossible", even for the constrained case. In such a 
case, I have another opportunity:


Say, I define two lambda functions as strings in, say a json 
file. I'm going to parse them and build functions out of them.
How to force the input in a unified manner? In such a case, I 
could compare the given strings with some weird regular 
expression...

So, the question for this case is:
Is there some normalized form of writing a lambda expression? Any 
literature would be more then sufficient as an answer...


Many thanks in advance :)


Re: Garbage Collector : Ignoring a reference

2016-04-26 Thread ciechowoj via Digitalmars-d-learn
Thus, i need a way to tell the gc to ignore the reference ( or 
something similar ) in that hashmap.


So, having pointer that doesn't hold a reference isn't that hard 
(store it in memory region that is unreachable to gc), but don't 
you need a way to tell if that pointer ins't dangling, beyond 
initial problem?


Re: Garbage Collector : Ignoring a reference

2016-04-26 Thread ag0aep6g via Digitalmars-d-learn

On 26.04.2016 15:35, Begah wrote:

Nothing will reload.

An example :
I load a texture "button.png" in a class and draw it to the screen,
When the screen switches to another screen ie from menu to the game,
I want that the "button.png" texture is automaticly destroyed by the gc.
But this will never happen because i still have a reference to it in my
hashmap.
Thus, i need a way to tell the gc to ignore the reference ( or something
similar ) in that hashmap.


How would you prevent reads of that now-invalid element of the hashmap?


Re: Garbage Collector : Ignoring a reference

2016-04-26 Thread Begah via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 13:01:26 UTC, ciechowoj wrote:

On Tuesday, 26 April 2016 at 09:07:59 UTC, Begah wrote:
How could i tell the garbage collector to ignore the reference 
in the hashmap and to free it if there isn't any other 
reference that in my hashmap?


You could always zero the reference in the hashmap, as it won't 
be valid after reload anyway...


Nothing will reload.

An example :
I load a texture "button.png" in a class and draw it to the 
screen,
When the screen switches to another screen ie from menu to the 
game,
I want that the "button.png" texture is automaticly destroyed by 
the gc.
But this will never happen because i still have a reference to it 
in my hashmap.
Thus, i need a way to tell the gc to ignore the reference ( or 
something similar ) in that hashmap.


Re: Garbage Collector : Ignoring a reference

2016-04-26 Thread ciechowoj via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 09:07:59 UTC, Begah wrote:
How could i tell the garbage collector to ignore the reference 
in the hashmap and to free it if there isn't any other 
reference that in my hashmap?


You could always zero the reference in the hashmap, as it won't 
be valid after reload anyway...


Re: Garbage Collector : Ignoring a reference

2016-04-26 Thread Alex Parrill via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 09:07:59 UTC, Begah wrote:
I am trying to create an asset manager for my textures. I had 
the idea ( it may be a wrong idea ) to create a hashmap of my 
textures with a string as the key. When the program request a 
texture, it firts check if it is in the hashmap and then 
returns if it is :


[...]


What you want are "weak references". I don't think D supports 
them yet.


Re: Lazy Generation of Random Sequence

2016-04-26 Thread Seb via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 10:50:27 UTC, Nordlöw wrote:

On Tuesday, 26 April 2016 at 10:31:22 UTC, Nordlöw wrote:
How do I lazily generate a sequence of random instances of 
type `T` as an `InputRange`?


Ahh, I found it:

import std.range : generate, take;
import std.random : uniform;
auto randomSamples = generate!(() => 
uniform!Key).take(n);


I should have guessed that...


Btw if you do random generation at the moment, you should always 
be aware that it's super-easy to do an implicit copy if you pass 
around the rndGen, see:


http://dconf.org/2015/talks/wakeling.html


Re: Lazy Generation of Random Sequence

2016-04-26 Thread Nordlöw via Digitalmars-d-learn

On Tuesday, 26 April 2016 at 10:31:22 UTC, Nordlöw wrote:
How do I lazily generate a sequence of random instances of type 
`T` as an `InputRange`?


Ahh, I found it:

import std.range : generate, take;
import std.random : uniform;
auto randomSamples = generate!(() => uniform!Key).take(n);

I should have guessed that...


Lazy Generation of Random Sequence

2016-04-26 Thread Nordlöw via Digitalmars-d-learn
How do I lazily generate a sequence of random instances of type 
`T` as an `InputRange`?


traits getMember gives a deprecation warning

2016-04-26 Thread Adil via Digitalmars-d-learn

I'm using DMD 2.071 and am getting this new deprecation error:

source/screener/lib/syntax/semantics.d(42): Deprecation: 
screener.lib.virtualmachine.functions.object is not visible from 
module semantics
source/screener/lib/syntax/semantics.d(42): Deprecation: 
screener.lib.virtualmachine.functions.object is not visible from 
module semantics
source/screener/lib/syntax/semantics.d(42): Deprecation: 
screener.lib.virtualmachine.functions.screener is not visible 
from module semantics
source/screener/lib/syntax/semantics.d(42): Deprecation: 
screener.lib.virtualmachine.functions.screener is not visible 
from module semantics



The offendding line is this :

 import screener.lib.virtualmachine.functions;
alias funcModule = screener.lib.virtualmachine.functions;

bool found = false;
foreach(m; __traits(allMembers, funcModule)) {
static if ((__traits(isStaticFunction, 
__traits(getMember, funcModule, m)) || __traits(isTemplate, 
__traits(getMember, funcModule, m))) && __traits(getProtection, 
__traits(getMember, funcModule, m)) != "private")

{

The module 'screener.lib.virtualmachine.functions' defines 
'public' functions/templates. How can i fix this?


Garbage Collector : Ignoring a reference

2016-04-26 Thread Begah via Digitalmars-d-learn
I am trying to create an asset manager for my textures. I had the 
idea ( it may be a wrong idea ) to create a hashmap of my 
textures with a string as the key. When the program request a 
texture, it firts check if it is in the hashmap and then returns 
if it is :


Texture[string] textures;

Texture loadTexture(string filename) {
  if(filename in textures)
return textures[filename]
  else
// Load image and put it in hashmap
}

Warning : I haven't tested if it actually doesn't work, but 
thinking about it, i think it should not.
My problem is that i return a reference of the texture to the 
program, but i keep one to myself and i want to free the texture 
if my program isn't using it anymore ( no more reference to it ). 
The problem i see, is that i will always have atleast one 
reference to the texture in my hashmap, but i want the garbage 
collector to ignore that reference and to free the texture if 
there are no more references anywhere in my program ( except in 
the hashmap ).


How could i tell the garbage collector to ignore the reference in 
the hashmap and to free it if there isn't any other reference 
that in my hashmap?