在SDL2 C中使用for循环显示矩形 [英] Displaying the rectangle using a for loop in SDL2 C

查看:96
本文介绍了在SDL2 C中使用for循环显示矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用for循环沿x轴创建4个矩形.当我运行程序时,矩形的图像会出现几毫秒,然后消失.您能告诉我我要做什么才能使图像永久显示在屏幕上.

I'm currently using a for loop to create 4 rectangles along the x-axis. When I run the program the image of the rectangle appears for a few millisecond before it disappears. Can you show me what I have to do in order for the image to display on the screen permanently.

当我创建单个矩形时,它显示的图像不会消失.

When I create individual rectangles it display's the image without vanishing.

一个例子将是有帮助的.谢谢

An example will be helpful. Thanks

int main(){
int barrack1_xposition = 167,i=1;
int cnt_barrack=0;
SDL_Window *o = NULL;
SDL_Renderer *r = NULL;
SDL_Rect bar1[4];
SDL_Event e;


SDL_Init(SDL_INIT_VIDEO);

o = SDL_CreateWindow("SPACE INVADERS",
                        SDL_WINDOWPOS_UNDEFINED,
                        SDL_WINDOWPOS_UNDEFINED,
                        1024,
                        800,
                        SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

r = SDL_CreateRenderer(o, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

while(i)
{
    while(SDL_PollEvent(&e) !=0)
    {
        if(e.type == SDL_QUIT)
            i=0;

   }

    for(cnt_barrack = 0; cnt_barrack < 4; cnt_barrack++)
    {
        bar1[cnt_barrack].x=barrack1_xposition;
        bar1[cnt_barrack].y=250;
        bar1[cnt_barrack].h=50;
        bar1[cnt_barrack].w=50;

        barrack1_xposition += 200;
    }

    SDL_SetRenderDrawColor(r,255,255,255,255);
    SDL_RenderFillRect(r,&bar1[0]);
    SDL_RenderFillRect(r,&bar1[1]);
    SDL_RenderFillRect(r,&bar1[2]);
    SDL_RenderFillRect(r,&bar1[3]);
    SDL_RenderPresent(r);   
}
SDL_DestroyWindow(o);
SDL_DestroyRenderer(r);;
SDL_Quit();
}

推荐答案

您的问题是由于您没有在每一帧上重置barrack1_xposition所致,所以它一直在越来越高.最初我没有注意到这一点,因为没有RenderClear,所以看起来不错,但实际上不是.

Your problem caused by the fact you didn't reset barrack1_xposition on each frame, so it keeps going higher and higher. I initially didn't notice that because there was no RenderClear so it seemed to be fine but actually wasn't.

#include "SDL.h"

int main(){
    int barrack1_xposition,i=1;
    int cnt_barrack=0;
    SDL_Window *o = NULL;
    SDL_Renderer *r = NULL;
    SDL_Rect bar1[4];
    SDL_Event e;


    SDL_Init(SDL_INIT_VIDEO);

    o = SDL_CreateWindow("SPACE INVADERS",
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            1024,
            800,
            SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

    r = SDL_CreateRenderer(o, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    while(i)
    {
        while(SDL_PollEvent(&e) !=0)
        {
            if(e.type == SDL_QUIT)
                i=0;

        }

        barrack1_xposition=167;

        for(cnt_barrack = 0; cnt_barrack < 4; cnt_barrack++)
        {
            bar1[cnt_barrack].x=barrack1_xposition;
            bar1[cnt_barrack].y=250;
            bar1[cnt_barrack].h=50;
            bar1[cnt_barrack].w=50;

            barrack1_xposition += 200;
        }

        SDL_SetRenderDrawColor(r,0,0,0,0);
        SDL_RenderClear(r);

        SDL_SetRenderDrawColor(r,255,255,255,255);
        SDL_RenderFillRect(r,&bar1[0]);
        SDL_RenderFillRect(r,&bar1[1]);
        SDL_RenderFillRect(r,&bar1[2]);
        SDL_RenderFillRect(r,&bar1[3]);
        SDL_RenderPresent(r);   
    }
    SDL_DestroyWindow(o);
    SDL_DestroyRenderer(r);;
    SDL_Quit();

    return 0;
}

这篇关于在SDL2 C中使用for循环显示矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆