[C++] Keep crashing when calling "surface()->DrawTexturedPolygon()" and some other functions

Hi, I’m still trying to write a binary graphical module and I’ve got another problem.
When the function “surface()->DrawTexturedPolygon()” is calling, GMod crashes for unknown reason.
Other similar functions like “DrawTexturedRect” or “DrawOutlinedRect” work fine.

A fragment of code where I use it:

struct Vertex2D {
	float x;
	float y;
	float u;
	float v;

vertex1 = Vertex2D {
vertex2 = Vertex2D {
	cenx + vec1x,
	ceny + vec1y,
	0.5f + 0.5f * vec1x * w2_,
	0.5f + 0.5f * vec1y * h2_

Vertex_t poly_draw[3];
for (int i = 0; i < count; i++)
	Vertex2D vertex3 = verts_draw*;
	// if counterclockwise
	if (ccw) {
		poly_draw[0].Init(Vector2D(vertex3.x, vertex3.y), Vector2D(vertex3.u, vertex3.v));
		poly_draw[1].Init(Vector2D(vertex2.x, vertex2.y), Vector2D(vertex2.u, vertex2.v));
		poly_draw[2].Init(Vector2D(vertex1.x, vertex1.y), Vector2D(vertex1.u, vertex1.v));
	else {
		poly_draw[0].Init(Vector2D(vertex1.x, vertex1.y), Vector2D(vertex1.u, vertex1.v));
		poly_draw[1].Init(Vector2D(vertex2.x, vertex2.y), Vector2D(vertex2.u, vertex2.v));
		poly_draw[2].Init(Vector2D(vertex1.x, vertex3.y), Vector2D(vertex3.u, vertex3.v));

	surface()->DrawTexturedPolygon(3, poly_draw);
	vertex2 = vertex3;

If I exlude line with “surface()->DrawTexturedPolygon(3, poly_draw)”, the whole code works fine. I’ve also tested output values via console.
What I noticed, in source files there are some functions that commented with “SRC only functions” in “vgui/ISurface.h”.

// SRC only functions!!!
virtual void DrawTexturedLine( const Vertex_t &a, const Vertex_t &b ) = 0;
virtual void DrawOutlinedCircle(int x, int y, int radius, int segments) = 0;
virtual void DrawTexturedPolyLine( const Vertex_t *p,int n ) = 0; // (Note: this connects the first and last points).
virtual void DrawTexturedSubRect( int x0, int y0, int x1, int y1, float texs0, float text0, float texs1, float text1 ) = 0;
virtual void DrawTexturedPolygon(int n, Vertex_t *pVertice, bool bClipVertices = true ) = 0;
virtual const wchar_t *GetTitle(VPANEL panel) = 0;
virtual bool IsCursorLocked( void ) const = 0;
virtual void SetWorkspaceInsets( int left, int top, int right, int bottom ) = 0;

As I tested, using any of them causes the game to crash.
I haven’t found any documentation to this functions and what does “SRC only” means.
So I’m asking for help.

Why are you making a surface.DrawTexturedPolygon when u could just use surface.DrawPoly

You do realize that this is C++ API and not Lua API he’s trying to use here?

Yes but whats the purpose of recreating something in C++ for Gmod that’s built into Gmod already?

Optimizing for better performance.

What does your surface header look like and where did you get it from? Is surface() function from the source sdk or did you make it yourself? If you made it yourself, where are you getting the surface class instance from?

it’s from Source SDK 2013, I thought I mentioned it above:

// vgui_controls/Controls.h

// #include <vgui/ISurface.h>
inline vgui::ISurface *surface()
	return g_pVGuiSurface;

Btw, I’ve already connected “vguimatsurface.dll” library to make “g_pVGuiSurface” work:

CDllDemandLoader vguisurface_module("vguimatsurface");

	g_pVGuiSurface	= (vgui::ISurface*)vguisurface_module.GetFactory()(VGUI_SURFACE_INTERFACE_VERSION, NULL);
	// ...

Maybe, there are some other modules I need to connect.

The ISurface header is misaligned if I recall correctly.

Does it mean that this header file differs from GMod’s one? (I’m rather new to c++ programming)
Is there a way to fix it?

This may seem like a very stupid question. I never used the C++ api for rendering stuffs. But did you set the texture before drawing? :pudge:

I don’t think that this can cause problems, because “DrawTexturedRect” works normally, anyway, I’m setting a texture (material) using Lua before calling this function.
Also I tested other functions listed below the “DrawTexturedPolygon” in ISurface class, calling most of them causing the game to crash or just nothing happens.
It seems, that after line where “enum EFontFlags” declared (236 in vgui/ISurface.h), all functions are broken.

I don’t think what you need to do is worth your time, nothing will really be optimized by calling these functions in C++.

Declaring an enum in a class does nothing to the class after it’s compiled. The reason that you’re crashing is because the header is misaligned.

Basically, the surface class in memory may have had it’s methods moved around in order, it may have had functions removed and it may have had functions added. All this means that when you call DrawTexturedPolygon, you’re actually calling something else, expecting different parameters and then it crashes.

You need to realign your classes by figuring out how it’s laid out in memory, the easiest way to do this is with IDA and the mac compiled .dylib’s for the module that contains isurface functions. There are tutorials on how to do this somewhere.

All as I expected it to be.

Maybe there’s no gain in single use of coding graphics in c++, but I’m also trying to draw texture fonts, to render too much stuff on screen and I think, lua works not so fast as binary code works (I have already wrote it on lua).

I didn’t say that enums are the reason of game crashing, I said that functions that come after enums are not working anymore, meaning that there’s some stuff added between enums and them.
If it’s so, it sucks. Official wiki offers you to write binary modules, but you can hardly use source files from SDK. Anyway, I’ll do what I can, it’s an interesting puzzle.

Hilarious, there’s a note for this man who figured out to add functions this way:

vgui/ISurface.h, end of file

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !! Add your new stuff to the bottom of IMatSystemSurface instead.        !!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

But maybe it’s not his fault, I’ll try to compare files in SDK 2013 and SKD 2007, however, I’ve compiled it with both versions and it gave no result.

I made this in about 2015, it might still be aligned. If it’s not there’s no point doing anything but what I said to align it, unless you have a different method.

Also, you can’t paste in my header, the type names and stuff are changed. Just change the methods in your header to be aligned with mine, using the SDK’s types (or just blank functions).

OMG, It worked. Thanks for help.
Haven’t tested other functions yet, but this is a good result anyway.

By optimization I meant something like this:
Maybe in this example it’s not a big deal, but it’s already 1,5 times faster.

This is my ISurface.h header after all manipulations.