Skip to content

Commit

Permalink
Brings back country flags to the server list
Browse files Browse the repository at this point in the history
* linear filter news images
* linear filter fog gradient
  • Loading branch information
xtreme8000 committed Oct 15, 2020
1 parent b28b283 commit dd71c71
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
28 changes: 22 additions & 6 deletions src/hud.c
Expand Up @@ -2142,6 +2142,7 @@ static void hud_serverlist_render(mu_Context* ctx, float scalex, float scaley) {
mu_begin_panel(ctx, "Servers");
int width = mu_get_current_container(ctx)->body.w;

int flag_width = ctx->style->size.y + ctx->style->padding * 2;
mu_layout_row(ctx, 5, (int[]) {0.12F * width, 0.418F * width, 0.22F * width, 0.117F * width, -1}, 0);

if(mu_button(ctx, "Players")) {
Expand Down Expand Up @@ -2170,6 +2171,11 @@ static void hud_serverlist_render(mu_Context* ctx, float scalex, float scaley) {
pthread_mutex_unlock(&serverlist_lock);
}

mu_layout_row(ctx, 6,
(int[]) {0.12F * width, flag_width, 0.418F * width - flag_width - ctx->style->spacing * 2,
0.22F * width, 0.117F * width, -1},
0);

pthread_mutex_lock(&serverlist_lock);
if(server_count > 0) {
for(int k = 0; k < server_count; k++) {
Expand All @@ -2185,29 +2191,38 @@ static void hud_serverlist_render(mu_Context* ctx, float scalex, float scaley) {
1 :
2;

mu_push_id(ctx, &serverlist[k].identifier, strlen(serverlist[k].identifier));

mu_text_color(ctx, 230 / f, 230 / f, 230 / f);
bool join = false;
if(mu_button_ex(ctx, total_str, 0, MU_OPT_NOFRAME | MU_OPT_ALIGNCENTER))
join = true;
if(mu_button_ex(ctx, "", texture_flag_index(serverlist[k].country) + HUD_FLAG_INDEX_START,
MU_OPT_NOFRAME))
join = true;
if(mu_button_ex(ctx, serverlist[k].name, 0, MU_OPT_NOFRAME))
join = true;
if(mu_button_ex(ctx, serverlist[k].map, 0, MU_OPT_NOFRAME))
join = true;
if(mu_button_ex(ctx, serverlist[k].gamemode, 0, MU_OPT_NOFRAME | MU_OPT_ALIGNCENTER))
join = true;

if(serverlist[k].ping < 110)
mu_text_color(ctx, 0, 255 / f, 0);
else if(serverlist[k].ping < 200)
mu_text_color(ctx, 255 / f, 255 / f, 0);
else
mu_text_color(ctx, 255 / f, 0, 0);
if(serverlist[k].ping >= 0) {
if(serverlist[k].ping < 110)
mu_text_color(ctx, 0, 255 / f, 0);
else if(serverlist[k].ping < 200)
mu_text_color(ctx, 255 / f, 255 / f, 0);
else
mu_text_color(ctx, 255 / f, 0, 0);
}

sprintf(total_str, "%ims", serverlist[k].ping);
if(mu_button_ex(ctx, (serverlist[k].ping >= 0) ? total_str : "?", 0,
MU_OPT_NOFRAME | MU_OPT_ALIGNCENTER))
join = true;

mu_pop_id(ctx);

if(join) {
server_c(serverlist[k].identifier, serverlist[k].name);
break;
Expand Down Expand Up @@ -2281,6 +2296,7 @@ static void hud_serverlist_render(mu_Context* ctx, float scalex, float scaley) {
int width, height;
lodepng_decode32(&buffer, &width, &height, img, size);
texture_create_buffer(&current->image, width, height, buffer, 1);
texture_filter(&current->image, TEXTURE_FILTER_LINEAR);
}
current->next = (k < news_entries - 1) ? malloc(sizeof(struct serverlist_news_entry)) : NULL;
current = current->next;
Expand Down
2 changes: 2 additions & 0 deletions src/hud.h
Expand Up @@ -62,6 +62,8 @@ extern struct hud hud_controls;
extern struct hud* hud_active;
extern struct window_instance* hud_window;

#define HUD_FLAG_INDEX_START 64

void hud_change(struct hud* new);
void hud_init();
void hud_mousemode(int mode);
Expand Down
16 changes: 12 additions & 4 deletions src/main.c
Expand Up @@ -451,14 +451,22 @@ void display() {
cmd->rect.rect.w, cmd->rect.rect.h);
break;
case MU_COMMAND_ICON:
if(hud_active->ui_images) {
glColor4ub(cmd->icon.color.r, cmd->icon.color.g, cmd->icon.color.b, cmd->icon.color.a);
int size = min(cmd->icon.rect.w, cmd->icon.rect.h);

if(cmd->icon.id >= HUD_FLAG_INDEX_START - 1) {
float u, v;
texture_flag_offset(cmd->icon.id - HUD_FLAG_INDEX_START, &u, &v);

texture_draw_sector(&texture_ui_flags, cmd->icon.rect.x,
settings.window_height - cmd->icon.rect.y - size * 0.167F, size,
size * 0.667F, u, v, 18.0F / 256.0F, 12.0F / 256.0F);
glEnable(GL_BLEND);
} else if(hud_active->ui_images) {
bool resize = false;
struct texture* img = hud_active->ui_images(cmd->icon.id, &resize);

if(img) {
glColor4ub(cmd->icon.color.r, cmd->icon.color.g, cmd->icon.color.b, cmd->icon.color.a);
int size = min(cmd->icon.rect.w, cmd->icon.rect.h);

texture_draw(img, cmd->icon.rect.x, settings.window_height - cmd->icon.rect.y,
resize ? size : cmd->icon.rect.w, resize ? size : cmd->icon.rect.h);
glEnable(GL_BLEND);
Expand Down
19 changes: 12 additions & 7 deletions src/texture.c
Expand Up @@ -89,12 +89,16 @@ static int texture_flag_cmp(const void* a, const void* b) {
return strcmp(a, *(const void* const*)b);
}

void texture_flag_offset(const char* country, float* u, float* v) {
char** res = bsearch(country, texture_flags, 251, sizeof(char*), texture_flag_cmp);
if(res) {
int i = res - texture_flags;
*u = (i % 14) * (18.0F / 256.0F);
*v = (i / 14) * (12.0F / 256.0F);
int texture_flag_index(const char* country) {
char** res = bsearch(country, texture_flags, sizeof(texture_flags) / sizeof(texture_flags[0]), sizeof(char*),
texture_flag_cmp);
return res ? (res - texture_flags) : -1;
}

void texture_flag_offset(int index, float* u, float* v) {
if(index >= 0) {
*u = (index % 14) * (18.0F / 256.0F);
*v = (index / 14) * (12.0F / 256.0F);
} else {
*u = 0.0F;
*v = 0.9375F;
Expand Down Expand Up @@ -217,7 +221,7 @@ void texture_draw_empty(float x, float y, float w, float h) {
glDisableClientState(GL_VERTEX_ARRAY);
}

#define texture_emit_rotated(tx, ty, x, y, a) cos(a) * x - sin(a) * y + tx, sin(a) * x + cos(a) * y + ty
#define texture_emit_rotated(tx, ty, x, y, a) cos(a) * (x)-sin(a) * (y) + (tx), sin(a) * (x) + cos(a) * (y) + (ty)

void texture_draw_empty_rotated(float x, float y, float w, float h, float angle) {
float vertices[12]
Expand Down Expand Up @@ -392,6 +396,7 @@ void texture_init() {
CHECK_ALLOCATION_ERROR(gradient)
texture_gradient_fog(gradient);
texture_create_buffer(&texture_gradient, 512, 512, (unsigned char*)gradient, 1);
texture_filter(&texture_gradient, TEXTURE_FILTER_LINEAR);

texture_create_buffer(&texture_dummy, 1, 1, (unsigned char[]) {0, 0, 0, 0}, 1);
}
3 changes: 2 additions & 1 deletion src/texture.h
Expand Up @@ -71,7 +71,8 @@ extern struct texture texture_ui_knob;
#define TEXTURE_FILTER_NEAREST 0
#define TEXTURE_FILTER_LINEAR 1

void texture_flag_offset(const char* country, float* u, float* v);
int texture_flag_index(const char* country);
void texture_flag_offset(int index, float* u, float* v);
void texture_filter(struct texture* t, int filter);
void texture_init(void);
int texture_create(struct texture* t, char* filename);
Expand Down

0 comments on commit dd71c71

Please sign in to comment.