add file and string modules to csqc. the file nodule is rather parnoid

about the path it's handed (need to get even more paranoid, though). the
string module just has char replacement so far. Add Draw_CenterPic to
r_progs.c, but this will only last till qpic_t is supported in qc.
the load menu almost works: just need to add key handling and fix a bug in
PF_sprintf
This commit is contained in:
Bill Currie 2002-01-21 07:24:51 +00:00
parent d59e7d3da4
commit 5b0e6dc342
7 changed files with 357 additions and 12 deletions

View file

@ -12,15 +12,27 @@ void (integer () func) Menu_SetQuit = #0;
void () Menu_Quit = #0;
void (integer x, integer y, string name) Draw_Pic = #0;
void (integer x, integer y, string name) Draw_CenterPic = #0;
void (integer x, integer y, integer chr) Draw_Character = #0;
void (integer x, integer y, string text) Draw_String = #0;
void (integer x, integer y, string text, integer n) Draw_nString = #0;
void (integer x, integer y, string text) Draw_AltString = #0;
void (string text) Cbuf_AddText = #0;
void (string text) Cbuf_InsertText = #0;
void () Cbuf_Execute = #0;
void () Cbuf_Execute_Sets = #0;
//FIXME need a proper file struct, string sucks
string (string path, string mode) File_Open = #0;
void (string file) File_Close = #0;
string (string file) File_GetLine = #0;
string (integer old, integer new, string str) String_ReplaceChar = #0;
float () random = #0;
string () gametype = #0;
string (...) sprintf = #0;
float time;
entity self;
@ -127,6 +139,66 @@ void (integer x, integer y, integer width, integer lines) text_box =
Draw_Pic (cx, cy + 8, "gfx/box_br.lmp");
};
// ********* LOAD
#define MAX_SAVEGAMES 12
string [MAX_SAVEGAMES] filenames;
integer [MAX_SAVEGAMES] loadable;
integer load_cursor;
void () scan_saves =
{
local integer i;
local string f; //FIXME need a file type;
for (i = 0; i < MAX_SAVEGAMES; i++) {
loadable[i] = 0;
filenames[i] = "--- UNUSED SLOT ---";
f = File_Open (sprintf ("s%i.sav", i), "rz");
if (!f)
continue;
File_GetLine (f);
filenames[i] = String_ReplaceChar ('_', ' ', File_GetLine (f));
loadable[i] = 1;
File_Close (f);
}
};
void (string text, integer key) load_f =
{
scan_saves ();
Menu_SelectMenu ("load");
};
void (string text, integer key) save_f =
{
scan_saves ();
Menu_SelectMenu ("save");
};
void () load_draw =
{
local integer i;
Draw_CenterPic (160, 4, "gfx/p_load.lmp");
for (i=0 ; i< MAX_SAVEGAMES; i++)
Draw_String (16, 32 + 8 * i, filenames[i]);
Draw_Character (8, 32 + load_cursor * 8, 12 + (integer (time * 4) & 1));
};
integer (integer key, integer unicode, integer down) load_keyevent =
{
};
void () load_menu =
{
Menu_Begin (0, 0, "load");
Menu_KeyEvent (load_keyevent);
Menu_Draw (load_draw);
Menu_End ();
};
// ********* QUIT
integer () quit =
{
Menu_SelectMenu ("quit");
@ -162,6 +234,14 @@ void () quit_draw =
Draw_String (64, 108, quitMessage[quit_index *4 + 3]);
};
void () quit_menu =
{
Menu_Begin (0, 0, "quit");
Menu_KeyEvent (quit_keyevent);
Menu_Draw (quit_draw);
Menu_End ();
};
void (string text, integer key) sp_start =
{
Menu_SelectMenu (NIL);
@ -181,8 +261,8 @@ void () single_player_menu =
Menu_Pic (72, 32, "gfx/sp_menu.lmp");
Menu_Cursor (spinner);
Menu_Item (54, 32, "", sp_start);
Menu_Item (54, 52, "", quit_f);
Menu_Item (54, 72, "", quit_f);
Menu_Item (54, 52, "", load_f);
Menu_Item (54, 72, "", save_f);
Menu_End ();
};
@ -217,14 +297,6 @@ void () main_menu =
Menu_End ();
};
void () quit_menu =
{
Menu_Begin (0, 0, "quit");
Menu_KeyEvent (quit_keyevent);
Menu_Draw (quit_draw);
Menu_End ();
};
void () menu_init =
{
switch (gametype ()) {
@ -239,6 +311,7 @@ void () menu_init =
}
main_menu ();
quit_menu ();
load_menu ();
Menu_TopMenu ("main");
Menu_SetQuit (quit);
};

View file

@ -34,4 +34,9 @@
void BI_Init ();
struct progs_s;
void File_Progs_Init (struct progs_s *pr);
void String_Progs_Init (struct progs_s *pr);
#endif//__QF_csqc_h

View file

@ -35,6 +35,7 @@ static const char rcsid[] =
#include "QF/cmd.h"
#include "QF/console.h"
#include "QF/csqc.h"
#include "QF/draw.h"
#include "QF/hash.h"
#include "QF/plugin.h"
@ -316,6 +317,8 @@ Menu_Init (void)
PR_AddBuiltin (&menu_pr_state, "Menu_Quit", bi_Menu_Quit, -1);
Cbuf_Progs_Init (&menu_pr_state);
File_Progs_Init (&menu_pr_state);
String_Progs_Init (&menu_pr_state);
PR_Cmds_Init (&menu_pr_state);
R_Progs_Init (&menu_pr_state);
@ -396,7 +399,7 @@ Menu_Draw (void)
}
for (i = 0; i < menu->num_items; i++) {
if (menu->items[i]->text) {
Draw_String (menu->items[i]->x, menu->items[i]->y,
Draw_String (menu->items[i]->x + 8, menu->items[i]->y,
menu->items[i]->text);
}
}

View file

@ -8,4 +8,4 @@ libQFgamecode_builtins_la_LDFLAGS= -version-info 1:0:0
libQFgamecode_builtins_la_SOURCES= pr_cmds.c
libQFcsqc_la_LDFLAGS= -version-info 1:0:0
libQFcsqc_la_SOURCES= bi_init.c bi_cbuf.c
libQFcsqc_la_SOURCES= bi_init.c bi_cbuf.c bi_file.c bi_string.c

View file

@ -0,0 +1,145 @@
/*
bi_file.c
CSQC file builtins
Copyright (C) 1996-1997 Id Software, Inc.
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:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
static const char rcsid[] =
"$Id$";
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include "string.h"
#endif
#ifdef HAVE_STRINGS_H
# include "strings.h"
#endif
#include "QF/progs.h"
#include "QF/va.h"
#include "QF/vfs.h"
#include "QF/zone.h"
#define RETURN_STRING(p, s) ((p)->pr_globals[OFS_RETURN].integer_var = PR_SetString((p), s))
#define MAX_HANDLES 20
static VFile *handles[MAX_HANDLES];
static void
bi_File_Open (progs_t *pr)
{
const char *pth = G_STRING (pr, OFS_PARM0);
const char *mode = G_STRING (pr, OFS_PARM1);
char *path= Hunk_TempAlloc (strlen (pth) + 1);
char *p, *d;
int h;
for (d = path; *pth; d++, pth++) {
if (*pth == '\\')
*d = '/';
else
*d = *pth;
}
*d = 0;
p = path;
while (*p) {
if (p[0] == '.') {
if (p[1] == '.') {
if (p[2] == '/' || p[2] == 0) {
d = p;
while (d > path && d[-1] != '/')
d--;
if (d == path
|| (d[-1] == '.' && d[-2] == '.'
&& (d - 2 == path || d[-3] == '/')))
continue;
strcpy (d, p + 2);
p = d + 1;
}
} else if (p[1] == '/') {
strcpy (p, p + 2);
continue;
} else if (p[1] == 0) {
p[0] = 0;
}
}
while (*p && *p != '/')
p++;
if (*p == '/')
p++;
}
printf ("'%s' '%s'\n", G_STRING (pr, OFS_PARM0), path);
if (!path[0])
goto error;
if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || path [2] == 0))
goto error;
if (path[strlen (path) - 1] =='/')
goto error;
for (h = 0; h < MAX_HANDLES && handles[h]; h++)
;
if (h == MAX_HANDLES)
goto error;
if (!(handles[h] = Qopen (va ("%s/%s", com_gamedir, path), mode)))
goto error;
G_INT (pr, OFS_RETURN) = h + 1;
return;
error:
G_INT (pr, OFS_RETURN) = 0;
}
static void
bi_File_Close (progs_t *pr)
{
int h = G_INT (pr, OFS_PARM0) - 1;
if (h < 0 || h >= MAX_HANDLES || !handles[h])
return;
Qclose (handles[h]);
handles[h] = 0;
}
static void
bi_File_GetLine (progs_t *pr)
{
int h = G_INT (pr, OFS_PARM0) - 1;
const char *s;
if (h < 0 || h >= MAX_HANDLES || !handles[h]) {
G_INT (pr, OFS_RETURN) = 0;
return;
}
s = Qgetline (handles[h]);
RETURN_STRING (pr, s);
}
void
File_Progs_Init (progs_t *pr)
{
PR_AddBuiltin (pr, "File_Open", bi_File_Open, -1);
PR_AddBuiltin (pr, "File_Close", bi_File_Close, -1);
PR_AddBuiltin (pr, "File_GetLine", bi_File_GetLine, -1);
}

View file

@ -0,0 +1,69 @@
/*
bi_cbuf.c
CSQC cbuf builtins
Copyright (C) 1996-1997 Id Software, Inc.
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:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
static const char rcsid[] =
"$Id$";
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include "string.h"
#endif
#ifdef HAVE_STRINGS_H
# include "strings.h"
#endif
#include "QF/progs.h"
#include "QF/zone.h"
#define RETURN_STRING(p, s) ((p)->pr_globals[OFS_RETURN].integer_var = PR_SetString((p), s))
static void
bi_String_ReplaceChar (progs_t *pr)
{
char old = G_INT (pr, OFS_PARM0);
char new = G_INT (pr, OFS_PARM1);
const char *src = G_STRING (pr, OFS_PARM2);
const char *s;
char *dst = Hunk_TempAlloc (strlen (src) + 1);
char *d;
for (d = dst, s = src; *s; d++, s++) {
if (*s == old)
*d = new;
else
*d = *s;
}
RETURN_STRING (pr, dst);
}
void
String_Progs_Init (progs_t *pr)
{
PR_AddBuiltin (pr, "String_ReplaceChar", bi_String_ReplaceChar, -1);
}

View file

@ -51,6 +51,31 @@ bi_Draw_Pic (progs_t *pr)
Draw_Pic (x, y, pic);
}
static void
bi_Draw_CenterPic (progs_t *pr)
{
int x = G_INT (pr, OFS_PARM0);
int y = G_INT (pr, OFS_PARM1);
const char *path = G_STRING (pr, OFS_PARM2);
qpic_t *pic = Draw_CachePic (path, 1);
if (!pic) {
Con_DPrintf ("can't load %s\n", path);
return;
}
Draw_Pic (x + pic->width / 2, y, pic);
}
static void
bi_Draw_Character (progs_t *pr)
{
int x = G_INT (pr, OFS_PARM0);
int y = G_INT (pr, OFS_PARM1);
int c = G_INT (pr, OFS_PARM2);
Draw_Character (x, y, c);
}
static void
bi_Draw_String (progs_t *pr)
{
@ -61,9 +86,34 @@ bi_Draw_String (progs_t *pr)
Draw_String (x, y, text);
}
static void
bi_Draw_nString (progs_t *pr)
{
int x = G_INT (pr, OFS_PARM0);
int y = G_INT (pr, OFS_PARM1);
const char *text = G_STRING (pr, OFS_PARM2);
int n = G_INT (pr, OFS_PARM3);
Draw_nString (x, y, text, n);
}
static void
bi_Draw_AltString (progs_t *pr)
{
int x = G_INT (pr, OFS_PARM0);
int y = G_INT (pr, OFS_PARM1);
const char *text = G_STRING (pr, OFS_PARM2);
Draw_AltString (x, y, text);
}
void
R_Progs_Init (progs_t *pr)
{
PR_AddBuiltin (pr, "Draw_Pic", bi_Draw_Pic, -1);
PR_AddBuiltin (pr, "Draw_CenterPic", bi_Draw_CenterPic, -1);
PR_AddBuiltin (pr, "Draw_Character", bi_Draw_Character, -1);
PR_AddBuiltin (pr, "Draw_String", bi_Draw_String, -1);
PR_AddBuiltin (pr, "Draw_nString", bi_Draw_nString, -1);
PR_AddBuiltin (pr, "Draw_AltString", bi_Draw_AltString, -1);
}