From d25e07085cb5d9b77ecafccece4b02f1cb91fe27 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Thu, 20 Mar 2025 17:12:11 +0000 Subject: [PATCH] Remove last usage of vector FossilOrigin-Name: 08c9d7b0fa8f592b68069fe2444e016d4a06025d04cae3f8271575cf0763a182 --- src/editing.mm | 29 ++++++---- src/protos.h | 4 +- src/rendergl.mm | 39 ++++++++----- src/sound.mm | 49 ++++++++++------- src/tools.h | 137 ---------------------------------------------- src/world.mm | 2 +- src/worldlight.mm | 2 +- 7 files changed, 78 insertions(+), 184 deletions(-) diff --git a/src/editing.mm b/src/editing.mm index 63f5d17..b2f829e 100644 --- a/src/editing.mm +++ b/src/editing.mm @@ -239,25 +239,33 @@ cursorupdate() // called every frame from hud } } -vector undos; // unlimited undo -VARP(undomegs, 0, 1, 10); // bounded by n megs +static OFMutableData *undos; // unlimited undo +VARP(undomegs, 0, 1, 10); // bounded by n megs void pruneundos(int maxremain) // bound memory { int t = 0; - loopvrev(undos) - { - t += undos[i]->xs * undos[i]->ys * sizeof(sqr); - if (t > maxremain) - OFFreeMemory(undos.remove(i)); + for (ssize_t i = (ssize_t)undos.count - 1; i >= 0; i--) { + block *undo = *(block **)[undos itemAtIndex:i]; + + t += undo->xs * undo->ys * sizeof(sqr); + if (t > maxremain) { + OFFreeMemory(undo); + [undos removeItemAtIndex:i]; + } } } void makeundo() { - undos.add(blockcopy(sel)); + if (undos == nil) + undos = + [[OFMutableData alloc] initWithItemSize:sizeof(block *)]; + + block *copy = blockcopy(sel); + [undos addItem:©]; pruneundos(undomegs << 20); } @@ -265,11 +273,12 @@ void editundo() { EDITMP; - if (undos.empty()) { + if (undos.count == 0) { conoutf(@"nothing more to undo"); return; } - block *p = undos.pop(); + block *p = *(block **)undos.lastItem; + [undos removeLastItem]; blockpaste(*p); OFFreeMemory(p); } diff --git a/src/protos.h b/src/protos.h index 4438c76..c36ae8b 100644 --- a/src/protos.h +++ b/src/protos.h @@ -115,7 +115,7 @@ extern void renderscores(); extern void setupworld(int factor); extern void empty_world(int factor, bool force); extern void remip(block &b, int level = 0); -extern void remipmore(block &b, int level = 0); +extern void remipmore(const block &b, int level = 0); extern int closestent(); extern int findentity(int type, int index = 0); extern void trigger(int tag, int type, bool savegame); @@ -130,7 +130,7 @@ extern void dodynlight(const OFVector3D &vold, const OFVector3D &v, int reach, int strength, DynamicEntity *owner); extern void cleardlights(); extern block *blockcopy(block &b); -extern void blockpaste(block &b); +extern void blockpaste(const block &b); // worldrender extern void render_world(float vx, float vy, float vh, int yaw, int pitch, diff --git a/src/rendergl.mm b/src/rendergl.mm index 3f9c9b0..2d1daf8 100644 --- a/src/rendergl.mm +++ b/src/rendergl.mm @@ -274,27 +274,37 @@ int skyoglid; struct strip { int tex, start, num; }; -vector strips; +static OFMutableData *strips; void renderstripssky() { glBindTexture(GL_TEXTURE_2D, skyoglid); - loopv(strips) if (strips[i].tex == skyoglid) - glDrawArrays(GL_TRIANGLE_STRIP, strips[i].start, strips[i].num); + + const strip *items = (const strip *)strips.items; + size_t count = strips.count; + for (size_t i = 0; i < count; i++) + if (items[i].tex == skyoglid) + glDrawArrays( + GL_TRIANGLE_STRIP, items[i].start, items[i].num); } void renderstrips() { int lasttex = -1; - loopv(strips) if (strips[i].tex != skyoglid) - { - if (strips[i].tex != lasttex) { - glBindTexture(GL_TEXTURE_2D, strips[i].tex); - lasttex = strips[i].tex; + const strip *items = (const strip *)strips.items; + size_t count = strips.count; + for (size_t i = 0; i < count; i++) { + if (items[i].tex == skyoglid) + continue; + + if (items[i].tex != lasttex) { + glBindTexture(GL_TEXTURE_2D, items[i].tex); + lasttex = items[i].tex; } - glDrawArrays(GL_TRIANGLE_STRIP, strips[i].start, strips[i].num); + + glDrawArrays(GL_TRIANGLE_STRIP, items[i].start, items[i].num); } } @@ -308,10 +318,11 @@ overbright(float amount) void addstrip(int tex, int start, int n) { - strip &s = strips.add(); - s.tex = tex; - s.start = start; - s.num = n; + if (strips == nil) + strips = [[OFMutableData alloc] initWithItemSize:sizeof(strip)]; + + strip s = { .tex = tex, .start = start, .num = n }; + [strips addItem:&s]; } VARFP(gamma, 30, 100, 300, { @@ -434,7 +445,7 @@ gl_drawframe(int w, int h, float curfps) resetcubes(); curvert = 0; - strips.setsize(0); + [strips removeAllItems]; render_world(player1.o.x, player1.o.y, player1.o.z, (int)player1.yaw, (int)player1.pitch, (float)fov, w, h); diff --git a/src/sound.mm b/src/sound.mm index c5c2bac..5f42cf4 100644 --- a/src/sound.mm +++ b/src/sound.mm @@ -2,35 +2,34 @@ #import "DynamicEntity.h" +#include + VARP(soundvol, 0, 255, 255); VARP(musicvol, 0, 128, 255); bool nosound = false; #define MAXCHAN 32 #define SOUNDFREQ 22050 +#define MAXVOL MIX_MAX_VOLUME struct soundloc { OFVector3D loc; bool inuse; } soundlocs[MAXCHAN]; -#include -#define MAXVOL MIX_MAX_VOLUME -Mix_Music *mod = NULL; -void *stream = NULL; +static Mix_Music *mod = NULL; void stopsound() { if (nosound) return; - if (mod) { + + if (mod != NULL) { Mix_HaltMusic(); Mix_FreeMusic(mod); mod = NULL; } - if (stream != NULL) - stream = NULL; } VAR(soundbufferlen, 128, 1024, 4096); @@ -53,7 +52,9 @@ music(OFString *name) { if (nosound) return; + stopsound(); + if (soundvol && musicvol) { name = [name stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]; @@ -71,8 +72,7 @@ music(OFString *name) } COMMAND(music, ARG_1STR) -vector samples; - +static OFMutableData *samples; static OFMutableArray *snames; int @@ -88,12 +88,16 @@ registersound(OFString *name) if (snames == nil) snames = [[OFMutableArray alloc] init]; + if (samples == nil) + samples = [[OFMutableData alloc] + initWithItemSize:sizeof(Mix_Chunk *)]; [snames addObject:[name stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]]; - samples.add(NULL); + Mix_Chunk *sample = NULL; + [samples addItem:&sample]; - return samples.length() - 1; + return samples.count - 1; } COMMAND(registersound, ARG_1EST) @@ -165,41 +169,48 @@ playsound(int n, const OFVector3D *loc) { if (nosound) return; + if (!soundvol) return; + if (lastmillis == lastsoundmillis) soundsatonce++; else soundsatonce = 1; + lastsoundmillis = lastmillis; + if (soundsatonce > 5) - return; // avoid bursts of sounds with heavy packetloss - // and in sp - if (n < 0 || n >= samples.length()) { + // avoid bursts of sounds with heavy packetloss and in sp + return; + + if (n < 0 || n >= samples.count) { conoutf(@"unregistered sound: %d", n); return; } - if (!samples[n]) { + Mix_Chunk **sample = (Mix_Chunk **)[samples mutableItemAtIndex:n]; + if (*sample == NULL) { OFString *path = [OFString stringWithFormat:@"packages/sounds/%@.wav", snames[n]]; OFIRI *IRI = [Cube.sharedInstance.gameDataIRI IRIByAppendingPathComponent:path]; - samples[n] = - Mix_LoadWAV(IRI.fileSystemRepresentation.UTF8String); + *sample = Mix_LoadWAV(IRI.fileSystemRepresentation.UTF8String); - if (!samples[n]) { + if (*sample == NULL) { conoutf(@"failed to load sample: %@", IRI.string); return; } } - int chan = Mix_PlayChannel(-1, samples[n], 0); + int chan = Mix_PlayChannel(-1, *sample, 0); if (chan < 0) return; + if (loc) newsoundloc(chan, loc); + updatechanvol(chan, loc); } diff --git a/src/tools.h b/src/tools.h index bf6aa1f..826acf7 100644 --- a/src/tools.h +++ b/src/tools.h @@ -19,19 +19,9 @@ #include #include #include -#ifdef __GNUC__ -# include -#else -# include -#endif #import -#ifdef NULL -# undef NULL -#endif -#define NULL 0 - typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; @@ -52,137 +42,10 @@ typedef unsigned int uint; #ifndef OF_WINDOWS # define __cdecl -# define _vsnprintf vsnprintf #endif #define fast_f2nat(val) ((int)(val)) extern void endianswap(void *, int, int); -template struct vector { - T *buf; - int alen; - int ulen; - - vector() - { - alen = 8; - buf = (T *)OFAllocMemory(alen, sizeof(T)); - ulen = 0; - } - - ~vector() - { - setsize(0); - free(buf); - } - - vector(vector &v); - void operator=(vector &v); - - T & - add(const T &x) - { - if (ulen == alen) - realloc(); - new (&buf[ulen]) T(x); - return buf[ulen++]; - } - - T & - add() - { - if (ulen == alen) - realloc(); - new (&buf[ulen]) T; - return buf[ulen++]; - } - - T & - pop() - { - return buf[--ulen]; - } - - T & - last() - { - return buf[ulen - 1]; - } - - bool - empty() - { - return ulen == 0; - } - - int - length() - { - return ulen; - } - - T & - operator[](int i) - { - assert(i >= 0 && i < ulen); - return buf[i]; - } - - void - setsize(int i) - { - for (; ulen > i; ulen--) - buf[ulen - 1].~T(); - } - - T * - getbuf() - { - return buf; - } - - void - sort(void *cf) - { - qsort(buf, ulen, sizeof(T), - (int(__cdecl *)(const void *, const void *))cf); - } - - void - realloc() - { - buf = (T *)OFResizeMemory(buf, (alen *= 2), sizeof(T)); - } - - T - remove(int i) - { - T e = buf[i]; - for (int p = i + 1; p < ulen; p++) - buf[p - 1] = buf[p]; - ulen--; - return e; - } - - T & - insert(int i, const T &e) - { - add(T()); - for (int p = ulen - 1; p > i; p--) - buf[p] = buf[p - 1]; - buf[i] = e; - return buf[i]; - } -}; - -#define loopv(v) \ - if (false) { \ - } else \ - for (int i = 0; i < (v).length(); i++) -#define loopvrev(v) \ - if (false) { \ - } else \ - for (int i = (v).length() - 1; i >= 0; i--) - #endif diff --git a/src/world.mm b/src/world.mm index b6a66a7..9206565 100644 --- a/src/world.mm +++ b/src/world.mm @@ -242,7 +242,7 @@ remip(block &b, int level) } void -remipmore(block &b, int level) +remipmore(const block &b, int level) { block bb = b; if (bb.x > 1) diff --git a/src/worldlight.mm b/src/worldlight.mm index 95a32e4..76cb443 100644 --- a/src/worldlight.mm +++ b/src/worldlight.mm @@ -262,7 +262,7 @@ blockcopy(block &s) } void -blockpaste(block &b) +blockpaste(const block &b) { sqr *q = (sqr *)((&b) + 1); for (int x = b.x; x < b.xs + b.x; x++)