Post: How to Center Engine Text (SPRX)
04-05-2016, 01:10 AM #1
Jim Halpert
Bounty hunter
(adsbygoogle = window.adsbygoogle || []).push({}); Hey, I started making a non-host SPRX for BO1 and since I want it to look beautiful, I wanted the options to be nice and centered. The thing is, as far as I know, there's no game function that you can use to center non-host engine text. So here's what I did to center the text:

1st you need the function R_TextWidth. I found its address for BO1 1.13, and it's 0x734570. To get the function in your project, add this (you will need a different address for a different game):
    opd_s R_TextWidth_t = { 0x734570, TOC };
int(*R_TextWidth)(const char *text, int maxChars, Font_s *font) = (int(*)(const char*, int, Font_s*))&R_TextWidth_t;


This function returns an int, the width of your text hud, and it puts in account everything about the text (i.e. it's font, scale, etc.). In order for it to work, you need to define a struct called "Font_s" like I have in one of the parameters. I got this struct from Snow Engine, btw.

    struct Font_s
{
int fontName;
float pixelHeight;
int glyphCount;
int material;
int glowMaterial;
int glyphs;
};


Once the struct is defined, you can then make a handy function that finds the width of text by passing the text's attributes.

    float textWidth(float x, float y, int font, float scale, char* text) {
ScreenPlacement* scr = (ScreenPlacement*)0xD69D04;
scr->realViewableMaxX = (int32_t)x;
scr->realViewableMaxY = (int32_t)y;
Font_s* font_p = UI_GetFontHandle(scr, font, scale);
return (float)R_TextWidth(text, 15, font_p);
}


If you are using a struct for your huds, this is what you should rather do:

    float textWidth(UI::TextHud text_hud) {
ScreenPlacement* scr = (ScreenPlacement*)0xD69D04;
scr->realViewableMaxX = (int32_t)text_hud.hud.x;
scr->realViewableMaxY = (int32_t)text_hud.hud.y;
Font_s* font_p = UI_GetFontHandle(scr, text_hud.font, text_hud.scale);
return (float)R_TextWidth(text_hud.text, 15, font_p);
}


""TextHud" is just what I'm using and have defined. Pass whatever you use into the function, and where it says something like "text_hud.hud.x", put whatever you use to access the text's attribute.

The whole point of this thread is to center options, though. So here's what you do:

    void centerText(UI::TextHud** options, float center_point) {
for (int i = 0; i < number_of_options; ++i) {
options[i]->hud.x = (center_point - (UI::textWidth(*options[i]) / 3.5f));
}
}


You make a function like above that takes in two parameters. The first one is an array of options that you want to center. I've also made it an array of pointers so that when you pass the options in, the function will directly modify the options' attributes (how passing in by reference works). The second parameter, "center_point" is the point on the screen that you the options to align/center to (it's supposed to be an x-coordinate). Again, my struct is different from your's, so you'll need to adjust accordingly.

NOTE: in the above function, I divide by 3.5. This is what turned out to work for me in BO1, but you should mess with that value until things align correctly. I though it would be 2 when I was thinking of the algorithm but for some reason 2 didn't work, and after a few tests I found that 3.5 was good.

Here's how I would call it:

    UI::TextHud options[20]; // 20 options

centerText(&options, 600.0f) // pass in your array of options by reference


This is all you need to center text. Here is a pastebin with everything I have put together: You must login or register to view this content.

Credits:
Me (Hacked Tutorials)
Creators of Snow Engine (I pulled some stuff from there)
SC58 (Told me about the function R_TextWidth)

If you have any questions, let me know Winky Winky
Last edited by Jim Halpert ; 04-05-2016 at 01:14 AM.

The following 5 users say thank you to Jim Halpert for this useful post:

mason, Kam, RakzMods, TheGreenPlanet, uykjtrhgewa
04-05-2016, 10:22 AM #2
Joker.
Banned
Tanks.
04-23-2016, 04:34 PM #3
S63
Space Ninja
Originally posted by Jim
Hey, I started making a non-host SPRX for BO1 and since I want it to look beautiful, I wanted the options to be nice and centered. The thing is, as far as I know, there's no game function that you can use to center non-host engine text. So here's what I did to center the text:

1st you need the function R_TextWidth. I found its address for BO1 1.13, and it's 0x734570. To get the function in your project, add this (you will need a different address for a different game):
    opd_s R_TextWidth_t = { 0x734570, TOC };
int(*R_TextWidth)(const char *text, int maxChars, Font_s *font) = (int(*)(const char*, int, Font_s*))&R_TextWidth_t;


This function returns an int, the width of your text hud, and it puts in account everything about the text (i.e. it's font, scale, etc.). In order for it to work, you need to define a struct called "Font_s" like I have in one of the parameters. I got this struct from Snow Engine, btw.

    struct Font_s
{
int fontName;
float pixelHeight;
int glyphCount;
int material;
int glowMaterial;
int glyphs;
};


Once the struct is defined, you can then make a handy function that finds the width of text by passing the text's attributes.

    float textWidth(float x, float y, int font, float scale, char* text) {
ScreenPlacement* scr = (ScreenPlacement*)0xD69D04;
scr->realViewableMaxX = (int32_t)x;
scr->realViewableMaxY = (int32_t)y;
Font_s* font_p = UI_GetFontHandle(scr, font, scale);
return (float)R_TextWidth(text, 15, font_p);
}


If you are using a struct for your huds, this is what you should rather do:

    float textWidth(UI::TextHud text_hud) {
ScreenPlacement* scr = (ScreenPlacement*)0xD69D04;
scr->realViewableMaxX = (int32_t)text_hud.hud.x;
scr->realViewableMaxY = (int32_t)text_hud.hud.y;
Font_s* font_p = UI_GetFontHandle(scr, text_hud.font, text_hud.scale);
return (float)R_TextWidth(text_hud.text, 15, font_p);
}


""TextHud" is just what I'm using and have defined. Pass whatever you use into the function, and where it says something like "text_hud.hud.x", put whatever you use to access the text's attribute.

The whole point of this thread is to center options, though. So here's what you do:

    void centerText(UI::TextHud** options, float center_point) {
for (int i = 0; i < number_of_options; ++i) {
options[i]->hud.x = (center_point - (UI::textWidth(*options[i]) / 3.5f));
}
}


You make a function like above that takes in two parameters. The first one is an array of options that you want to center. I've also made it an array of pointers so that when you pass the options in, the function will directly modify the options' attributes (how passing in by reference works). The second parameter, "center_point" is the point on the screen that you the options to align/center to (it's supposed to be an x-coordinate). Again, my struct is different from your's, so you'll need to adjust accordingly.

NOTE: in the above function, I divide by 3.5. This is what turned out to work for me in BO1, but you should mess with that value until things align correctly. I though it would be 2 when I was thinking of the algorithm but for some reason 2 didn't work, and after a few tests I found that 3.5 was good.

Here's how I would call it:

    UI::TextHud options[20]; // 20 options

centerText(&options, 600.0f) // pass in your array of options by reference


This is all you need to center text. Here is a pastebin with everything I have put together: You must login or register to view this content.

Credits:
Me (Hacked Tutorials)
Creators of Snow Engine (I pulled some stuff from there)
SC58 (Told me about the function R_TextWidth)

If you have any questions, let me know Winky Winky


Nice post bro keep it up ��
04-24-2016, 08:46 PM #4
Nice Post !

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo