Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:28:31+00:00 2026-06-04T00:28:31+00:00

This lightning is really affecting my game’s performance because I am constantly adding and

  • 0

This lightning is really affecting my game’s performance because I am constantly adding and removing the lightning, but also each lighting strike is composed of 3 anti aliased lines using:

void ccDrawSmoothLine(CGPoint pos1, CGPoint pos2, float width)
{
    GLfloat lineVertices[12], curc[4];
    GLint   ir, ig, ib, ia;
    CGPoint dir, tan;

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_VERTEX_ARRAY,
    // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    //glEnable(GL_LINE_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    pos1.x *= CC_CONTENT_SCALE_FACTOR();
    pos1.y *= CC_CONTENT_SCALE_FACTOR();
    pos2.x *= CC_CONTENT_SCALE_FACTOR();
    pos2.y *= CC_CONTENT_SCALE_FACTOR();
    width *= CC_CONTENT_SCALE_FACTOR();

    width = width*2;
    dir.x = pos2.x - pos1.x;
    dir.y = pos2.y - pos1.y;
    float len = sqrtf(dir.x*dir.x+dir.y*dir.y);
    if(len<0.00001)
        return;
    dir.x = dir.x/len;
    dir.y = dir.y/len;
    tan.x = -width*dir.y;
    tan.y = width*dir.x;

    lineVertices[0] = pos1.x + tan.x;
    lineVertices[1] = pos1.y + tan.y;
    lineVertices[2] = pos2.x + tan.x;
    lineVertices[3] = pos2.y + tan.y;
    lineVertices[4] = pos1.x;
    lineVertices[5] = pos1.y;
    lineVertices[6] = pos2.x;
    lineVertices[7] = pos2.y;
    lineVertices[8] = pos1.x - tan.x;
    lineVertices[9] = pos1.y - tan.y;
    lineVertices[10] = pos2.x - tan.x;
    lineVertices[11] = pos2.y - tan.y;

    glGetFloatv(GL_CURRENT_COLOR,curc);
    ir = 255.0*curc[0];
    ig = 255.0*curc[1];
    ib = 255.0*curc[2];
    ia = 255.0*curc[3];

    const GLubyte lineColors[] = {
        ir, ig, ib, 0,
        ir, ig, ib, 0,
        ir, ig, ib, ia,
        ir, ig, ib, ia,
        ir, ig, ib, 0,
        ir, ig, ib, 0,
    };

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, lineVertices);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, lineColors);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
    glDisableClientState(GL_COLOR_ARRAY);

    // restore default state
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);
}

My FPS will drop to about 40, then shoot back up to 60. I’ve read that texture mapping the line could improve my game’s performance.

I have been trying to figure this out for several weeks now, with no luck. Can someone PLEASE help me with this?

This is my current ccDrawLines and draw method

-(void) draw
{
    numPoints_ = 0;
    glColor4ub(_color.r, _color.g, _color.b, _opacity);

    if (_opacity != 255)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    drawLightning(_strikePoint2, _strikePoint, _displacement, _minDisplacement, _seed, lightningPoints_, &numPoints_);
    ccDrawLines(lightningPoints_, numPoints_, texture);

    if (_opacity != 255)
        glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);

    glColor4f(1.0, 1.0, 1.0, 1.0);
}

void ccDrawLines( CGPoint* points, uint numberOfPoints, CCTexture2D* texture )
{
    //layout of points [0] = origin, [1] = destination and so on

    ccVertex2F vertices[numberOfPoints];
    if (CC_CONTENT_SCALE_FACTOR() != 1 )
    {
        for (int i = 0; i < numberOfPoints; i++)
        {
            vertices[i].x = points[i].x * CC_CONTENT_SCALE_FACTOR();
            vertices[i].y= points[i].y * CC_CONTENT_SCALE_FACTOR();
        }
        glVertexPointer(2, GL_FLOAT, 0, vertices);
    }
    else glVertexPointer(2, GL_FLOAT, 0, points);

    ccTex2F texCoords[numberOfPoints];

    float width = texture.pixelsWide;
    float height = texture.pixelsHigh;

    if (CC_CONTENT_SCALE_FACTOR() != 1 )
    {
        for (int i = 0; i < numberOfPoints; i++)
        {
            texCoords[i].u = (vertices[i].x * CC_CONTENT_SCALE_FACTOR()) / width;
            texCoords[i].v = (vertices[i].y * CC_CONTENT_SCALE_FACTOR()) / height;
        }
        glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    }
    else glTexCoordPointer(2, GL_FLOAT, 0, points);

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_VERTEX_ARRAY,
    // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
    glPushMatrix();
    glBindTexture(GL_TEXTURE_2D, [texture name]);
    glDisableClientState(GL_COLOR_ARRAY);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, numberOfPoints);

    // restore default state
    glEnableClientState(GL_COLOR_ARRAY);

    glPopMatrix();
}

The texture is just a 32×32 .png file with a small blue dot.

If you look at ccDrawLines I have added the code to texture map the line. The problem with it is, gaps in the line, multiple lines being drawn, and it looks horrible.


EDIT:
I decided not to texture map the line and use ccDrawSmoothLine.

All I did was allocate the lightning in my gamelayer’s init

lightningStrike_ = [Lightning lightningWithStrikePoint:ccp(-100, -100) strikePoint2:ccp(-100, -100)];
[self addChild:lightningStrike_ z:1];

Then, I created an instance method to set the _strikePoint and _strikePoint2 properties and call the strikeRandom method.

-(Lightning *)lightningStrike:(CGPoint)p end:(CGPoint)p2
{
    lightningStrike_.strikePoint = ccp(p.x, p.y);
    lightningStrike_.strikePoint2 = ccp(p2.x, p2.y);
    [lightningStrike_ strikeRandom];

    return lightningStrike_;
}

Usage:

[self lightningStrike:ccp(100, 100) end:ccp(100, 100)];

This fixed the FPS drop. After 24 hours I will answer and accept my own answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:28:32+00:00Added an answer on June 4, 2026 at 12:28 am

    EDIT: I decided not to texture map the line and use ccDrawSmoothLine.

    All I did was allocate the lightning in my gamelayer’s init

    lightningStrike_ = [Lightning lightningWithStrikePoint:ccp(-100, -100) strikePoint2:ccp(-100, -100)];
    [self addChild:lightningStrike_ z:1];

    Then, I created an instance method to set the _strikePoint and _strikePoint2 properties and call the strikeRandom method.

    -(Lightning *)lightningStrike:(CGPoint)p end:(CGPoint)p2
    {
        lightningStrike_.strikePoint = ccp(p.x, p.y);
        lightningStrike_.strikePoint2 = ccp(p2.x, p2.y);
        [lightningStrike_ strikeRandom];

        return lightningStrike_;
    }

    Usage:

    [self lightningStrike:ccp(100, 100) end:ccp(100, 100)];

    This fixed the FPS drop. After 24 hours I will answer and accept my own answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is really just for my own use: I would like to be able
This should be simple, but the answer is eluding me. If I've got a
this is my code that I write it but I want to use LINQ
This is probably a stupid question, but how much of a security risk is
This is a complex question, because there are a lot of moving parts. My
Performance is of the utmost importance on this one guys... This thing needs to
This is long but I promise it's interesting. :) I'm trying to mimic the
Been coding for 20+ years. I'm new to Java3d but I am really impressed
This is probably a really simple question, and I'm just not getting it. I'd
I asked this in the jboss forum already but didn't get an answer yet:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.