简体   繁体   English

如何在SDL中创建盒子错觉?

[英]How to create box illusion in SDL?

I want to create a optical illusion for now just using rects and nothing else I am able to pull it off with 2 rects but I can't think of any way to keep generating rects after one another. 我现在想只使用rects来创造一种视觉上的错觉,我无法用2个rects来实现它,但我想不出任何办法来继续生成rects。 Right now the code I have only generates 2 rects but I want to know how I can change my code where it keep generating rects and do not stop until user presses exit. 现在,我仅有的代码只生成2个rect,但是我想知道如何在继续生成rect的地方更改我的代码,直到用户按下exit才停止。 Below is my code any help will be appreciated. 下面是我的代码,将不胜感激任何帮助。

 #include <iostream>
#include <math.h>
#include "SDL.h"

using namespace std;

const int screen_width = 500;
const int screen_height = 500;

int main(int argc, char ** argv)
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("SDLDEMOSCENE", 50, 50, screen_width, screen_height, SDL_WINDOW_SHOWN);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);

    SDL_Rect rectangle, rectangle2;

    double rect1_x = screen_width / 2;
    double rect1_y = screen_height / 2;
    double rect1_width = 5;
    double rect1_height = 5;

    double rect2_x = screen_width / 2;
    double rect2_y = screen_height / 2;
    double rect2_width = 5;
    double rect2_height = 5;



    bool quit = false;

    while (quit == false)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                quit = true;
            }
        }

        rectangle.x = rect1_x;
        rectangle.y = rect1_y;
        rectangle.h = rect1_height;
        rectangle.w = rect1_width;

        rectangle2.x = rect2_x;
        rectangle2.y = rect2_y;
        rectangle2.h = rect2_height;
        rectangle2.w = rect2_width;


        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        SDL_RenderFillRect(renderer, &rectangle);

        rect1_x -= 0.01;
        rect1_y -= 0.01;
        rect1_width += 0.02;
        rect1_height += 0.02;

        if (rect1_x < 200 || rect1_y < 200)
        {
            SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
            SDL_RenderFillRect(renderer, &rectangle2);

            rect2_x -= 0.01;
            rect2_y -= 0.01;
            rect2_width += 0.02;
            rect2_height += 0.02;
        }

        if (rect2_x < 0 || rect2_y < 0)
        {
            rect2_height = 0;
            rect2_width = 0;
            rect2_x = 250;
            rect2_y = 250;
            rect1_height = 5;
            rect1_width = 5;
            rect1_x = 250;
            rect1_y = 250;
        }



        SDL_RenderPresent(renderer);
    }

    SDL_Quit();
    return 0;
}

With this edited code now 2 squares are coming and the program is running continuously but they are coming like pulses both rects are forming together now I just want the help to make the changes where there is no delay and the 2 rects should form continuously so it gives the feel of illusion. 有了这个经过编辑的代码,现在有2个方格出现,并且程序正在连续运行,但是它们就像脉冲一样出现了,两个矩形现在都在一起形成了,我只希望帮助做出无延迟的更改,并且2个矩形应该连续形成,所以给人一种幻觉。

Here's it: You forgot to reset rect1's properties after rect2 finishes it's turn. 就是这样:在rect2完成后,您忘记了重置rect1的属性。 Change the block if(rect2_x<0 || rect2_y<0) to this: 将块if(rect2_x<0 || rect2_y<0)更改为此:

if (rect2_x < 0 || rect2_y < 0)
        {
            rect2_height = 0;
            rect2_width = 0;
            rect2_x = 250;
            rect2_y = 250;
            rect1_height = 5;
            rect1_width = 5;
            rect1_x = 250;
            rect1_y = 250;
        }

Why does if(rect1_x<0 || rect1_y<0) contain if(rect1_x<250 || rect1_y<250) inside it? 为什么if(rect1_x<0 || rect1_y<0)包含if(rect1_x<250 || rect1_y<250) rect_x<0 implies rect1_x<250 rect_x<0表示rect1_x<250

So you can remove that extra nesting if-block and place it in the previous one. 因此,您可以删除多余的嵌套if块,并将其放在上一个中。

Not part of the answer, but heres what random things I did with your program: https://pastebin.com/zBBn7eBQ Call it inside any SDL_Window! 这不是答案的一部分,但是这是我对程序执行的随机操作: https ://pastebin.com/zBBn7eBQ在任何SDL_Window中调用它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM