Move util functions to util.c

Move vec2_t to util.h
This commit is contained in:
8dcc 2023-07-22 16:15:57 +02:00
parent 0f473bba4e
commit 33494b415f
3 changed files with 66 additions and 40 deletions

View File

@ -2,48 +2,9 @@
#include "include/esp.h" #include "include/esp.h"
#include "../include/globals.h" #include "../include/globals.h"
#include "../include/cvars.h" #include "../include/cvars.h"
#include "../include/util.h"
/* TODO: Move all these to utils.c */
/* TODO: Replace src/features/include/X.h -> src/features/features.h */ /* TODO: Replace src/features/include/X.h -> src/features/features.h */
static bool is_alive(cl_entity_t* ent) {
return ent && ent->curstate.movetype != 6 && ent->curstate.movetype != 0;
}
static bool valid_client(cl_entity_t* ent) {
return is_alive(ent) && ent->index != localplayer->index;
}
static bool vec_is_zero(vec3_t v) {
return v[0] == 0.0f && v[1] == 0.0f && v[2] == 0.0f;
}
static char* get_name(int ent_idx) {
hud_player_info_t info;
i_engine->pfnGetPlayerInfo(ent_idx, &info);
return info.name;
}
typedef float vec2_t[2];
static bool world_to_screen(vec3_t vec, vec2_t screen) {
int engine_w2s = i_engine->pTriAPI->WorldToScreen(vec, screen);
if (engine_w2s)
return false;
SCREENINFO scr_inf;
scr_inf.iSize = sizeof(SCREENINFO);
i_engine->pfnGetScreenInfo(&scr_inf);
/* If within bounds, transform to screen scale */
if (screen[0] < 1 && screen[1] < 1 && screen[0] > -1 && screen[1] > -1) {
screen[0] = screen[0] * (scr_inf.iWidth / 2) + (scr_inf.iWidth / 2);
screen[1] = -screen[1] * (scr_inf.iHeight / 2) + (scr_inf.iHeight / 2);
return true;
}
return false;
}
void esp(void) { void esp(void) {
if (!CVAR_ON(esp)) if (!CVAR_ON(esp))
@ -58,6 +19,7 @@ void esp(void) {
float screen_point[2]; float screen_point[2];
if (world_to_screen(ent->origin, screen_point)) { if (world_to_screen(ent->origin, screen_point)) {
/* TODO: Draw name at bottom-left of box ESP */
i_engine->pfnDrawSetTextColor(1, 1, 1); i_engine->pfnDrawSetTextColor(1, 1, 1);
i_engine->pfnDrawConsoleString(screen_point[0], screen_point[1], i_engine->pfnDrawConsoleString(screen_point[0], screen_point[1],
get_name(ent->index)); get_name(ent->index));

18
src/include/util.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _UTIL_H
#define _UTIL_H
#include "sdk.h"
#include "globals.h"
/* Vector 2 for 2d points */
typedef float vec2_t[2];
/*----------------------------------------------------------------------------*/
bool is_alive(cl_entity_t* ent);
bool valid_client(cl_entity_t* ent);
char* get_name(int ent_idx);
bool vec_is_zero(vec3_t v);
bool world_to_screen(vec3_t vec, vec2_t screen);
#endif /* _UTIL_H */

46
src/util.c Normal file
View File

@ -0,0 +1,46 @@
#include "include/util.h"
#include "include/sdk.h"
#include "include/globals.h"
bool is_alive(cl_entity_t* ent) {
return ent && ent->curstate.movetype != 6 && ent->curstate.movetype != 0;
}
bool valid_client(cl_entity_t* ent) {
return is_alive(ent) && ent->index != localplayer->index;
}
char* get_name(int ent_idx) {
hud_player_info_t info;
i_engine->pfnGetPlayerInfo(ent_idx, &info);
return info.name;
}
bool vec_is_zero(vec3_t v) {
return v[0] == 0.0f && v[1] == 0.0f && v[2] == 0.0f;
}
bool world_to_screen(vec3_t vec, vec2_t screen) {
if (vec_is_zero(vec))
return false;
int engine_w2s = i_engine->pTriAPI->WorldToScreen(vec, screen);
if (engine_w2s)
return false;
SCREENINFO scr_inf;
scr_inf.iSize = sizeof(SCREENINFO);
i_engine->pfnGetScreenInfo(&scr_inf);
/* If within bounds, transform to screen scale */
if (screen[0] < 1 && screen[1] < 1 && screen[0] > -1 && screen[1] > -1) {
screen[0] = screen[0] * (scr_inf.iWidth / 2) + (scr_inf.iWidth / 2);
screen[1] = -screen[1] * (scr_inf.iHeight / 2) + (scr_inf.iHeight / 2);
return true;
}
return false;
}