Remove last usage of vector

FossilOrigin-Name: 08c9d7b0fa8f592b68069fe2444e016d4a06025d04cae3f8271575cf0763a182
This commit is contained in:
Jonathan Schleifer 2025-03-20 17:12:11 +00:00
parent 5b200abd87
commit d25e07085c
7 changed files with 78 additions and 184 deletions

View file

@ -239,25 +239,33 @@ cursorupdate() // called every frame from hud
} }
} }
vector<block *> undos; // unlimited undo static OFMutableData *undos; // unlimited undo
VARP(undomegs, 0, 1, 10); // bounded by n megs VARP(undomegs, 0, 1, 10); // bounded by n megs
void void
pruneundos(int maxremain) // bound memory pruneundos(int maxremain) // bound memory
{ {
int t = 0; int t = 0;
loopvrev(undos) for (ssize_t i = (ssize_t)undos.count - 1; i >= 0; i--) {
{ block *undo = *(block **)[undos itemAtIndex:i];
t += undos[i]->xs * undos[i]->ys * sizeof(sqr);
if (t > maxremain) t += undo->xs * undo->ys * sizeof(sqr);
OFFreeMemory(undos.remove(i)); if (t > maxremain) {
OFFreeMemory(undo);
[undos removeItemAtIndex:i];
}
} }
} }
void void
makeundo() makeundo()
{ {
undos.add(blockcopy(sel)); if (undos == nil)
undos =
[[OFMutableData alloc] initWithItemSize:sizeof(block *)];
block *copy = blockcopy(sel);
[undos addItem:&copy];
pruneundos(undomegs << 20); pruneundos(undomegs << 20);
} }
@ -265,11 +273,12 @@ void
editundo() editundo()
{ {
EDITMP; EDITMP;
if (undos.empty()) { if (undos.count == 0) {
conoutf(@"nothing more to undo"); conoutf(@"nothing more to undo");
return; return;
} }
block *p = undos.pop(); block *p = *(block **)undos.lastItem;
[undos removeLastItem];
blockpaste(*p); blockpaste(*p);
OFFreeMemory(p); OFFreeMemory(p);
} }

View file

@ -115,7 +115,7 @@ extern void renderscores();
extern void setupworld(int factor); extern void setupworld(int factor);
extern void empty_world(int factor, bool force); extern void empty_world(int factor, bool force);
extern void remip(block &b, int level = 0); 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 closestent();
extern int findentity(int type, int index = 0); extern int findentity(int type, int index = 0);
extern void trigger(int tag, int type, bool savegame); 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); int strength, DynamicEntity *owner);
extern void cleardlights(); extern void cleardlights();
extern block *blockcopy(block &b); extern block *blockcopy(block &b);
extern void blockpaste(block &b); extern void blockpaste(const block &b);
// worldrender // worldrender
extern void render_world(float vx, float vy, float vh, int yaw, int pitch, extern void render_world(float vx, float vy, float vh, int yaw, int pitch,

View file

@ -274,27 +274,37 @@ int skyoglid;
struct strip { struct strip {
int tex, start, num; int tex, start, num;
}; };
vector<strip> strips; static OFMutableData *strips;
void void
renderstripssky() renderstripssky()
{ {
glBindTexture(GL_TEXTURE_2D, skyoglid); 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 void
renderstrips() renderstrips()
{ {
int lasttex = -1; int lasttex = -1;
loopv(strips) if (strips[i].tex != skyoglid) const strip *items = (const strip *)strips.items;
{ size_t count = strips.count;
if (strips[i].tex != lasttex) { for (size_t i = 0; i < count; i++) {
glBindTexture(GL_TEXTURE_2D, strips[i].tex); if (items[i].tex == skyoglid)
lasttex = strips[i].tex; 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 void
addstrip(int tex, int start, int n) addstrip(int tex, int start, int n)
{ {
strip &s = strips.add(); if (strips == nil)
s.tex = tex; strips = [[OFMutableData alloc] initWithItemSize:sizeof(strip)];
s.start = start;
s.num = n; strip s = { .tex = tex, .start = start, .num = n };
[strips addItem:&s];
} }
VARFP(gamma, 30, 100, 300, { VARFP(gamma, 30, 100, 300, {
@ -434,7 +445,7 @@ gl_drawframe(int w, int h, float curfps)
resetcubes(); resetcubes();
curvert = 0; curvert = 0;
strips.setsize(0); [strips removeAllItems];
render_world(player1.o.x, player1.o.y, player1.o.z, (int)player1.yaw, render_world(player1.o.x, player1.o.y, player1.o.z, (int)player1.yaw,
(int)player1.pitch, (float)fov, w, h); (int)player1.pitch, (float)fov, w, h);

View file

@ -2,35 +2,34 @@
#import "DynamicEntity.h" #import "DynamicEntity.h"
#include <SDL_mixer.h>
VARP(soundvol, 0, 255, 255); VARP(soundvol, 0, 255, 255);
VARP(musicvol, 0, 128, 255); VARP(musicvol, 0, 128, 255);
bool nosound = false; bool nosound = false;
#define MAXCHAN 32 #define MAXCHAN 32
#define SOUNDFREQ 22050 #define SOUNDFREQ 22050
#define MAXVOL MIX_MAX_VOLUME
struct soundloc { struct soundloc {
OFVector3D loc; OFVector3D loc;
bool inuse; bool inuse;
} soundlocs[MAXCHAN]; } soundlocs[MAXCHAN];
#include <SDL_mixer.h> static Mix_Music *mod = NULL;
#define MAXVOL MIX_MAX_VOLUME
Mix_Music *mod = NULL;
void *stream = NULL;
void void
stopsound() stopsound()
{ {
if (nosound) if (nosound)
return; return;
if (mod) {
if (mod != NULL) {
Mix_HaltMusic(); Mix_HaltMusic();
Mix_FreeMusic(mod); Mix_FreeMusic(mod);
mod = NULL; mod = NULL;
} }
if (stream != NULL)
stream = NULL;
} }
VAR(soundbufferlen, 128, 1024, 4096); VAR(soundbufferlen, 128, 1024, 4096);
@ -53,7 +52,9 @@ music(OFString *name)
{ {
if (nosound) if (nosound)
return; return;
stopsound(); stopsound();
if (soundvol && musicvol) { if (soundvol && musicvol) {
name = [name stringByReplacingOccurrencesOfString:@"\\" name = [name stringByReplacingOccurrencesOfString:@"\\"
withString:@"/"]; withString:@"/"];
@ -71,8 +72,7 @@ music(OFString *name)
} }
COMMAND(music, ARG_1STR) COMMAND(music, ARG_1STR)
vector<Mix_Chunk *> samples; static OFMutableData *samples;
static OFMutableArray<OFString *> *snames; static OFMutableArray<OFString *> *snames;
int int
@ -88,12 +88,16 @@ registersound(OFString *name)
if (snames == nil) if (snames == nil)
snames = [[OFMutableArray alloc] init]; snames = [[OFMutableArray alloc] init];
if (samples == nil)
samples = [[OFMutableData alloc]
initWithItemSize:sizeof(Mix_Chunk *)];
[snames addObject:[name stringByReplacingOccurrencesOfString:@"\\" [snames addObject:[name stringByReplacingOccurrencesOfString:@"\\"
withString:@"/"]]; withString:@"/"]];
samples.add(NULL); Mix_Chunk *sample = NULL;
[samples addItem:&sample];
return samples.length() - 1; return samples.count - 1;
} }
COMMAND(registersound, ARG_1EST) COMMAND(registersound, ARG_1EST)
@ -165,41 +169,48 @@ playsound(int n, const OFVector3D *loc)
{ {
if (nosound) if (nosound)
return; return;
if (!soundvol) if (!soundvol)
return; return;
if (lastmillis == lastsoundmillis) if (lastmillis == lastsoundmillis)
soundsatonce++; soundsatonce++;
else else
soundsatonce = 1; soundsatonce = 1;
lastsoundmillis = lastmillis; lastsoundmillis = lastmillis;
if (soundsatonce > 5) if (soundsatonce > 5)
return; // avoid bursts of sounds with heavy packetloss // avoid bursts of sounds with heavy packetloss and in sp
// and in sp return;
if (n < 0 || n >= samples.length()) {
if (n < 0 || n >= samples.count) {
conoutf(@"unregistered sound: %d", n); conoutf(@"unregistered sound: %d", n);
return; return;
} }
if (!samples[n]) { Mix_Chunk **sample = (Mix_Chunk **)[samples mutableItemAtIndex:n];
if (*sample == NULL) {
OFString *path = [OFString OFString *path = [OFString
stringWithFormat:@"packages/sounds/%@.wav", snames[n]]; stringWithFormat:@"packages/sounds/%@.wav", snames[n]];
OFIRI *IRI = [Cube.sharedInstance.gameDataIRI OFIRI *IRI = [Cube.sharedInstance.gameDataIRI
IRIByAppendingPathComponent:path]; IRIByAppendingPathComponent:path];
samples[n] = *sample = Mix_LoadWAV(IRI.fileSystemRepresentation.UTF8String);
Mix_LoadWAV(IRI.fileSystemRepresentation.UTF8String);
if (!samples[n]) { if (*sample == NULL) {
conoutf(@"failed to load sample: %@", IRI.string); conoutf(@"failed to load sample: %@", IRI.string);
return; return;
} }
} }
int chan = Mix_PlayChannel(-1, samples[n], 0); int chan = Mix_PlayChannel(-1, *sample, 0);
if (chan < 0) if (chan < 0)
return; return;
if (loc) if (loc)
newsoundloc(chan, loc); newsoundloc(chan, loc);
updatechanvol(chan, loc); updatechanvol(chan, loc);
} }

View file

@ -19,19 +19,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef __GNUC__
# include <new>
#else
# include <new.h>
#endif
#import <ObjFW/ObjFW.h> #import <ObjFW/ObjFW.h>
#ifdef NULL
# undef NULL
#endif
#define NULL 0
typedef unsigned char uchar; typedef unsigned char uchar;
typedef unsigned short ushort; typedef unsigned short ushort;
typedef unsigned int uint; typedef unsigned int uint;
@ -52,137 +42,10 @@ typedef unsigned int uint;
#ifndef OF_WINDOWS #ifndef OF_WINDOWS
# define __cdecl # define __cdecl
# define _vsnprintf vsnprintf
#endif #endif
#define fast_f2nat(val) ((int)(val)) #define fast_f2nat(val) ((int)(val))
extern void endianswap(void *, int, int); extern void endianswap(void *, int, int);
template <class T> 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<T> &v);
void operator=(vector<T> &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 #endif

View file

@ -242,7 +242,7 @@ remip(block &b, int level)
} }
void void
remipmore(block &b, int level) remipmore(const block &b, int level)
{ {
block bb = b; block bb = b;
if (bb.x > 1) if (bb.x > 1)

View file

@ -262,7 +262,7 @@ blockcopy(block &s)
} }
void void
blockpaste(block &b) blockpaste(const block &b)
{ {
sqr *q = (sqr *)((&b) + 1); sqr *q = (sqr *)((&b) + 1);
for (int x = b.x; x < b.xs + b.x; x++) for (int x = b.x; x < b.xs + b.x; x++)