c++ - 使用 SDL2/C++ 在屏幕上留下痕迹

标签 c++ processing sdl-2

我正在使用 SDL2 和 C++ 使用 http://www.openprocessing.org/sketch/15109 制作 Ant 模拟程序作为我的引用。链接是处理草图。

除了 Ant 在屏幕上留下的痕迹会随着时间消失的部分外,我已经想出了如何用 C++/SDL2 完成所有事情。现在轨迹是算法本身不可或缺的一部分,但同时将它们显示在屏幕上会增加程序的美感:D。我设法让算法部分工作。现在我只需要查看路径。

相关代码(在处理中)是:

void draw() {

  loadPixels();
  for (int i=0; i<pherHome.length; i++) {
    color pixelColor;
    if (food.getValue(i)) {
      // Draw food
      pixelColor = FOOD_COLOR;
    } 
    else {
      // Draw pheromones

      float pixelAlpha = pherHome.getPercentage(i);
      int pixel_r = int(HOME_R * pixelAlpha + DIRT_R * (1-pixelAlpha));
      int pixel_g = int(HOME_G * pixelAlpha + DIRT_G * (1-pixelAlpha));
      int pixel_b = int(HOME_B * pixelAlpha + DIRT_B * (1-pixelAlpha));

      pixelAlpha = pherFood.getPercentage(i);
      pixel_r = int(FOOD_R * pixelAlpha + pixel_r * (1-pixelAlpha));
      pixel_g = int(FOOD_G * pixelAlpha + pixel_g * (1-pixelAlpha));
      pixel_b = int(FOOD_B * pixelAlpha + pixel_b * (1-pixelAlpha));

      // Using bitwise color math instead of color() on the following line
      // upped the framerate from 43 to 58 on my computer
      //pixelColor = color(pixel_r, pixel_g, pixel_b);
      pixelColor = pixel_r << 16 | pixel_g << 8 | pixel_b;
    }
    // Set
    pixels[i] = pixelColor;
  }
  updatePixels();

  // Draw ants and do other stuff}

请建议一些我可以达到相同效果的方法。

我的 SDL2 代码:

/* draw loop**********************************************************************************************************************************/

bool quit = false;

while (!quit)

{   
    SDL_Event e;

    while ( SDL_PollEvent(&e) != 0)
    {
        if (e.type == SDL_QUIT)
        {
            quit = true;
        }
    }
    int mousex,mousey;
    SDL_GetMouseState(&mousex,&mousey);
    if ( e.type == SDL_MOUSEBUTTONDOWN )
    {
        food.addFood(mousex,mousey);
    }

    SDL_BlitSurface (background, NULL, gsurface, NULL);
    // displaying food
    for(int i =0 ; i < width; i ++)
    {
        for(int j = 0; j < height ; j++)
        {
            if (food.getValue(i,j))
            {
                SDL_Rect pos;
                pos.x = i;
                pos.y = j;

                SDL_BlitSurface (food_source_image, NULL, gsurface, &pos);
            }
        }
    }



    for (int i = 0; i < col.ants.size(); i++) 
    {
      col.ants[i].step();
      int thisXi = col.ants[i].intX;// position in int
      int thisYi = col.ants[i].intY;

      float thisXf = col.ants[i].x;// position in float
      float thisYf = col.ants[i].y;

      if (col.ants[i].hasFood) 
      {
        if (thisXi>col.x-10 && thisXi<col.x+10 && thisYi>col.y-10 && thisYi<col.y+10) 
        {
          // Close enough to home
          cout << "dropped" << endl;
          col.ants[i].hasFood = false;
          col.ants[i].homePher = 100;
        }
      }
      else if(food.getValue(thisXi, thisYi)) 
      {
        cout << "found " << endl;
        col.ants[i].hasFood = true;
        col.ants[i].foodPher = 100;
        food.bite(thisXi, thisYi);
      }

      SDL_Rect pos; pos.x = thisXi; pos.y = thisYi;
      SDL_BlitSurface (human_image, NULL, gsurface, &pos);

    }

    // Evaporate
    pherHome.step();
    pherFood.step();

    SDL_UpdateWindowSurface(gwindow);
    SDL_Delay(1);
}

最佳答案

我不太确定,但就纹理而言,让我举个例子。

您希望屏幕上的图像随着时间变亮并消失,所以-

SDL_Texture* image = IMG_LoadTexture(putRendererHere, FilePath.c_str());

FilePath.c_str() 应替换为您的文件地址 示例:“Resources/Intro/15.png”

SDL_Rect rect;

rect.x = x; //x coordinate

rect.y = y; //y coordinate

rect.w = w; //width

rect.h = h; //height

rect 负责处理图像的位置和大小。

下一部分将负责您的“淡出”效果。

int elapsedTime = xx; //xx should be your in-game time. You can use SDL_GetTicks()

int alpha1 = 250; //In alpha terms, 250 is Opaque while 0 is invisible

int alpha2 = 250;

SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);//This sets the texture in blendmode

if (alpha1 > 0) { //This checks if the image is not yet invisible

  alpha2 -= xf * elapsedTime; // change x into any float

alpha1 = alpha2; //so alpha1 starts to slowly decrease. (Fading Out)

}

if (alpha1 <= 0) { //This code will repeat in the while loop above, once it hits 0 or below 0 then all looping stops and this loop sets the image to a state where it can't be seen.

  alpha1 = 0;

  alpha2 = 0;

SDL_SetTextureAlphaMod(纹理, 透明度);

SDL_RenderCopy(渲染器, 纹理, NULL, &rect);

这些是基础知识,但由于您正在制作“轨迹”,所以我假设您将制作数百万个这样的东西,您应该创建一个函数来负责创建图像、绘制和更改 alpha纹理。

在那之后,你应该能够只用一行来复制这些。

在最后一个 if 语句中,您可以调整内容以使程序忘记图像,从而释放生成图像所占用的所有内存。

快乐编码:)

//我无法打开您的链接,所以我不确定这是否正是您所需要的,但作为提示-

  • 创建一个 int 来处理游戏中的一般时间

  • 创建一组其他 int 来处理每个足迹的时间,完成后,它可以处理另一个足迹。

  • 用上面的 int(s) 做一些数学运算,你应该得到类似足迹的结果。

关于c++ - 使用 SDL2/C++ 在屏幕上留下痕迹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660066/

相关文章:

c++ - 无法在 SDL 2 中创建流畅的动画

python - PySDL2 和 SDL_FreeSurface

C++11:使用右值引用实现装饰器的最佳方式

c++ - 在 C 窗口中将参数传递给线程的最佳方法是什么?

c++ - 为什么 istream/ostream 慢

java - 将 PApplet 框架保存为磁盘上的图像不起作用

php - opencart cookies 问题 - 如何解决?

opencv - 使用带有 OpenCV 和处理的卡尔曼滤波器进行人脸跟踪

java - 处理绘制一个矩形并保留一段时间

c函数重复执行而不被调用,导致段错误