Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.40 KB | None | 0 0
  1.  
  2. SDL_Surface *lightSurf;
  3. SDL_Texture *lightTex;
  4.  
  5. SDL_Surface *fpsVision;
  6. SDL_Texture *fpsTex;
  7.  
  8. SDL_Surface *predrawnEllipses[20];
  9. SDL_Texture *ellipsesTextures[20];
  10.  
  11. int width, height;
  12. int derp = 0;
  13. Uint32 format;
  14.  
  15.  
  16. void PutPixel32_nolock(SDL_Surface *surface, int x, int y, Uint32 color)
  17. {
  18.     Uint8 *pixel = (Uint8 *)surface->pixels;
  19.     pixel += (y * surface->pitch) + (x * sizeof(Uint32));
  20.     *((Uint32 *)pixel) = color;
  21. }
  22.  
  23. Uint32 GetPixel32(SDL_Surface *surface, int x, int y)
  24. {
  25.     Uint8 *pixel = (Uint8 *)surface->pixels;
  26.     pixel += (y * surface->pitch) + (x * sizeof(Uint32));
  27.     return *((Uint32 *)pixel);
  28. }
  29.  
  30. POINT gameToScreen(int targetRow, int targetCol)
  31. {
  32.     int playerRow = plr[myplr].WorldX;
  33.     int playerCol = plr[myplr].WorldY;
  34.     int sx = 32 * (targetRow - playerRow) + 32 * (playerCol - targetCol) + SCREEN_WIDTH / 2;
  35.     if (ScrollInfo._sdir == 3) {
  36.         sx -= 32;
  37.     } else if (ScrollInfo._sdir == 7) {
  38.         sx += 32;
  39.     }
  40.     int sy = 32 * (targetCol - playerCol) + sx / 2;
  41.     if (ScrollInfo._sdir == 7) {
  42.         sy -= 32;
  43.     }
  44.     POINT ret;
  45.     ret.x = sx;
  46.     ret.y = sy;
  47.     return ret;
  48. }
  49.  
  50. float distance(int x1, int y1, int x2, int y2)
  51. {
  52.     // Calculating distance
  53.     return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
  54. }
  55.  
  56. float distance2(int x1, int y1, int x2, int y2)
  57. {
  58.     return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) * 4;
  59. }
  60.  
  61. int mergeChannel(int a, int b, float amount)
  62. {
  63.     float result = (a * amount) + (b * (1 - amount));
  64.     return (int)result;
  65. }
  66.  
  67. Uint32 blendColors(Uint32 c1, Uint32 c2, float howmuch)
  68. {
  69.     int r = mergeChannel(c1 & 0x0000FF, c2 & 0x0000FF, howmuch);
  70.     int g = mergeChannel((c1 & 0x00FF00) >> 8, (c2 & 0x00FF00) >> 8, howmuch);
  71.     int b = mergeChannel((c1 & 0xFF0000) >> 16, (c2 & 0xFF0000) >> 16, howmuch);
  72.     return r + (g << 8) + (b << 16);
  73.  
  74.     /*
  75.     int r1 = (c1 & 0x0000FF);
  76.     int g1 = (c1 & 0x00FF00);
  77.     int b1 = (c1 & 0xFF0000);
  78.     Uint32 out = r1 + (g1 << 8) + (b1 << 16);
  79.     printf("IN: %08X OUT: %08X  %d %d %d\n", c1, out, r1,b1,g1);
  80.     return out;
  81.     */
  82. }
  83.  
  84. void drawRadius(int lid, int row, int col, int radius)
  85. {
  86.     POINT pos = gameToScreen(row, col);
  87.     int sx = pos.x;
  88.     int sy = pos.y;
  89.  
  90.     int xoff = 0;
  91.     int yoff = 0;
  92.  
  93.     for (int i = 0; i < nummissiles; i++) {
  94.         MissileStruct *mis = &missile[missileactive[i]];
  95.         if (mis->_mlid == lid) {
  96.             xoff = mis->_mixoff;
  97.             yoff = mis->_miyoff;
  98.             break;
  99.         }
  100.     }
  101.     printf("RADIUS %d", radius);
  102.     sx += xoff;
  103.     sy += yoff;
  104.     /*
  105.     int hey = radius * 64;
  106.     for (int x = sx - hey; x < sx + hey; x++) {
  107.         for (int y = sy - hey/2; y < sy + hey/2; y++) {
  108.             if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT) {
  109.                 float howmuch;
  110.                 float diffx = sx - x;
  111.                 float diffy = sy - y;
  112.                 float sa = diffx / 32;
  113.                 float a = sa * sa;
  114.                 float sb = diffy / 16;
  115.                 float b = sb * sb;
  116.                 float c = radius * 32;
  117.                 float ab = a + b;
  118.                 if (ab <= c) {
  119.  
  120.                 //float dist = distance2(sx, sy, x, y);
  121.                 //if (dist <= radius * 32) {
  122.  
  123.                     //howmuch = 0;
  124.                     //howmuch = 0.5*a/c + 0.5*b/c;
  125.                     switch (testvar1) {
  126.                     case 0:
  127.                         howmuch = 0;
  128.                         break;
  129.                     case 1:
  130.                         howmuch = (ab) / c;
  131.                         break;
  132.                     case 2:
  133.                         howmuch = cbrt(ab / c);
  134.                         break;
  135.                     }
  136.                    
  137.                     Uint32 pix = GetPixel32(lightSurf, x, y);
  138.                     Uint32 base_color = 0x000000;
  139.                     if (pix != 0) {
  140.                         //pix = GetPixel32(GetOutputSurface(), x, y);
  141.                         base_color = pix;
  142.                     }
  143.                     PutPixel32_nolock(lightSurf, x, y, blendColors(base_color, 0xFFFFFF, howmuch));
  144.                     //PutPixel32_nolock(lightSurf, x, y, blendColors(0x000000, pix, howmuch));
  145.                     //PutPixel32_nolock(lightSurf, x, y, pix);
  146.                     //PutPixel32_nolock(GetOutputSurface(), x, y, 0x000000);
  147.                 }
  148.             }
  149.         }
  150.     }*/
  151.     SDL_Rect rect;
  152.  
  153.     int srcx = width / 2;
  154.     int srcy = height / 2;
  155.     int targetx = sx;
  156.     int targety = sy;
  157.     int offsetx = targetx - srcx;
  158.     int offsety = targety - srcy;
  159.  
  160.     rect.x = offsetx;
  161.     rect.y = offsety;
  162.     rect.w = width;
  163.     rect.h = height;
  164.     //SDL_BlitSurface(predrawnEllipses[radius], NULL, lightSurf, &rect);
  165.     SDL_RenderCopy(renderer, ellipsesTextures[radius], NULL, &rect);
  166. }
  167.  
  168. void turbopotato()
  169. {
  170.     for (int i = 0; i < numlights; i++) {
  171.         int lid = lightactive[i];
  172.         drawRadius(lid, LightList[lid]._lx, LightList[lid]._ly, LightList[lid]._lradius+1);
  173.     }
  174.  
  175.     /*
  176.     for (int i = 0; i < 100; i++) {
  177.         if (staticLights[0][i]._lradius == -1) {
  178.             break;
  179.         }
  180.         drawRadius(-1, staticLights[0][i]._lx, staticLights[0][i]._ly, staticLights[0][i]._lradius);
  181.     }
  182.     */
  183. }
  184.  
  185. void predrawEllipse(int radius)
  186. {
  187.  
  188.     int sx = width / 2;
  189.     int sy = height / 2;
  190.     int hey = radius * 16;
  191.     for (int x = 0; x < width; x++) {
  192.         for (int y = 0; y < height; y++) {
  193.             if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT) {
  194.                 float howmuch;
  195.                 float diffx = sx - x;
  196.                 float diffy = sy - y;
  197.                 float sa = diffx / 32;
  198.                 float a = sa * sa;
  199.                 float sb = diffy / 16;
  200.                 float b = sb * sb;
  201.                 float c = hey;
  202.                 float ab = a + b;
  203.                 if (ab <= c) {
  204.                 //float dist = distance2(sx, sy, x, y);
  205.                 //if (dist <= hey*hey) {
  206.                     howmuch = cbrt(ab / c);
  207.                     //howmuch = cbrt(dist / (hey * hey));
  208.                     Uint32 base_color = 0x000000;
  209.                     PutPixel32_nolock(predrawnEllipses[radius], x, y, blendColors(base_color, 0xFFFFFF, howmuch));
  210.                 }
  211.             }
  212.         }
  213.     }
  214.  
  215.  
  216. }
  217.  
  218. void prepareFPS(){
  219.     /////////
  220.     fpsVision = SDL_CreateRGBSurfaceWithFormat(0, 50, 50, SDL_BITSPERPIXEL(format), format);
  221.     SDL_SetSurfaceBlendMode(fpsVision, SDL_BLENDMODE_ADD);
  222.     SDL_FillRect(fpsVision, NULL, SDL_MapRGB(fpsVision->format, 255, 255, 255));
  223.     //SDL_BlitSurface(fpsVision, NULL, lightSurf, &rect);
  224.     fpsTex = SDL_CreateTextureFromSurface(renderer, fpsVision);
  225.     SDL_SetTextureBlendMode(fpsTex, SDL_BLENDMODE_ADD);
  226.     //////////
  227. }
  228.  
  229. void showFPS(){
  230.     SDL_Rect rect;
  231.     rect.x = 0;
  232.     rect.y = 35;
  233.     rect.w = 50;
  234.     rect.h = 50;
  235.     SDL_RenderCopy(renderer, fpsTex, NULL, &rect);
  236. }
  237.  
  238. void RenderPresent()
  239. {
  240.     SDL_Surface *surface = GetOutputSurface();
  241.     assert(!SDL_MUSTLOCK(surface));
  242.  
  243.     if (!gbActive) {
  244.         LimitFrameRate();
  245.         return;
  246.     }
  247.  
  248. #ifndef USE_SDL1
  249.     if (renderer) {
  250.         if (testvar3 == 0) {
  251.             SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  252.         } else {
  253.             if (derp == 0) {
  254.                 derp = 1;
  255.                 /*
  256.                 for (int lv = 0; lv < 25; lv++) {
  257.                     for (int i = 0; i < 100; i++) {
  258.                         staticLights[lv][i]._lradius = -1;
  259.                     }
  260.                 }*/
  261.                 SDL_RenderGetLogicalSize(renderer, &width, &height);
  262.                 if (SDL_QueryTexture(texture, &format, nullptr, nullptr, nullptr) < 0)
  263.                     ErrSdl();
  264.                 lightSurf = SDL_CreateRGBSurfaceWithFormat(0, width, height, SDL_BITSPERPIXEL(format), format);
  265.                 for (int i = 1; i <= 15; i++) {
  266.                     predrawnEllipses[i] = SDL_CreateRGBSurfaceWithFormat(0, width, height, SDL_BITSPERPIXEL(format), format);
  267.                     SDL_SetSurfaceBlendMode(predrawnEllipses[i], SDL_BLENDMODE_ADD);
  268.                     SDL_FillRect(predrawnEllipses[i], NULL, SDL_MapRGB(predrawnEllipses[i]->format, 0, 0, 0));
  269.                     predrawEllipse(i);
  270.                     ellipsesTextures[i] = SDL_CreateTextureFromSurface(renderer, predrawnEllipses[i]);
  271.                     SDL_SetTextureBlendMode(ellipsesTextures[i], SDL_BLENDMODE_ADD);
  272.                 }
  273.                 prepareFPS();
  274.             }
  275.             SDL_FillRect(lightSurf, NULL, SDL_MapRGB(lightSurf->format, 0, 0, 0));
  276.             SDL_BlendMode bm;
  277.             switch (testvar5) {
  278.             case 0:
  279.                 bm = SDL_BLENDMODE_NONE;
  280.                 break;
  281.             case 1:
  282.                 bm = SDL_BLENDMODE_BLEND;
  283.                 break;
  284.             case 2:
  285.                 bm = SDL_BLENDMODE_ADD;
  286.                 break;
  287.             case 3:
  288.                 bm = SDL_BLENDMODE_MOD;
  289.                 break;
  290.             }
  291.             lightTex = SDL_CreateTextureFromSurface(renderer, lightSurf);
  292.             SDL_SetTextureBlendMode(lightTex, bm);
  293.             SDL_SetTextureBlendMode(texture, bm);
  294.             //turbopotato();
  295.             //showFPS();
  296.  
  297.  
  298.             //SDL_BlitSurface(lightSurf, NULL, surface, NULL);
  299.             //SDL_UpdateTexture(texture, NULL, lightSurf->pixels, lightSurf->pitch);
  300.             SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  301.         }
  302.  
  303.         // Clear buffer to avoid artifacts in case the window was resized
  304.         if (SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) <= -1) { // TODO only do this if window was resized
  305.             ErrSdl();
  306.         }
  307.  
  308.         if (SDL_RenderClear(renderer) <= -1) {
  309.             ErrSdl();
  310.         }
  311.         SDL_RenderCopy(renderer, lightTex, NULL, NULL);
  312.         turbopotato();
  313.         showFPS();
  314.         if (SDL_RenderCopy(renderer, texture, NULL, NULL) <= -1) {
  315.             ErrSdl();
  316.         }
  317.         SDL_RenderPresent(renderer);
  318.     } else {
  319.         if (SDL_UpdateWindowSurface(window) <= -1) {
  320.             ErrSdl();
  321.         }
  322.         LimitFrameRate();
  323.     }
  324. #else
  325.     if (SDL_Flip(surface) <= -1) {
  326.         ErrSdl();
  327.     }
  328.     LimitFrameRate();
  329. #endif
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement