view pwplib/pwplib-win.c @ 56:5d819ba6891c

More cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 27 May 2010 22:48:37 +0300
parents 85671798fdb3
children
line wrap: on
line source

#define __PWPLIB_C

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include <windows.h>

#ifdef DRIVE_SDL
#include <SDL.h>
#endif

#include "pwplib.h"
#include "sound.h"
#include "video.h"

static int sdl_init = 0;

/******************** random stuff *************************/

extern void pwplib_dummy();
#define pwp_dummy pwplib_dummy

void pwpwrite(const char *fmt, ...)
{
    if (!pwplib.setup[SETUP_SHUTUP]) {
        va_list ap;
        va_start(ap, fmt);
        vfprintf(stderr, fmt, ap);
        va_end(ap);

        usleep(1000 * pwplib.set.infodelay);
    }
}

/********************* timer  ******************************/

#define TIMERHZ 72
int pwp_w32_ticks()
{
#ifdef DRIVE_SDL
    if (sdl_init) {
        int quit = 0;
        SDL_Event event;
        if (SDL_PollEvent(&event) >= 0) {
            switch (event.type) {
              case SDL_KEYDOWN:
                  if (event.key.keysym.sym == SDLK_ESCAPE)
                      quit = 1;
                  break;
              case SDL_QUIT:
                  quit = 1;
                  break;
            }
        }
        if (quit) {
            SDL_Quit();
            pwplib_shutdown();
            exit(1);
        }
        return (SDL_GetTicks() * TIMERHZ) / 1000;
    }
    else
#endif
    {
        int ticks = GetTickCount();
        return ((ticks*TIMERHZ)/1000);
        // (10000000/10441));
    }
}

/******************** initialization *********************/

int pwplib_initcore()
{
    int sdl_flags = 0;

    pwplib_init_common();

#ifdef DRIVE_SDL
    if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
        pwpwrite("* SDL could not be initialized (%s).\n", SDL_GetError());
        sdl_init = 0;
    } else
        sdl_init = 1;

    if (sdl_init && !pwp_SDL_init())
    {
        sdl_init = 0;
        SDL_Quit();
    }
    if (sdl_init && !pwp_SDL_audio_init())
    {
        sdl_init = 0;
        SDL_Quit();
    }
#endif

    if (!sdl_init) {
#ifdef DRIVE_WIN32
        win32con_init();
        win32snd_init();
        if (pwplib.dump_rast == pwplib_dummy && pwplib.dump_attr != pwplib_dummy)
            pwplib.dump_rast = pwplib_dump_rast_plain;
#else
        return 0;
#endif
    }

    pwplib.timerfunc = pwp_w32_ticks;

    return 1;
}

/*********************************************************************/

void pwplib_startup()
{
  /* just dummy here */
}

void pwplib_end()
{
  pwplib_shutdown();

  /* write some stuph */
}

#ifdef __WIN32
int usleep(int t)
{
    Sleep(t / 1000);
}
#endif

/***************************************************************/

extern void pwplib_getopts();

int pwplib_init(int argc,char**argv)
{
    pwplib.argc=argc;
    pwplib.argv=argv;

    pwplib_getopts();

    if (pwplib.setup[SETUP_WANTHELP])
    {
        pwplib_printhelp();
        exit(0);
    }

    if (!pwplib_initcore())
    {
        pwpwrite("initialization failed!\n");
        return 0;
    }

    if (pwplib.setup[SETUP_WANTHELP])
        return 0;

    return 1;
}