view mde/SDL.d @ 22:249eb6620685

Changes to Options, particularly regarding window sizes. Window sizes for fullscreen and windowed modes are now independant. Window resizes by the WM are now persistant. Options class contents are now generated by templates. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Tue, 25 Mar 2008 12:24:04 +0000
parents a60cbb7359dd
children 47478557428d
line wrap: on
line source

/* LICENSE BLOCK
Part of mde: a Modular D game-oriented Engine
Copyright © 2007-2008 Diggory Hardy

This program is free software; you can redistribute it and/or modify it under the terms of
the GNU General Public License, version 2, as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

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

import mde.scheduler.InitStage;
import mde.input.joystick;
import mde.options;

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

import derelict.sdl.sdl;
import derelict.opengl.gl;
import derelict.util.exception;

Logger logger;
static this() {
    logger = Log.getLogger ("mde.SDL");
    
    init2.addFunc (&initSdlAndGl);
    init4.addFunc (&setupWindow);
}

private uint flags = 0;

void initSdlAndGl() {   // init2 func
    logger.trace ("init2: initSdlAndGl() started");
    
    // Load SDL and GL dynamic libs
    try {
        DerelictSDL.load();
        DerelictGL.load();
    } catch (DerelictException de) {
        logger.fatal ("Loading dynamic library failed:");
        logger.fatal (de.msg);
        
        setInitFailure ();
        return;
    }
    logger.trace ("Derelict: loaded SDL and OpenGL");
    
    // Initialise 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");
        
        setInitFailure ();
        return;
    }
    
    cleanup2.addFunc (&cleanupSDL);
    logger.trace ("SDL initialised");
    
    // Must be called after SDL has been initialised, so cannot be a separate Init function.
    openJoysticks ();                   // after SDL init
    cleanup2.addFunc (&closeJoysticks);

    logger.trace ("init2: initSdlAndGl() finished");
}

void setupWindow() {    // init4 func
    logger.trace ("init4: setupWindow() started");
    
    // Window creation flags
    /* NOTE: I'm getting an API mismatch error from the nvidia driver when using OpenGL,
    * thus I've temporarily disabled it. */
    version (MDE_OPENGL) flags = SDL_OPENGL;
    if (Options.video.fullscreen) flags |= SDL_FULLSCREEN;
    else {
        if (Options.video.resizable) flags |= SDL_RESIZABLE;
        if (Options.video.noFrame) flags |= SDL_NOFRAME;
    }
    if (Options.video.hardware) flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
    else flags |= SDL_SWSURFACE;
    
    version (MDE_OPENGL) {
    // OpenGL attributes
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,    8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,  8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,   8);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
    }
    
    // Open a window
    SDL_Surface* surface;
    if (Options.video.fullscreen) {
        surface = SDL_SetVideoMode (Options.video.screenW, Options.video.screenH, 32, flags);
    } else {
        surface = SDL_SetVideoMode (Options.video.windowW, Options.video.windowH, 32, flags);
    }
    if (surface is null) {
        logger.fatal ("Unable to set video mode:");
        char* msg = SDL_GetError ();
        logger.fatal (msg ? fromStringz(msg) : "no reason available");
        
        setInitFailure ();
        return;
    }
    
    // Window-manager settings
    SDL_WM_SetCaption (toStringz ("mde"), null);
    // SDL_WM_GrabInput (use later)
    
    logger.trace ("init4: setupWindow() finished");
}

void resizeWindow (int w, int h) {
    if (Options.video.fullscreen) {
        Options.video.screenW = w;
        Options.video.screenH = h;
    } else {
        Options.video.windowW = w;
        Options.video.windowH = h;
    }
    
    /+ Does SDL_SetVideoMode need to be called?
    setupWindow ();
    +/
}

void cleanupSDL () {    // cleanup2 func
    logger.trace ("cleanup2: cleanupSDL() started");
    SDL_Quit();
    logger.trace ("cleanup2: cleanupSDL() finished");
}

    /+ Load of info-printing stuff (currently doesn't have a use)
    // 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));
    }
    }
    +/