If statement with rendering illusions before/after depending on position relative to set y point not working

huangapple go评论74阅读模式
英文:

If statement with rendering illusions before/after depending on position relative to set y point not working

问题

以下是已经翻译好的部分:

"To be more clear this block started as a code for solely mainchar and Realchair so that when my character is above a certain y level it will give the illusion that he is behind the chair by rendering the character before the chair and if my character is under the set y level then he is rendered after the chair to give the illusion that he is in front of the chair."

"ok but as you can see this is a switch because i have ImageTextureSide (mainchar facing right) and ImageTextureSideTwo (mainchar facing left), so i need to put this idea of the rendering illusion in both of the cases."

"Now here is where the problem arises. I have multiple furniture assets in my project so i figured i would add the idea of the rendering illusion to Rightchair, but for some reason when i added the same logic, everything worked perfectly fine except for when mainchar.y < the set y value. In the case where it is less than the set y value it should be rendered before the chair which is what is written in the if statement for that situation. But only when i added the logic for the right chair is when mainchar.y < the set y value it renders the mainchar after the chair."

英文:

To be more clear this block started as a code for solely mainchar and Realchair so that when my character is above a certain y level it will give the illusion that he is behind the chair by rendering the character before the chair and if my character is under the set y level then he is rendered after the chair to give the illusion that he is in front of the chair.

ok but as you can see this is a switch because i have ImageTextureSide (mainchar facing right) and ImageTextureSideTwo (mainchar facing left), so i need to put this idea of the rendering illusion in both of the cases.

Now here is where the problem arises. I have multiple furniture assets in my project so i figured i would add the idea of the rendering illusion to Rightchair, but for some reason when i added the same logic, everything worked perfectly fine except for when mainchar.y < the set y value. In the case where it is less than the set y value it should be rendered before the chair which is what is written in the if statement for that situation. But only when i added the logic for the right chair is when mainchar.y < the set y value it renders the mainchar after the chair.

I added an image where you can see mainchar rendered after the chair despite the if statement running?

 switch (currentDirection) {
        case SideLeft:
            SDL_RenderCopy(renderer, imageTextureglassybig, nullptr, &amp;Glassy);

            if (mainchar.y &gt; 250) {
                // Main character is above the chair, render chair first
                SDL_RenderCopy(renderer, imageTextureChair, nullptr, &amp;Realchair);
                SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &amp;mainchar);
            }
            else {
                // Main character is below the chair, render main character first
                SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &amp;mainchar);
                SDL_RenderCopy(renderer, imageTextureChair, nullptr, &amp;Realchair);
            }

            if (mainchar.y &gt; 250) {
                // Main character is above the chair, render chair first
                SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &amp;Rightchair);
                SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &amp;mainchar);
            }
            else {
                // Main character is below the chair, render main character first
                SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &amp;mainchar);
                SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &amp;Rightchair);
            }
           
            SDL_RenderCopy(renderer, imageTextureTChair, nullptr, &amp;Topchair);
            SDL_RenderCopy(renderer, imageTextureTChair3, nullptr, &amp;Topchair3);
            SDL_RenderCopy(renderer, imageTexturetable, nullptr, &amp;Bigtable);
            SDL_RenderCopy(renderer, imageTextureBChair, nullptr, &amp;Bottomchair);
            SDL_RenderCopy(renderer, imageTextureBChair2, nullptr, &amp;Bottomchair2);
            SDL_RenderCopy(renderer, imageTextureRminitable, nullptr, &amp;Minitbl);


            break;

        case SideRight:

            SDL_RenderCopy(renderer, imageTextureglassybig, nullptr, &amp;Glassy);

            if (mainchar.y &lt; 250) {
                // Main character is above the chair, render chair first
                SDL_RenderCopy(renderer, imageTextureSide, nullptr, &amp;mainchar);
                SDL_RenderCopy(renderer, imageTextureChair, nullptr, &amp;Realchair);

                cout &lt;&lt; (&quot;\n RIGHTif&quot;);
            }
            else {
                // Main character is below the chair, render main character first
                SDL_RenderCopy(renderer, imageTextureChair, nullptr, &amp;Realchair);
                SDL_RenderCopy(renderer, imageTextureSide, nullptr, &amp;mainchar);
                cout &lt;&lt; (&quot;RIGHTelse&quot;);

            }

            if (mainchar.y &lt; 250) {
                // Main character is above the chair, render chair first
                SDL_RenderCopy(renderer, imageTextureSide, nullptr, &amp;mainchar);
                SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &amp;Rightchair);

                cout &lt;&lt; (&quot;\n RIGHTif&quot;);
            }
            else {
                // Main character is below the chair, render main character first
                SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &amp;Rightchair);
                SDL_RenderCopy(renderer, imageTextureSide, nullptr, &amp;mainchar);
                cout &lt;&lt; (&quot;RIGHTelse&quot;);

            }

            
            SDL_RenderCopy(renderer, imageTextureTChair, nullptr, &amp;Topchair);
            SDL_RenderCopy(renderer, imageTextureTChair3, nullptr, &amp;Topchair3);
            SDL_RenderCopy(renderer, imageTexturetable, nullptr, &amp;Bigtable);
            SDL_RenderCopy(renderer, imageTextureBChair, nullptr, &amp;Bottomchair);
            SDL_RenderCopy(renderer, imageTextureBChair2, nullptr, &amp;Bottomchair2);
            SDL_RenderCopy(renderer, imageTextureRminitable, nullptr, &amp;Minitbl);







            break;

        }

        SDL_RenderPresent(renderer);

enter image description here

答案1

得分: 1

你的代码中有两次渲染主角twice。由于两种情况下条件完全相同,将它们合并成一个单独的情况:

if (mainchar.y > 250) {
    // 主角在椅子上方,先渲染椅子
    SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
    SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
    SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
}
else {
    // 主角在椅子下方,先渲染主角
    SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &mainchar);
    SDL_RenderCopy(renderer, imageTextureChair, nullptr, &Realchair);
    SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &Rightchair);
}
英文:

You're rendering the main char twice in your code. Since the condition is exactly the same in both cases, combine them into a single case:

if (mainchar.y &gt; 250) {
    // Main character is above the chair, render chair first
    SDL_RenderCopy(renderer, imageTextureChair, nullptr, &amp;Realchair);
    SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &amp;Rightchair);
    SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &amp;mainchar);
}
else {
    // Main character is below the chair, render main character first
    SDL_RenderCopy(renderer, imageTextureSideTwo, nullptr, &amp;mainchar);
    SDL_RenderCopy(renderer, imageTextureChair, nullptr, &amp;Realchair);
    SDL_RenderCopy(renderer, imageTextureRChair, nullptr, &amp;Rightchair);
}

huangapple
  • 本文由 发表于 2023年7月6日 14:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定