view src/editlev.cc @ 83:002bc70a3982

More cleanups. Mostly variable renames.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 26 Sep 2011 18:06:30 +0300
parents 2f1ecc1c5f72
children 20aa5a515896
line wrap: on
line source

/*
 *        editlev.cc
 *        AYM 1998-09-06
 */


/*
This file is part of Yadex.

Yadex incorporates code from DEU 5.21 that was put in the public domain in
1994 by Raphaël Quinet and Brendon Wyber.

The rest of Yadex is Copyright © 1997-2003 André Majorel and others.

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.
*/


#include "yadex.h"
#include <time.h>
#ifdef Y_X11
#include <X11/Xlib.h>
#endif
#include "editlev.h"
#include "editloop.h"
#include "events.h"
#include "game.h"
#include "gfx.h"
#include "levels.h"
#include "patchdir.h"
#include "wadfile.h"


static void WriteYadexLog(const char *file, const char *level,
                          time_t * t0, time_t * t1);


/*
   the driving program
*/

void EditLevel(const char *levelname, bool newlevel)
{
    ReadWTextureNames();
    ReadFTextureNames();
    patch_dir.refresh(cfg.MasterDir);
    if (InitGfx())
        return;
/* Call init_input_status() as shortly as possible after the creation
   of the window to minimize the risk of calling get_input_status(),
   get_key(), have_key(), etc. with <is> still uninitialized. */
    init_input_status();
    init_event();
    if (newlevel && !levelname)        // "create"
    {
        EmptyLevelData(levelname);
        MapMinX = -2000;
        MapMinY = -2000;
        MapMaxX = 2000;
        MapMaxY = 2000;
        Level = 0;
    }
    else if (newlevel && levelname)        // "create <level_name>"
    {
        printf("Sorry, \"create <level_name>\" is not implemented."
               " Try \"create\" without argument.\n");
        TermGfx();
        return;
    }
    else                        // "edit <level_name>" or "edit"
    {
#if 0
        if (levelname == 0 || !levelname2levelno(levelname)
            || !FindMasterDir(cfg.MasterDir, levelname))
            levelname = SelectLevel(atoi(levelname));        /* returns "" on Esc */
        if (levelname2levelno(levelname))
        {
#endif
            ClearScreen();
            if (ReadLevelData(levelname))
            {
                goto done;        // Failure!
            }
#if 0
        }
#endif
    }
    LogMessage(": Editing %s...\n", levelname ? levelname : "new level");

// Set the name of the window
    {
#define BUFSZ 100
        char buf[BUFSZ + 1];

#ifdef OLD_TITLE
        al_scps(buf, "Yadex - ", BUFSZ);
        if (Level && Level->wadfile)
            al_saps(buf, Level->wadfile->filename, BUFSZ);
        else
            al_saps(buf, "New level", BUFSZ);
        if (Level)
        {
            al_saps(buf, " - ", BUFSZ);
            al_saps(buf, Level->dir.name, BUFSZ);
        }
        else if (levelname)
        {
            al_saps(buf, " - ", BUFSZ);
            al_saps(buf, levelname, BUFSZ);
        }
#else
        al_scps(buf, "Yadex: ", BUFSZ);
        al_saps(buf, (levelname) ? levelname : "(null)", BUFSZ);
#endif
        XStoreName(dpy, win, buf);
#undef BUFSZ
    }

    {
        time_t t0, t1;
        time(&t0);
        EditorLoop(levelname);
        time(&t1);
        LogMessage(": Finished editing %s...\n",
                   levelname ? levelname : "new level");
        if (Level && Level->wadfile)
        {
            const char *const file_name =
                Level->wadfile ? Level->wadfile->pathname() : "(New level)";
            WriteYadexLog(file_name, levelname, &t0, &t1);
        }
    }
  done:
    TermGfx();
    if (!cfg.registered)
        printf("Please register the game"
               " if you want to be able to save your changes.\n");

    ForgetLevelData();
/* forget the level pointer */
    Level = 0;
    ForgetWTextureNames();
    ForgetFTextureNames();
}


/*
 *        WriteYadexLog - Keep track of time spent editing that wad file
 *        FIXME should be in a separate module
 */
static void WriteYadexLog(const char *file, const char *level, time_t * t0,
                          time_t * t1)
{
    al_fspec_t logname;
    al_fdrv_t drive;
    al_fpath_t path;
    al_fbase_t base;

    al_fana(file, drive, path, base, 0);
    sprintf(logname, "%s%s%s.yl", drive, path, base);

/* if log file does not already exist, do _not_ create it */
    if (al_fnature(logname) == 1)
    {
        FILE *logfd;
        logfd = fopen(logname, "a");
        if (logfd)
        {
            struct tm *tm = localtime(t0);
            fprintf(logfd, "%04d%02d%02d\tedit\t%s\t%ld\n",
                    tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, level,
                    (long) (*t1 - *t0) / 60);
            fclose(logfd);
        }
    }
}