www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dub / Derelict confusion

reply "Paul" <paul example.com> writes:
I would like to create a simple program using SDL. I've read this 
page http://dblog.aldacron.net/derelict-help/using-derelict/ and 
this one http://code.dlang.org/about and decided that using 'dub' 
would be the sensible option for a beginner so I downloaded the 
dub executable and put it in the same directory as my source file 
'test.d'.

As per the example at the above url, I created a dub.json file in 
the same directory with contents as so:

"dependencies": {
"derelict-sdl2":"~>1.0.0",
"derelict-gl3":"~master",
}

If I have the relevant import statements in test.d, am I right in 
thinking I can use dub to build my program (not just download and 
build the derelict libraries)? The command dub build test.d gives 
me a 'failed to load package' error.

I'm using 32 bit Linux.

Any help appreciated :D

Paul
Nov 19 2014
next sibling parent reply "wobbles" <grogan.colin gmail.com> writes:
On Wednesday, 19 November 2014 at 09:12:52 UTC, Paul wrote:
 I would like to create a simple program using SDL. I've read 
 this page 
 http://dblog.aldacron.net/derelict-help/using-derelict/ and 
 this one http://code.dlang.org/about and decided that using 
 'dub' would be the sensible option for a beginner so I 
 downloaded the dub executable and put it in the same directory 
 as my source file 'test.d'.

 As per the example at the above url, I created a dub.json file 
 in the same directory with contents as so:

 "dependencies": {
 "derelict-sdl2":"~>1.0.0",
 "derelict-gl3":"~master",
 }

 If I have the relevant import statements in test.d, am I right 
 in thinking I can use dub to build my program (not just 
 download and build the derelict libraries)? The command dub 
 build test.d gives me a 'failed to load package' error.

 I'm using 32 bit Linux.

 Any help appreciated :D

 Paul
Dub will download and build the D packages from code.dlang.org. However, some D packages require you to download compiled libraries they link to for your syste. (or build them yourself from source) For sdl2 on linux, you need to go to: https://www.libsdl.org/download-2.0.php and download the source. Compile those libs using ($ ./configure; make; make install;) Put them into a place your linker can find (usually /usr/lib/ iirc). I believe that should work for you!
Nov 19 2014
parent "wobbles" <grogan.colin gmail.com> writes:
 Put them into a place your linker can find (usually /usr/lib/
 iirc).
I forgot, I'm pretty sure make install does that step for you.
Nov 19 2014
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 19 November 2014 at 09:12:52 UTC, Paul wrote:
 I would like to create a simple program using SDL. I've read 
 this page 
 http://dblog.aldacron.net/derelict-help/using-derelict/ and 
 this one http://code.dlang.org/about and decided that using 
 'dub' would be the sensible option for a beginner so I 
 downloaded the dub executable and put it in the same directory 
 as my source file 'test.d'.

 As per the example at the above url, I created a dub.json file 
 in the same directory with contents as so:

 "dependencies": {
 "derelict-sdl2":"~>1.0.0",
 "derelict-gl3":"~master",
 }

 If I have the relevant import statements in test.d, am I right 
 in thinking I can use dub to build my program (not just 
 download and build the derelict libraries)? The command dub 
 build test.d gives me a 'failed to load package' error.

 I'm using 32 bit Linux.

 Any help appreciated :D

 Paul
could you give some more details? Full package.json, the file-structure of your project and the full output from dub would help.
Nov 19 2014
parent reply "Paul" <paul example.com> writes:
 wobbles:

Sorry, should have said, I built SDL and the libraries are 
installed in /usr/local/lib

 could you give some more details? Full package.json, the 
 file-structure of your project and the full output from dub 
 would help.
John Colvin The entire contents of dub.json are as above. My program is a single file just to check that I can init SDL: import std.stdio; import derelict.sdl2.sdl; void main(){ // Load the SDL 2 library. DerelictSDL2.load(); //init SDL ..... } The error is: Error executing command build: Failed to load package at /home/paul/D-programs/: Expected end of string after JSON value, not ': { "derelict-sdl2":"~>1.0.0", "derelict-gl3":"~master", }'. TIA
Nov 19 2014
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 19 November 2014 at 10:09:55 UTC, Paul wrote:
  wobbles:

 Sorry, should have said, I built SDL and the libraries are 
 installed in /usr/local/lib

 could you give some more details? Full package.json, the 
 file-structure of your project and the full output from dub 
 would help.
John Colvin The entire contents of dub.json are as above. My program is a single file just to check that I can init SDL: import std.stdio; import derelict.sdl2.sdl; void main(){ // Load the SDL 2 library. DerelictSDL2.load(); //init SDL ..... } The error is: Error executing command build: Failed to load package at /home/paul/D-programs/: Expected end of string after JSON value, not ': { "derelict-sdl2":"~>1.0.0", "derelict-gl3":"~master", }'. TIA
your dub.json is invalid. Please see http://code.dlang.org/package-format I'm not sure what the exact requirements are for a minimal dub.json, but you should at very least have a name. Also, watch out for where your source files are: dub expects them in a source/ or src/ folder by default, you can change this by setting sourcePaths in your dub.json
Nov 19 2014
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On 11/19/2014 6:12 PM, Paul wrote:
 I would like to create a simple program using SDL. I've read this page
 http://dblog.aldacron.net/derelict-help/using-derelict/ and this one
 http://code.dlang.org/about and decided that using 'dub' would be the
 sensible option for a beginner so I downloaded the dub executable and
 put it in the same directory as my source file 'test.d'.
It doesn't need to be in the same directory as the source. Put it somewhere on the global path.
 As per the example at the above url, I created a dub.json file in the
 same directory with contents as so:

 "dependencies": {
 "derelict-sdl2":"~>1.0.0",
 "derelict-gl3":"~master",
 }
You shouldn't be depending on ~master for anything anymore. The newest version of dub will complain about dependencies on git branches. For derelict-gl3, you can use >=1.0.0.
 If I have the relevant import statements in test.d, am I right in
 thinking I can use dub to build my program (not just download and build
 the derelict libraries)?
Yes, assuming that your dub.json is properly configured.
 The command dub build test.d gives me a 'failed
 to load package' error.
That's the right command, but the wrong parameters. If you are going to pass a parameter to the build command, it should be the name of a dub package, not the name of a file. Typically, you don't need to pass it a package name if all is properly configured. Try this: 1 -- make sure dub is on the global path. 2 -- mkdir dubtest cd dubtest dub init You should see the following output in side the dubtest dir -dub.json ---source -----app.d 3 -- Edit dub.json and add the following dependencies: "derelict-sdl2":"~>1.0.0", "derelict-gl3":">=1.0.0" 4 -- Edit app.d to look like this: import derelict.opengl3.gl3, derelict.sdl2.sdl; void main() { DerelictGL3.load(); DerelictSDL2.load(); import std.stdio : writeln; writeln( "Success!"); } 5-- Execute the following command: dub You'll see some messages about fetching and building the Derelict packages and then, assuming you have SDL2 and OpenGL on your system library search path, you should see it print "Success!". Otherwise, you'll get a DerelictException of one kind or another. Note that I didn't give dub a command option. Executing 'dub' with no options will cause it to build and run whatever package is in the current directory. By default, dub will look for source/app.d if you don't configure something differently in dub.json. Executing 'dub build' will build the executable, but will not run it.
Nov 19 2014
next sibling parent "Paul" <paul example.com> writes:
On Wednesday, 19 November 2014 at 10:07:56 UTC, Mike Parker wrote:
 On 11/19/2014 6:12 PM, Paul wrote:
 I would like to create a simple program using SDL. I've read 
 this page
 http://dblog.aldacron.net/derelict-help/using-derelict/ and 
 this one
 http://code.dlang.org/about and decided that using 'dub' would 
 be the
 sensible option for a beginner so I downloaded the dub 
 executable and
 put it in the same directory as my source file 'test.d'.
It doesn't need to be in the same directory as the source. Put it somewhere on the global path.
 As per the example at the above url, I created a dub.json file 
 in the
 same directory with contents as so:

 "dependencies": {
 "derelict-sdl2":"~>1.0.0",
 "derelict-gl3":"~master",
 }
You shouldn't be depending on ~master for anything anymore. The newest version of dub will complain about dependencies on git branches. For derelict-gl3, you can use >=1.0.0.
 If I have the relevant import statements in test.d, am I right 
 in
 thinking I can use dub to build my program (not just download 
 and build
 the derelict libraries)?
Yes, assuming that your dub.json is properly configured.
 The command dub build test.d gives me a 'failed
 to load package' error.
That's the right command, but the wrong parameters. If you are going to pass a parameter to the build command, it should be the name of a dub package, not the name of a file. Typically, you don't need to pass it a package name if all is properly configured. Try this: 1 -- make sure dub is on the global path. 2 -- mkdir dubtest cd dubtest dub init You should see the following output in side the dubtest dir -dub.json ---source -----app.d 3 -- Edit dub.json and add the following dependencies: "derelict-sdl2":"~>1.0.0", "derelict-gl3":">=1.0.0" 4 -- Edit app.d to look like this: import derelict.opengl3.gl3, derelict.sdl2.sdl; void main() { DerelictGL3.load(); DerelictSDL2.load(); import std.stdio : writeln; writeln( "Success!"); } 5-- Execute the following command: dub You'll see some messages about fetching and building the Derelict packages and then, assuming you have SDL2 and OpenGL on your system library search path, you should see it print "Success!". Otherwise, you'll get a DerelictException of one kind or another. Note that I didn't give dub a command option. Executing 'dub' with no options will cause it to build and run whatever package is in the current directory. By default, dub will look for source/app.d if you don't configure something differently in dub.json. Executing 'dub build' will build the executable, but will not run it.
Thank you Mike, that works perfectly and pretty easy when you know how - something else that is making D feel very comfortable! For anyone else reading this... app.d is in the /source directory created after executing 'dub init' and 'dub' or 'dub build' needs to be run from the project file (the directory containing the dub.json file and /source subdirectory). Both pretty obvious but just in case ;) Thanks John, your tip about dub.json noted!
Nov 19 2014
prev sibling parent reply "Paul" <paul example.com> writes:
On Wednesday, 19 November 2014 at 10:07:56 UTC, Mike Parker wrote:
 On 11/19/2014 6:12 PM, Paul wrote:
 I would like to create a simple program using SDL. I've read 
 this page
 http://dblog.aldacron.net/derelict-help/using-derelict/ and 
 this one
 http://code.dlang.org/about and decided that using 'dub' would 
 be the
 sensible option for a beginner so I downloaded the dub 
 executable and
 put it in the same directory as my source file 'test.d'.
It doesn't need to be in the same directory as the source. Put it somewhere on the global path.
 As per the example at the above url, I created a dub.json file 
 in the
 same directory with contents as so:

 "dependencies": {
 "derelict-sdl2":"~>1.0.0",
 "derelict-gl3":"~master",
 }
You shouldn't be depending on ~master for anything anymore. The newest version of dub will complain about dependencies on git branches. For derelict-gl3, you can use >=1.0.0.
 If I have the relevant import statements in test.d, am I right 
 in
 thinking I can use dub to build my program (not just download 
 and build
 the derelict libraries)?
Yes, assuming that your dub.json is properly configured.
 The command dub build test.d gives me a 'failed
 to load package' error.
That's the right command, but the wrong parameters. If you are going to pass a parameter to the build command, it should be the name of a dub package, not the name of a file. Typically, you don't need to pass it a package name if all is properly configured. Try this: 1 -- make sure dub is on the global path. 2 -- mkdir dubtest cd dubtest dub init You should see the following output in side the dubtest dir -dub.json ---source -----app.d 3 -- Edit dub.json and add the following dependencies: "derelict-sdl2":"~>1.0.0", "derelict-gl3":">=1.0.0" 4 -- Edit app.d to look like this: import derelict.opengl3.gl3, derelict.sdl2.sdl; void main() { DerelictGL3.load(); DerelictSDL2.load(); import std.stdio : writeln; writeln( "Success!"); } 5-- Execute the following command: dub You'll see some messages about fetching and building the Derelict packages and then, assuming you have SDL2 and OpenGL on your system library search path, you should see it print "Success!". Otherwise, you'll get a DerelictException of one kind or another. Note that I didn't give dub a command option. Executing 'dub' with no options will cause it to build and run whatever package is in the current directory. By default, dub will look for source/app.d if you don't configure something differently in dub.json. Executing 'dub build' will build the executable, but will not run it.
This is a tad off topic now but I'm struggling to get a window on screen as SDL_CreateWindow is failing, here's what I've got: try{ DerelictSDL2.load(); }catch(Exception e){ writeln("Failed to load DerelictSDL2 :( %s", e.msg); return; } try{ SDL_Init(SDL_INIT_EVERYTHING); }catch(Exception e){ writeln("Can't init SDL :( %s", e.msg); return; } SDL_Window *testWindow; //temporary magic numbers ahoy testWindow = SDL_CreateWindow( "Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_RESIZABLE ); if( testWindow == null ) { writeln("Window could not be created!"); return; } else { SDL_Surface* testSurface; testSurface = SDL_GetWindowSurface(testWindow); } Everything seems to initialise ok but SDL_CreateWindow always returns null. For some reason I can't use DerelictException which I guess might help me dig deeper. AFAIK there are no issues with this machine's hardware - I can certainly run other SDL-based apps on it.
Nov 20 2014
parent reply Mike Parker <aldacron gmail.com> writes:
On 11/21/2014 5:57 AM, Paul wrote:

 This is a tad off topic now but I'm struggling to get a window on screen
 as SDL_CreateWindow is failing, here's what I've got:

 try{
      DerelictSDL2.load();
 }catch(Exception e){
      writeln("Failed to load DerelictSDL2 :( %s", e.msg);
      return;
 }
Before getting to your issue, I'd like to point out that this is a needless redundancy. You are adding anything by catching an exception here and printing to the console that the library didn't load. The exception message will tell you that already. I suggest you just remove the try..catch and let the exception propagate. The only reason to catch a DerelictException here is if you want to do something other than print the message to the console, like display a message box or write to a log file.
 try{
      SDL_Init(SDL_INIT_EVERYTHING);
 }catch(Exception e){
       writeln("Can't init SDL :( %s", e.msg);
       return;
 }
SDL_Init isn't goint to throw an exception. It can't. It's a function in a C library and C doesn't know anything about D exceptions. If you look at the documentation for SDL_Init[1], you will find that it returns a negative number on failure and 0 on success. if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { // See below for how to handle this }
 SDL_Window *testWindow;
 //temporary magic numbers ahoy
 testWindow = SDL_CreateWindow( "Test Window", SDL_WINDOWPOS_UNDEFINED,
 SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_RESIZABLE );
 if( testWindow == null ) {
      writeln("Window could not be created!");
      return;
 }     else {
      SDL_Surface* testSurface;
      testSurface = SDL_GetWindowSurface(testWindow);
 }

 Everything seems to initialise ok but SDL_CreateWindow always returns
 null. For some reason I can't use DerelictException which I guess might
 help me dig deeper. AFAIK there are no issues with this machine's
 hardware - I can certainly run other SDL-based apps on it.
Again, DerelictExceptions aren't going to help you here. They are only thrown when the load function fails (to manipulate a DerelictException directly, import derelict.util.exception.) To get an error message out of SDL, you need to call SDL_GetError, which returns a const( char )*. void printSDLError( string msg ) { import std.conv : to; import std.stdio : writefln; writefln("%s: %s", msg, to!string( SDL_GetError() )); } ... if( testWindow == null ) { printSDLError( "Failed to create window." ); return; } I prefer to wrap it up in a subclass of Error. Others may prefer Exception, but when I throw an SDLError I don't intend to catch it. I really want the app to exit. class SDLError : Error { public static string errStr() property { import std.conv : to; import derelict.sdl2.sdl : SDL_GetError; return to!string( SDL_GetError() ); } public this( string msg, string file = __FILE__, size_t line = __LINE__ ) { import std.string : format; auto fmsg = format( "%s: %s", msg, errStr ); super( fmsg, file, line ); } } Now it becomes: DerelictSDL2.load(); if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { throw new SDLError( "Failed to initialize SDL" ); } testWindow = SDL_CreateWindow( ... ); if( !testWindow ) { throw new SDLError( "Failed to create window" ); } Whichever approach you choose, SDL_GetError will shed light on why any SDL call failed as long as you call it immediately upon failure. [1] https://wiki.libsdl.org/SDL_Init?highlight=%28%5CbCategoryInit%5Cb%29%7C%28CategoryEnum%29%7C%28CategoryStruct%29
Nov 20 2014
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On 11/21/2014 10:22 AM, Mike Parker wrote:

 You are adding anything
You /aren't/
Nov 20 2014
parent reply "Paul" <paul example.com> writes:
On Friday, 21 November 2014 at 01:23:55 UTC, Mike Parker wrote:
 On 11/21/2014 10:22 AM, Mike Parker wrote:

 You are adding anything
You /aren't/
The problem is 'no available video device' when trying to init SDL. I've recently wiped/re-installed this machine so something must be missing. I've googled extensively and tried all the suggestions on the SDL wiki to no avail: https://wiki.libsdl.org/FAQLinux#I_get_the_error:_.22no_video_devices_available.22 Whenever I try to learn a new language I always seem to end up fighting the OS or the IDE rather than spending time where I should. Therefore, I'm going to put this idea on hold and stick to console programs for a while (tried to install ncurses as well earlier but dub doesn't like the "~master" setting in dub.json - grrr!). Cheers Paul
Nov 22 2014
parent reply Mike Parker <aldacron gmail.com> writes:
On 11/23/2014 3:52 AM, Paul wrote:
 Whenever I try to learn a new language I always seem to end up fighting
 the OS or the IDE rather than spending time where I should. Therefore,
 I'm going to put this idea on hold and stick to console programs for a
 while (tried to install ncurses as well earlier but dub doesn't like the
 "~master" setting in dub.json - grrr!).
DUB used to support using github branches for the version, but it was deprecated not so long ago. There's a workaround for it, though, IIRC. If you search the dub forums [1], or ask about it there, you should get an answer. [1] http://forum.rejectedsoftware.com/groups/rejectedsoftware.dub/
Nov 22 2014
parent reply "Paul" <paul example.com> writes:
On Sunday, 23 November 2014 at 00:37:07 UTC, Mike Parker wrote:
 On 11/23/2014 3:52 AM, Paul wrote:
 Whenever I try to learn a new language I always seem to end up 
 fighting
 the OS or the IDE rather than spending time where I should. 
 Therefore,
 I'm going to put this idea on hold and stick to console 
 programs for a
 while (tried to install ncurses as well earlier but dub 
 doesn't like the
 "~master" setting in dub.json - grrr!).
DUB used to support using github branches for the version, but it was deprecated not so long ago. There's a workaround for it, though, IIRC. If you search the dub forums [1], or ask about it there, you should get an answer. [1] http://forum.rejectedsoftware.com/groups/rejectedsoftware.dub/
I've finally got this working - the SDL FAQ page sort of identifies the cause; the xorg dev files have to be installed before building SDL otherwise you end up with a 'non-x' version (not sure what you'd do with that!). I built SDL first and then tried installing the missing x development files.
Nov 25 2014
parent Mike Parker <aldacron gmail.com> writes:
On 11/26/2014 3:27 AM, Paul wrote:

 I've finally got this working - the SDL FAQ page sort of identifies the
 cause; the xorg dev files have to be installed before building SDL
 otherwise you end up with a 'non-x' version (not sure what you'd do with
 that!). I built SDL first and then tried installing the missing x
 development files.
Yes, the development files aren't going to help you if you've already built the project! They need to be available for the compile & link process.
Nov 25 2014
prev sibling parent "Paul" <paul example.com> writes:
On Friday, 21 November 2014 at 01:22:34 UTC, Mike Parker wrote:
 On 11/21/2014 5:57 AM, Paul wrote:

 This is a tad off topic now but I'm struggling to get a window 
 on screen
 as SDL_CreateWindow is failing, here's what I've got:

 try{
     DerelictSDL2.load();
 }catch(Exception e){
     writeln("Failed to load DerelictSDL2 :( %s", e.msg);
     return;
 }
Before getting to your issue, I'd like to point out that this is a needless redundancy. You are adding anything by catching an exception here and printing to the console that the library didn't load. The exception message will tell you that already. I suggest you just remove the try..catch and let the exception propagate. The only reason to catch a DerelictException here is if you want to do something other than print the message to the console, like display a message box or write to a log file.
 try{
     SDL_Init(SDL_INIT_EVERYTHING);
 }catch(Exception e){
      writeln("Can't init SDL :( %s", e.msg);
      return;
 }
SDL_Init isn't goint to throw an exception. It can't. It's a function in a C library and C doesn't know anything about D exceptions. If you look at the documentation for SDL_Init[1], you will find that it returns a negative number on failure and 0 on success. if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { // See below for how to handle this }
 SDL_Window *testWindow;
 //temporary magic numbers ahoy
 testWindow = SDL_CreateWindow( "Test Window", 
 SDL_WINDOWPOS_UNDEFINED,
 SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_RESIZABLE );
 if( testWindow == null ) {
     writeln("Window could not be created!");
     return;
 }     else {
     SDL_Surface* testSurface;
     testSurface = SDL_GetWindowSurface(testWindow);
 }

 Everything seems to initialise ok but SDL_CreateWindow always 
 returns
 null. For some reason I can't use DerelictException which I 
 guess might
 help me dig deeper. AFAIK there are no issues with this 
 machine's
 hardware - I can certainly run other SDL-based apps on it.
Again, DerelictExceptions aren't going to help you here. They are only thrown when the load function fails (to manipulate a DerelictException directly, import derelict.util.exception.) To get an error message out of SDL, you need to call SDL_GetError, which returns a const( char )*. void printSDLError( string msg ) { import std.conv : to; import std.stdio : writefln; writefln("%s: %s", msg, to!string( SDL_GetError() )); } ... if( testWindow == null ) { printSDLError( "Failed to create window." ); return; } I prefer to wrap it up in a subclass of Error. Others may prefer Exception, but when I throw an SDLError I don't intend to catch it. I really want the app to exit. class SDLError : Error { public static string errStr() property { import std.conv : to; import derelict.sdl2.sdl : SDL_GetError; return to!string( SDL_GetError() ); } public this( string msg, string file = __FILE__, size_t line = __LINE__ ) { import std.string : format; auto fmsg = format( "%s: %s", msg, errStr ); super( fmsg, file, line ); } } Now it becomes: DerelictSDL2.load(); if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { throw new SDLError( "Failed to initialize SDL" ); } testWindow = SDL_CreateWindow( ... ); if( !testWindow ) { throw new SDLError( "Failed to create window" ); } Whichever approach you choose, SDL_GetError will shed light on why any SDL call failed as long as you call it immediately upon failure. [1] https://wiki.libsdl.org/SDL_Init?highlight=%28%5CbCategoryInit%5Cb%29%7C%28CategoryEnum%29%7C%28CategoryStruct%29
Thank you for that extensive reply :D As you can tell I'm not very familiar with exceptions (or D!) so I need to digest that little lot. Outcome to follow... :D
Nov 20 2014