簡體   English   中英

如何在SDL中創建盒子錯覺?

[英]How to create box illusion in SDL?

我現在想只使用rects來創造一種視覺上的錯覺,我無法用2個rects來實現它,但我想不出任何辦法來繼續生成rects。 現在,我僅有的代碼只生成2個rect,但是我想知道如何在繼續生成rect的地方更改我的代碼,直到用戶按下exit才停止。 下面是我的代碼,將不勝感激任何幫助。

 #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;
}

有了這個經過編輯的代碼,現在有2個方格出現,並且程序正在連續運行,但是它們就像脈沖一樣出現了,兩個矩形現在都在一起形成了,我只希望幫助做出無延遲的更改,並且2個矩形應該連續形成,所以給人一種幻覺。

就是這樣:在rect2完成后,您忘記了重置rect1的屬性。 將塊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;
        }

為什么if(rect1_x<0 || rect1_y<0)包含if(rect1_x<250 || rect1_y<250) rect_x<0表示rect1_x<250

因此,您可以刪除多余的嵌套if塊,並將其放在上一個中。

這不是答案的一部分,但是這是我對程序執行的隨機操作: https ://pastebin.com/zBBn7eBQ在任何SDL_Window中調用它!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM