Re: How to use @safe when a C library integration needed
On Friday, 14 April 2023 at 16:19:22 UTC, Paul Backus wrote: On Friday, 14 April 2023 at 14:10:41 UTC, Leonardo wrote: [...] No, there isn't. C is an unsafe language, so if you want to call C from `@safe` code, you have to do the work to make sure that each individual call is `@safe`. [...] Thanks for your response.
Re: How to use @safe when a C library integration needed
On Monday, 23 January 2023 at 16:46:48 UTC, Dom DiSc wrote: 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). Thanks. But this works only to one function per time. Is there any way to do this to an imported library at all? something like `@trusted import library`
How to use @safe when a C library integration needed
Hello. How to use @safe when a C library integration needed? Everything need a system function...
Re: unique_ptr | Unique for autoclose handle
On Wednesday, 14 December 2022 at 11:30:07 UTC, Vitaliy Fadeev wrote: Hi! I open a device under Windows: ``` HANDLE h = CreateFileW( ... ); ``` in procedure: ``` HANDLE open_keyboard_device2( LPCWSTR path, int* error_number ) { ... HANDLE dev_handle = CreateFileW( path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); ... return dev_handle; } ``` and I want to close HANDLE automatically, when **dev_handle** destroyed by GC: ``` CloseHandle( h ); ``` How to do it? How to define HANDLE var ? What to return from procedure? How to call CloseHandle( h ) when variable destroyed? I was trying **std.typecons.Unique**. But where I must put **CloseHandle( h )** ? I was trying **std.typecons.Unique** with custom class **SafeHabdle** ``` class SafeHandle { HANDLE h; this( HANDLE h ) { this.h = h; } ~this() { if ( h != INVALID_HANDLE_VALUE ) CloseHandle( h ); } } ``` and using it: ``` Unique!SafeHandle open_keyboard_device2( LPCWSTR path, int* error_number ) { ... Unique!SafeHandle dev_handle = new SafeHandle( CreateFileW( path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ) ); ... } ``` It complex. Because needed: ``` Unique!SafeHandle open_keyboard_device2( LPCWSTR path, int* error_number ) Unique!SafeHandle dev_handle = new SafeHandle( CreateFileW( ... ) ); DeviceIoControl( dev_handle.h, ...); ``` vs ``` HANDLE open_keyboard_device2( LPCWSTR path, int* error_number ) HANDLE dev_handle = CreateFileW( ... ); DeviceIoControl( dev_handle, ...); ``` Last is readable. Teach me the most beautiful way. How to make beautiful? Thanks! If you need an specific shutdown maybe you can use scopes. But I'm here learning too. https://tour.dlang.org/tour/en/gems/scope-guards
Re: How to use version in dub?
On Tuesday, 13 December 2022 at 20:35:28 UTC, ryuukk_ wrote: On Tuesday, 13 December 2022 at 20:01:40 UTC, torhu wrote: On Tuesday, 13 December 2022 at 19:50:15 UTC, torhu wrote: On Tuesday, 13 December 2022 at 19:28:44 UTC, Leonardo A wrote: Hello. How to use version in dub? https://dlang.org/spec/version.html "The version level and version identifier can be set on the command line by the -version" I tried everything but noting. In SDL syntax, either at the top level, in a configuration, or build type: ``` versions "something" "other" ``` To be more clear: When using dub you need to put this in the dub file, dub.sdl or dub.json. If you want to be able to choose from the command line, use a configuration: ``` configuration "something" { versions "something" } ``` Then you can do: ``` dub build -c=something ``` How can this be the official solution? It should be as easy as dub build -v "something" Thanks for the replies. I think definitely we need a better documentation of this at least. It seems to have more than one way to do. I tried this at first: ``` dub build -version=USE_SDL Error Error processing arguments: Can't parse string: bool should be case->> insensitive 'true' or 'false' Run 'dub help' for usage information. ``` Dub seems to understand my 'version' argument, but this has not in the help section. And in this documentation we have more than one way to declare versions: https://dub.pm/package-format-json.html#configurations 1. { "versions": ["PrintfDebugging"] } 2. like out friend said in configuration { "name": "somepackage", "configurations": [ { "name": "glut-app", "targetType": "executable", "versions": ["GlutApp"] } ] } The -c argument refers to config build. Maybe I'm confusing with package version. But my thoughts about dub is that we need to have the best documentation as we can to provide a good understanding for newcomers in D language.
Re: How to use ImportC?
On Friday, 4 March 2022 at 17:17:17 UTC, MoonlightSentinel wrote: On Friday, 4 March 2022 at 01:30:00 UTC, Leonardo wrote: Thanks but not worked here. ``` [leonardo@leonardo-pc dimportc]$ dmd --version DMD64 D Compiler v2.098.1 ``` Please retry with the [beta](https://dlang.org/download.html#dmd_beta) or [nightly build](https://github.com/dlang/dmd/releases/tag/nightly). I think your bug was already fixed since 2.098.1. Thank you all. In v2.099.0 it works.
Re: How to use ImportC?
Thanks but not worked here. ``` [leonardo@leonardo-pc dimportc]$ dmd --version DMD64 D Compiler v2.098.1 Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved written by Walter Bright [leonardo@leonardo-pc dimportc]$ ls foo.c program.d [leonardo@leonardo-pc dimportc]$ cat foo.c double twice(double x) { return 2.0*x; } [leonardo@leonardo-pc dimportc]$ cat program.d import foo; import std.stdio; void main() { writeln(twice(6.8)); } [leonardo@leonardo-pc dimportc]$ dmd program.d foo.c /usr/include/dlang/dmd/core/stdc/stdio.d(1256): Error: function `core.stdc.stdio.vfprintf` `pragma(printf)` functions must be `extern(C) int vfprintf([parameters...], const(char)*, va_list)` /usr/include/dlang/dmd/core/stdc/stdio.d(1259): Error: function `core.stdc.stdio.vfscanf` `pragma(scanf)` functions must be `extern(C) int vfscanf([parameters...], const(char)*, va_list)` /usr/include/dlang/dmd/core/stdc/stdio.d(1262): Error: function `core.stdc.stdio.vsprintf` `pragma(printf)` functions must be `extern(C) int vsprintf([parameters...], const(char)*, va_list)` /usr/include/dlang/dmd/core/stdc/stdio.d(1265): Error: function `core.stdc.stdio.vsscanf` `pragma(scanf)` functions must be `extern(C) int vsscanf([parameters...], const(char)*, va_list)` /usr/include/dlang/dmd/core/stdc/stdio.d(1268): Error: function `core.stdc.stdio.vprintf` `pragma(printf)` functions must be `extern(C) int vprintf([parameters...], const(char)*, va_list)` /usr/include/dlang/dmd/core/stdc/stdio.d(1271): Error: function `core.stdc.stdio.vscanf` `pragma(scanf)` functions must be `extern(C) int vscanf([parameters...], const(char)*, va_list)` /usr/include/dlang/dmd/core/stdc/stdio.d(1485): Error: function `core.stdc.stdio.vsnprintf` `pragma(printf)` functions must be `extern(C) int vsnprintf([parameters...], const(char)*, va_list)` ```
How to use ImportC?
I saw the new feature called ImportC, it's cool to be able to use C code/libraries, but I'm not much experience in C and didn't understand this incomplete documentation: https://dlang.org/spec/importc.html How to use ImportC?
Re: How to dinamically create Tuples?
On Wednesday, 27 January 2021 at 17:28:00 UTC, H. S. Teoh wrote: On Wed, Jan 27, 2021 at 05:17:18PM +, Paul Backus via Digitalmars-d-learn wrote: On Wednesday, 27 January 2021 at 17:11:52 UTC, Leonardo wrote: > Hi, I want to know if are some way to dinamically create > Tuples, with variable size and types defined at runtime. > Thanks. No. D is a statically-typed language, so all types have to be defined at compile time. But you can probably achieve equivalent semantics with an array of Variant (see std.variant). T Thanks you all, variant is more like I was expecting.
How to dinamically create Tuples?
Hi, I want to know if are some way to dinamically create Tuples, with variable size and types defined at runtime. Thanks.
Re: Is it possible to store different subclasses in one array?
On Monday, 13 April 2020 at 05:54:52 UTC, evilrat wrote: On Monday, 13 April 2020 at 04:21:48 UTC, Leonardo wrote: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be? Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack() can be simplified as: if (auto weapon = cast(Weapon) gi) weapon.Attack(); Even better. Thanks folks!
Re: Is it possible to store different subclasses in one array?
On Monday, 13 April 2020 at 04:15:04 UTC, Leonardo wrote: On Monday, 13 April 2020 at 01:47:11 UTC, Adam D. Ruppe wrote: On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote: Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet? Oh, thanks, this works. Now it seems obvious. But fitting another question, this case is only representative, if I want to use one method present in only one of these classes like this: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be? Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack()
Re: Is it possible to store different subclasses in one array?
On Monday, 13 April 2020 at 01:47:11 UTC, Adam D. Ruppe wrote: On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote: Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet? Oh, thanks, this works. Now it seems obvious. But fitting another question, this case is only representative, if I want to use one method present in only one of these classes like this: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be?
Is it possible to store different subclasses in one array?
Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? public class BaseItem{ public string name = ""; } public class Weapon : BaseItem{ public int damage = 10; } public class Potion : BaseItem{ public int hpRestore = 50; } var GameItems = new List(); GameItems.Add(new Weapon()); GameItems.Add(new Potion());
Re: Game and GC
On Friday, 23 February 2018 at 03:25:33 UTC, Norm wrote: On Friday, 23 February 2018 at 01:54:07 UTC, Leonardo wrote: Hi, I'm new to language and games. Many people say that GC is bad and can slow down your project in some moments. What can happen if I create a game using D without worrying with memory management? (using full GC) Have a look at https://github.com/gecko0307/atrium and see how memory is handled there. TBH though every game I've written I have not worried about the GC and just code it up. This works fine for 2d games, platformers etc. If it ever does bite you can always schedule the pauses (they are deterministic in the sense a collect will occur on allocation) or do pretty much what every game does in C++/C and allocate in pools. Cheers, Norm Atrium game use Dlib, more specific this module to manually manage memory. Appears to be very easy to use. Thanks. https://github.com/gecko0307/dlib/wiki/dlib.core.memory
Re: Game and GC
On Saturday, 24 February 2018 at 07:12:21 UTC, Guillaume Piolat wrote: From my experience a combination of the following is necessary: - not having the audio thread registered - using pools aggressively for game entities I'll read the resources you gave. Thanks for the all answers. Great community here.
Re: Game and GC
On Friday, 23 February 2018 at 02:16:38 UTC, Jonathan M Davis wrote: The GC won't slow down your code in general (in fact, it will probably speed it up in comparison to reference counting), but whenever the GC does a collection, that means that it stops all threads that it manages. So, you could suddenly have everything stop for 100ms (the actual length of a collection is going to depend on how much memory the GC has to scan, and I don't know what the typical length of a collection is; that will depend on the program). For programs that can afford to occasionally stop like that, that's not a problem. For a game that's trying to maintain 60fps, that's likely a really big deal. - Jonathan M Davis That's what I thought for a game, but probably no one tested yet to see the impact. Thanks, I'll read on.
Re: Game and GC
On Friday, 23 February 2018 at 02:02:12 UTC, StickYourLeftFootIn wrote: On Friday, 23 February 2018 at 01:54:07 UTC, Leonardo wrote: Hi, I'm new to language and games. Many people say that GC is bad and can slow down your project in some moments. What can happen if I create a game using D without worrying with memory management? (using full GC) What do you think will happen? Anytime you delegate power to something else what can go wrong? Nothing is perfect. The GC exists to automate a job. The job it does is not the problem... It does it well. The issue is when it does it. It's like the noisy garbage man coming in as 3AM to get your trash... are you ok with that? Some people are. I understand, I'm not saying GC is bad, but I want to know if it would be slow to the point of being noticeable in a game. If so, what is the best way to do this? Placing @nogc everywhere? Thanks.
Game and GC
Hi, I'm new to language and games. Many people say that GC is bad and can slow down your project in some moments. What can happen if I create a game using D without worrying with memory management? (using full GC)
Re: Equivalent to nullptr
On Thursday, 4 May 2017 at 04:34:40 UTC, Stanislav Blinov wrote: In the meantime, you can get around the issue by redeclaring the function with another name and loading it manually just after calling DerelictSDL2.load(): import derelict.sdl2.sdl; __gshared SDL_bool function (const(SDL_Point)*, int, const(SDL_Rect)*, SDL_Rect*) SDL_EnclosePoints_; void main() { DerelictSDL2.load(); DerelictSDL2.bindFunc(cast(void**)_EnclosePoints_, "SDL_EnclosePoints"); // ... } Now you'd need to call SDL_EnclosePoints_ instead of SDL_EnclosePoints. Thank you.
Re: Equivalent to nullptr
On Thursday, 4 May 2017 at 02:45:30 UTC, Adam D. Ruppe wrote: On Thursday, 4 May 2017 at 02:12:13 UTC, Leonardo wrote: nullptr word. How I use this? Does it work if you just use `null` ? No. First I got: source/app.d(45,69): Error: expression (*SDL_EnclosePoints)(& mousePos, 1, , null) of type void does not have a boolean value then I change to: event.button.button == SDL_BUTTON_LEFT && SDL_TRUE == SDL_EnclosePoints(, 1, , null) and got: source/app.d(45,81): Error: void has no value source/app.d(45,52): Error: incompatible types for ((SDL_TRUE) == ((*SDL_EnclosePoints)(& mousePos, 1, , null))): 'int' and 'void'
Equivalent to nullptr
I was trying to use Derelict-SDL2 library, and the tutorial do this code below, as C++ code has nullptr word. How I use this? - struct Ball { SDL_Rect bbox = {0, 0, 100, 100}; SDL_Point vel = {1, 1}; } - case SDL_MOUSEBUTTONDOWN: { SDL_Point mousePos = {event.button.x, event.button.y}; if(event.button.button == SDL_BUTTON_LEFT && SDL_EnclosePoints(, 1, , nullptr) ) { SDL_Log("CLICK"); if(abs(ball.vel.x) < 32) { ball.vel.x *= 2; ball.vel.y *= 2; } } break; }