Jump to content
A 2021 backup has been restored. Forums are closed and work in progress. Join our Discord server for more updates! ×
SoaH City Message Board

[Game Maker] Need help to optimize code


thevaleev

Recommended Posts

I use this code for background parallax. It work when I activate one view, but when I activate two views, fps slown down.

Please help me to optimize this code, make it easier and smaller.


var _Scale, _i;

// UNDERWATER AND WATER
draw_background_stretched(backGHUnderWater2,view_xview[view_current],objWater.y,view_wview[view_current],view_hview[view_current]);

// Update position, relative to the current scroll
x = floor(view_xview[view_current]);
y = min(objWater.y-200, 400+abs(view_yview[view_current]/-1.15))

// Calculate scale and limit it over the MaxScaleY
_Scale = min(max((objWater.y - y)/background_get_height(backParallax), -MaxScaleY), MaxScaleY);

// Render all the pieces, in movement and properly scaled.
for (_i = 0; _i<20 _i+=1) {
draw_background_general(backParallax, 0, _i*8, background_get_width(backParallax), 8,
x-((current_time*0.01)*_i mod 100),
y+(_i*8)*_Scale,
1, _Scale, 0, c_white, c_white, c_white, c_white, 1);
}

scrDrawSpriteTiledHorizontal(bgSB,0,view_xview[view_current]/1.005, min(objWater.y-200, 400+abs(view_yview[view_current]/-1.15)))
scrDrawSpriteTiledHorizontal(bgSB,1,view_xview[view_current]/1.007, min(objWater.y-200, 400+abs(view_yview[view_current]/-1.15)))
scrDrawSpriteTiledHorizontal(bgSB,2,view_xview[view_current]/1.009, min(objWater.y-200, 400+abs(view_yview[view_current]/-1.15)))
scrDrawBackgroundTiledHorizontal(backGHUnderWater,view_xview[view_current]/1.1, objWater.y)


// SHIPS
for(xx=1750/2;xx<5250/2;xx+=600)
{
draw_sprite(bgShips,0,xx+view_xview[view_current]/1.05,min(objWater.y-180, 470+abs(view_yview[view_current]/-1.2)))
};
for(xx=2500/2;xx<5500/2;xx+=600)
{
draw_sprite(bgShips,1,xx+view_xview[view_current]/1.1,min(objWater.y-115, 460+65+abs(view_yview[view_current]/-1.2)))
};
for(xx=3000/2;xx<6000/2;xx+=600)
{
draw_sprite(bgShips,2,xx+view_xview[view_current]/1.2,min(objWater.y-60, 460+65+55+abs(view_yview[view_current]/-1.2)))
};


for(xx=(18500/2)/2;xx<(24500/2)/2;xx+=600)
{
draw_sprite(bgShips,0,xx+view_xview[view_current]/1.05,min(objWater.y-180, 470+abs(view_yview[view_current]/-1.2)))
};
for(xx=(19000/2)/2;xx<(25000/2)/2;xx+=600)
{
draw_sprite(bgShips,1,xx+view_xview[view_current]/1.1,min(objWater.y-115, 460+65+abs(view_yview[view_current]/-1.2)))
};
for(xx=(20000/2)/2;xx<(26000/2)/2;xx+=600)
{
draw_sprite(bgShips,2,xx+view_xview[view_current]/1.2,min(objWater.y-60, 460+65+55+abs(view_yview[view_current]/-1.2)))
};

// GREEN CANYON PART
for(xx=19000/2;xx<26000/2;xx+=600)
{
draw_sprite(backGreenResort,2,xx+view_xview[view_current]/1.9,max(view_yview[view_current]+390, 270+abs(view_yview[view_current]/-1.05)))
};
for(xx=18000/2;xx<25000/2;xx+=700)
{
draw_sprite(backGreenResort,1,xx+view_xview[view_current]/1.8,max(view_yview[view_current]+390, 290+abs(view_yview[view_current]/-1.05)))
};
for(xx=17000/2;xx<23000/2;xx+=800)
{
draw_sprite(backGreenResort,0,xx+view_xview[view_current]/1.7,max(view_yview[view_current]+390, 310+abs(view_yview[view_current]/-1.05)))
};
for(xx=20000/2;xx<27300/2;xx+=174)
{
draw_sprite(backGreenResort2,0,xx+view_xview[view_current]/2,max(view_yview[view_current]+390, 330+abs(view_yview[view_current]/-1.05)))
};

// CITY
for(xx=0;xx<2120/2;xx+=128)
{draw_sprite(sprite100,0,xx+view_xview[view_current]/1.1,max(view_yview[view_current]+190, 90+abs(view_yview[view_current]/-1.05)))};
for(xx=0;xx<3180/2;xx+=820)
{draw_sprite(sprite1200,0,xx+view_xview[view_current]/1.2,max(view_yview[view_current]+130+104, 90+64+64+64+abs(view_yview[view_current]/-1.1)))}
for(xx=0;xx<4680/2;xx+=240)
{draw_sprite(sprite1300,0,xx+view_xview[view_current]/1.3,max(view_yview[view_current]+110+110, 90+64+64+32+abs(view_yview[view_current]/-1.1)))}
for(xx=0;xx<5680/2;xx+=387)
{draw_sprite(sprite1400,0,xx+view_xview[view_current]/1.4,max(view_yview[view_current]+170+50, 90+64+64+64+abs(view_yview[view_current]/-1.1)))}
for(xx=0;xx<10680/2;xx+=179)
{draw_sprite(sprite1700,0,xx+view_xview[view_current]/1.8,max(view_yview[view_current]+90, 90+64+64+abs(view_yview[view_current]/-1.1)))}
for(xx=0;xx<11680/2;xx+=90)
{draw_sprite(sprite000,0,xx+view_xview[view_current]/2,max(view_yview[view_current]+300, 90+64+64+64+64+64+abs(view_yview[view_current]/-1.1)))}

for(xx=38200/2;xx<45000/2;xx+=128)
{draw_sprite(sprite100,0,xx+view_xview[view_current]/2,max(view_yview[view_current]+190, 90+abs(view_yview[view_current]/-1.05)))};
for(xx=39200/2;xx<45000/2;xx+=820)
{draw_sprite(sprite1200,0,xx+view_xview[view_current]/2,max(view_yview[view_current]+130+104, 90+64+64+64+abs(view_yview[view_current]/-1.1)))}
for(xx=39500/2;xx<45000/2;xx+=240)
{draw_sprite(sprite1300,0,xx+view_xview[view_current]/2,max(view_yview[view_current]+110+110, 90+64+64+32+abs(view_yview[view_current]/-1.1)))}
for(xx=39500/2;xx<45000/2;xx+=387)
{draw_sprite(sprite1400,0,xx+view_xview[view_current]/2,max(view_yview[view_current]+170+50, 90+64+64+64+abs(view_yview[view_current]/-1.1)))}
for(xx=34500/2;xx<45000/2;xx+=179)
{draw_sprite(sprite1700,0,xx+view_xview[view_current]/1.8,max(view_yview[view_current]+90, 90+64+64+abs(view_yview[view_current]/-1.1)))}
for(xx=38500/2;xx<45000/2;xx+=90)
{draw_sprite(sprite000,0,xx+view_xview[view_current]/2,max(view_yview[view_current]+300, 90+64+64+64+64+64+abs(view_yview[view_current]/-1.1)))}
[/CODE]

Link to comment
Share on other sites

  • 4 weeks later...

Are the two views split screen, or is one the camera and another a dummy so player 2 doesn't fall through the floor?

If it's the later, you can simply check at the top if view_current==0, and then it'll only draw these things in the first view.

If it's the former, you'll want to take the advice of the posters above me. See if you can't unravel some of those loops, or simplify the background by using more compound images.

I'd offer more advice, but I'd have to know more about what it is you're actually trying to render before I can do so.

Link to comment
Share on other sites

Are the two views split screen, or is one the camera and another a dummy so player 2 doesn't fall through the floor?

Split screen

...but I'd have to know more about what it is you're actually trying to render before I can do so.

I want to know how make parallax to draw background from one custom x-position to another.

Link to comment
Share on other sites

Oh, no one is going to point out that he is drawing shit every frame that he should really only be drawing if he is within the immediate proximity?

Fact of the matter is you need to be more clever about this in general. You don't draw every sprite in a background to the scene every frame. Instead you figure out what sprites you need based on where you are at and you draw just those sprites. And the trick isn't to figure out which sprites you need to draw by having them in preset places, the trick is to figure out where your sprites need to go based on where you are at.

Link to comment
Share on other sites

  • 2 weeks later...
I want to know how make parallax to draw background from one custom x-position to another.

Meant the elements of the background itself. What is it supposed to look like?

Oh, no one is going to point out that he is drawing shit every frame that he should really only be drawing if he is within the immediate proximity?

Based on the code he's posted, the overhead to check when to draw each element in addition to where would probably cost more than just drawing everything every frame.

He's better off finding a way to reduce the number of elements drawn altogether. (Hence my question)

Link to comment
Share on other sites

Except it's not since you don't need to check it on an individual element basis. It's a simple if else ladder based on x position.

if (camera_x &lt; x_pos_1) {
    //draw objects 1, 2, 3
} else if (camera_x &lt; x_pos_2) {
    //draw objects 2, 3, 4
}

etc. etc. until the list is exhausted.

Keeping in mind that you need to draw them at the needed positions likely using the mod operation based on your x resolution

It's a complex parallax, but figuring out how to optimize it should be pretty easy if you just consider what pieces need to be rendered at what positions rather than trying to make a huge map out of the whole thing.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...