changeset 623:dfdff30ad363

Some more work.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 13 Apr 2013 07:01:17 +0300
parents 8ba49471c143
children 5df8ef3f88bb e8fc083b7499
files dmengine.h dmsimple.c
diffstat 2 files changed, 89 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/dmengine.h	Sat Apr 13 06:23:54 2013 +0300
+++ b/dmengine.h	Sat Apr 13 07:01:17 2013 +0300
@@ -249,8 +249,8 @@
     void   (*demoQuit)(struct DMEngineData *);
 
     // Setup specifics
-    DMVector setupMenuPos, setupMenuDim,
-        setupText1Pos, setupText2Pos;
+    DMVector setupMenuPos, setupMenuDim, setupText1Pos;
+    BOOL setupMenuCenter;
 } DMEngineData;
 
 
--- a/dmsimple.c	Sat Apr 13 06:23:54 2013 +0300
+++ b/dmsimple.c	Sat Apr 13 07:01:17 2013 +0300
@@ -7,6 +7,7 @@
 #include "setupimage.h"
 #include "setupmenubar.h"
 
+static const char *engineSetupDataName = "SetupData.txt";
 static const char *engineSetupImageName = "SetupImage.png";
 static const char *engineSetupMenuBarName = "SetupMenuBar.png";
 static const char *engineSetupFontName = "SetupFont.dmf";
@@ -233,13 +234,68 @@
 }
 
 
+int engineParseSetupConfig(const char *filename)
+{
+    DMResource *file = NULL;
+    int res = DMERR_OK;
+    char buf[128];
+
+    if ((file = dmf_open(engine.resources, filename)) == NULL)
+        return DMERR_FOPEN;
+
+    while (dmfgets(buf, sizeof(buf), file) != NULL)
+    {
+        ssize_t pos;
+        // Trim line ending
+        for (pos = strlen(buf) - 1; pos >= 0 && isspace(buf[pos]); pos--)
+            buf[pos] = 0;
+
+        // Find start of the line
+        for (pos = 0; isspace(buf[pos]); pos++);
+        
+        // Skip empty lines and comments
+        if (buf[pos] == 0 || buf[pos] == '#')
+            continue;
+
+        if (sscanf(buf+pos, "menuPos %f %f",
+            &engine.setupMenuPos.x, &engine.setupMenuPos.y) != 2 &&
+            sscanf(buf+pos, "menuDim %f %f",
+            &engine.setupMenuDim.x, &engine.setupMenuDim.y) != 2 &&
+            sscanf(buf+pos, "text1Pos %f %f",
+            &engine.setupText1Pos.x, &engine.setupText1Pos.y) != 2 &&
+            sscanf(buf+pos, "menuCenter %d", &engine.setupMenuCenter) != 1)
+        {
+            dmError("Syntax error in configuration:\n%s\n", buf);
+            res = DMERR_INVALID_DATA;
+            goto out;
+        }
+    }
+
+out:
+    dmf_close(file);
+    return res;
+}
+
+
+static inline DMFloat vsX(DMVector vec)
+{
+    return (DMFloat) engine.screen->w * vec.x;
+}
+
+
+static inline DMFloat vsY(DMVector vec)
+{
+    return (DMFloat) engine.screen->h * vec.y;
+}
+
+
 int engineVideoSetup()
 {
     DMBitmapFont *menuFont = NULL;
     DMResource *file = NULL;
     SDL_Surface *menuBgImage = NULL, *menuBarImage = NULL;
     int result, menuState = -1;
-    BOOL fullScreen = TRUE;
+    BOOL menuFullScreen = TRUE;
 
     // Compute a list of valid modes
     if (!engineGenInitializeVideo(DM_VSETUP_WIDTH, DM_VSETUP_HEIGHT, engine.optVidDepth, engine.optVFlags))
@@ -271,6 +327,18 @@
     if (!engineGenInitializeVideo(DM_VSETUP_WIDTH, DM_VSETUP_HEIGHT, 32, SDL_SWSURFACE | SDL_DOUBLEBUF))
         goto out;
 
+    // Get setup data
+    if (engineParseSetupConfig(engineSetupDataName) != DMERR_OK)
+    {
+        engine.setupMenuPos.x = 0.18750f;
+        engine.setupMenuPos.y = 0.41666f;
+        engine.setupMenuDim.x = 0.625f;
+        engine.setupMenuDim.y = 0.41666f;
+        
+        engine.setupText1Pos.x = 0.3f;
+        engine.setupText1Pos.y = 0.7f;
+    }
+
     // Fetch and decompress setup image, try regular resources first
     file = dmf_open(engine.resources, engineSetupImageName);
     if (file == NULL)
@@ -331,7 +399,6 @@
     menuBgImage = tmp;
 
     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
-    
 
 
     // Enter the main loop of the menu
@@ -339,7 +406,7 @@
     int menuOffset = 0,
         menuIndex = 0,
         menuEntryHeight = menuFont->height + 2,
-        menuHeight = DM_VSETUP_MENU_HEIGHT / menuEntryHeight;
+        menuHeight = vsY(engine.setupMenuDim) / menuEntryHeight;
 
     menuState = 0;
 
@@ -378,7 +445,7 @@
                         break;
                     
                     case SDLK_SPACE:
-                        fullScreen = !fullScreen;
+                        menuFullScreen = !menuFullScreen;
                         break;
 
                     default:
@@ -414,9 +481,10 @@
             if (entry == menuOffset + menuIndex)
             {
                 dmScaledBlitSurface32to32TransparentGA(menuBarImage,
-                    DM_VSETUP_MENU_XC,
-                    DM_VSETUP_MENU_YC + (index * menuEntryHeight),
-                    DM_VSETUP_MENU_WIDTH, menuEntryHeight + 2,
+                    vsX(engine.setupMenuPos),
+                    vsY(engine.setupMenuPos) + (index * menuEntryHeight),
+                    vsX(engine.setupMenuDim),
+                    menuEntryHeight + 2,
                     engine.screen,
                     200 + sin(t * 10.0) * 50);
             }
@@ -427,26 +495,24 @@
                 mode->aspect / 1000,
                 mode->aspect % 1000);
 
+            DMFloat posX = engine.setupMenuCenter ?
+                2.0f + (vsX(engine.setupMenuDim) - menuFont->width * strlen(menuStr)) / 2.0f :
+                2.0f;
+            
             dmDrawBMTextConst(
                 engine.screen, menuFont, DMD_TRANSPARENT,
-                DM_VSETUP_MENU_XC + 2,
-                DM_VSETUP_MENU_YC + 2 + (index * menuEntryHeight),
+                vsX(engine.setupMenuPos) + posX,
+                vsY(engine.setupMenuPos) + 2 + (index * menuEntryHeight),
                 menuStr);
         }
         
         dmDrawBMTextConst(
             engine.screen, menuFont, DMD_TRANSPARENT,
-            80,
-            DM_VSETUP_MENU_YC + menuEntryHeight * (1 + menuHeight),
-            fullScreen ?
-            "[ FULLSCREEN ] - SPACE TO TOGGLE" :
-            "[  WINDOWED  ] - SPACE TO TOGGLE");
-
-        dmDrawBMTextConst(
-            engine.screen, menuFont, DMD_TRANSPARENT,
-            80,
-            DM_VSETUP_MENU_YC + menuEntryHeight * (2 + menuHeight),
-            "     PRESS [ENTER] TO START     ");
+            vsX(engine.setupText1Pos),
+            vsY(engine.setupText1Pos),
+            menuFullScreen ?
+            "FULLSCREEN" :
+            " WINDOWED ");
 
         // Flip screen
         if (SDL_MUSTLOCK(engine.screen) != 0)
@@ -463,7 +529,7 @@
         engine.optVidWidth = mode->w;
         engine.optVidHeight = mode->h;
         engine.optVidAspect = mode->aspect;
-        if (fullScreen)
+        if (menuFullScreen)
             engine.optVFlags |= SDL_FULLSCREEN;
     }