@@ -76,14 +76,21 @@ void HookD3D9Device(IDirect3DDevice9* pDevice, bool force = false) noexcept;
7676
7777bool can_render (jint srctype, jint width, jint height)
7878{
79- /* SRC_TYPE = OpenGLSurfaceData.PF_INT_RGBX = 3; //GL_RGBA, GL_UNSIGNED_INT_8_8_8_8
80- * OGLPixelFormat pf = PixelFormats[srctype];
81- *
82- * It's used to render java.awt.canvas..
83- * */
84- if (srctype == 3 /* OpenGLSurfaceData.PF_INT_RGBX -- D3DSurfaceData.ST_INT_RGB*/ )
79+ extern std::unique_ptr<ControlCenter> control_center;
80+ if (!control_center)
81+ {
82+ return false ;
83+ }
84+
85+ std::int32_t canvas_x = 0 ;
86+ std::int32_t canvas_y = 0 ;
87+ std::size_t canvas_width = 0 ;
88+ std::size_t canvas_height = 0 ;
89+
90+ control_center->get_canvas_dimensions (&canvas_x, &canvas_y, &canvas_width, &canvas_height);
91+ if (width > canvas_width || height > canvas_height)
8592 {
86- return true ;
93+ return false ;
8794 }
8895
8996 // Arbitrarily chosen because the java.awt.canvas should never be smaller than this value.
@@ -920,79 +927,95 @@ BOOL __stdcall mSwapBuffers(HDC hdc) noexcept
920927 GLint width = ViewPort[2 ] - ViewPort[0 ];
921928 GLint height = ViewPort[3 ] - ViewPort[1 ];
922929
923- if (can_render (-1 , width, height))
930+ std::int32_t canvas_x = 0 ;
931+ std::int32_t canvas_y = 0 ;
932+ std::size_t canvas_width = 0 ;
933+ std::size_t canvas_height = 0 ;
934+
935+ control_center->get_canvas_dimensions (&canvas_x, &canvas_y, &canvas_width, &canvas_height);
936+ control_center->set_target_dimensions (canvas_width, canvas_height);
937+
938+ // Do not render on an invalid canvas
939+ if (width != canvas_width || height != canvas_height)
924940 {
925- control_center->set_target_dimensions (width, height);
941+ if (opengl_swap_hook)
942+ {
943+ return opengl_swap_hook->call <BOOL, decltype (mSwapBuffers )>(hdc);
944+ }
945+ return false ;
946+ }
926947
927- // Check if extensions are supported
928- // This check is needed for renderers that do not support pixel buffer objects or vertex buffer objects
929- // static bool hasGLExtension = IsGLExtensionsSupported(hdc, "GL_ARB_vertex_buffer_object") || IsGLExtensionsSupported(hdc, "GL_ARB_pixel_buffer_object");
948+ // Check if extensions are supported
949+ // This check is needed for renderers that do not support pixel buffer objects or vertex buffer objects
950+ // static bool hasGLExtension = IsGLExtensionsSupported(hdc, "GL_ARB_vertex_buffer_object") || IsGLExtensionsSupported(hdc, "GL_ARB_pixel_buffer_object");
930951
931- // The above extension check is unreliable!
932- // It's best to attempt to load the extensions and see if they exist.
933- static bool hasGLExtension = LoadOpenGLExtensions ();
952+ // The above extension check is unreliable!
953+ // It's best to attempt to load the extensions and see if they exist.
954+ static bool hasGLExtension = LoadOpenGLExtensions ();
934955
935- // Render to Shared Memory
936- std::uint8_t * dest = control_center->get_image ();
937- ImageFormat format = control_center->get_image_format ();
938- if (dest)
956+ // Render to Shared Memory
957+ std::uint8_t * dest = control_center->get_image ();
958+ ImageFormat format = control_center->get_image_format ();
959+ if (dest)
960+ {
961+ if (hasGLExtension)
939962 {
940- if (hasGLExtension)
941- {
942- // Performance Boost! :D
943- GeneratePixelBuffers (hdc, pbo, width, height, 4 );
944- ReadPixelBuffers (hdc, dest, pbo, width, height, 4 , format);
945- }
946- else
947- {
948- GLenum gl_format = [](ImageFormat format) -> GLenum {
949- switch (format)
950- {
951- case ImageFormat::BGR_BGRA: return GL_BGRA;
952- case ImageFormat::BGRA: return GL_BGRA;
953- case ImageFormat::RGBA: return GL_RGBA;
954- case ImageFormat::ARGB: return 0 ; // Not Supported
955- case ImageFormat::ABGR: return 0 ; // Not Supported
956- default : return GL_BGRA;
957- }
958- }(format);
963+ // Performance Boost! :D
964+ GeneratePixelBuffers (hdc, pbo, width, height, 4 );
965+ ReadPixelBuffers (hdc, dest, pbo, width, height, 4 , format);
966+ }
967+ else
968+ {
969+ GLenum gl_format = [](ImageFormat format) -> GLenum {
970+ switch (format)
971+ {
972+ case ImageFormat::BGR_BGRA: return GL_BGRA;
973+ case ImageFormat::BGRA: return GL_BGRA;
974+ case ImageFormat::RGBA: return GL_RGBA;
975+ case ImageFormat::ARGB: return 0 ; // Not Supported
976+ case ImageFormat::ABGR: return 0 ; // Not Supported
977+ default : return GL_BGRA;
978+ }
979+ }(format);
959980
960- // Sad rendering implementation
961- glReadPixels (0 , 0 , width, height, gl_format, GL_UNSIGNED_BYTE, dest);
962- FlipImageVertically (width, height, dest);
963- }
981+ // Sad rendering implementation
982+ glReadPixels (0 , 0 , width, height, gl_format, GL_UNSIGNED_BYTE, dest);
983+ FlipImageVertically (width, height, dest);
964984 }
985+ }
965986
966- // Push Rendering Context
967- HGLRC old_ctx = wglGetCurrentContext ();
987+ // Push Rendering Context
988+ HGLRC old_ctx = wglGetCurrentContext ();
989+ if (old_ctx)
990+ {
968991 PushGLContext (hdc, width, height);
992+ }
969993
970- // Render Debug Graphics
971- if (control_center->get_debug_graphics ())
994+ // Render Debug Graphics
995+ if (control_center->get_debug_graphics ())
996+ {
997+ std::uint8_t * src = control_center->get_debug_image ();
998+ if (src)
972999 {
973- std::uint8_t * src = control_center->get_debug_image ();
974- if (src)
975- {
976- gl_draw_image (hdc, src, 0 , 0 , width, height, 4 , format);
977- }
1000+ gl_draw_image (hdc, src, 0 , 0 , width, height, 4 , format);
9781001 }
1002+ }
9791003
980- // Render Cursor
981- std::int32_t x = -1 ;
982- std::int32_t y = -1 ;
983- control_center->get_applet_mouse_position (&x, &y);
1004+ // Render Cursor
1005+ std::int32_t x = -1 ;
1006+ std::int32_t y = -1 ;
1007+ control_center->get_applet_mouse_position (&x, &y);
9841008
985- if (x > -1 && y > -1 )
986- {
987- glColor4ub (0xFF , 0x00 , 0x00 , 0xFF );
988- gl_draw_point (hdc, x, y, 0 , 4 );
989- }
1009+ if (x > -1 && y > -1 )
1010+ {
1011+ glColor4ub (0xFF , 0x00 , 0x00 , 0xFF );
1012+ gl_draw_point (hdc, x, y, 0 , 4 );
1013+ }
9901014
991- // Pop Rendering Context
992- if (old_ctx)
993- {
994- PopGLContext (hdc, old_ctx);
995- }
1015+ // Pop Rendering Context
1016+ if (old_ctx)
1017+ {
1018+ PopGLContext (hdc, old_ctx);
9961019 }
9971020 }
9981021
@@ -1044,25 +1067,28 @@ HRESULT __stdcall D3D9Device_EndScene(IDirect3DDevice9* device) noexcept
10441067 std::int32_t width = static_cast <std::int32_t >(viewport.Width );
10451068 std::int32_t height = static_cast <std::int32_t >(viewport.Height );
10461069
1047- std::int32_t applet_x = 0 ;
1048- std::int32_t applet_y = 0 ;
1049- std::size_t applet_width = 0 ;
1050- std::size_t applet_height = 0 ;
1070+ std::int32_t canvas_x = 0 ;
1071+ std::int32_t canvas_y = 0 ;
1072+ std::size_t canvas_width = 0 ;
1073+ std::size_t canvas_height = 0 ;
1074+
1075+ control_center->get_canvas_dimensions (&canvas_x, &canvas_y, &canvas_width, &canvas_height);
1076+ control_center->set_target_dimensions (canvas_width, canvas_height);
10511077
1052- control_center->get_applet_dimensions (&applet_x, &applet_y, &applet_width, &applet_height);
1078+ // Do not render on an invalid canvas
1079+ if (width != canvas_width || height != canvas_height)
1080+ {
1081+ if (directx_device9_endscene_hook)
1082+ {
1083+ return directx_device9_endscene_hook->call <HRESULT, decltype (D3D9Device_EndScene)>(device);
1084+ }
10531085
1054- // Terrible. RuneLite's UI flickers with icons because it draws like trash
1055- // So we need to feed the client the Applet's Width/Height and not the Viewport's Width/Height
1056- // Even though the ViewPort is what's being drawn on. RL draws outside that.
1057- control_center->set_target_dimensions (applet_width, applet_height);
1086+ return E_FAIL;
1087+ }
10581088
10591089 bool minimized = false ;
10601090 ImageFormat image_format = control_center->get_image_format ();
1061-
1062- if (width <= applet_width && height == applet_height)
1063- {
1064- dx_read_pixels (device, control_center->get_image (), width, height, minimized, image_format);
1065- }
1091+ dx_read_pixels (device, control_center->get_image (), canvas_width, canvas_height, minimized, image_format);
10661092
10671093 IDirect3DStateBlock9* block;
10681094 device->CreateStateBlock (D3DSBT_ALL, &block);
@@ -1077,16 +1103,13 @@ HRESULT __stdcall D3D9Device_EndScene(IDirect3DDevice9* device) noexcept
10771103 device->SetRenderState (D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
10781104 device->SetRenderState (D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
10791105
1080- if (width <= applet_width && height == applet_height )
1106+ if (control_center-> get_debug_graphics () && !minimized )
10811107 {
1082- if (control_center->get_debug_graphics () && !minimized)
1083- {
1084- dx_load_texture (device, debug_texture, debug_px_shader, debug_px_shader_constants_table, image_format, control_center->get_debug_image (), width, height);
1108+ dx_load_texture (device, debug_texture, debug_px_shader, debug_px_shader_constants_table, image_format, control_center->get_debug_image (), canvas_width, canvas_height);
10851109
1086- if (debug_texture)
1087- {
1088- dx_draw_texture (device, debug_texture, debug_constant_table, debug_shader, debug_vertex_buffer, image_format, 0.0 , 0.0 , static_cast <float >(width), static_cast <float >(height));
1089- }
1110+ if (debug_texture)
1111+ {
1112+ dx_draw_texture (device, debug_texture, debug_constant_table, debug_shader, debug_vertex_buffer, image_format, 0.0 , 0.0 , static_cast <float >(canvas_width), static_cast <float >(canvas_height));
10901113 }
10911114 }
10921115
0 commit comments