November 21, 2014 Re: Dub / Derelict confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On 11/21/2014 10:22 AM, Mike Parker wrote:
> You are adding anything
You /aren't/
|
November 21, 2014 Re: Dub / Derelict confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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
|
November 22, 2014 Re: Dub / Derelict confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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 |
November 23, 2014 Re: Dub / Derelict confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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/ |
November 25, 2014 Re: Dub / Derelict confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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.
|
November 26, 2014 Re: Dub / Derelict confusion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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.
|
Copyright © 1999-2021 by the D Language Foundation