Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void NGN_Render::TiledBgTransform(NGN_TiledBg* bg) {
- // Rotacion y FLIP
- _rotation = 0.0f;
- _flip = SDL_FLIP_NONE;
- // Define las areas de origen y destino
- _source = {0, 0, 0, 0};
- _destination = {0, 0, 0, 0};
- // Area del render
- Size2I32 render_area;
- render_area.width = ngn->graphics->render_resolution.width;
- render_area.height = ngn->graphics->render_resolution.height;
- // Analiza si es necesario redibujar la textura
- int32_t bg_offset_x = (std::floor(bg->position.x) - std::floor(bg->last_position.x));
- int32_t bg_offset_y = (std::floor(bg->position.y) - std::floor(bg->last_position.y));
- // Si hay desplazamiento o se ha forzado, redibuja la textura en el backbuffer
- if ((bg_offset_x != 0) || (bg_offset_y != 0) || (ngn->graphics->current_viewport != bg->last_viewport) || bg->tile_mode || ngn->graphics->force_redraw) {
- // Calcula el centro del tile
- _center->x = _center->y = ((float)bg->tile_size / 2.0f);
- // Parametros del mapa
- int32_t map_x = 0;
- int32_t map_y = 0;
- int32_t map_w = std::floor(bg->map_size.width / bg->tile_size);
- int32_t map_h = std::floor(bg->map_size.height / bg->tile_size);
- // Tileset
- uint32_t id, tile, flip; // Informacion
- // Coordenadas de dibujado
- int32_t px, py;
- // Parametros de lectura del tile
- _source.w = _source.h = bg->tile_size;
- // Parametros de escritura del tile
- _destination.w = _destination.h = bg->tile_size;
- // Informa al renderer que la textura "backbuffer" es su destino
- SDL_SetRenderTarget(ngn->graphics->renderer, bg->backbuffer);
- // Borra el contenido de la textura actual
- SDL_SetRenderDrawColor(ngn->graphics->renderer, 0x00, 0x00, 0x00, 0x00);
- SDL_SetTextureBlendMode(bg->backbuffer, SDL_BLENDMODE_BLEND);
- SDL_SetTextureAlphaMod(bg->backbuffer, 0x00);
- SDL_RenderFillRect(ngn->graphics->renderer, nullptr);
- // Tamaño del area de dibujado
- int32_t tile_last_x = (std::floor(render_area.width / bg->tile_size) + 2); // +2 soluciona el encage en resoluciones anamorficas
- int32_t tile_last_y = (std::floor(render_area.height / bg->tile_size) + 2);
- // Calcula el offset del dibujado de los tiles
- int32_t tile_offset_x = (std::floor(bg->position.x / bg->tile_size));
- int32_t tile_offset_y = (std::floor(bg->position.y / bg->tile_size));
- int32_t get_tile_x = 0, get_tile_y = 0;
- int32_t draw_offset_x = ((int32_t)std::floor(bg->position.x) % bg->tile_size);
- int32_t draw_offset_y = ((int32_t)std::floor(bg->position.y) % bg->tile_size);
- // Recorre la pantalla para dibujar el fondo
- for (map_y = 0; map_y < tile_last_y; map_y ++) {
- // Tile Y
- get_tile_y = (map_y + tile_offset_y);
- // Si estas fuera del rango...
- if ((get_tile_y < 0) || (get_tile_y >= map_h)) continue;
- // Punto Y de dibujado
- py = ((map_y * bg->tile_size) - draw_offset_y);
- for (map_x = 0; map_x < tile_last_x; map_x ++) {
- // Tile X
- get_tile_x = (map_x + tile_offset_x);
- // Si estas fuera del rango...
- if ((get_tile_x < 0) || (get_tile_x >= map_w)) continue;
- // Punto X de dibujado
- px = ((map_x * bg->tile_size) - draw_offset_x);
- // ID de tile
- id = (((get_tile_y * map_w) + get_tile_x) << 2); // * 4 bytes
- // Numero de Tile
- tile = bg->bgdata->tmap[id];
- tile |= (bg->bgdata->tmap[(id + 1)] << 8);
- tile |= (bg->bgdata->tmap[(id + 2)] << 16);
- // Si el tile ES TRANSPARENTE (no es el primero del mapa), ignora el dibujado
- if (tile == 0) continue;
- // Orientacion del tile
- flip = bg->bgdata->tmap[(id + 3)];
- // Define el punto de destino
- _destination.x = px;
- _destination.y = py;
- // Parametros del FLIP del tile
- switch (flip) {
- case 1: // Normal (BIT 0) [1 << 0]
- SDL_RenderCopy(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination);
- break;
- case 2: // Flip H (BIT 1) [1 << 1]
- _flip = SDL_FLIP_HORIZONTAL;
- _rotation = 0.0f;
- SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
- break;
- case 4: // Flip V (BIT 2) [1 << 2]
- _flip = SDL_FLIP_VERTICAL;
- _rotation = 0.0f;
- SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
- break;
- case 8: // 180º (BIT 3) [1 << 3]
- _flip = SDL_FLIP_NONE;
- _rotation = 180.0f;
- // Compensa el desplazamiento
- _destination.x --;
- _destination.y --;
- SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
- break;
- case 16: // 90º CW (BIT 4) [1 << 4]
- _flip = SDL_FLIP_NONE;
- _rotation = 270.0f;
- // Compensa el desplazamiento
- _destination.y --;
- SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
- break;
- case 32: // 90º ACW (BIT 5) [1 << 5]
- _flip = SDL_FLIP_NONE;
- _rotation = 90.0f;
- // Compensa el desplazamiento
- _destination.x --;
- SDL_RenderCopyEx(ngn->graphics->renderer, bg->bgdata->tiles[tile], &_source, &_destination, _rotation, _center, _flip);
- break;
- }
- }
- }
- // Restaura el render al seleccionado
- ngn->graphics->RenderToSelected();
- }
- // Calculo del alpha del fondo
- _alpha = bg->alpha;
- if (_alpha < 0) {
- _alpha = 0;
- } else if (_alpha > 0xFF) {
- _alpha = 0xFF;
- }
- SDL_SetTextureBlendMode(bg->backbuffer, bg->blend_mode);
- SDL_SetTextureAlphaMod(bg->backbuffer, (uint8_t)_alpha);
- // Color mod
- if (bg->NewTint()) SDL_SetTextureColorMod(bg->backbuffer, bg->tint_color.r, bg->tint_color.g, bg->tint_color.b);
- // Define los puntos de entrada y salida de la textura
- _source.x = 0; _source.y = 0; _source.w = render_area.width; _source.h = render_area.height;
- _destination.w = (int32_t)((float)render_area.width * bg->scale.x);
- _destination.h = (int32_t)((float)render_area.height * bg->scale.y);
- _destination.x = (int32_t)(((float)ngn->graphics->render_resolution.width / 2.0f) - ((float)_destination.w / 2.0f));
- _destination.y = (int32_t)(((float)ngn->graphics->render_resolution.height / 2.0f) - ((float)_destination.h / 2.0f));
- _rotation = bg->rotation;
- _center->x = (int32_t)(((float)_destination.w / 2.0f) + bg->center.x);
- _center->y = (int32_t)(((float)_destination.h / 2.0f) + bg->center.y);
- _flip = SDL_FLIP_NONE;
- // Envia el backbuffer al renderer
- SDL_RenderCopyEx(ngn->graphics->renderer, bg->backbuffer, &_source, &_destination, _rotation, _center, _flip);
- // Guarda la ubicacion del fondo para el siguiente frame
- bg->last_position.x = bg->position.x;
- bg->last_position.y = bg->position.y;
- bg->last_viewport = ngn->graphics->current_viewport;
- bg->tile_mode = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment