Skip to content

Commit d95de81

Browse files
Reorder rlSetTexture and copy mode only if drawcounter added
1 parent 7f1c549 commit d95de81

File tree

8 files changed

+41
-37
lines changed

8 files changed

+41
-37
lines changed

examples/models/models_textured_cube.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
103103
float y = position.y;
104104
float z = position.z;
105105

106-
// Set desired texture to be enabled while drawing following vertex data
107-
rlSetTexture(texture.id);
108106

109107
// Vertex data transformation can be defined with the commented lines,
110108
// but in this example we calculate the transformed vertex data directly when calling rlVertex3f()
@@ -115,6 +113,10 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
115113
//rlScalef(2.0f, 2.0f, 2.0f);
116114

117115
rlBegin(RL_QUADS);
116+
117+
// Set desired texture to be enabled while drawing following vertex data
118+
rlSetTexture(texture.id);
119+
118120
rlColor4ub(color.r, color.g, color.b, color.a);
119121
// Front Face
120122
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
@@ -167,12 +169,13 @@ void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, f
167169
float texWidth = (float)texture.width;
168170
float texHeight = (float)texture.height;
169171

170-
// Set desired texture to be enabled while drawing following vertex data
171-
rlSetTexture(texture.id);
172-
173172
// We calculate the normalized texture coordinates for the desired texture-source-rectangle
174173
// It means converting from (tex.width, tex.height) coordinates to [0.0f, 1.0f] equivalent
175174
rlBegin(RL_QUADS);
175+
176+
// Set desired texture to be enabled while drawing following vertex data
177+
rlSetTexture(texture.id);
178+
176179
rlColor4ub(color.r, color.g, color.b, color.a);
177180

178181
// Front face

examples/shapes/shapes_rectangle_advanced.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ static void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, fl
149149
const float angles[4] = { 180.0f, 270.0f, 0.0f, 90.0f };
150150

151151
#if defined(SUPPORT_QUADS_DRAW_MODE)
152+
153+
rlBegin(RL_QUADS);
152154
rlSetTexture(GetShapesTexture().id);
153155
Rectangle shapeRect = GetShapesTextureRectangle();
154156

155-
rlBegin(RL_QUADS);
156157
// Draw all the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
157158
for (int k = 0; k < 4; ++k)
158159
{

examples/text/text_3d_drawing.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,12 @@ static void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, floa
488488
if (SHOW_LETTER_BOUNDRY) DrawCubeWiresV((Vector3){ position.x + width/2, position.y, position.z + height/2}, (Vector3){ width, LETTER_BOUNDRY_SIZE, height }, LETTER_BOUNDRY_COLOR);
489489

490490
rlCheckRenderBatchLimit(4 + 4*backface);
491-
rlSetTexture(font.texture.id);
492491

493492
rlPushMatrix();
494493
rlTranslatef(position.x, position.y, position.z);
495494

496495
rlBegin(RL_QUADS);
496+
rlSetTexture(font.texture.id);
497497
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
498498

499499
// Front Face

examples/textures/textures_textured_curve.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ static void DrawTexturedCurve(void)
207207
Vector2 currentNegNormal = Vector2Add(current, Vector2Scale(normal, -curveWidth));
208208

209209
// Draw the segment as a quad
210-
rlSetTexture(texRoad.id);
211210
rlBegin(RL_QUADS);
211+
rlSetTexture(texRoad.id);
212212
rlColor4ub(255,255,255,255);
213213
rlNormal3f(0.0f, 0.0f, 1.0f);
214214

src/rlgl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,13 +1676,13 @@ void rlSetTexture(unsigned int id)
16761676
RLGL.State.vertexCounter += RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment;
16771677

16781678
RLGL.currentBatch->drawCounter++;
1679+
1680+
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 2].mode;
16791681
}
16801682
}
16811683

16821684
if (RLGL.currentBatch->drawCounter >= RL_DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch);
1683-
if (RLGL.currentBatch->drawCounter > 1) {
1684-
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 2].mode;
1685-
}
1685+
16861686
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = id;
16871687
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount = 0;
16881688
}

src/rmodels.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3939,8 +3939,8 @@ void DrawBillboardPro(Camera camera, Texture2D texture, Rectangle source, Vector
39393939
texcoords[2] = (Vector2){ (float)(source.x + source.width)/texture.width, (float)source.y/texture.height };
39403940
texcoords[3] = (Vector2){ (float)source.x/texture.width, (float)source.y/texture.height };
39413941

3942-
rlSetTexture(texture.id);
39433942
rlBegin(RL_QUADS);
3943+
rlSetTexture(texture.id);
39443944

39453945
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
39463946
for (int i = 0; i < 4; i++)

src/rshapes.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ void DrawPixel(int posX, int posY, Color color)
132132
void DrawPixelV(Vector2 position, Color color)
133133
{
134134
#if defined(SUPPORT_QUADS_DRAW_MODE)
135-
rlSetTexture(GetShapesTexture().id);
136-
Rectangle shapeRect = GetShapesTextureRectangle();
137135

138136
rlBegin(RL_QUADS);
137+
rlSetTexture(GetShapesTexture().id);
138+
Rectangle shapeRect = GetShapesTextureRectangle();
139139

140140
rlNormal3f(0.0f, 0.0f, 1.0f);
141141
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -311,10 +311,10 @@ void DrawCircleSector(Vector2 center, float radius, float startAngle, float endA
311311
float angle = startAngle;
312312

313313
#if defined(SUPPORT_QUADS_DRAW_MODE)
314-
rlSetTexture(GetShapesTexture().id);
315-
Rectangle shapeRect = GetShapesTextureRectangle();
316314

317315
rlBegin(RL_QUADS);
316+
rlSetTexture(GetShapesTexture().id);
317+
Rectangle shapeRect = GetShapesTextureRectangle();
318318

319319
// NOTE: Every QUAD actually represents two segments
320320
for (int i = 0; i < segments/2; i++)
@@ -552,10 +552,10 @@ void DrawRing(Vector2 center, float innerRadius, float outerRadius, float startA
552552
float angle = startAngle;
553553

554554
#if defined(SUPPORT_QUADS_DRAW_MODE)
555-
rlSetTexture(GetShapesTexture().id);
556-
Rectangle shapeRect = GetShapesTextureRectangle();
557555

558556
rlBegin(RL_QUADS);
557+
rlSetTexture(GetShapesTexture().id);
558+
Rectangle shapeRect = GetShapesTextureRectangle();
559559
for (int i = 0; i < segments; i++)
560560
{
561561
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -732,10 +732,10 @@ void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color
732732
}
733733

734734
#if defined(SUPPORT_QUADS_DRAW_MODE)
735-
rlSetTexture(GetShapesTexture().id);
736-
Rectangle shapeRect = GetShapesTextureRectangle();
737735

738736
rlBegin(RL_QUADS);
737+
rlSetTexture(GetShapesTexture().id);
738+
Rectangle shapeRect = GetShapesTextureRectangle();
739739

740740
rlNormal3f(0.0f, 0.0f, 1.0f);
741741
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -787,10 +787,10 @@ void DrawRectangleGradientH(int posX, int posY, int width, int height, Color lef
787787
// Draw a gradient-filled rectangle
788788
void DrawRectangleGradientEx(Rectangle rec, Color topLeft, Color bottomLeft, Color bottomRight, Color topRight)
789789
{
790-
rlSetTexture(GetShapesTexture().id);
791-
Rectangle shapeRect = GetShapesTextureRectangle();
792790

793791
rlBegin(RL_QUADS);
792+
rlSetTexture(GetShapesTexture().id);
793+
Rectangle shapeRect = GetShapesTextureRectangle();
794794
rlNormal3f(0.0f, 0.0f, 1.0f);
795795

796796
// NOTE: Default raylib font character 95 is a white square
@@ -952,10 +952,10 @@ void DrawRectangleRounded(Rectangle rec, float roundness, int segments, Color co
952952
const float angles[4] = { 180.0f, 270.0f, 0.0f, 90.0f };
953953

954954
#if defined(SUPPORT_QUADS_DRAW_MODE)
955-
rlSetTexture(GetShapesTexture().id);
956-
Rectangle shapeRect = GetShapesTextureRectangle();
957955

958956
rlBegin(RL_QUADS);
957+
rlSetTexture(GetShapesTexture().id);
958+
Rectangle shapeRect = GetShapesTextureRectangle();
959959
// Draw all the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
960960
for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
961961
{
@@ -1206,10 +1206,10 @@ void DrawRectangleRoundedLinesEx(Rectangle rec, float roundness, int segments, f
12061206
if (lineThick > 1)
12071207
{
12081208
#if defined(SUPPORT_QUADS_DRAW_MODE)
1209-
rlSetTexture(GetShapesTexture().id);
1210-
Rectangle shapeRect = GetShapesTextureRectangle();
12111209

12121210
rlBegin(RL_QUADS);
1211+
rlSetTexture(GetShapesTexture().id);
1212+
Rectangle shapeRect = GetShapesTextureRectangle();
12131213

12141214
// Draw all the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
12151215
for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
@@ -1382,10 +1382,10 @@ void DrawRectangleRoundedLinesEx(Rectangle rec, float roundness, int segments, f
13821382
void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
13831383
{
13841384
#if defined(SUPPORT_QUADS_DRAW_MODE)
1385-
rlSetTexture(GetShapesTexture().id);
1386-
Rectangle shapeRect = GetShapesTextureRectangle();
13871385

13881386
rlBegin(RL_QUADS);
1387+
rlSetTexture(GetShapesTexture().id);
1388+
Rectangle shapeRect = GetShapesTextureRectangle();
13891389
rlColor4ub(color.r, color.g, color.b, color.a);
13901390

13911391
rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
@@ -1436,10 +1436,10 @@ void DrawTriangleFan(const Vector2 *points, int pointCount, Color color)
14361436
{
14371437
if (pointCount >= 3)
14381438
{
1439-
rlSetTexture(GetShapesTexture().id);
1440-
Rectangle shapeRect = GetShapesTextureRectangle();
14411439

14421440
rlBegin(RL_QUADS);
1441+
rlSetTexture(GetShapesTexture().id);
1442+
Rectangle shapeRect = GetShapesTextureRectangle();
14431443
rlColor4ub(color.r, color.g, color.b, color.a);
14441444

14451445
for (int i = 1; i < pointCount - 1; i++)
@@ -1497,10 +1497,10 @@ void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color col
14971497
float angleStep = 360.0f/(float)sides*DEG2RAD;
14981498

14991499
#if defined(SUPPORT_QUADS_DRAW_MODE)
1500-
rlSetTexture(GetShapesTexture().id);
1501-
Rectangle shapeRect = GetShapesTextureRectangle();
15021500

15031501
rlBegin(RL_QUADS);
1502+
rlSetTexture(GetShapesTexture().id);
1503+
Rectangle shapeRect = GetShapesTextureRectangle();
15041504
for (int i = 0; i < sides; i++)
15051505
{
15061506
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -1566,10 +1566,10 @@ void DrawPolyLinesEx(Vector2 center, int sides, float radius, float rotation, fl
15661566
float innerRadius = radius - (lineThick*cosf(DEG2RAD*exteriorAngle/2.0f));
15671567

15681568
#if defined(SUPPORT_QUADS_DRAW_MODE)
1569-
rlSetTexture(GetShapesTexture().id);
1570-
Rectangle shapeRect = GetShapesTextureRectangle();
15711569

15721570
rlBegin(RL_QUADS);
1571+
rlSetTexture(GetShapesTexture().id);
1572+
Rectangle shapeRect = GetShapesTextureRectangle();
15731573
for (int i = 0; i < sides; i++)
15741574
{
15751575
rlColor4ub(color.r, color.g, color.b, color.a);

src/rtextures.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4568,8 +4568,8 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2
45684568
bottomRight.y = y + (dx + dest.width)*sinRotation + (dy + dest.height)*cosRotation;
45694569
}
45704570

4571-
rlSetTexture(texture.id);
45724571
rlBegin(RL_QUADS);
4572+
rlSetTexture(texture.id);
45734573

45744574
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
45754575
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
@@ -4603,13 +4603,13 @@ void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2
46034603
// I leave here the old implementation for educational purposes,
46044604
// just in case someone wants to do some performance test
46054605
/*
4606-
rlSetTexture(texture.id);
46074606
rlPushMatrix();
46084607
rlTranslatef(dest.x, dest.y, 0.0f);
46094608
if (rotation != 0.0f) rlRotatef(rotation, 0.0f, 0.0f, 1.0f);
46104609
rlTranslatef(-origin.x, -origin.y, 0.0f);
46114610
46124611
rlBegin(RL_QUADS);
4612+
rlSetTexture(texture.id);
46134613
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
46144614
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
46154615
@@ -4698,14 +4698,14 @@ void DrawTextureNPatch(Texture2D texture, NPatchInfo nPatchInfo, Rectangle dest,
46984698
coordD.x = (nPatchInfo.source.x + nPatchInfo.source.width)/width;
46994699
coordD.y = (nPatchInfo.source.y + nPatchInfo.source.height)/height;
47004700

4701-
rlSetTexture(texture.id);
47024701

47034702
rlPushMatrix();
47044703
rlTranslatef(dest.x, dest.y, 0.0f);
47054704
rlRotatef(rotation, 0.0f, 0.0f, 1.0f);
47064705
rlTranslatef(-origin.x, -origin.y, 0.0f);
47074706

47084707
rlBegin(RL_QUADS);
4708+
rlSetTexture(texture.id);
47094709
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
47104710
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
47114711

0 commit comments

Comments
 (0)