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
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:&copy];
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);
}

View file

@ -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,

View file

@ -274,27 +274,37 @@ int skyoglid;
struct strip {
int tex, start, num;
};
vector<strip> 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);

View file

@ -2,35 +2,34 @@
#import "DynamicEntity.h"
#include <SDL_mixer.h>
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 <SDL_mixer.h>
#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<Mix_Chunk *> samples;
static OFMutableData *samples;
static OFMutableArray<OFString *> *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);
}

View file

@ -19,19 +19,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
# include <new>
#else
# include <new.h>
#endif
#import <ObjFW/ObjFW.h>
#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 <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

View file

@ -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)

View file

@ -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++)