Skip to content

Commit 19b0cf8

Browse files
authored
Improve Stbar Color Background Algorithm (#691)
1 parent b22be23 commit 19b0cf8

File tree

3 files changed

+88
-52
lines changed

3 files changed

+88
-52
lines changed

prboom2/src/r_draw.c

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -501,65 +501,37 @@ void R_InitBuffer(int width, int height)
501501
void R_FillBackColor (void)
502502
{
503503
extern patchnum_t stbarbg;
504-
byte col, col_top;
505-
int i, j, pixel_cnt;
506-
int r, g, b;
507-
int stbar_top = SCREENHEIGHT - ST_SCALED_HEIGHT;
508-
int ST_SCALED_BORDER = brdr_b.height * patches_scaley/2;
509-
const unsigned char *playpal = V_GetPlaypal();
510-
511-
const byte* lump;
512-
const byte* p;
513-
short width;
514-
byte length;
515-
byte entry;
516-
517-
pixel_cnt = 0;
518-
r = g = b = 0;
519-
520-
lump = W_LumpByNum(stbarbg.lumpnum);
521-
width = *((const int16_t *) lump);
522-
width = LittleShort(width);
523-
524-
for (i = 0; i < width; ++i) {
525-
// Skip irrelevant data in the doom patch header
526-
int32_t offset;
527-
p = lump + 8 + 4 * i;
528-
offset = *((const int32_t *) p);
529-
p = lump + LittleLong(offset);
530-
531-
while (*p != 0xff) {
532-
p++;
533-
length = *p++;
534-
p++;
535-
536-
// Get RGB values per pixel
537-
for (j = 0; j < length; ++j) {
538-
entry = *p++;
539-
r += playpal[3 * entry + 0];
540-
g += playpal[3 * entry + 1];
541-
b += playpal[3 * entry + 2];
542-
pixel_cnt++;
543-
}
504+
static byte col;
505+
static byte col_top;
506+
static int prevlump = -1;
507+
const int stbar_top = SCREENHEIGHT - ST_SCALED_HEIGHT;
508+
const int ST_SCALED_BORDER = brdr_b.height * patches_scaley/2;
509+
int lump = stbarbg.lumpnum;
510+
511+
if (prevlump != lump)
512+
{
513+
const unsigned char *playpal = V_GetPlaypal();
514+
SDL_Color stbar_color = V_GetPatchColor(lump);
515+
int r = stbar_color.r;
516+
int g = stbar_color.g;
517+
int b = stbar_color.b;
544518

545-
p++;
546-
}
547-
}
519+
// Convert to palette and tune down saturation
520+
col = V_BestColor(playpal, r/3, g/3, b/3);
521+
col_top = V_BestColor(playpal, r/2, g/2, b/2);
548522

549-
// Average RGB values
550-
r /= pixel_cnt;
551-
g /= pixel_cnt;
552-
b /= pixel_cnt;
523+
// If colors are the same, brighten top
524+
if (col_top == col)
525+
col_top = V_BestColor(playpal, r, g, b);
553526

554-
// Convert to palette and tune down saturation
555-
col = V_BestColor(playpal, r/3, g/3, b/3);
556-
col_top = V_BestColor(playpal, r/2, g/2, b/2);
527+
prevlump = lump;
528+
}
557529

558530
V_BeginMenuDraw();
559-
V_FillRect(1, 0, stbar_top, SCREENWIDTH, ST_SCALED_HEIGHT, col);
560531
V_FillRect(1, 0, stbar_top, SCREENWIDTH, ST_SCALED_BORDER, col_top);
532+
V_FillRect(1, 0, stbar_top + ST_SCALED_BORDER, SCREENWIDTH, ST_SCALED_HEIGHT - ST_SCALED_BORDER, col);
561533
V_EndMenuDraw();
562-
}
534+
}
563535

564536
//
565537
// R_FillBackScreen

prboom2/src/v_video.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,66 @@ int V_GetPlaypalCount(void)
12041204
return (dsda_PlayPalData()->length / PALETTE_SIZE);
12051205
}
12061206

1207+
//
1208+
// V_GetPatchColor
1209+
// Get the color of a Doom-Format
1210+
// graphic via lumpnum
1211+
//
1212+
1213+
SDL_Color V_GetPatchColor (int lumpnum)
1214+
{
1215+
SDL_Color col = {0,0,0,0};
1216+
int r = 0, g = 0, b = 0;
1217+
const unsigned char *playpal = V_GetPlaypal();
1218+
int x, y, pixel_cnt = 0;
1219+
const byte* lump;
1220+
short width;
1221+
1222+
lump = W_LumpByNum(lumpnum);
1223+
1224+
width = *((const int16_t *) lump);
1225+
width = LittleShort(width);
1226+
1227+
for (x = 0; x < width; ++x) {
1228+
byte length;
1229+
byte entry;
1230+
const byte* p;
1231+
int32_t offset;
1232+
1233+
// Only calculate for the leftmost and rightmost 16 columns
1234+
if (width > 32 && x > 16 && x < width - 16)
1235+
continue;
1236+
1237+
// Skip irrelevant data in the doom patch header
1238+
p = lump + 8 + 4 * x;
1239+
offset = *((const int32_t *) p);
1240+
p = lump + LittleLong(offset);
1241+
1242+
while (*p != 0xff) {
1243+
p++;
1244+
length = *p++;
1245+
p++;
1246+
1247+
// Get RGB values per pixel
1248+
for (y = 0; y < length; ++y) {
1249+
entry = *p++;
1250+
r += playpal[3 * entry + 0];
1251+
g += playpal[3 * entry + 1];
1252+
b += playpal[3 * entry + 2];
1253+
pixel_cnt++;
1254+
}
1255+
p++;
1256+
}
1257+
}
1258+
1259+
// Average RGB values
1260+
col.r = r / pixel_cnt;
1261+
col.g = g / pixel_cnt;
1262+
col.b = b / pixel_cnt;
1263+
1264+
return col;
1265+
}
1266+
12071267
void V_ClearBorder(void)
12081268
{
12091269
int bordtop, bordbottom, bordleft, bordright;

prboom2/src/v_video.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#ifndef __V_VIDEO__
3838
#define __V_VIDEO__
3939

40+
#include "SDL.h"
41+
4042
#include "doomtype.h"
4143
#include "doomdef.h"
4244
// Needed because we are refering to patches.
@@ -309,6 +311,8 @@ void V_FreePlaypal(void);
309311
// [XA] get number of palettes in the current playpal
310312
int V_GetPlaypalCount(void);
311313

314+
SDL_Color V_GetPatchColor (int lumpnum);
315+
312316
// e6y: wide-res
313317
void V_ClearBorder(void);
314318

0 commit comments

Comments
 (0)