More string cleanups

FossilOrigin-Name: a401a49dd9b3759c48509aa50fa13dab309bf09462a137f4ecbf3c98dce9d9e4
This commit is contained in:
Jonathan Schleifer 2025-03-04 01:13:36 +00:00
parent 0e59beaf43
commit db3bb84a6a
12 changed files with 52 additions and 44 deletions

View file

@ -35,11 +35,13 @@ quit() // normal exit
} }
void void
fatal(char *s, char *o) // failure exit fatal(OFString *s, OFString *o) // failure exit
{ {
sprintf_sd(msg)("%s%s (%s)\n", s, o, SDL_GetError()); OFString *msg =
[OFString stringWithFormat:@"%@%@ (%s)\n", s, o, SDL_GetError()];
OFApplication *app = OFApplication.sharedApplication; OFApplication *app = OFApplication.sharedApplication;
[(Cube *)app.delegate showMessage:@(msg)]; [(Cube *)app.delegate showMessage:msg];
[app terminateWithStatus:1]; [app terminateWithStatus:1];
} }
@ -48,7 +50,7 @@ alloc(int s) // for some big chunks... most other allocs use the memory pool
{ {
void *b = calloc(1, s); void *b = calloc(1, s);
if (!b) if (!b)
fatal("out of memory!"); fatal(@"out of memory!");
return b; return b;
} }
@ -151,11 +153,11 @@ int framesinmap = 0;
passwd = @""; passwd = @"";
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | par) < 0) if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | par) < 0)
fatal("Unable to initialize SDL"); fatal(@"Unable to initialize SDL");
log("net"); log("net");
if (enet_initialize() < 0) if (enet_initialize() < 0)
fatal("Unable to initialise network module"); fatal(@"Unable to initialise network module");
initclient(); initclient();
// never returns if dedicated // never returns if dedicated
@ -167,13 +169,13 @@ int framesinmap = 0;
log("video: sdl"); log("video: sdl");
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
fatal("Unable to initialize SDL Video"); fatal(@"Unable to initialize SDL Video");
log("video: mode"); log("video: mode");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (SDL_SetVideoMode(scr_w, scr_h, 0, if (SDL_SetVideoMode(scr_w, scr_h, 0,
SDL_OPENGL | (!windowed ? SDL_FULLSCREEN : 0)) == NULL) SDL_OPENGL | (!windowed ? SDL_FULLSCREEN : 0)) == NULL)
fatal("Unable to create OpenGL screen"); fatal(@"Unable to create OpenGL screen");
log("video: misc"); log("video: misc");
SDL_WM_SetCaption("cube engine", NULL); SDL_WM_SetCaption("cube engine", NULL);
@ -195,8 +197,8 @@ int framesinmap = 0;
!installtex(4, path(newstring("data/explosion.jpg")), xs, ys) || !installtex(4, path(newstring("data/explosion.jpg")), xs, ys) ||
!installtex(5, path(newstring("data/items.png")), xs, ys) || !installtex(5, path(newstring("data/items.png")), xs, ys) ||
!installtex(1, path(newstring("data/crosshair.png")), xs, ys)) !installtex(1, path(newstring("data/crosshair.png")), xs, ys))
fatal("could not find core textures (hint: run cube from the " fatal(@"could not find core textures (hint: run cube from the "
"parent of the bin directory)"); @"parent of the bin directory)");
log("sound"); log("sound");
initsound(); initsound();

View file

@ -192,14 +192,14 @@ addmsg(int rel, int num, int type, ...)
if (demoplayback) if (demoplayback)
return; return;
if (num != msgsizelookup(type)) { if (num != msgsizelookup(type)) {
sprintf_sd(s)("inconsistant msg size for %d (%d != %d)", type, fatal([OFString
num, msgsizelookup(type)); stringWithFormat:@"inconsistant msg size for %d (%d != %d)",
fatal(s); type, num, msgsizelookup(type)]);
}; }
if (messages.length() == 100) { if (messages.length() == 100) {
conoutf(@"command flood protection (type %d)", type); conoutf(@"command flood protection (type %d)", type);
return; return;
}; }
ivector &msg = messages.add(); ivector &msg = messages.add();
msg.add(num); msg.add(num);
msg.add(rel); msg.add(rel);

View file

@ -140,7 +140,7 @@ extern void computeraytable(float vx, float vy);
extern int isoccluded(float vx, float vy, float cx, float cy, float csize); extern int isoccluded(float vx, float vy, float cx, float cy, float csize);
// main // main
extern void fatal(char *s, char *o = ""); extern void fatal(OFString *s, OFString *o = @"");
extern void *alloc(int s); extern void *alloc(int s);
extern void keyrepeat(bool on); extern void keyrepeat(bool on);

View file

@ -13,7 +13,7 @@ setarraypointers()
glVertexPointer(3, GL_FLOAT, sizeof(vertex), &verts[0].x); glVertexPointer(3, GL_FLOAT, sizeof(vertex), &verts[0].x);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertex), &verts[0].r); glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertex), &verts[0].r);
glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), &verts[0].u); glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), &verts[0].u);
}; }
void void
reallocv() reallocv()
@ -21,9 +21,9 @@ reallocv()
verts = (vertex *)realloc(verts, (curmaxverts *= 2) * sizeof(vertex)); verts = (vertex *)realloc(verts, (curmaxverts *= 2) * sizeof(vertex));
curmaxverts -= 10; curmaxverts -= 10;
if (!verts) if (!verts)
fatal("no vertex memory!"); fatal(@"no vertex memory!");
setarraypointers(); setarraypointers();
}; }
// generating the actual vertices is done dynamically every frame and sits at // generating the actual vertices is done dynamically every frame and sits at
// the leaves of all these functions, and are part of the cpu bottleneck on // the leaves of all these functions, and are part of the cpu bottleneck on

View file

@ -56,7 +56,7 @@ gl_init(int w, int h)
purgetextures(); purgetextures();
if (!(qsphere = gluNewQuadric())) if (!(qsphere = gluNewQuadric()))
fatal("glu sphere"); fatal(@"glu sphere");
gluQuadricDrawStyle(qsphere, GLU_FILL); gluQuadricDrawStyle(qsphere, GLU_FILL);
gluQuadricOrientation(qsphere, GLU_INSIDE); gluQuadricOrientation(qsphere, GLU_INSIDE);
gluQuadricTexture(qsphere, GL_TRUE); gluQuadricTexture(qsphere, GL_TRUE);
@ -111,7 +111,7 @@ installtex(int tnum, char *texname, int &xs, int &ys, bool clamp)
}; };
if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, xs, ys, GL_RGB, if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, xs, ys, GL_RGB,
GL_UNSIGNED_BYTE, scaledimg)) GL_UNSIGNED_BYTE, scaledimg))
fatal("could not build mipmaps"); fatal(@"could not build mipmaps");
if (xs != s->w) if (xs != s->w)
free(scaledimg); free(scaledimg);
SDL_FreeSurface(s); SDL_FreeSurface(s);
@ -191,7 +191,7 @@ lookuptexture(int tex, int &xs, int &ys)
}; };
if (curtex == MAXTEX) if (curtex == MAXTEX)
fatal("loaded too many textures"); fatal(@"loaded too many textures");
int tnum = curtex + FIRSTTEX; int tnum = curtex + FIRSTTEX;
strcpy_s(texname[curtex], mapname[tex][frame]); strcpy_s(texname[curtex], mapname[tex][frame]);

View file

@ -238,7 +238,7 @@ delayedload(MD2 *m)
sprintf_sd(name1)("packages/models/%s/tris.md2", sprintf_sd(name1)("packages/models/%s/tris.md2",
m.loadname.UTF8String); m.loadname.UTF8String);
if (![m loadWithPath:path(name1)]) if (![m loadWithPath:path(name1)])
fatal("loadmodel: ", name1); fatal(@"loadmodel: ", @(name1));
sprintf_sd(name2)("packages/models/%s/skin.jpg", sprintf_sd(name2)("packages/models/%s/skin.jpg",
m.loadname.UTF8String); m.loadname.UTF8String);
int xs, ys; int xs, ys;

View file

@ -40,7 +40,7 @@ void
gzcheck(int a, int b) gzcheck(int a, int b)
{ {
if (a != b) if (a != b)
fatal("savegame file corrupt (short)"); fatal(@"savegame file corrupt (short)");
} }
int int

View file

@ -523,9 +523,9 @@ initserver(bool dedicated, int uprate, const char *sdesc, const char *ip,
printf("WARNING: server ip not resolved"); printf("WARNING: server ip not resolved");
serverhost = enet_host_create(&address, MAXCLIENTS, 0, uprate); serverhost = enet_host_create(&address, MAXCLIENTS, 0, uprate);
if (!serverhost) if (!serverhost)
fatal("could not create server host\n"); fatal(@"could not create server host\n");
loopi(MAXCLIENTS) serverhost->peers[i].data = (void *)-1; loopi(MAXCLIENTS) serverhost->peers[i].data = (void *)-1;
}; }
resetserverifempty(); resetserverifempty();

View file

@ -143,8 +143,8 @@ serverms(int mode, int numplayers, int minremain, char *smapname, int seconds,
sendstring(serverdesc, p); sendstring(serverdesc, p);
buf.dataLength = p - pong; buf.dataLength = p - pong;
enet_socket_send(pongsock, &addr, &buf, 1); enet_socket_send(pongsock, &addr, &buf, 1);
}; }
}; }
void void
servermsinit(const char *master, const char *sdesc, bool listen) servermsinit(const char *master, const char *sdesc, bool listen)
@ -161,6 +161,6 @@ servermsinit(const char *master, const char *sdesc, bool listen)
pongsock = pongsock =
enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM, &address); enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM, &address);
if (pongsock == ENET_SOCKET_NULL) if (pongsock == ENET_SOCKET_NULL)
fatal("could not create server info socket\n"); fatal(@"could not create server info socket\n");
}; }
}; }

View file

@ -132,22 +132,27 @@ recvmap(int n)
#ifdef STANDALONE #ifdef STANDALONE
void localservertoclient(uchar *buf, int len) {};
void void
fatal(char *s, char *o) localservertoclient(uchar *buf, int len)
{
}
void
fatal(OFString *s, OFString *o)
{ {
cleanupserver(); cleanupserver();
printf("servererror: %s\n", s); [OFStdOut writeFormat:@"servererror: %@\n", s];
exit(1); exit(1);
}; }
void * void *
alloc(int s) alloc(int s)
{ {
void *b = calloc(1, s); void *b = calloc(1, s);
if (!b) if (!b)
fatal("no memory!"); fatal(@"no memory!");
return b; return b;
}; }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -183,7 +188,7 @@ main(int argc, char *argv[])
}; };
if (enet_initialize() < 0) if (enet_initialize() < 0)
fatal("Unable to initialise network module"); fatal(@"Unable to initialise network module");
initserver(true, uprate, sdesc, ip, master, @(passwd), maxcl); initserver(true, uprate, sdesc, ip, master, @(passwd), maxcl);
return 0; return 0;
} }

View file

@ -70,7 +70,7 @@ initsound()
Mix_AllocateChannels(MAXCHAN); Mix_AllocateChannels(MAXCHAN);
#else #else
if (FSOUND_GetVersion() < FMOD_VERSION) if (FSOUND_GetVersion() < FMOD_VERSION)
fatal("old FMOD dll"); fatal(@"old FMOD dll");
if (!FSOUND_Init(SOUNDFREQ, MAXCHAN, FSOUND_INIT_GLOBALFOCUS)) { if (!FSOUND_Init(SOUNDFREQ, MAXCHAN, FSOUND_INIT_GLOBALFOCUS)) {
conoutf(@"sound init failed (FMOD): %d", FSOUND_GetError()); conoutf(@"sound init failed (FMOD): %d", FSOUND_GetError());
nosound = true; nosound = true;

View file

@ -262,11 +262,11 @@ load_world(char *mname) // still supports all map formats that have existed
gzread(f, &hdr, sizeof(header) - sizeof(int) * 16); gzread(f, &hdr, sizeof(header) - sizeof(int) * 16);
endianswap(&hdr.version, sizeof(int), 4); endianswap(&hdr.version, sizeof(int), 4);
if (strncmp(hdr.head, "CUBE", 4) != 0) if (strncmp(hdr.head, "CUBE", 4) != 0)
fatal("while reading map: header malformatted"); fatal(@"while reading map: header malformatted");
if (hdr.version > MAPVERSION) if (hdr.version > MAPVERSION)
fatal("this map requires a newer version of cube"); fatal(@"this map requires a newer version of cube");
if (sfactor < SMALLEST_FACTOR || sfactor > LARGEST_FACTOR) if (sfactor < SMALLEST_FACTOR || sfactor > LARGEST_FACTOR)
fatal("illegal map size"); fatal(@"illegal map size");
if (hdr.version >= 4) { if (hdr.version >= 4) {
gzread(f, &hdr.waterlevel, sizeof(int) * 16); gzread(f, &hdr.waterlevel, sizeof(int) * 16);
endianswap(&hdr.waterlevel, sizeof(int), 16); endianswap(&hdr.waterlevel, sizeof(int), 16);
@ -329,8 +329,9 @@ load_world(char *mname) // still supports all map formats that have existed
} }
default: { default: {
if (type < 0 || type >= MAXTYPE) { if (type < 0 || type >= MAXTYPE) {
sprintf_sd(t)("%d @ %d", type, k); OFString *t = [OFString
fatal("while reading map: type out of range: ", stringWithFormat:@"%d @ %d", type, k];
fatal(@"while reading map: type out of range: ",
t); t);
} }
s->type = type; s->type = type;