[image] Change tex_t data from array to pointer

This makes tex_t more generally useable and probably more portable. The
goal was to be able to use tex_t with data that is in a separate chunk
of memory.
This commit is contained in:
Bill Currie 2021-01-25 00:54:41 +09:00
parent af5415010a
commit 7a19be7265
14 changed files with 47 additions and 26 deletions

View file

@ -48,7 +48,7 @@ typedef struct tex_s {
QFFormat format;
int loaded; // 0 if size info only, otherwise data loaded
byte *palette; // 0 = 32 bit, otherwise 8
byte data[4]; // variable length
byte *data;
} tex_t;
tex_t *LoadImage (const char *imageFile, int load);

View file

@ -93,11 +93,13 @@ LoadPCX (QFile *f, qboolean convert, byte *pal, int load)
count = load ? (pcx->xmax + 1) * (pcx->ymax + 1) : 0;
if (convert) {
tex = Hunk_TempAlloc (field_offset (tex_t, data[count * 3]));
tex = Hunk_TempAlloc (sizeof (tex_t) + count * 3);
tex->data = (byte *) (tex + 1);
tex->format = tex_rgb;
tex->palette = 0;
} else {
tex = Hunk_TempAlloc (field_offset (tex_t, data[count]));
tex = Hunk_TempAlloc (sizeof (tex_t) + count);
tex->data = (byte *) (tex + 1);
tex->format = tex_palette;
if (pal)
tex->palette = pal;

View file

@ -162,9 +162,11 @@ LoadPNG (QFile *infile, int load)
/* Allocate tex_t structure */
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
tex = Hunk_TempAlloc (field_offset (tex_t, data[height * rowbytes]));
tex = Hunk_TempAlloc (sizeof (tex_t) + height * rowbytes);
tex->data = (byte *) (tex + 1);
} else {
tex = Hunk_TempAlloc (field_offset (tex_t, data[0]));
tex = Hunk_TempAlloc (sizeof (tex_t));
tex->data = 0;
}
tex->width = width;

View file

@ -655,7 +655,8 @@ LoadTGA (QFile *fin, int load)
} else {
numPixels = 0;
}
tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 4]));
tex = Hunk_TempAlloc (sizeof (tex_t) + numPixels * 4);
tex->data = (byte *) (tex + 1);
tex->width = targa->width;
tex->height = targa->height;
tex->palette = 0;

View file

@ -72,7 +72,8 @@ do_fb_skin (glskin_t *s)
{
int size = s->tex->width * s->tex->height;
s->fb_tex = realloc (s->fb_tex, field_offset(tex_t, data[size]));
s->fb_tex = realloc (s->fb_tex, sizeof (tex_t) + size);
s->fb_tex->data = (byte *) (s->fb_tex + 1);
s->fb_tex->width = s->tex->width;
s->fb_tex->height = s->tex->height;
s->fb_tex->format = tex_palette;
@ -87,7 +88,8 @@ gl_Skin_SetPlayerSkin (int width, int height, const byte *data)
glskin_t *s;
s = &player_skin;
s->tex = realloc (s->tex, field_offset(tex_t, data[size]));
s->tex = realloc (s->tex, sizeof (tex_t) + size);
s->tex->data = (byte *) (s->tex + 1);
s->tex->width = width;
s->tex->height = height;
s->tex->format = tex_palette;

View file

@ -53,9 +53,8 @@
#include "mod_internal.h"
#include "r_internal.h"
static tex_t null_texture = {
2, 2, tex_palette, 1, 0, {15, 15, 15, 15}
};
static byte null_data[] = {15, 15, 15, 15};
static tex_t null_texture = { 2, 2, tex_palette, 1, 0, null_data };
static void
sw_iqm_clear (model_t *mod, void *data)
@ -109,7 +108,8 @@ convert_tex (tex_t *tex)
int i;
pixels = tex->width * tex->height;
new = malloc (field_offset (tex_t, data[pixels]));
new = malloc (sizeof (tex_t) + pixels);
new->data = (byte *) (new + 1);
new->width = tex->width;
new->height = tex->height;
new->format = tex_palette;

View file

@ -193,7 +193,8 @@ Skin_SetSkin (skin_t *skin, int cmap, const char *skinname)
tex = 0;
break;
}
out = malloc (field_offset (tex_t, data[PLAYER_WIDTH*PLAYER_HEIGHT]));
out = malloc (sizeof (tex_t) + PLAYER_WIDTH*PLAYER_HEIGHT);
out->data = (byte *) (out + 1);
out->width = PLAYER_WIDTH;
out->height = PLAYER_HEIGHT;
out->format = tex_palette;

View file

@ -70,7 +70,8 @@ gl_SCR_CaptureBGR (void)
tex_t *tex;
count = vid.width * vid.height;
tex = malloc (field_offset (tex_t, data[count * 3]));
tex = malloc (sizeof (tex_t) + count * 3);
tex->data = (byte *) (tex + 1);
SYS_CHECKMEM (tex);
tex->width = vid.width;
tex->height = vid.height;
@ -100,7 +101,8 @@ gl_SCR_ScreenShot (int width, int height)
fracw = (float) vid.width / (float) w;
frach = (float) vid.height / (float) h;
tex = malloc (field_offset (tex_t, data[w * h]));
tex = malloc (sizeof (tex_t) + w * h);
tex->data = (byte *) (tex + 1);
if (!tex)
return 0;

View file

@ -1409,7 +1409,8 @@ glsl_R_LoadSkys (const char *sky)
int size = tex->height / 2;
skybox_loaded = true;
sub = malloc (field_offset (tex_t, data[size * size * tex->format]));
sub = malloc (sizeof (tex_t) + size * size * tex->format);
sub->data = (byte *) (sub + 1);
sub->width = size;
sub->height = size;
sub->format = tex->format;

View file

@ -201,7 +201,8 @@ glsl_SCR_CaptureBGR (void)
tex_t *tex;
count = vid.width * vid.height;
tex = malloc (field_offset (tex_t, data[count * 3]));
tex = malloc (sizeof (tex_t) + count * 3);
tex->data = (byte *) (tex + 1);
SYS_CHECKMEM (tex);
tex->width = vid.width;
tex->height = vid.height;

View file

@ -50,7 +50,8 @@ R_DotParticleTexture (void)
int x, y, dx2, dy, d;
tex_t *tex;
tex = malloc (field_offset (tex_t, data[sizeof (*data)]));
tex = malloc (sizeof (tex_t) + sizeof (*data));
tex->data = (byte *) (tex + 1);
tex->width = 32;
tex->height = 32;
tex->format = tex_la;
@ -79,7 +80,8 @@ R_SparkParticleTexture (void)
int x, y, dx2, dy, d;
tex_t *tex;
tex = malloc (field_offset (tex_t, data[sizeof (*data)]));
tex = malloc (sizeof (tex_t) + sizeof (*data));
tex->data = (byte*) (tex + 1);
tex->width = 32;
tex->height = 32;
tex->format = tex_la;
@ -113,7 +115,8 @@ R_SmokeParticleTexture (void)
int x, y, c;
tex_t *tex;
tex = malloc (field_offset (tex_t, data[sizeof (*data)]));
tex = malloc (sizeof (tex_t) + sizeof (*data));
tex->data = (byte *) (tex + 1);
tex->width = 32;
tex->height = 32;
tex->format = tex_la;

View file

@ -64,7 +64,8 @@ SCR_CaptureBGR (void)
byte *dst;
count = vid.width * vid.height;
tex = malloc (field_offset (tex_t, data[count * 3]));
tex = malloc (sizeof (tex_t) + count * 3);
tex->data = (byte *) (tex + 1);
SYS_CHECKMEM (tex);
tex->width = vid.width;
tex->height = vid.height;
@ -102,7 +103,8 @@ SCR_ScreenShot (int width, int height)
fracw = (float) vid.width / (float) w;
frach = (float) vid.height / (float) h;
tex = malloc (field_offset (tex_t, data[w * h]));
tex = malloc (sizeof (tex_t) + w * h);
tex->data = (byte *) (tex + 1);
if (!tex)
return 0;

View file

@ -67,7 +67,8 @@ sw32_SCR_CaptureBGR (void)
byte *dst;
count = vid.width * vid.height;
tex = malloc (field_offset (tex_t, data[count * 3]));
tex = malloc (sizeof (tex_t) + count * 3);
tex->data = (byte *) (tex + 1);
SYS_CHECKMEM (tex);
tex->width = vid.width;
tex->height = vid.height;

View file

@ -282,9 +282,12 @@ Vulkan_UnloadTex (vulkan_ctx_t *ctx, qfv_tex_t *tex)
free (tex);
}
static tex_t default_black_tex = {1, 1, tex_rgba, 1, 0, {0, 0, 0, 0 }};
static tex_t default_white_tex = {1, 1, tex_rgba, 1, 0, {255, 255, 255, 255 }};
static tex_t default_magenta_tex = {1, 1, tex_rgba, 1, 0, {255, 0, 255, 255 }};
static byte black_data[] = {0, 0, 0, 0};
static byte white_data[] = {255, 255, 255, 255};
static byte magenta_data[] = {255, 0, 255, 255};
static tex_t default_black_tex = {1, 1, tex_rgba, 1, 0, black_data};
static tex_t default_white_tex = {1, 1, tex_rgba, 1, 0, white_data};
static tex_t default_magenta_tex = {1, 1, tex_rgba, 1, 0, magenta_data};
void
Vulkan_Texture_Init (vulkan_ctx_t *ctx)