changeset 11:fcae85b39931

Slowly getting closer. Does not compile yet, and it's messy.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 15 Mar 2013 15:47:20 +0200
parents fee680bb71fd
children df40b5e4ca71
files 3x666.c config.h
diffstat 2 files changed, 241 insertions(+), 154 deletions(-) [+]
line wrap: on
line diff
--- a/3x666.c	Fri Mar 15 06:09:08 2013 +0200
+++ b/3x666.c	Fri Mar 15 15:47:20 2013 +0200
@@ -6,21 +6,50 @@
 #include "config.h"
 #include "3xfont.h"
 
+
 /** typedefs **/
+#if !defined(FALSE) && !defined(TRUE) && !defined(BOOL)
+typedef enum { FALSE = 0, TRUE = 1 } BOOL;
+#endif
+
+#ifndef BOOL
+#    ifdef bool
+#        define BOOL bool
+#    else
+#        define BOOL int
+#    endif
+#endif
+
+struct
+{
+    int tickLen;
+    int frameTime, frameCount,
+        startTime, endTime;
+
+    BOOL exitFlag;
+
+    int optVFlags;
+    SDL_Surface *screen;
+    SDL_Event event;
+    SDL_AudioSpec optAfmt;
+} engine;
+
+
 
 typedef struct
 {
     int x, y, z;
 } vec3d;
 
-int TIKLGT;
 
-SDL_Color pal[256];
-Uint8 *ruutu;
-
-int *mxbuf;
 int *ballz;
 
+
+static inline int dmClamp(const int v, const int min, const int max)
+{
+    return (v < min ? min : (v > max ? max : v));
+}
+    
 /**************** tEXT dRAWiNG rOUTiNES **********
 
 bitmaps are for lamers :) let's use a little 12-segment calculator font...
@@ -30,15 +59,15 @@
 void drawseg(int y, int x, int w, int h)
 {
     /* clip clip clip */
-    if (x + w > BUFW)
-        w = BUFW - x;
+    if (x + w > SET_VID_BUFW)
+        w = SET_VID_BUFW - x;
     if (x < 0)
     {
         w += x;
         x = 0;
     }
-    if (y + h > BUFH)
-        h = BUFH - y;
+    if (y + h > SET_VID_BUFH)
+        h = SET_VID_BUFH - y;
     if (y < 0)
     {
         h += y;
@@ -46,11 +75,11 @@
     }
     if (w > 0 && h > 0)
     {
-        Uint8 *b = ruutu + y * BUFW + x;
+        Uint8 *b = engine.screen->pixels + (y * engine.screen->pitch) + x;
         for (; h; h--)
         {
             memset(b, 122, w);
-            b += BUFW;
+            b += engine.screen->pitch;
         }
     }
 }
@@ -88,18 +117,18 @@
 
 void drawtxtscr(char *str)
 {
-    int x = BUFW >> 4, y = BUFH >> 3;
+    int x = SET_VID_BUFW >> 4, y = SET_VID_BUFH >> 3;
     while (*str)
     {
         if (*str >= 32)
         {
-            drawchar(x, y, phont[*str - 32], BUFW / 50, BUFW / 80);
-            x += BUFW / 10;
+            drawchar(x, y, phont[*str - 32], SET_VID_BUFW / 50, SET_VID_BUFW / 80);
+            x += SET_VID_BUFW / 10;
         }
         else
         {
-            x = BUFW >> 4;
-            y += BUFW / 10;
+            x = SET_VID_BUFW >> 4;
+            y += SET_VID_BUFW / 10;
         }
         str++;
     }
@@ -108,11 +137,11 @@
 
 void flashtxt(char *str)
 {
-    int x = (BUFW >> 1) - (strlen(str) + 1) * 3 * BUFW / 80;
+    int x = (SET_VID_BUFW >> 1) - (strlen(str) + 1) * 3 * SET_VID_BUFW / 80;
     while (*str)
     {
-        drawchar(x, BUFH >> 1, phont[*str++ - 32], BUFW / 50, BUFW / 80);
-        x += BUFW / 10;
+        drawchar(x, SET_VID_BUFH >> 1, phont[*str++ - 32], SET_VID_BUFW / 50, SET_VID_BUFW / 80);
+        x += SET_VID_BUFW / 10;
     }
 }
 
@@ -144,10 +173,10 @@
 
 ***/
 
-#if (BUFH < BUFW)
-#    define maxR (BUFH >> 1)
+#if (SET_VID_BUFH < SET_VID_BUFW)
+#    define maxR (SET_VID_BUFH >> 1)
 #else
-#    define maxR (BUFW >> 1)
+#    define maxR (SET_VID_BUFW >> 1)
 #endif
 
 struct
@@ -197,7 +226,7 @@
     signed int doty = (-(R - 1) * l->y);
     signed int y = R * 2 - 2;
 
-    d += (BUFW >> 1) - R + ((BUFH >> 1) - R) * BUFW;
+    d += (SET_VID_BUFW >> 1) - R + ((SET_VID_BUFH >> 1) - R) * SET_VID_BUFW;
 
     if (y)
     {
@@ -209,7 +238,7 @@
                                 (doty - (l->x * halfw)) << 8,
                                 (l->x + l->z) << 8,
                                 0 - ((l->z << 8) / halfw), halfw << 1);
-            d += BUFW;
+            d += SET_VID_BUFW;
             doty += l->y;
         }
     }
@@ -220,14 +249,14 @@
 
 void drawplasma(char *d,float t)
 {
-	int y=BUFH; float u=0,du=500/BUFH;
+	int y=SET_VID_BUFH; float u=0,du=500/SET_VID_BUFH;
 
 	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/BUFW,
-			-2*cos(t*0.04+1+u*0.022)*65536*4096/(BUFW*BUFW), BUFW);
-		d+=BUFW;
+			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;
 		u+=du;
 	}
 }
@@ -235,23 +264,25 @@
 
 /************************ oTHA FX ***************/
 
-void rotochess(Uint8 *d, int du, int dv, int iu, int iv)
+void rotochess(SDL_Surface *screen, int du, int dv, int iu, int iv)
 {
-    int hu = iu - (dv * (BUFH >> 1)) - (du * (BUFW >> 1)),
-        hv = iv + (du * (BUFH >> 1)) - (dv * (BUFW >> 1));
+    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 y;
-    for (y = BUFH; y; y--)
+    Uint8 *dp = screen->pixels;
+
+    for (y = SET_VID_BUFH; y; y--)
     {
+        Uint8 *d = dp;
+        int u = hu, v = hv, x;
+        for (x = SET_VID_BUFW; x; x--)
         {
-            register int u = hu, v = hv, x = BUFW;
-            for (; x; x--)
-            {
-                u += du;
-                v += dv;
-                *d++ = ((u ^ v) >> 8) & 0xb1;
-            }
+            u += du;
+            v += dv;
+            *d++ = ((u ^ v) >> 8) & 0xb1;
         }
+        dp += screen->pitch;
         hu += dv;
         hv -= du;
     }
@@ -262,20 +293,23 @@
 
 void setpal()
 {
+    SDL_Color pal[SET_VID_COLORS];
     int i, a = 3, b = 0;
+
     for (i = 255; i; i--)
     {
-        SDL_Color *d = &pal[(i + 128) & 255];
-        d->r =  (abs(i - 140) >> a) & 255;
-        d->g = ((abs(i - 128) >> b) & 255) ^ 1;
-        d->b =  (abs(i - 96) >> b) & 255;
+        int n = (i + 128) & 255;
+        pal[n].r =  (abs(i - 140) >> a) & 255;
+        pal[n].g = ((abs(i - 128) >> b) & 255) ^ 1;
+        pal[n].b =  (abs(i - 96) >> b) & 255;
         if (i == 128)
         {
             a = 0;
             b = 1;
         }
     }
-    oxl_setpalette(pal);
+
+    SDL_SetColors(engine.screen, pal, 0, SET_VID_COLORS);
 }
 
 
@@ -338,7 +372,7 @@
 /* sampling sucks! */
 void audio_precalcs()
 {
-    int drumlgt = TIKLGT * ROWTIX * 4;
+    int drumlgt = engine.tickLen * SET_ROWTIX * 4;
     int *d = drum0 = malloc(drumlgt * sizeof(int)),
         *e = drum1 = malloc(drumlgt * sizeof(int)), i,
         vol = 24680, dvol = 35000 / (float) drumlgt;
@@ -372,14 +406,15 @@
 }
 
 
-/* mixes the next row of music into b */
-int *audio_mix(void)
+static void engineAudioCallback(void *userdata, Uint8 *stream, int len)
 {
+#if 0
+    (void) userdata;
     static int rowno = 0;
     static signed int delta = -5;
     static char ismelody = 0, silend = 0;
 
-    int rowlgt = TIKLGT * ROWTIX, i;
+    int rowlgt = engine.tickLen * SET_ROWTIX, i;
     int *d = mxbuf, note;
 
     /* BASS (sawtooth ofcoz) */
@@ -401,7 +436,7 @@
     {
         int ps = 16384, dps;
         note += delta;
-        dps = ((noterate[note] << 10) / AUFREQ);
+        dps = ((noterate[note] << 10) / SET_AUDIO_FREQ);
         for (i = rowlgt; i; i--)
         {
             *d++ = ps;
@@ -429,7 +464,7 @@
             {
                 int ps = 16384, dps;    /* this loop is different */
                 note += delta;
-                dps = ((noterate[note] << 12) / AUFREQ);
+                dps = ((noterate[note] << 12) / engine.optAfmt.freq);
                 for (i = rowlgt; i; i--)
                 {
                     *d++ += ps;
@@ -477,13 +512,17 @@
             delta = -5;
         }
     }
-    return mxbuf;
+
+//    if (engine.dev != NULL)
+//        jvmRenderAudio(engine.dev, stream, len / jvmGetSampleSize(engine.dev));
+#endif
 }
 
 
 /**************** tEXT gENERATORS eTC ***************/
 
-char skrtxt[] = {
+char skrtxt[] =
+{
     "         HI THERE !  THIS IS THE FIRST OCSA RELEASE FOR LINUX !  "
         "IT'S A STUPID INTRO CALLED 3X666 !          "
 };
@@ -495,14 +534,14 @@
 void plainscroll(int t)
 {
     int chno = t / CHTIME;
-    int x = 0 - ((t % CHTIME) * (BUFW / CHPSCR)) / CHTIME;
-    int h = (abs((t % 48) - 24) * BUFH) / 256, i;
+    int x = 0 - ((t % CHTIME) * (SET_VID_BUFW / CHPSCR)) / CHTIME;
+    int h = (abs((t % 48) - 24) * SET_VID_BUFH) / 256, i;
     char *c = skrtxt + chno;
 
     for (i = 0; i < CHPSCR + 1; i++)
     {
-        drawchar(x, (BUFH * 3) / 4, phont[*c++ - 32], BUFW / (6 * CHPSCR), h);
-        x += BUFW / CHPSCR;
+        drawchar(x, (SET_VID_BUFH * 3) / 4, phont[*c++ - 32], SET_VID_BUFW / (6 * CHPSCR), h);
+        x += SET_VID_BUFW / CHPSCR;
     }
 }
 
@@ -729,20 +768,20 @@
 void doendscroll(int t)
 {
     const char *s = endscroll;
-    int y = BUFH - (BUFH * t / 512), x = BUFW / 24;
+    int y = SET_VID_BUFH - (SET_VID_BUFH * t / 512), x = SET_VID_BUFW / 24;
 
     while (*s)
     {
         if (*s < 32)
         {
-            x = BUFW / 24;
-            y += BUFH / 8;
+            x = SET_VID_BUFW / 24;
+            y += SET_VID_BUFH / 8;
         }
         else
-        if (y >= 0 - (BUFH / 8) && y < BUFH)
+        if (y >= 0 - (SET_VID_BUFH / 8) && y < SET_VID_BUFH)
         {
-            drawchar(x, y, phont[*s - 32], BUFW / 60, BUFH / 60);
-            x += BUFW / 13;
+            drawchar(x, y, phont[*s - 32], SET_VID_BUFW / 60, SET_VID_BUFH / 60);
+            x += SET_VID_BUFW / 13;
         }
         s++;
     }
@@ -789,27 +828,108 @@
 
 int main(int argc, char *argv[])
 {
+    BOOL initSDL = FALSE;
     vec3d joo;
-    const int U = BUFW / 40;
+    const int U = SET_VID_BUFW / 40;
     int flagz = 0;
     const short *dez = dezign;
     char *phiword = NULL, *dizainword = NULL;
     int flixtim = 0;
 
-    preball();
-    srand((int) time(NULL));
+    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0)
+    {
+        dmError("Could not initialize SDL: %s\n", SDL_GetError());
+        goto error_exit;
+    }
+    initSDL = TRUE;
 
-    TIKLGT = AUFREQ / DEMOHZ;
-    mxbuf = malloc(MAXROWLGT * sizeof(int));
+    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;
+    
+    if (SDL_OpenAudio(&engine.optAfmt, NULL) < 0)
+    {
+        dmError("Couldn't open SDL audio: %s\n", SDL_GetError());
+    }
+
+    // foo
+    engine.screen = SDL_SetVideoMode(SET_VID_BUFW, SET_VID_BUFH, 8, engine.optVFlags);
+    if (engine.screen == NULL)
+    {
+        dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError());
+        return FALSE;
+    }
+
+    SDL_ShowCursor(SDL_DISABLE);
+    SDL_WM_SetCaption(SET_WIN_NAME, SET_WIN_NAME);
+
+    preball();
+    srand(0);
+
+    engine.tickLen = engine.optAfmt.freq / SET_DEMOHZ;
     audio_precalcs();
-
     setpal();
 
-    for (;;)
+    SDL_LockAudio();
+    SDL_PauseAudio(0);
+    SDL_UnlockAudio();
+    engine.startTime = SDL_GetTicks();
+
+    while (!engine.exitFlag)
     {
-        int t = oxl_timer();
+        while (SDL_PollEvent(&engine.event))
+        switch (engine.event.type)
+        {
+            case SDL_KEYDOWN:
+                switch (engine.event.key.keysym.sym)
+                {
+                    case SDLK_ESCAPE:
+                        engine.exitFlag = TRUE;
+                        break;
+                    
+                    case SDLK_f:
+                        engine.optVFlags ^= SDL_FULLSCREEN;
+                        if (!engineInitializeVideo())
+                            goto error_exit;
+                        break;
+
+                    case SDLK_RETURN:
+                        if (engine.event.key.keysym.mod & KMOD_ALT)
+                        {
+                            engine.optVFlags ^= SDL_FULLSCREEN;
+                            if (!engineInitializeVideo())
+                                goto error_exit;
+                        }
+                        break;
 
-        while ((t / ROWTIX >= *dez) && (flagz & DEMOEND) == 0)
+                    default:
+                        break;
+                }
+
+                break;
+
+            case SDL_VIDEOEXPOSE:
+                break;
+
+            case SDL_QUIT:
+                engine.exitFlag = TRUE;
+                break;
+        }
+
+        // Draw frame
+        engine.frameTime = SDL_GetTicks();
+
+        if (SDL_MUSTLOCK(engine.screen) != 0 && SDL_LockSurface(engine.screen) != 0)
+        {
+            dmError("Can't lock surface.\n");
+            goto error_exit;
+        }
+
+        // Main rendering code
+
+        while ((engine.frameTime / SET_ROWTIX >= *dez) && (flagz & DEMOEND) == 0)
         {
             dez++;
             flagz = *dez++;
@@ -821,7 +941,7 @@
 
         if (flagz & FLASHTXT)
         {
-            while ((t / ROWTIX) >= flixtim)
+            while ((engine.frameTime / SET_ROWTIX) >= flixtim)
             {
                 phiword = lyrix();
                 flixtim += 4;
@@ -829,24 +949,23 @@
         }
 
         if (flagz & DEMOEND)
-            break;
+            engine.exitFlag = TRUE;
 
         if (flagz & BLACKBG)
         {
-            memset(ruutu, 0, BUFH * BUFW);
+            SDL_FillRect(engine.screen, NULL, 0);
         }
         else
         if (flagz & FLASHBG)
         {
             unsigned char col = 130 + (t % 48) * 2;
-            memset(ruutu, col, BUFH * BUFW);
+            SDL_FillRect(engine.screen, NULL, col);
         }
 
         if (flagz & CHESSBG)
         {
-            int zoom = ((10 + abs(((t >> 1) % 96) - 48)) * 4096 / BUFW);
-            rotochess(ruutu,
-                      sin(t * 0.03) * zoom, cos(t * 0.03) * zoom, 0, 0);
+            int zoom = (10 + abs(((t >> 1) % 96) - 48)) * 4096 / SET_VID_BUFW;
+            rotochess(engine.screen, sin(t * 0.03) * zoom, cos(t * 0.03) * zoom, 0, 0);
         }
 
 /*	if(flagz&PLASMABG) drawplasma(ruutu,t); */
@@ -873,26 +992,24 @@
                 zoom = abs((t % 96) - 48);
             else
                 zoom = 47;
-            if (zoom < 0)
-                zoom = 0;
-            else if (zoom > 47)
-                zoom = 47;
+            
+            zoom = dmClamp(zoom, 0, 47);
 
             unitvec(&joo, 0.038 * t, 0.023 * t, 0.011 * t,
                     32000 / balltab[zoom].R);
             joo.z <<= 1;
 
-            drawball(ruutu, &joo, zoom);
+            drawball(engine.screen, &joo, zoom);
         }
 
         if (flagz & FLASHTXT)
             flashtxt(phiword);
 
-        if ((flagz & TXTSCR) && ((t / ROWTIX) & 2))
+        if ((flagz & TXTSCR) && ((t / SET_ROWTIX) & 2))
             drawtxtscr(dizainword);
 
         if (flagz & ENDSCR)
-            doendscroll(t - 1024 * ROWTIX);
+            doendscroll(t - 1024 * SET_ROWTIX);
 
         if (flagz & COUNTAH)
         {
@@ -900,27 +1017,44 @@
             int dis = (rand() % U) >> 1;
             if (n > 666)
                 n = 666;
+
             if (n > 600)
             {
-                drawchar(U * 12 + dis, (BUFH >> 1) + dis + U * 6,
+                drawchar(U * 12 + dis, (SET_VID_BUFH >> 1) + dis + U * 6,
                          phont['X' - 32], U, U);
-                drawchar(U * 22 + dis, (BUFH >> 1) + dis + U * 6,
+                drawchar(U * 22 + dis, (SET_VID_BUFH >> 1) + dis + U * 6,
                          phont['3' - 32], U, U);
             }
-            drawchar(U * 28 + dis, BUFH >> 1, phont[16 + (n % 10)], U, U);
+
+            drawchar(U * 28 + dis, SET_VID_BUFH >> 1, phont[16 + (n % 10)], U, U);
             n /= 10;
-            drawchar(U * 18 + dis, BUFH >> 1, phont[16 + (n % 10)], U, U);
+            drawchar(U * 18 + dis, SET_VID_BUFH >> 1, phont[16 + (n % 10)], U, U);
             n /= 10;
-            drawchar(U * 8 + dis, BUFH >> 1, phont[16 + (n % 10)], U, U);
+            drawchar(U * 8 + dis, SET_VID_BUFH >> 1, phont[16 + (n % 10)], U, U);
             n /= 10;
         }
 
-        /* blitzz */
+        // Flip screen
+        if (SDL_MUSTLOCK(engine.screen) != 0)
+            SDL_UnlockSurface(engine.screen);
 
-        SDL_flip(...);
-
-        SDL_Delay(25);
+        engine.frameCount++;
+        SDL_Flip(engine.screen);
+        SDL_Delay(20);
     }
 
-    SDL_Quit();
+error_exit:
+    // Shutdown
+    SDL_ShowCursor(SDL_ENABLE);
+    if (engine.screen)
+        SDL_FreeSurface(engine.screen);
+
+    SDL_LockAudio();
+    SDL_PauseAudio(1);
+    SDL_UnlockAudio();
+
+    if (initSDL)
+        SDL_Quit();
+    
+    return 0;
 }
--- a/config.h	Fri Mar 15 06:09:08 2013 +0200
+++ b/config.h	Fri Mar 15 15:47:20 2013 +0200
@@ -1,65 +1,18 @@
-/******************************o.
-
-Here are some settings you may want to fix :)
-VIDEO and AUDIO are set in the makefile.	 
-*/
-
-#define BUFW 320
-#define BUFH 200
-/*
-	The size (width&height) of the display buffer.
-	
-	320x200 is smooth on a 386.
+// Settings
+#define SET_VID_BUFW   640
+#define SET_VID_BUFH   480
 
-	Svgalib: the best-fitting 256 color mode will be searched
-	automatically.
-*/
+#define SET_AUDIO_FREQ 44100
 
-#define OUTFREQ 22050
-/*
-	Audio output frequency. 
-*/
 
-#define FRAGSIZE 10	/* size = 2^n, n=7..17 */
-#define NUMFRAGS 40	/* somewhere between 2..255, i think */
-/*
-	The number and size of the audio buffers.
+#define SET_WIN_NAME   "Ocsa (PWP) - 3x666"
 
-	If your machine has problems in keeping the mixbuffer filled, you
-	an try to increase these values. However those 10,40 are quite ok
-	for a 386dx/25 with an old soundblaster.
-	
-	Prefer increasing NUMFRAGS unless you want an action-halt-
-	action-halt style multimedia performance a la windows.
-
-	We still have problems in keeping the audio up in all
-	circumstances.. such as leaving the svgalib console or while
-	performing some window manager functions.		
-*/
-
-#define WINNAME "3x666"
-/*
-	The name for the x window.
-*/
 
-#define SLEEPY 0
-/* 
-	1 = sleep between frames if there's nothing to do
-	    originally implemented for a better cpu usage
-	    but i don't think it is of any relevant use here
-	    
-	0 = use dummyloop
+// Do not touch these
+#define SET_DEMOHZ     100
+#define SET_VID_COLORS 256
+#define SET_AUDIO_CHN  1
 
- */
-/*	All right! Now save the file and type make ("em ay kee ee")
-/ \
-
-.o*************/
-
-/* don't touch these: */
-
-#define DEMOHZ 100
-
-/* maybe these shouldn't be here */
-#define ROWTIX 6
-#define MAXROWLGT (TIKLGT*ROWTIX)
+// Maybe these shouldn't be here
+#define SET_ROWTIX     6
+#define SET_MAXROWLGT  (TIKLGT * SET_ROWTIX)