view mde/SDL.d @ 16:9cb7b9310168

Improvements to Options and Init. Revamped Options with sections and auto saving/loading. Moved some of init's functions outside the module. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 15 Mar 2008 11:56:13 +0000
parents
children 5f90774ea1ef
line wrap: on
line source

/** Just a temporary place to put SDL Init and Video stuff.
*/
module mde.SDL;

import mde.Init;
import mde.input.joystick;

import tango.util.log.Log : Log, Logger;

import derelict.sdl.sdl;
import derelict.util.exception;

Logger logger;
static this() {
    logger = Log.getLogger ("mde.init.SDL");
    
    Init.addFunc (&initSDL);
}

void initSDL() {
            
    // Inits SDL and related stuff (joystick).
    try {
        DerelictSDL.load();
    } catch (DerelictException de) {
        logger.fatal ("Loading dynamic library failed:");
        logger.fatal (de.msg);
                
        init.setFailure ();		// abort
        return;
    }
    logger.trace ("Derelict: loaded SDL");
            
    if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK /+| SDL_INIT_EVENTTHREAD+/)) {
        logger.fatal ("SDL initialisation failed:");
        char* msg = SDL_GetError ();
        logger.fatal (msg ? fromStringz(msg) : "no reason available");
                
        init.setFailure ();		// abort
        return;
    }
            
    init.addCleanupFct (&cleanupSDL);
    logger.trace ("SDL initialised");
            
    /+ Load of info-printing stuff
    // Print a load of info:
    logger.info ("Available video modes:");
    char[128] tmp;
    SDL_Rect** modes = SDL_ListModes (null, SDL_FULLSCREEN);
    if (modes is null) logger.info ("None!");
    else if (modes is cast(SDL_Rect**) -1) logger.info ("All modes are available");
    else {
    for (uint i = 0; modes[i] !is null; ++i) {
    logger.info (logger.format (tmp, "\t{}x{}", modes[i].w, modes[i].h));
    }
    }
            
    SDL_VideoInfo* vi = SDL_GetVideoInfo ();
    if (vi !is null) {
    logger.info ("Video info:");
    logger.info ("Hardware surface support: "~ (vi.flags & SDL_HWSURFACE ? "yes" : "no"));
    logger.info (logger.format (tmp, "Video memory: {}", vi.video_mem));
                
    if (vi.vfmt !is null) {
    logger.info ("Best video mode:");
    logger.info (logger.format (tmp, "Bits per pixel: {}", vi.vfmt.BitsPerPixel));
    }
    }
    +/
            
    // FIXME: make this non-fatal and provide a way to re-set video mode
    if (SDL_SetVideoMode (800, 600, 0, 0) is null) {   // Can't open in windows!!
        logger.fatal ("Unable to set video mode:");
        char* msg = SDL_GetError ();
        logger.fatal (msg ? fromStringz(msg) : "no reason available");
                
        init.setFailure ();
        return;
    }
            
    openJoysticks ();                        // after SDL init
    init.addCleanupFct (&closeJoysticks);
}

void cleanupSDL () {
    SDL_Quit();
}