Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SDL_Surface *lightSurf;
- SDL_Texture *lightTex;
- SDL_Surface *fpsVision;
- SDL_Texture *fpsTex;
- SDL_Surface *predrawnEllipses[20];
- SDL_Texture *ellipsesTextures[20];
- int width, height;
- int derp = 0;
- Uint32 format;
- void PutPixel32_nolock(SDL_Surface *surface, int x, int y, Uint32 color)
- {
- Uint8 *pixel = (Uint8 *)surface->pixels;
- pixel += (y * surface->pitch) + (x * sizeof(Uint32));
- *((Uint32 *)pixel) = color;
- }
- Uint32 GetPixel32(SDL_Surface *surface, int x, int y)
- {
- Uint8 *pixel = (Uint8 *)surface->pixels;
- pixel += (y * surface->pitch) + (x * sizeof(Uint32));
- return *((Uint32 *)pixel);
- }
- POINT gameToScreen(int targetRow, int targetCol)
- {
- int playerRow = plr[myplr].WorldX;
- int playerCol = plr[myplr].WorldY;
- int sx = 32 * (targetRow - playerRow) + 32 * (playerCol - targetCol) + SCREEN_WIDTH / 2;
- if (ScrollInfo._sdir == 3) {
- sx -= 32;
- } else if (ScrollInfo._sdir == 7) {
- sx += 32;
- }
- int sy = 32 * (targetCol - playerCol) + sx / 2;
- if (ScrollInfo._sdir == 7) {
- sy -= 32;
- }
- POINT ret;
- ret.x = sx;
- ret.y = sy;
- return ret;
- }
- float distance(int x1, int y1, int x2, int y2)
- {
- // Calculating distance
- return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
- }
- float distance2(int x1, int y1, int x2, int y2)
- {
- return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) * 4;
- }
- int mergeChannel(int a, int b, float amount)
- {
- float result = (a * amount) + (b * (1 - amount));
- return (int)result;
- }
- Uint32 blendColors(Uint32 c1, Uint32 c2, float howmuch)
- {
- int r = mergeChannel(c1 & 0x0000FF, c2 & 0x0000FF, howmuch);
- int g = mergeChannel((c1 & 0x00FF00) >> 8, (c2 & 0x00FF00) >> 8, howmuch);
- int b = mergeChannel((c1 & 0xFF0000) >> 16, (c2 & 0xFF0000) >> 16, howmuch);
- return r + (g << 8) + (b << 16);
- /*
- int r1 = (c1 & 0x0000FF);
- int g1 = (c1 & 0x00FF00);
- int b1 = (c1 & 0xFF0000);
- Uint32 out = r1 + (g1 << 8) + (b1 << 16);
- printf("IN: %08X OUT: %08X %d %d %d\n", c1, out, r1,b1,g1);
- return out;
- */
- }
- void drawRadius(int lid, int row, int col, int radius)
- {
- POINT pos = gameToScreen(row, col);
- int sx = pos.x;
- int sy = pos.y;
- int xoff = 0;
- int yoff = 0;
- for (int i = 0; i < nummissiles; i++) {
- MissileStruct *mis = &missile[missileactive[i]];
- if (mis->_mlid == lid) {
- xoff = mis->_mixoff;
- yoff = mis->_miyoff;
- break;
- }
- }
- printf("RADIUS %d", radius);
- sx += xoff;
- sy += yoff;
- /*
- int hey = radius * 64;
- for (int x = sx - hey; x < sx + hey; x++) {
- for (int y = sy - hey/2; y < sy + hey/2; y++) {
- if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT) {
- float howmuch;
- float diffx = sx - x;
- float diffy = sy - y;
- float sa = diffx / 32;
- float a = sa * sa;
- float sb = diffy / 16;
- float b = sb * sb;
- float c = radius * 32;
- float ab = a + b;
- if (ab <= c) {
- //float dist = distance2(sx, sy, x, y);
- //if (dist <= radius * 32) {
- //howmuch = 0;
- //howmuch = 0.5*a/c + 0.5*b/c;
- switch (testvar1) {
- case 0:
- howmuch = 0;
- break;
- case 1:
- howmuch = (ab) / c;
- break;
- case 2:
- howmuch = cbrt(ab / c);
- break;
- }
- Uint32 pix = GetPixel32(lightSurf, x, y);
- Uint32 base_color = 0x000000;
- if (pix != 0) {
- //pix = GetPixel32(GetOutputSurface(), x, y);
- base_color = pix;
- }
- PutPixel32_nolock(lightSurf, x, y, blendColors(base_color, 0xFFFFFF, howmuch));
- //PutPixel32_nolock(lightSurf, x, y, blendColors(0x000000, pix, howmuch));
- //PutPixel32_nolock(lightSurf, x, y, pix);
- //PutPixel32_nolock(GetOutputSurface(), x, y, 0x000000);
- }
- }
- }
- }*/
- SDL_Rect rect;
- int srcx = width / 2;
- int srcy = height / 2;
- int targetx = sx;
- int targety = sy;
- int offsetx = targetx - srcx;
- int offsety = targety - srcy;
- rect.x = offsetx;
- rect.y = offsety;
- rect.w = width;
- rect.h = height;
- //SDL_BlitSurface(predrawnEllipses[radius], NULL, lightSurf, &rect);
- SDL_RenderCopy(renderer, ellipsesTextures[radius], NULL, &rect);
- }
- void turbopotato()
- {
- for (int i = 0; i < numlights; i++) {
- int lid = lightactive[i];
- drawRadius(lid, LightList[lid]._lx, LightList[lid]._ly, LightList[lid]._lradius+1);
- }
- /*
- for (int i = 0; i < 100; i++) {
- if (staticLights[0][i]._lradius == -1) {
- break;
- }
- drawRadius(-1, staticLights[0][i]._lx, staticLights[0][i]._ly, staticLights[0][i]._lradius);
- }
- */
- }
- void predrawEllipse(int radius)
- {
- int sx = width / 2;
- int sy = height / 2;
- int hey = radius * 16;
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT) {
- float howmuch;
- float diffx = sx - x;
- float diffy = sy - y;
- float sa = diffx / 32;
- float a = sa * sa;
- float sb = diffy / 16;
- float b = sb * sb;
- float c = hey;
- float ab = a + b;
- if (ab <= c) {
- //float dist = distance2(sx, sy, x, y);
- //if (dist <= hey*hey) {
- howmuch = cbrt(ab / c);
- //howmuch = cbrt(dist / (hey * hey));
- Uint32 base_color = 0x000000;
- PutPixel32_nolock(predrawnEllipses[radius], x, y, blendColors(base_color, 0xFFFFFF, howmuch));
- }
- }
- }
- }
- }
- void prepareFPS(){
- /////////
- fpsVision = SDL_CreateRGBSurfaceWithFormat(0, 50, 50, SDL_BITSPERPIXEL(format), format);
- SDL_SetSurfaceBlendMode(fpsVision, SDL_BLENDMODE_ADD);
- SDL_FillRect(fpsVision, NULL, SDL_MapRGB(fpsVision->format, 255, 255, 255));
- //SDL_BlitSurface(fpsVision, NULL, lightSurf, &rect);
- fpsTex = SDL_CreateTextureFromSurface(renderer, fpsVision);
- SDL_SetTextureBlendMode(fpsTex, SDL_BLENDMODE_ADD);
- //////////
- }
- void showFPS(){
- SDL_Rect rect;
- rect.x = 0;
- rect.y = 35;
- rect.w = 50;
- rect.h = 50;
- SDL_RenderCopy(renderer, fpsTex, NULL, &rect);
- }
- void RenderPresent()
- {
- SDL_Surface *surface = GetOutputSurface();
- assert(!SDL_MUSTLOCK(surface));
- if (!gbActive) {
- LimitFrameRate();
- return;
- }
- #ifndef USE_SDL1
- if (renderer) {
- if (testvar3 == 0) {
- SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
- } else {
- if (derp == 0) {
- derp = 1;
- /*
- for (int lv = 0; lv < 25; lv++) {
- for (int i = 0; i < 100; i++) {
- staticLights[lv][i]._lradius = -1;
- }
- }*/
- SDL_RenderGetLogicalSize(renderer, &width, &height);
- if (SDL_QueryTexture(texture, &format, nullptr, nullptr, nullptr) < 0)
- ErrSdl();
- lightSurf = SDL_CreateRGBSurfaceWithFormat(0, width, height, SDL_BITSPERPIXEL(format), format);
- for (int i = 1; i <= 15; i++) {
- predrawnEllipses[i] = SDL_CreateRGBSurfaceWithFormat(0, width, height, SDL_BITSPERPIXEL(format), format);
- SDL_SetSurfaceBlendMode(predrawnEllipses[i], SDL_BLENDMODE_ADD);
- SDL_FillRect(predrawnEllipses[i], NULL, SDL_MapRGB(predrawnEllipses[i]->format, 0, 0, 0));
- predrawEllipse(i);
- ellipsesTextures[i] = SDL_CreateTextureFromSurface(renderer, predrawnEllipses[i]);
- SDL_SetTextureBlendMode(ellipsesTextures[i], SDL_BLENDMODE_ADD);
- }
- prepareFPS();
- }
- SDL_FillRect(lightSurf, NULL, SDL_MapRGB(lightSurf->format, 0, 0, 0));
- SDL_BlendMode bm;
- switch (testvar5) {
- case 0:
- bm = SDL_BLENDMODE_NONE;
- break;
- case 1:
- bm = SDL_BLENDMODE_BLEND;
- break;
- case 2:
- bm = SDL_BLENDMODE_ADD;
- break;
- case 3:
- bm = SDL_BLENDMODE_MOD;
- break;
- }
- lightTex = SDL_CreateTextureFromSurface(renderer, lightSurf);
- SDL_SetTextureBlendMode(lightTex, bm);
- SDL_SetTextureBlendMode(texture, bm);
- //turbopotato();
- //showFPS();
- //SDL_BlitSurface(lightSurf, NULL, surface, NULL);
- //SDL_UpdateTexture(texture, NULL, lightSurf->pixels, lightSurf->pitch);
- SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
- }
- // Clear buffer to avoid artifacts in case the window was resized
- if (SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) <= -1) { // TODO only do this if window was resized
- ErrSdl();
- }
- if (SDL_RenderClear(renderer) <= -1) {
- ErrSdl();
- }
- SDL_RenderCopy(renderer, lightTex, NULL, NULL);
- turbopotato();
- showFPS();
- if (SDL_RenderCopy(renderer, texture, NULL, NULL) <= -1) {
- ErrSdl();
- }
- SDL_RenderPresent(renderer);
- } else {
- if (SDL_UpdateWindowSurface(window) <= -1) {
- ErrSdl();
- }
- LimitFrameRate();
- }
- #else
- if (SDL_Flip(surface) <= -1) {
- ErrSdl();
- }
- LimitFrameRate();
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement