changeset 13:3a358d053ffc

Runs now, audio does not work yet and the pace seems slightly too fast (not sure if that is the case however.)
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 15 Mar 2013 17:27:26 +0200
parents df40b5e4ca71
children 2663b7bb23b9
files 3x666.c
diffstat 1 files changed, 79 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/3x666.c	Fri Mar 15 15:48:38 2013 +0200
+++ b/3x666.c	Fri Mar 15 17:27:26 2013 +0200
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <stdarg.h>
 #include "config.h"
 #include "3xfont.h"
 
@@ -20,6 +21,7 @@
 #    endif
 #endif
 
+
 struct
 {
     int tickLen;
@@ -49,7 +51,45 @@
 {
     return (v < min ? min : (v > max ? max : v));
 }
+
+
+static int engineGetTick()
+{
+    return ((engine.frameTime - engine.startTime) * SET_DEMOHZ) / 1000;
+}
+
+
+void dmErrorVA(const char *fmt, va_list ap)
+{
+    fprintf(stderr, "3x666: ");
+    vfprintf(stderr, fmt, ap);
+}
+
+
+void dmError(const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    dmErrorVA(fmt, ap);
+    va_end(ap);
+}
+
     
+static BOOL engineInitializeVideo()
+{
+    SDL_FreeSurface(engine.screen);
+
+    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;
+    }
+    return TRUE;
+}
+
+
 /**************** tEXT dRAWiNG rOUTiNES **********
 
 bitmaps are for lamers :) let's use a little 12-segment calculator font...
@@ -219,28 +259,27 @@
     }
 }
 
-void drawball(Uint8 *d, vec3d * l, int relR)
+void drawball(SDL_Surface *screen, vec3d * l, int relR)
 {
     int R = balltab[relR].R, *s = balltab[relR].tab;
 
     signed int doty = (-(R - 1) * l->y);
     signed int y = R * 2 - 2;
 
-    d += (SET_VID_BUFW >> 1) - R + ((SET_VID_BUFH >> 1) - R) * SET_VID_BUFW;
+    Uint8 *dp = screen->pixels + (SET_VID_BUFW >> 1) - R + ((SET_VID_BUFH >> 1) - R) * screen->pitch;
 
-    if (y)
+    for (; y; y--)
     {
-        for (; y; y--)
+        int halfw = *s++;
+        if (halfw)
         {
-            int halfw = *s++;
-            if (halfw)
-                drawball_inloop(d + R - halfw,
-                                (doty - (l->x * halfw)) << 8,
-                                (l->x + l->z) << 8,
-                                0 - ((l->z << 8) / halfw), halfw << 1);
-            d += SET_VID_BUFW;
-            doty += l->y;
+            drawball_inloop(dp + R - halfw,
+                            (doty - (l->x * halfw)) << 8,
+                            (l->x + l->z) << 8,
+                            0 - ((l->z << 8) / halfw), halfw << 1);
         }
+        dp += screen->pitch;
+        doty += l->y;
     }
 }
 
@@ -836,6 +875,10 @@
     char *phiword = NULL, *dizainword = NULL;
     int flixtim = 0;
 
+//    engine.optVFlags |= SDL_FULLSCREEN;
+
+
+    // Initialize SDL components
     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0)
     {
         dmError("Could not initialize SDL: %s\n", SDL_GetError());
@@ -843,28 +886,27 @@
     }
     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)
     {
         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;
-    }
+    // Initialize SDL video
+    if (!engineInitializeVideo())
+        goto error_exit;
 
     SDL_ShowCursor(SDL_DISABLE);
     SDL_WM_SetCaption(SET_WIN_NAME, SET_WIN_NAME);
 
+    // Initialize effects
     preball();
     srand(0);
 
@@ -872,6 +914,7 @@
     audio_precalcs();
     setpal();
 
+    // Start audio, enter main loop
     SDL_LockAudio();
     SDL_PauseAudio(0);
     SDL_UnlockAudio();
@@ -879,6 +922,7 @@
 
     while (!engine.exitFlag)
     {
+        // Handle SDL events
         while (SDL_PollEvent(&engine.event))
         switch (engine.event.type)
         {
@@ -920,6 +964,7 @@
 
         // Draw frame
         engine.frameTime = SDL_GetTicks();
+        int qt = engineGetTick(&engine);
 
         if (SDL_MUSTLOCK(engine.screen) != 0 && SDL_LockSurface(engine.screen) != 0)
         {
@@ -928,8 +973,7 @@
         }
 
         // Main rendering code
-
-        while ((engine.frameTime / SET_ROWTIX >= *dez) && (flagz & DEMOEND) == 0)
+        while ((qt / SET_ROWTIX >= *dez) && (flagz & DEMOEND) == 0)
         {
             dez++;
             flagz = *dez++;
@@ -941,7 +985,7 @@
 
         if (flagz & FLASHTXT)
         {
-            while ((engine.frameTime / SET_ROWTIX) >= flixtim)
+            while ((qt / SET_ROWTIX) >= flixtim)
             {
                 phiword = lyrix();
                 flixtim += 4;
@@ -958,44 +1002,44 @@
         else
         if (flagz & FLASHBG)
         {
-            unsigned char col = 130 + (t % 48) * 2;
+            unsigned char col = 130 + (qt % 48) * 2;
             SDL_FillRect(engine.screen, NULL, col);
         }
 
         if (flagz & CHESSBG)
         {
-            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);
+            int zoom = (10 + abs(((qt >> 1) % 96) - 48)) * 4096 / SET_VID_BUFW;
+            rotochess(engine.screen, sin(qt * 0.03) * zoom, cos(qt * 0.03) * zoom, 0, 0);
         }
 
 /*	if(flagz&PLASMABG) drawplasma(ruutu,t); */
 
         if (flagz & OCSALOGO)
         {
-            drawchar(U * 6, U * 4, phont['O' - 32], U + U * sin(t * 0.10 + 3),
+            drawchar(U * 6, U * 4, phont['O' - 32], U + U * sin(qt * 0.10 + 3),
                      U);
             drawchar(U * 14, U * 4, phont['C' - 32], U,
-                     U + U * sin(t * 0.11 + 3));
+                     U + U * sin(qt * 0.11 + 3));
             drawchar(U * 22, U * 4, phont['S' - 32], U,
-                     U + U * sin(t * 0.12 + 3));
+                     U + U * sin(qt * 0.12 + 3));
             drawchar(U * 30, U * 4, phont['A' - 32],
-                     U + U * sin(t * 0.13 + 3), U);
+                     U + U * sin(qt * 0.13 + 3), U);
         }
 
         if (flagz & SCROLL0)
-            plainscroll(t);
+            plainscroll(qt);
 
         if (flagz & BALLIE)
         {
             int zoom;
             if (flagz & BALLJUMPS)
-                zoom = abs((t % 96) - 48);
+                zoom = abs((qt % 96) - 48);
             else
                 zoom = 47;
             
             zoom = dmClamp(zoom, 0, 47);
 
-            unitvec(&joo, 0.038 * t, 0.023 * t, 0.011 * t,
+            unitvec(&joo, 0.038 * qt, 0.023 * qt, 0.011 * qt,
                     32000 / balltab[zoom].R);
             joo.z <<= 1;
 
@@ -1005,15 +1049,15 @@
         if (flagz & FLASHTXT)
             flashtxt(phiword);
 
-        if ((flagz & TXTSCR) && ((t / SET_ROWTIX) & 2))
+        if ((flagz & TXTSCR) && ((qt / SET_ROWTIX) & 2))
             drawtxtscr(dizainword);
 
         if (flagz & ENDSCR)
-            doendscroll(t - 1024 * SET_ROWTIX);
+            doendscroll(qt - 1024 * SET_ROWTIX);
 
         if (flagz & COUNTAH)
         {
-            int n = ((t * 50 / 48) - 256 * 6);
+            int n = ((qt * 50 / 48) - 256 * 6);
             int dis = (rand() % U) >> 1;
             if (n > 666)
                 n = 666;