Sakura__Lu
Sakura__Lu
2016-09-29 00:00

SDL2雨点下落的实现却变成了面条一样的效果...

1

用SDL2实现雨点下落的结果,本人小白,是按照视频一步步来写的,不知道哪里出错了

本来是雨水一点点往下落的效果,为什么变成了面条一样的呢?求解释,求修改
素材图片是BMP格式的,无法上传, balck.bmp 是一个黑色的小点,用来做覆盖背景,circle是一个白色的圆,圆的不用的杂色的RGB是149,149,149.
大神们有空帮我跑一跑找找错误 啊

 #include<SDL.h>
#include<iostream>
#define WINDOW_HEIGHT 600
#define WINDOW_WIDTH 800
using namespace std;

typedef struct {
    int x;
    int y;
    int r;
    int speed;
}Spot;

void moveSpot(Spot* spot){
    spot->y += spot->speed;
    if(spot->y >= WINDOW_HEIGHT){
        spot->y = 0;
    }

}


void drawSpot(Spot *spot, SDL_Renderer *rend ,SDL_Texture *circle){
    SDL_Rect rect;
    rect.x = spot->x;
    rect.y = spot->y;
    rect.w = spot->r;
    rect.h = spot->r;
    SDL_RenderCopy(rend, circle, NULL , &rect);
}


int main( int argc ,char ** argv)
{
    /*
        窗口创建
    */
    SDL_Window *window = SDL_CreateWindow("meteor",
                                        SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
                                        WINDOW_WIDTH,WINDOW_HEIGHT,
                                        SDL_WINDOW_SHOWN
                                            );
    /*渲染器初始化*/
    SDL_Renderer* rend = SDL_CreateRenderer(window, -1 ,SDL_RENDERER_ACCELERATED );

    /*加载图片*/
    SDL_Surface * blackSurface = SDL_LoadBMP("balck.bmp");
    SDL_Surface * circleSurface = SDL_LoadBMP("circle.bmp");

    /*除杂色*/
    SDL_SetColorKey(circleSurface, SDL_TRUE, SDL_MapRGB(circleSurface->format,149,149,149));

    /*设置纹理*/
    SDL_Texture *black = SDL_CreateTextureFromSurface(rend,blackSurface);
    SDL_Texture *circle = SDL_CreateTextureFromSurface(rend,circleSurface);

    /*位置矩形*/
    SDL_Rect rectWindow;
    rectWindow.x = 0;
    rectWindow.y = 0;
    rectWindow.w = WINDOW_WIDTH;
    rectWindow.h = WINDOW_HEIGHT;

    SDL_Rect rectCircle;
    rectCircle.x = 0;
    rectCircle.y = 0;
    rectCircle.w = 5;
    rectCircle.h = 5;

    bool quit = false;

    //初始化50个白点
    Spot spots[50];
    for (int i = 0; i < 50; i++)
    {
        spots[i].x = rand()%WINDOW_WIDTH;
        spots[i].y = rand()%WINDOW_HEIGHT;
        spots[i].r = 4;
        spots[i].speed = rand() % 2 +1;
    }
    SDL_Event event;
    while(!quit){
        while(SDL_PollEvent(&event)){
            if(event.type == SDL_QUIT){
                quit = true;
            }
        }
        SDL_RenderCopy(rend, black, NULL , &rectWindow);
        /*画点*/
        for (int i = 0; i < 50; i++)
        {
            drawSpot(&spots[i],rend,circle);
            moveSpot(&spots[i]);
        }
        SDL_RenderPresent(rend);
        SDL_Delay(5);
    }

    SDL_DestroyWindow(window);
    return 0;
}

我把文件和代码贴出来,大神们帮我看看哇!求帮修改,谢谢各位大神啦!!图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答