changeset 42:5714d6494163

Add more configuration options via commandline.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 19 Mar 2013 05:07:40 +0200
parents bac75122a38e
children 8a7589da1bec
files 3x666.c config.h
diffstat 2 files changed, 100 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/3x666.c	Tue Mar 19 04:51:44 2013 +0200
+++ b/3x666.c	Tue Mar 19 05:07:40 2013 +0200
@@ -32,7 +32,7 @@
     int mixFill, mixBufSize;
     Sint32 *mixBuf;
 
-    int optVFlags;
+    int optScrWidth, optScrHeight, optVFlags;
     SDL_Surface *screen;
     SDL_Event event;
     SDL_AudioSpec optAfmt;
@@ -111,7 +111,7 @@
 {
     SDL_FreeSurface(engine.screen);
 
-    engine.screen = SDL_SetVideoMode(SET_VID_BUFW, SET_VID_BUFH, 8, engine.optVFlags);
+    engine.screen = SDL_SetVideoMode(engine.optScrWidth, engine.optScrHeight, 8, engine.optVFlags);
     if (engine.screen == NULL)
     {
         dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError());
@@ -132,15 +132,15 @@
 void txtDrawSegment(int y, int x, int w, int h)
 {
     /* clip clip clip */
-    if (x + w > SET_VID_BUFW)
-        w = SET_VID_BUFW - x;
+    if (x + w > engine.optScrWidth)
+        w = engine.optScrWidth - x;
     if (x < 0)
     {
         w += x;
         x = 0;
     }
-    if (y + h > SET_VID_BUFH)
-        h = SET_VID_BUFH - y;
+    if (y + h > engine.optScrHeight)
+        h = engine.optScrHeight - y;
     if (y < 0)
     {
         h += y;
@@ -190,18 +190,18 @@
 
 void drawtxtscr(char *str)
 {
-    int x = SET_VID_BUFW >> 4, y = SET_VID_BUFH >> 3;
+    int x = engine.optScrWidth >> 4, y = engine.optScrHeight >> 3;
     while (*str)
     {
         if (*str >= 32)
         {
-            txtDrawChar(x, y, phont[*str - 32], SET_VID_BUFW / 50, SET_VID_BUFW / 80);
-            x += SET_VID_BUFW / 10;
+            txtDrawChar(x, y, phont[*str - 32], engine.optScrWidth / 50, engine.optScrWidth / 80);
+            x += engine.optScrWidth / 10;
         }
         else
         {
-            x = SET_VID_BUFW >> 4;
-            y += SET_VID_BUFW / 10;
+            x = engine.optScrWidth >> 4;
+            y += engine.optScrWidth / 10;
         }
         str++;
     }
@@ -210,11 +210,11 @@
 
 void flashtxt(char *str)
 {
-    int x = (SET_VID_BUFW >> 1) - (strlen(str) + 1) * 3 * SET_VID_BUFW / 80;
+    int x = (engine.optScrWidth >> 1) - (strlen(str) + 1) * 3 * engine.optScrWidth / 80;
     while (*str)
     {
-        txtDrawChar(x, SET_VID_BUFH >> 1, phont[*str++ - 32], SET_VID_BUFW / 50, SET_VID_BUFW / 80);
-        x += SET_VID_BUFW / 10;
+        txtDrawChar(x, engine.optScrHeight >> 1, phont[*str++ - 32], engine.optScrWidth / 50, engine.optScrWidth / 80);
+        x += engine.optScrWidth / 10;
     }
 }
 
@@ -246,12 +246,6 @@
 
 ***/
 
-#if (SET_VID_BUFH < SET_VID_BUFW)
-#    define maxR (SET_VID_BUFH >> 1)
-#else
-#    define maxR (SET_VID_BUFW >> 1)
-#endif
-
 struct
 {
     int *tab;
@@ -260,6 +254,8 @@
 
 void preball()
 {
+    int maxR = (engine.optScrHeight < engine.optScrWidth) ?
+        (engine.optScrHeight >> 1) : (engine.optScrWidth >> 1);
     unsigned int rR;
     signed int R;
 
@@ -299,7 +295,7 @@
     signed int doty = (-(R - 1) * l->y);
     signed int y = R * 2 - 2;
 
-    Uint8 *dp = screen->pixels + (SET_VID_BUFW >> 1) - R + ((SET_VID_BUFH >> 1) - R) * screen->pitch;
+    Uint8 *dp = screen->pixels + (engine.optScrWidth >> 1) - R + ((engine.optScrHeight >> 1) - R) * screen->pitch;
 
     for (; y; y--)
     {
@@ -321,14 +317,14 @@
 
 void drawplasma(char *d,float t)
 {
-	int y=SET_VID_BUFH; float u=0,du=500/SET_VID_BUFH;
+	int y=engine.optScrHeight; float u=0,du=500/engine.optScrHeight;
 
 	for(;y;y--){
 		drawball_inloop(d,
 			sin(t*0.02+0+u*0.033)*65536*256,
-			cos(t*0.04+1+u*0.022)*65536*4096/SET_VID_BUFW,
-			-2*cos(t*0.04+1+u*0.022)*65536*4096/(SET_VID_BUFW*SET_VID_BUFW), SET_VID_BUFW);
-		d+=SET_VID_BUFW;
+			cos(t*0.04+1+u*0.022)*65536*4096/engine.optScrWidth,
+			-2*cos(t*0.04+1+u*0.022)*65536*4096/(engine.optScrWidth*engine.optScrWidth), engine.optScrWidth);
+		d+=engine.optScrWidth;
 		u+=du;
 	}
 }
@@ -338,17 +334,17 @@
 
 void rotochess(SDL_Surface *screen, int du, int dv, int iu, int iv)
 {
-    int hu = iu - (dv * (SET_VID_BUFH >> 1)) - (du * (SET_VID_BUFW >> 1)),
-        hv = iv + (du * (SET_VID_BUFH >> 1)) - (dv * (SET_VID_BUFW >> 1));
+    int hu = iu - (dv * (engine.optScrHeight >> 1)) - (du * (engine.optScrWidth >> 1)),
+        hv = iv + (du * (engine.optScrHeight >> 1)) - (dv * (engine.optScrWidth >> 1));
 
     int y;
     Uint8 *dp = screen->pixels;
 
-    for (y = SET_VID_BUFH; y; y--)
+    for (y = engine.optScrHeight; y; y--)
     {
         Uint8 *d = dp;
         int u = hu, v = hv, x;
-        for (x = SET_VID_BUFW; x; x--)
+        for (x = engine.optScrWidth; x; x--)
         {
             u += du;
             v += dv;
@@ -638,14 +634,14 @@
 void plainscroll(int t)
 {
     int chno = t / CHTIME;
-    int x = 0 - ((t % CHTIME) * (SET_VID_BUFW / CHPSCR)) / CHTIME;
-    int h = (abs((t % 48) - 24) * SET_VID_BUFH) / 256, i;
+    int x = 0 - ((t % CHTIME) * (engine.optScrWidth / CHPSCR)) / CHTIME;
+    int h = (abs((t % 48) - 24) * engine.optScrHeight) / 256, i;
     char *c = skrtxt + chno;
 
     for (i = 0; i < CHPSCR + 1; i++)
     {
-        txtDrawChar(x, (SET_VID_BUFH * 3) / 4, phont[*c++ - 32], SET_VID_BUFW / (6 * CHPSCR), h);
-        x += SET_VID_BUFW / CHPSCR;
+        txtDrawChar(x, (engine.optScrHeight * 3) / 4, phont[*c++ - 32], engine.optScrWidth / (6 * CHPSCR), h);
+        x += engine.optScrWidth / CHPSCR;
     }
 }
 
@@ -872,20 +868,20 @@
 void doendscroll(int t)
 {
     const char *s = endscroll;
-    int y = SET_VID_BUFH - (SET_VID_BUFH * t / 512), x = SET_VID_BUFW / 24;
+    int y = engine.optScrHeight - (engine.optScrHeight * t / 512), x = engine.optScrWidth / 24;
 
     while (*s)
     {
         if (*s < 32)
         {
-            x = SET_VID_BUFW / 24;
-            y += SET_VID_BUFH / 8;
+            x = engine.optScrWidth / 24;
+            y += engine.optScrHeight / 8;
         }
         else
-        if (y >= 0 - (SET_VID_BUFH / 8) && y < SET_VID_BUFH)
+        if (y >= 0 - (engine.optScrHeight / 8) && y < engine.optScrHeight)
         {
-            txtDrawChar(x, y, phont[*s - 32], SET_VID_BUFW / 60, SET_VID_BUFH / 60);
-            x += SET_VID_BUFW / 13;
+            txtDrawChar(x, y, phont[*s - 32], engine.optScrWidth / 60, engine.optScrHeight / 60);
+            x += engine.optScrWidth / 13;
         }
         s++;
     }
@@ -933,9 +929,8 @@
 int main(int argc, char *argv[])
 {
     BOOL initSDL = FALSE;
-    const int U = SET_VID_BUFW / 40;
     const Uint16  *dez = dezign;
-    int flagz = 0, flixtim = 0, argn;
+    int flagz = 0, flixtim = 0, argn, tmp;
     char *phiword = NULL, *dizainword = NULL;
 
     memset(&engine, 0, sizeof(engine));
@@ -944,6 +939,16 @@
     SET_PROG_NAME " by Ocsa (PWP) (c) 1998\n"
     "libSDL port by ccr/TNSP^PWP (c) 2013\n");
     
+    // Initialize audio parts
+    engine.optScrWidth      = SET_VID_BUFW;
+    engine.optScrHeight     = SET_VID_BUFH;
+
+    engine.optAfmt.freq     = SET_AUDIO_FREQ;
+    engine.optAfmt.format   = AUDIO_S16SYS;
+    engine.optAfmt.channels = SET_AUDIO_CHN;
+    engine.optAfmt.samples  = engine.optAfmt.freq / 16;
+    engine.optAfmt.callback = engineAudioCallback;
+
     // Parse commandline options
     for (argn = 1; argn < argc; argn++)
     {
@@ -955,6 +960,44 @@
                 engine.optVFlags |= SDL_FULLSCREEN;
                 break;
             
+            case 'v':
+                switch (arg[2])
+                {
+                    case '0':
+                        engine.optScrWidth  = 320;
+                        engine.optScrHeight = 200;
+                        break;
+
+                    case '1':
+                        engine.optScrWidth  = 640;
+                        engine.optScrHeight = 480;
+                        break;
+
+                    case '2':
+                        engine.optScrWidth  = 1280;
+                        engine.optScrHeight = 720;
+                        break;
+
+                    default:
+                        dmError("Invalid video option '%s'.\n", arg);
+                        goto error_exit;
+                }
+                break;
+
+            case 's':
+                if (sscanf(arg+2, "%d", &tmp) != 1)
+                {
+                    dmError("Invalid audio frequency setting '%s'.\n", arg);
+                    goto error_exit;
+                }
+                if (tmp < 8000 || tmp > 96000)
+                {
+                    dmError("Unsupported audio frequency %d Hz, must be %d < x < %d.\n",
+                        tmp, SET_MIN_FREQ, SET_MAX_FREQ);
+                }
+                engine.optAfmt.freq = tmp;
+                break;
+
             case '?':
             case 'h':
                 dmPrint(0,
@@ -962,6 +1005,8 @@
                 "\n"
                 " -f       Start in full screen\n"
                 " -h/-?    Show this help\n"
+                " -v<0-2>  Video 0=320x200, 1=640x480 (default), 2=1280x720 (ws)\n"
+                " -s<freq> Audio frequency (44100)\n"
                 "\n", argv[0]);
                 goto error_exit;
             
@@ -986,13 +1031,7 @@
     }
     initSDL = TRUE;
 
-    // Initialize audio parts
-    engine.optAfmt.freq     = SET_AUDIO_FREQ;
-    engine.optAfmt.format   = AUDIO_S16SYS;
-    engine.optAfmt.channels = SET_AUDIO_CHN;
-    engine.optAfmt.samples  = engine.optAfmt.freq / 16;
-    engine.optAfmt.callback = engineAudioCallback;
-    
+
     // Initialize SDL audio
     if (SDL_OpenAudio(&engine.optAfmt, NULL) < 0)
     {
@@ -1016,6 +1055,7 @@
 
     engine.mixBufSize = engine.optAfmt.freq * sizeof(Sint32) * 2;
     engine.mixBuf = malloc(engine.mixBufSize);
+    int U = engine.optScrWidth / 40;
 
     // Start audio, enter main loop
     dmPrint(0, "We are go.\n");
@@ -1113,7 +1153,7 @@
 
         if (flagz & CHESSBG)
         {
-            int zoom = (10 + abs(((qt >> 1) % 96) - 48)) * 4096 / SET_VID_BUFW;
+            int zoom = (10 + abs(((qt >> 1) % 96) - 48)) * 4096 / engine.optScrWidth;
             rotochess(engine.screen, sin(qt * 0.03) * zoom, cos(qt * 0.03) * zoom, 0, 0);
         }
 
@@ -1170,17 +1210,17 @@
 
             if (n > 600)
             {
-                txtDrawChar(U * 12 + dis, (SET_VID_BUFH >> 1) + dis + U * 6,
+                txtDrawChar(U * 12 + dis, (engine.optScrHeight >> 1) + dis + U * 6,
                          phont['X' - 32], U, U);
-                txtDrawChar(U * 22 + dis, (SET_VID_BUFH >> 1) + dis + U * 6,
+                txtDrawChar(U * 22 + dis, (engine.optScrHeight >> 1) + dis + U * 6,
                          phont['3' - 32], U, U);
             }
 
-            txtDrawChar(U * 28 + dis, SET_VID_BUFH >> 1, phont[16 + (n % 10)], U, U);
+            txtDrawChar(U * 28 + dis, engine.optScrHeight >> 1, phont[16 + (n % 10)], U, U);
             n /= 10;
-            txtDrawChar(U * 18 + dis, SET_VID_BUFH >> 1, phont[16 + (n % 10)], U, U);
+            txtDrawChar(U * 18 + dis, engine.optScrHeight >> 1, phont[16 + (n % 10)], U, U);
             n /= 10;
-            txtDrawChar(U * 8 + dis, SET_VID_BUFH >> 1, phont[16 + (n % 10)], U, U);
+            txtDrawChar(U * 8 + dis, engine.optScrHeight >> 1, phont[16 + (n % 10)], U, U);
             n /= 10;
         }
 
@@ -1196,11 +1236,14 @@
 error_exit:
     // Shutdown
     dmPrint(0, "Shutting down.\n");
-    dmPrint(1, "%d frames in %d ms, %1.2f fps\n",
+    if (engine.frameCount > 0 && engine.frameTime - engine.startTime > 0)
+    {
+        dmPrint(1, "%d frames in %d ms, %1.2f fps\n",
         engine.frameCount,
         engine.frameTime - engine.startTime,
         (float) (engine.frameCount * 1000.0f) / (float) (engine.frameTime - engine.startTime)
         );
+    }
 
     SDL_ShowCursor(SDL_ENABLE);
     if (engine.screen)
--- a/config.h	Tue Mar 19 04:51:44 2013 +0200
+++ b/config.h	Tue Mar 19 05:07:40 2013 +0200
@@ -3,7 +3,8 @@
 #define SET_VID_BUFH   480
 
 #define SET_AUDIO_FREQ 44100
-
+#define SET_MIN_FREQ   8000
+#define SET_MAX_FREQ   96000
 
 #define SET_PROG_NAME  "3x666"
 #define SET_WIN_NAME   "Ocsa (PWP) - 3x666"