Skip to content

Commit

Permalink
feat(extra-natives-rdr3): add GET_CURRENT_SCREEN_RESOLUTION for RedM
Browse files Browse the repository at this point in the history
  • Loading branch information
outsider31000 committed Feb 17, 2025
1 parent c1cff3d commit 6ee524a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
39 changes: 37 additions & 2 deletions code/components/extra-natives-rdr3/src/GraphicsNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static hook::cdecl_stub<void()> initScriptImBuffer([]()
return hook::get_pattern("48 89 5C 24 ? 57 48 83 EC 20 0F B7 05 ? ? ? ? 33 FF");
});

static void(*g_origRenderScriptIm)();
static void (*g_origRenderScriptIm)();
static void RenderScriptIm()
{
// The game never uses this buffer itself, but it *may* be changed in the future updates.
Expand Down Expand Up @@ -88,7 +88,7 @@ struct DrawOriginStore
char pad_404[12];
};

static bool(*isGamePaused)();
static bool (*isGamePaused)();

static int* g_renderBufferIndex;
static int* g_updateBufferIndex;
Expand All @@ -107,6 +107,9 @@ static WorldhorizonManager* g_worldhorizonMgr;

CViewportGame** g_viewportGame;

static int* g_screenWidth = 0;
static int* g_screenHeight = 0;

static HookFunction hookFunction([]()
{
static_assert(sizeof(ScriptImRequest) == 64);
Expand Down Expand Up @@ -145,6 +148,13 @@ static HookFunction hookFunction([]()
g_origRenderScriptIm = hook::trampoline(location, RenderScriptIm);
}

{
auto location = hook::get_pattern<char>("89 05 ? ? ? ? 89 0D ? ? ? ? F3 0F 5E");

g_screenWidth = hook::get_address<int*>(location + 2);
g_screenHeight = hook::get_address<int*>(location + 8);
}

fx::ScriptEngine::RegisterNativeHandler("SET_DRAW_ORIGIN", [](fx::ScriptContext& context)
{
DrawOriginStore& store = g_drawOriginStore[*g_updateBufferIndex];
Expand Down Expand Up @@ -313,4 +323,29 @@ static HookFunction hookFunction([]()
normalOut->y = XMVectorGetY(normalVector);
normalOut->z = XMVectorGetZ(normalVector);
});

fx::ScriptEngine::RegisterNativeHandler("GET_CURRENT_SCREEN_RESOLUTION", [](fx::ScriptContext& context)
{
const bool isNullPointer = g_screenWidth == nullptr || g_screenHeight == nullptr;
if (isNullPointer)
{
context.SetResult<bool>(false);
return;
}

const int width = *g_screenWidth;
const int height = *g_screenHeight;

const bool isInvalidRes = width == 0 || height == 0;
if (isInvalidRes)
{
context.SetResult<bool>(false);
return;
}

*context.GetArgument<int*>(0) = width;
*context.GetArgument<int*>(1) = height;

context.SetResult<bool>(true);
});
});
17 changes: 17 additions & 0 deletions ext/native-decls/GetCurrentScreenResolution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
ns: CFX
apiset: client
game: rdr3
---
## GET_CURRENT_SCREEN_RESOLUTION

```c
BOOL GET_CURRENT_SCREEN_RESOLUTION(int* width, int* height);
```
Gets the current screen resolution.
```lua
local _, width, height = GetCurrentScreenResolution()
print(width, height)
```

0 comments on commit 6ee524a

Please sign in to comment.