shithub: choc

Download patch

ref: e4aa75977e5a2e7ace5e7742193cfef427adaa77
parent: 71d013a435a6801e82279acb0678a30b526d91ad
author: Simon Howard <fraggle@gmail.com>
date: Sun Nov 9 11:39:12 EST 2008

Recreate Hexen graphical startup in SDL.

Subversion-branch: /branches/raven-branch
Subversion-revision: 1380

--- a/HH-TODO
+++ b/HH-TODO
@@ -13,7 +13,6 @@
  * Remove NeXT and WATCOMC defines
  * Structure packing macros for structures read from disk
  * Merge r_draw.c to common version and delete duplicate
- * Recreate text mode startup using textscreen code.
  * HHE patch support
  * v1.2 emulation (if possible)
  * Screensaver mode
@@ -27,7 +26,6 @@
  * Remove NeXT and WATCOMC defines
  * Structure packing macros for structures read from disk
  * Merge r_draw.c to common version and delete duplicate
- * Recreate graphical startup.
  * v1.0 emulation (if possible/necessary)
  * Screensaver mode
 
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -46,6 +46,7 @@
 i_system.c           i_system.h            \
 i_timer.c            i_timer.h             \
 i_video.c            i_video.h             \
+i_videohr.c          i_videohr.h           \
 m_argv.c             m_argv.h              \
 m_bbox.c             m_bbox.h              \
 m_cheat.c            m_cheat.h             \
--- a/src/hexen/h2_main.c
+++ b/src/hexen/h2_main.c
@@ -159,6 +159,7 @@
     M_BindHereticControls();
     M_BindHexenControls();
 
+    M_BindVariable("graphical_startup",      &graphical_startup);
     M_BindVariable("mouse_sensitivity",      &mouseSensitivity);
     M_BindVariable("sfx_volume",             &snd_MaxVolume);
     M_BindVariable("music_volume",           &snd_MusicVolume);
@@ -279,6 +280,8 @@
 
     HandleArgs();
 
+    I_PrintStartupBanner("Hexen");
+
     ST_Message("MN_Init: Init menu system.\n");
     MN_Init();
 
@@ -301,8 +304,6 @@
     ST_Message("ST_Init: Init startup screen.\n");
     ST_Init();
 
-    S_StartSongName("orb", true);
-
     // Show version message now, so it's visible during R_Init()
     ST_Message("R_Init: Init Hexen refresh daemon");
     R_Init();
@@ -330,6 +331,8 @@
     ST_Message("SB_Init: Loading patches.\n");
     SB_Init();
 
+    ST_Done();
+
     CheckRecordFrom();
 
     p = M_CheckParm("-record");
@@ -490,9 +493,15 @@
 {
     char file[256];
 
-    sprintf(file, "%s.lmp", args[1]);
+    strcpy(file, args[1]);
+
+    if (strcasecmp(file + strlen(file) - 4, ".lmp") != 0)
+    {
+        strcat(file, ".lmp");
+    }
+
     W_AddFile(file);
-    ST_Message("Playing demo %s.lmp.\n", args[1]);
+    ST_Message("Playing demo %s.\n", file);
 }
 
 //==========================================================================
@@ -820,6 +829,7 @@
     G_LoadGame(atoi(myargv[p + 1]));
     G_DoLoadGame();             // Load the gameskill etc info from savegame
     G_RecordDemo(gameskill, 1, gameepisode, gamemap, myargv[p + 2]);
+
     H2_GameLoop();              // Never returns
 }
 
--- a/src/hexen/st_start.c
+++ b/src/hexen/st_start.c
@@ -25,16 +25,14 @@
 
 
 // HEADER FILES ------------------------------------------------------------
-#include <ctype.h>
-#include <fcntl.h>
+
 #include <stdarg.h>
 
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
+#include "config.h"
 
 #include "h2def.h"
 #include "i_system.h"
+#include "i_videohr.h"
 #include "s_sound.h"
 #include "st_start.h"
 
@@ -52,36 +50,16 @@
 #define ST_NETNOTCH_HEIGHT		16
 #define ST_MAX_NETNOTCHES		8
 
-// TYPES -------------------------------------------------------------------
-
-// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
-extern void SetVideoModeHR(void);
-extern void ClearScreenHR(void);
-extern void SlamHR(char *buffer);
-extern void SlamBlockHR(int x, int y, int w, int h, char *src);
-extern void InitPaletteHR(void);
-extern void SetPaletteHR(byte * palette);
-extern void GetPaletteHR(byte * palette);
-extern void FadeToPaletteHR(byte * palette);
-extern void FadeToBlackHR(void);
-extern void BlackPaletteHR(void);
-extern void I_StartupReadKeys(void);
-
-// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
-
-// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
-char *ST_LoadScreen(void);
+byte *ST_LoadScreen(void);
 void ST_UpdateNotches(int notchPosition);
 void ST_UpdateNetNotches(int notchPosition);
 
-// EXTERNAL DATA DECLARATIONS ----------------------------------------------
-
-// PUBLIC DATA DEFINITIONS -------------------------------------------------
-
 // PRIVATE DATA DEFINITIONS ------------------------------------------------
-char *bitmap = NULL;
+static const byte *bitmap = NULL;
+int graphical_startup = 1;
+static boolean using_graphical_startup;
 
-char notchTable[] = {
+static const byte notchTable[] = {
     // plane 0
     0x00, 0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40,
     0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x03, 0xC0,
@@ -109,7 +87,7 @@
 
 
 // Red Network Progress notches
-char netnotchTable[] = {
+static const byte netnotchTable[] = {
     // plane 0
     0x80, 0x50, 0xD0, 0xf0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xD0, 0xF0, 0xC0,
     0x70, 0x50, 0x80, 0x60,
@@ -146,36 +124,44 @@
 
 void ST_Init(void)
 {
-#ifdef __WATCOMC__
-    char *pal;
-    char *buffer;
+    byte *pal;
+    byte *buffer;
+    
+    using_graphical_startup = false;
 
-    if (!debugmode)
+    if (graphical_startup && !debugmode)
     {
         // Set 640x480x16 mode
-        SetVideoModeHR();
-        ClearScreenHR();
-        InitPaletteHR();
-        BlackPaletteHR();
+        if (I_SetVideoModeHR())
+        {
+            I_SetWindowTitleHR("Hexen startup - " PACKAGE_STRING);
+            using_graphical_startup = true;
 
-        // Load graphic
-        buffer = ST_LoadScreen();
-        pal = buffer;
-        bitmap = buffer + 16 * 3;
+            S_StartSongName("orb", true);
 
-        SlamHR(bitmap);
-        FadeToPaletteHR(pal);
-        Z_Free(buffer);
+            I_ClearScreenHR();
+            I_InitPaletteHR();
+            I_BlackPaletteHR();
+
+            // Load graphic
+            buffer = ST_LoadScreen();
+            pal = buffer;
+            bitmap = buffer + 16 * 3;
+
+            I_SlamHR(bitmap);
+            I_FadeToPaletteHR(pal);
+            Z_Free(buffer);
+        }
     }
-#endif
 }
 
-
 void ST_Done(void)
 {
-#ifdef __WATCOMC__
-    ClearScreenHR();
-#endif
+    if (using_graphical_startup)
+    {
+        I_ClearScreenHR();
+        I_UnsetVideoModeHR();
+    }
 }
 
 
@@ -187,11 +173,9 @@
 
 void ST_UpdateNotches(int notchPosition)
 {
-#ifdef __WATCOMC__
     int x = ST_PROGRESS_X + notchPosition * ST_NOTCH_WIDTH;
     int y = ST_PROGRESS_Y;
-    SlamBlockHR(x, y, ST_NOTCH_WIDTH, ST_NOTCH_HEIGHT, notchTable);
-#endif
+    I_SlamBlockHR(x, y, ST_NOTCH_WIDTH, ST_NOTCH_HEIGHT, notchTable);
 }
 
 
@@ -203,11 +187,9 @@
 
 void ST_UpdateNetNotches(int notchPosition)
 {
-#ifdef __WATCOMC__
     int x = ST_NETPROGRESS_X + notchPosition * ST_NETNOTCH_WIDTH;
     int y = ST_NETPROGRESS_Y;
-    SlamBlockHR(x, y, ST_NETNOTCH_WIDTH, ST_NETNOTCH_HEIGHT, netnotchTable);
-#endif
+    I_SlamBlockHR(x, y, ST_NETNOTCH_WIDTH, ST_NETNOTCH_HEIGHT, netnotchTable);
 }
 
 
@@ -220,27 +202,24 @@
 void ST_Progress(void)
 {
 #ifdef __WATCOMC__
-    static int notchPosition = 0;
-
     // Check for ESC press -- during startup all events eaten here
     I_StartupReadKeys();
+#endif
 
-    if (debugmode)
+    if (using_graphical_startup)
     {
-        printf(".");
-    }
-    else
-    {
+        static int notchPosition = 0;
+
         if (notchPosition < ST_MAX_NOTCHES)
         {
             ST_UpdateNotches(notchPosition);
             S_StartSound(NULL, SFX_STARTUP_TICK);
+            //I_Sleep(1000);
             notchPosition++;
         }
     }
-#else
+
     printf(".");
-#endif
 }
 
 
@@ -252,14 +231,12 @@
 
 void ST_NetProgress(void)
 {
-#ifdef __WATCOMC__
-    static int netnotchPosition = 0;
-    if (debugmode)
+    printf("*");
+
+    if (using_graphical_startup)
     {
-        printf("*");
-    }
-    else
-    {
+        static int netnotchPosition = 0;
+
         if (netnotchPosition < ST_MAX_NETNOTCHES)
         {
             ST_UpdateNetNotches(netnotchPosition);
@@ -267,7 +244,6 @@
             netnotchPosition++;
         }
     }
-#endif
 }
 
 
@@ -278,7 +254,10 @@
 //==========================================================================
 void ST_NetDone(void)
 {
-    S_StartSound(NULL, SFX_PICKUP_WEAPON);
+    if (using_graphical_startup)
+    {
+        S_StartSound(NULL, SFX_PICKUP_WEAPON);
+    }
 }
 
 
@@ -344,14 +323,15 @@
 //==========================================================================
 
 
-char *ST_LoadScreen(void)
+byte *ST_LoadScreen(void)
 {
     int length, lump;
-    char *buffer;
+    byte *buffer;
 
     lump = W_GetNumForName("STARTUP");
     length = W_LumpLength(lump);
-    buffer = (char *) Z_Malloc(length, PU_STATIC, NULL);
+    buffer = (byte *) Z_Malloc(length, PU_STATIC, NULL);
     W_ReadLump(lump, buffer);
     return (buffer);
 }
+
--- a/src/hexen/st_start.h
+++ b/src/hexen/st_start.h
@@ -39,3 +39,6 @@
 extern void ST_NetDone(void);
 
 // PUBLIC DATA DECLARATIONS ------------------------------------------------
+ 
+extern int graphical_startup;
+
--- /dev/null
+++ b/src/i_videohr.c
@@ -1,0 +1,217 @@
+// Emacs style mode select   -*- C++ -*- 
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2008 Simon Howard
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// 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., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+//
+// DESCRIPTION:
+//     SDL emulation of VGA 640x480x4 planar video mode,
+//     for Hexen startup loading screen.
+//
+//-----------------------------------------------------------------------------
+
+#include "SDL.h"
+
+#include "doomtype.h"
+#include "i_timer.h"
+
+// Palette fade-in takes two seconds
+
+#define FADE_TIME 2000
+
+#define HR_SCREENWIDTH 640
+#define HR_SCREENHEIGHT 480
+
+static SDL_Surface *hr_surface = NULL;
+
+boolean I_SetVideoModeHR(void)
+{
+    Uint32 flags;
+
+    if (SDL_Init(SDL_INIT_VIDEO) < 0)
+    {
+        return false;
+    }
+
+    flags = SDL_SWSURFACE;
+
+    hr_surface = SDL_SetVideoMode(HR_SCREENWIDTH, HR_SCREENHEIGHT, 8, flags);
+
+    if (hr_surface == NULL)
+    {
+        SDL_QuitSubSystem(SDL_INIT_VIDEO);
+        return false;
+    }
+
+    return true;
+}
+
+void I_SetWindowTitleHR(char *title)
+{
+    SDL_WM_SetCaption(title,  NULL);
+}
+
+void I_UnsetVideoModeHR(void)
+{
+    if (hr_surface != NULL)
+    {
+        SDL_QuitSubSystem(SDL_INIT_VIDEO);
+        hr_surface = NULL;
+    }
+}
+
+void I_ClearScreenHR(void)
+{
+    SDL_Rect area = { 0, 0, HR_SCREENWIDTH, HR_SCREENHEIGHT };
+
+    SDL_FillRect(hr_surface, &area, 0);
+}
+
+void I_SlamBlockHR(int x, int y, int w, int h, const byte *src)
+{
+    const byte *srcptrs[4];
+    byte srcbits[4];
+    byte *dest;
+    int x1, y1;
+    int i;
+    int bit;
+
+    // Set up source pointers to read from source buffer - each 4-bit 
+    // pixel has its bits split into four sub-buffers
+
+    for (i=0; i<4; ++i)
+    {
+        srcptrs[i] = src + (i * w * h / 8);
+    }
+
+    if (SDL_LockSurface(hr_surface) < 0)
+    {
+        return;
+    }
+
+    // Draw each pixel
+
+    bit = 0;
+
+    for (y1=y; y1<y+h; ++y1)
+    {
+        dest = ((byte *) hr_surface->pixels) + y1 * hr_surface->pitch + x;
+
+        for (x1=x; x1<x+w; ++x1)
+        {
+            // Get the bits for this pixel
+            // For each bit, find the byte containing it, shift down
+            // and mask out the specific bit wanted.
+
+            for (i=0; i<4; ++i)
+            {
+                srcbits[i] = (srcptrs[i][bit / 8] >> (7 - (bit % 8))) & 0x1;
+            }
+
+            // Reassemble the pixel value
+
+            *dest = (srcbits[0] << 0) 
+                  | (srcbits[1] << 1)
+                  | (srcbits[2] << 2)
+                  | (srcbits[3] << 3);
+
+            // Next pixel!
+
+            ++dest;
+            ++bit;
+        }
+    }
+
+    SDL_UnlockSurface(hr_surface);
+
+    // Update the region we drew.
+
+    //SDL_UpdateRect(hr_surface, x, y, w, h);
+    SDL_Flip(hr_surface);
+}
+
+void I_SlamHR(const byte *buffer)
+{
+    I_SlamBlockHR(0, 0, HR_SCREENWIDTH, HR_SCREENHEIGHT, buffer);
+}
+
+void I_InitPaletteHR(void)
+{
+    // ...
+}
+
+void I_SetPaletteHR(const byte *palette)
+{
+    SDL_Color sdlpal[16];
+    int i;
+
+    for (i=0; i<16; ++i)
+    {
+        sdlpal[i].r = palette[i * 3 + 0] * 4;
+        sdlpal[i].g = palette[i * 3 + 1] * 4;
+        sdlpal[i].b = palette[i * 3 + 2] * 4;
+    }
+
+    SDL_SetColors(hr_surface, sdlpal, 0, 16);
+}
+
+void I_FadeToPaletteHR(const byte *palette)
+{
+    byte tmppal[16 * 3];
+    int starttime;
+    int elapsed;
+    int i;
+
+    starttime = I_GetTimeMS();
+
+    for (;;)
+    {
+        elapsed = I_GetTimeMS() - starttime;
+
+        if (elapsed >= FADE_TIME)
+        {
+            break;
+        }
+
+        // Generate the fake palette
+
+        for (i=0; i<16 * 3; ++i) 
+        {
+            tmppal[i] = (palette[i] * elapsed) / FADE_TIME;
+        }
+
+        I_SetPaletteHR(tmppal);
+
+        // Sleep a bit
+
+        I_Sleep(10);
+    }
+
+    // Set the final palette
+
+    I_SetPaletteHR(palette);
+}
+
+void I_BlackPaletteHR(void)
+{
+    byte blackpal[16 * 3];
+
+    memset(blackpal, 0, sizeof(blackpal));
+
+    I_SetPaletteHR(blackpal);
+}
+
--- /dev/null
+++ b/src/i_videohr.h
@@ -1,0 +1,42 @@
+// Emacs style mode select   -*- C++ -*- 
+//-----------------------------------------------------------------------------
+//
+// Copyright(C) 2008 Simon Howard
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// 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., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+//
+// DESCRIPTION:
+//     SDL emulation of VGA 640x480x4 planar video mode,
+//     for Hexen startup loading screen.
+//
+//-----------------------------------------------------------------------------
+
+#ifndef I_VIDEOHR_H
+#define I_VIDEOHR_H
+
+boolean I_SetVideoModeHR(void);
+void I_UnsetVideoModeHR(void);
+void I_SetWindowTitleHR(char *title);
+void I_ClearScreenHR(void);
+void I_SlamBlockHR(int x, int y, int w, int h, const byte *src);
+void I_SlamHR(const byte *buffer);
+void I_InitPaletteHR(void);
+void I_SetPaletteHR(const byte *palette);
+void I_FadeToPaletteHR(const byte *palette);
+void I_BlackPaletteHR(void);
+
+#endif /* #ifndef I_VIDEOHR_H */
+