changeset 15:fa87f8897f21

Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c. Quitting via ESC now works, as does signal handling (shouldn't segfault when sent SIGINT etc.) to some degree.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 22 May 2010 18:12:37 +0300
parents cf56b64a3c11
children e3b0773ba1be
files pwplib/pwplib-unix.c pwplib/sdl.c
diffstat 2 files changed, 70 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/pwplib/pwplib-unix.c	Thu May 20 17:17:36 2010 +0300
+++ b/pwplib/pwplib-unix.c	Sat May 22 18:12:37 2010 +0300
@@ -5,6 +5,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#ifdef DRIVE_SDL
+#include <SDL.h>
+#endif
 #include <signal.h>
 #include <time.h>
 #include <sys/time.h>
@@ -12,10 +15,13 @@
 #include "pwplib.h"
 #include "tty.h"
 
-#define DESTRUCTORS 8
 
 /******************** random stuff *************************/
 
+#ifdef DRIVE_SDL
+static int sdl_init = 0;
+#endif
+
 extern void pwplib_dummy();
 #define pwp_dummy pwplib_dummy
 
@@ -33,9 +39,34 @@
 #define TIMERHZ 72
 int pwp_unix_tod()
 {
+#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
+    {
         struct timeval tod;
         gettimeofday(&tod,NULL);
         return (tod.tv_sec*TIMERHZ)+(tod.tv_usec*TIMERHZ/1000000);
+    }
 }
 
 /********************* destructors, signals etc **********/
@@ -44,6 +75,15 @@
 {
    char buf[40];
 
+   pwpwrite("* pwplib shutting down\n");
+
+#ifdef DRIVE_SDL
+   if (sdl_init) {
+      sdl_init = 0;
+      SDL_Quit();
+   }
+#endif
+
    pwplib_shutdown();
 
    sprintf(buf,"* died to signal %d\n",n);
@@ -56,23 +96,42 @@
 
 int pwplib_initcore()
 {
+   int sdl_flags = 0;
    pwplib_init_common();
 
    /*** set signals ***/
 
-   signal(SIGTERM,pwp_fatalsignal);
-   signal(SIGINT,pwp_fatalsignal);
-   signal(SIGQUIT,pwp_fatalsignal);
-   signal(SIGKILL,pwp_fatalsignal);
-   signal(SIGSEGV,pwp_fatalsignal);
+#ifdef DRIVE_SDL
+#ifdef DRIVE_VIDEO
+    sdl_flags |= SDL_INIT_VIDEO;
+#endif
+#ifdef DRIVE_AUDIO
+    sdl_flags |= SDL_INIT_AUDIO;
+#endif
+   /* With SDL, we let it handle the signals */
+    if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_EVENTTHREAD | /* SDL_INIT_NOPARACHUTE | */ sdl_flags) != 0)
+    {
+        pwpwrite("* SDL could not be initialized.\n");
+        sdl_init = 0;
+    } else
+        sdl_init = 1;
+#endif
+
+   if (!sdl_init) {
+       signal(SIGTERM,pwp_fatalsignal);
+       signal(SIGINT,pwp_fatalsignal);
+       signal(SIGQUIT,pwp_fatalsignal);
+       signal(SIGKILL,pwp_fatalsignal);
+       signal(SIGSEGV,pwp_fatalsignal);
+   }
 
    /*** video ***/
 
 #   ifdef DRIVE_VIDEO
 
 #   ifdef DRIVE_SDL
-   if(pwp_SDL_init())
-   {   
+   if(sdl_init && pwp_SDL_init())
+   {
    }
    else
 #   endif
@@ -93,7 +152,7 @@
    {
       int snd=0;
 #ifdef DRIVE_SDL
-        if(!snd) { if(pwp_sdlaudio_init())snd++; }
+        if(!snd) { if(sdl_init && pwp_sdlaudio_init())snd++; }
 #endif
 
 #ifdef DRIVE_OSS
--- a/pwplib/sdl.c	Thu May 20 17:17:36 2010 +0300
+++ b/pwplib/sdl.c	Sat May 22 18:12:37 2010 +0300
@@ -95,11 +95,6 @@
     *b = (muller * (rgb[bg * 3 + 2] * 2 + rgb[fg * 3 + 2])) / 45;
 }
 
-void pwp_SDL_close(void)
-{
-    SDL_Quit();
-}
-
 int pwp_SDL_init(void)
 {
     int pal[PWP_NCOLORS * 3], i;
@@ -114,34 +109,19 @@
     pwplib.videobuf.height = pwp_SDL.height / 8;
     pwplib.dump_rast = pwp_SDL_dump_rast_4x4;
 
-    /* NOTICE! Here we assume that SDL audio will be used also .. this is kind of a hack,
-     * because obviously we cannot know what audio subsystem is used at this point.
-     * (Blame oversimplicity of pwplib)
-     * -- ccr
-     */
-    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTTHREAD | SDL_INIT_AUDIO) != 0)
-    {
-        pwpwrite("* SDL could not be initialized.\n");
-        return 0;
-    }
-
-    pwp_regdestr(pwp_SDL_close);
-
     /* Initialize a indexed / paletted video surface */
     pwp_SDL.screen = SDL_SetVideoMode(pwp_SDL.width, pwp_SDL.height, 8, pwp_SDL.vflags);
     if (pwp_SDL.screen == NULL)
     {
-        SDL_Quit();
         pwpwrite("* SDL could not initialize video surface/mode.\n");
         return 0;
     }
-
+    
     fprintf(stderr, "\nSDL_surface: %d x %d, pitch=%d\n", pwp_SDL.screen->w, pwp_SDL.screen->h, pwp_SDL.screen->pitch);
     
     /* Set window caption, if any */
     SDL_WM_SetCaption(PWP_WINNAME, PWP_WINNAME);
 
-
     /* Generate palette and rasterization lookup table */
     for (i = 0; i < PWP_NCOLORS; i++)
     {
@@ -154,7 +134,7 @@
     }
     SDL_SetColors(pwp_SDL.screen, pwp_SDL.cols, 0, PWP_NCOLORS);
 
-    pwpwrite("* SDL\n");
+    pwpwrite("* SDL video\n");
 
     return 1;
 }