NightFox

Old Ngine render

Jun 24th, 2025
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.08 KB | None | 0 0
  1. void NGN_Render::TiledBgTransform(NGN_TiledBg* bg) {
  2.  
  3.     // Rotacion y FLIP
  4.     _rotation = 0.0f;
  5.     _flip = SDL_FLIP_NONE;
  6.  
  7.     // Define las areas de origen y destino
  8.     _source = {0, 0, 0, 0};
  9.     _destination = {0, 0, 0, 0};
  10.  
  11.     // Area del render
  12.     Size2I32 render_area;
  13.     render_area.width = ngn->graphics->render_resolution.width;
  14.     render_area.height = ngn->graphics->render_resolution.height;
  15.  
  16.     // Analiza si es necesario redibujar la textura
  17.     int32_t bg_offset_x = (std::floor(bg->position.x) - std::floor(bg->last_position.x));
  18.     int32_t bg_offset_y = (std::floor(bg->position.y) - std::floor(bg->last_position.y));
  19.  
  20.     // Si hay desplazamiento o se ha forzado, redibuja la textura en el backbuffer
  21.     if ((bg_offset_x != 0) || (bg_offset_y != 0) || (ngn->graphics->current_viewport != bg->last_viewport) || bg->tile_mode || ngn->graphics->force_redraw) {
  22.  
  23.         // Calcula el centro del tile
  24.         _center->x = _center->y = ((float)bg->tile_size / 2.0f);
  25.  
  26.         // Parametros del mapa
  27.         int32_t map_x = 0;
  28.         int32_t map_y = 0;
  29.         int32_t map_w = std::floor(bg->map_size.width / bg->tile_size);
  30.         int32_t map_h = std::floor(bg->map_size.height / bg->tile_size);
  31.  
  32.         // Tileset
  33.         uint32_t id, tile, flip;      // Informacion
  34.  
  35.         // Coordenadas de dibujado
  36.         int32_t px, py;
  37.  
  38.         // Parametros de lectura del tile
  39.         _source.w = _source.h = bg->tile_size;
  40.         // Parametros de escritura del tile
  41.         _destination.w = _destination.h = bg->tile_size;
  42.  
  43.         // Informa al renderer que la textura "backbuffer" es su destino
  44.         SDL_SetRenderTarget(ngn->graphics->renderer, bg->backbuffer);
  45.  
  46.         // Borra el contenido de la textura actual
  47.         SDL_SetRenderDrawColor(ngn->graphics->renderer, 0x00, 0x00, 0x00, 0x00);
  48.         SDL_SetTextureBlendMode(bg->backbuffer, SDL_BLENDMODE_BLEND);
  49.         SDL_SetTextureAlphaMod(bg->backbuffer, 0x00);
  50.         SDL_RenderFillRect(ngn->graphics->renderer, nullptr);
  51.  
  52.         // Tamaño del area de dibujado
  53.         int32_t tile_last_x = (std::floor(render_area.width / bg->tile_size) + 2);      // +2 soluciona el encage en resoluciones anamorficas
  54.         int32_t tile_last_y = (std::floor(render_area.height / bg->tile_size) + 2);
  55.  
  56.         // Calcula el offset del dibujado de los tiles
  57.         int32_t tile_offset_x = (std::floor(bg->position.x / bg->tile_size));
  58.         int32_t tile_offset_y = (std::floor(bg->position.y / bg->tile_size));
  59.         int32_t get_tile_x = 0, get_tile_y = 0;
  60.         int32_t draw_offset_x = ((int32_t)std::floor(bg->position.x) % bg->tile_size);
  61.         int32_t draw_offset_y = ((int32_t)std::floor(bg->position.y) % bg->tile_size);
  62.  
  63.         // Recorre la pantalla para dibujar el fondo
  64.         for (map_y = 0; map_y < tile_last_y; map_y ++) {
  65.             // Tile Y
  66.             get_tile_y = (map_y + tile_offset_y);
  67.             // Si estas fuera del rango...
  68.             if ((get_tile_y < 0) || (get_tile_y >= map_h)) continue;
  69.             // Punto Y de dibujado
  70.             py = ((map_y * bg->tile_size) - draw_offset_y);
  71.             for (map_x = 0; map_x < tile_last_x; map_x ++) {
  72.                 // Tile X
  73.                 get_tile_x = (map_x + tile_offset_x);
  74.                 // Si estas fuera del rango...
  75.                 if ((get_tile_x < 0) || (get_tile_x >= map_w)) continue;
  76.                 // Punto X de dibujado
  77.                 px = ((map_x * bg->tile_size) - draw_offset_x);
  78.                 // ID de tile
  79.                 id = (((get_tile_y * map_w) + get_tile_x) << 2);      // * 4 bytes
  80.                 // Numero de Tile
  81.                 tile = bg->bgdata->tmap[id];
  82.                 tile |= (bg->bgdata->tmap[(id + 1)] << 8);
  83.                 tile |= (bg->bgdata->tmap[(id + 2)] << 16);
  84.                 // Si el tile ES TRANSPARENTE (no es el primero del mapa), ignora el dibujado
  85.                 if (tile == 0) continue;
  86.                 // Orientacion del tile
  87.                 flip = bg->bgdata->tmap[(id + 3)];
  88.                 // Define el punto de destino
  89.                 _destination.x = px;
  90.                 _destination.y = py;
  91.                 // Parametros del FLIP del tile
  92.                 switch (flip) {
  93.                     case 1:     // Normal       (BIT 0) [1 << 0]
  94.                         SDL_RenderCopy(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination);
  95.                         break;
  96.                     case 2:     // Flip H       (BIT 1) [1 << 1]
  97.                         _flip = SDL_FLIP_HORIZONTAL;
  98.                         _rotation = 0.0f;
  99.                         SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
  100.                         break;
  101.                     case 4:     // Flip V       (BIT 2) [1 << 2]
  102.                         _flip = SDL_FLIP_VERTICAL;
  103.                         _rotation = 0.0f;
  104.                         SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
  105.                         break;
  106.                     case 8:     // 180º         (BIT 3) [1 << 3]
  107.                         _flip = SDL_FLIP_NONE;
  108.                         _rotation = 180.0f;
  109.                         // Compensa el desplazamiento
  110.                         _destination.x --;
  111.                         _destination.y --;
  112.                         SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
  113.                         break;
  114.                     case 16:     // 90º CW      (BIT 4) [1 << 4]
  115.                         _flip = SDL_FLIP_NONE;
  116.                         _rotation = 270.0f;
  117.                         // Compensa el desplazamiento
  118.                         _destination.y --;
  119.                         SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
  120.                         break;
  121.                     case 32:     // 90º ACW     (BIT 5) [1 << 5]
  122.                         _flip = SDL_FLIP_NONE;
  123.                         _rotation = 90.0f;
  124.                         // Compensa el desplazamiento
  125.                         _destination.x --;
  126.                         SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
  127.                         break;
  128.                 }
  129.             }
  130.         }
  131.  
  132.         // Restaura el render al seleccionado
  133.         ngn->graphics->RenderToSelected();
  134.  
  135.     }
  136.  
  137.     // Calculo del alpha del fondo
  138.     _alpha = bg->alpha;
  139.     if (_alpha < 0) {
  140.         _alpha = 0;
  141.     } else if (_alpha > 0xFF) {
  142.         _alpha = 0xFF;
  143.     }
  144.     SDL_SetTextureBlendMode(bg->backbuffer, bg->blend_mode);
  145.     SDL_SetTextureAlphaMod(bg->backbuffer, (uint8_t)_alpha);
  146.  
  147.     // Color mod
  148.     if (bg->NewTint()) SDL_SetTextureColorMod(bg->backbuffer, bg->tint_color.r, bg->tint_color.g, bg->tint_color.b);
  149.  
  150.     // Define los puntos de entrada y salida de la textura
  151.     _source.x = 0; _source.y = 0; _source.w = render_area.width; _source.h = render_area.height;
  152.     _destination.w = (int32_t)((float)render_area.width * bg->scale.x);
  153.     _destination.h = (int32_t)((float)render_area.height * bg->scale.y);
  154.     _destination.x = (int32_t)(((float)ngn->graphics->render_resolution.width / 2.0f) - ((float)_destination.w / 2.0f));
  155.     _destination.y = (int32_t)(((float)ngn->graphics->render_resolution.height / 2.0f) - ((float)_destination.h / 2.0f));
  156.     _rotation = bg->rotation;
  157.     _center->x = (int32_t)(((float)_destination.w / 2.0f) + bg->center.x);
  158.     _center->y = (int32_t)(((float)_destination.h / 2.0f) + bg->center.y);
  159.     _flip = SDL_FLIP_NONE;
  160.  
  161.     // Envia el backbuffer al renderer
  162.     SDL_RenderCopyEx(ngn->graphics->renderer, bg->backbuffer, &_source, &_destination, _rotation, _center, _flip);
  163.  
  164.     // Guarda la ubicacion del fondo para el siguiente frame
  165.     bg->last_position.x = bg->position.x;
  166.     bg->last_position.y = bg->position.y;
  167.     bg->last_viewport = ngn->graphics->current_viewport;
  168.     bg->tile_mode = false;
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment