sdlpal精灵素材内存截取并保存到硬盘

2022/8/25 5:22:57

本文主要是介绍sdlpal精灵素材内存截取并保存到硬盘,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

github地址

https://github.com/sdlpal/sdlpal

 

简单阅读了一下源码,虽然项目用到了stb_image.h,但是我们还需要stb_image_write.h来写文件,所以还得再搞一下

https://github.com/nothings/stb

 

打开项目文件

sdlpal_20220502\win32\sdlpal.sln

确保先能编译,并正确运行,我以DOS仙剑为例测试的

 

项目中加入stb_image_write.h的include路径

打开 palette.c 文件,头部加入

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

加入全局变量

SDL_Color pe_palette[256];
int pe_palette_ready = 0;

static int bOnce_day = 0;
static int bOnce_night = 0;

static int bRec_day = 0;
static int bRec_night = 0;

static int pe_id = 0;

在下面的 PAL_GetPalette 函数中找到 palette 部分代码,修改如下

for (i = 0; i < 256; i++)
   {
      palette[i].r = buf[(fNight ? 256 * 3 : 0) + i * 3] << 2;
      palette[i].g = buf[(fNight ? 256 * 3 : 0) + i * 3 + 1] << 2;
      palette[i].b = buf[(fNight ? 256 * 3 : 0) + i * 3 + 2] << 2;

      if (iPaletteNum == 0)
      {
          if (pe_id == 1)
          {
              pe_palette[i].r = palette[i].r;
              pe_palette[i].g = palette[i].g;
              pe_palette[i].b = palette[i].b;
          }
          
      }
#if 0
      palette[i].r += (255 - palette[i].r) / 5;
      palette[i].g += (255 - palette[i].g) / 5;
      palette[i].b += (255 - palette[i].b) / 5;
#endif
   }
   
   if (iPaletteNum == 0)
   {
       if (!fNight)
       {
           if (bRec_day)
           {
               if (pe_id == 1)
               {
                   if (!bOnce_day)
                   {
                       bOnce_day = 1;

                       char* peOutFile = "pat_0_day.png";
                       int w = 256;
                       int h = 1;
                       int n = 4;
                       unsigned char* data = malloc(w * h * n);

                       if (data != 0)
                       {
                           for (int i = 0; i < 256; i++)
                           {
                               data[i * 4 + 0] = palette[i].r;
                               data[i * 4 + 1] = palette[i].g;
                               data[i * 4 + 2] = palette[i].b;
                               data[i * 4 + 3] = 255;
                           }
                           stbi_write_png(peOutFile, w, h, n, data, w * n);

                           free(data);
                       }
                   }
               }
           }
           
           if (pe_id == 1)
           {
               pe_palette_ready = 1;
           }

           pe_id++;
       }
       //night palette
       else
       {
           if (bRec_night)
           {
               if (!bOnce_night)
               {
                   bOnce_night = 1;

                   char* peOutFile = "pat_0_night.png";
                   int w = 256;
                   int h = 1;
                   int n = 4;
                   unsigned char* data = malloc(w * h * n);

                   if (data != 0)
                   {
                       for (int i = 0; i < 256; i++)
                       {
                           data[i * 4 + 0] = palette[i].r;
                           data[i * 4 + 1] = palette[i].g;
                           data[i * 4 + 2] = palette[i].b;
                           data[i * 4 + 3] = 255;
                       }
                       stbi_write_png(peOutFile, w, h, n, data, w * n);

                       free(data);
                   }
               }
           }
       }
   }

 



这篇关于sdlpal精灵素材内存截取并保存到硬盘的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程