<commit_before>/*
 * Crystals (working title) 
 *
 * Copyright (c) 2010 <NAME>, <NAME> and <NAME>.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *
 *   * The names of contributors may not be used to endorse or promote
 *     products derived from this software without specific prior
 *     written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AFOREMENTIONED COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @file    src/field/mapview.c
 *  @author  <NAME>
 *  @brief   Map rendering functions.
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <glib-2.0/glib/gstrfuncs.h>
#include <glib-2.0/glib/gslist.h>

#include "mapview.h"
#include "map.h"
#include "object.h"
#include "object-image.h"
#include "../util.h"
#include "../module.h"
#include "../graphics.h"


/* -- CONSTANTS -- */

const uint16_t TILE_W = 32;
const uint16_t TILE_H = 32;


/* -- STATIC DECLARATIONS -- */

/**
 * Handles a dirty map rectangle.
 * 
 * @param rectangle  A pointer to the dirty rectangle to handle.
 * @param mapview    A pointer to the mapview to enqueue changes to.
 */
static void
handle_dirty_rectangle (gpointer rectangle,
                        gpointer mapview);

/** 
 * Renders a given layer on a map.
 *
 * @param mapview    Pointer to the map view to render.
 *
 * @param layer      The layer to render.
 */
static void
render_map_layer (mapview_t *mapview,
                  layer_index_t layer);

/**
 * Renders a given rectangular layer "slice" on a map.
 * 
 * @param rectangle  A pointer to the dirty rectangle to render.
 * @param data       A pointer to the data required to perform the render.
 */
static void
render_rect_layer (gpointer rectangle,
                   gpointer data);

/** 
 * Render any map objects to be placed on top of this layer.
 *
 * This will, if this layer is the first defined with its tag, blit
 * all dirty objects of the same tag on top of this map layer in
 * z-order.
 *
 * @param mapview  Pointer to the map view to render.
 *
 * @param layer    The layer to render.
 */

static void
render_map_objects (mapview_t *mapview, layer_index_t layer);


/** Structure of data needed during a rectangle layer render pass. */
typedef struct render_rect_layer_data
{
  mapview_t *mapview;
  image_t *tileset;
  layer_index_t layer;
} render_rect_layer_data_t;


/* -- DEFINITIONS -- */

mapview_t *
init_mapview (map_t *map)
{
  g_assert (map != NULL);
  g_assert (map->width > 0 && map->height > 0);
  
  {
    mapview_t *mapview = xcalloc (1, sizeof (mapview_t));

    mapview->map = map;

    /* Get the number of object queues to reserve, by finding the
       highest tag number in the map. */
    mapview->num_object_queues = get_max_tag (mapview->map);

    /* There should be at least one tag! */
    g_assert (mapview->num_object_queues != 0);

    mapview->object_queue = xcalloc (mapview->num_object_queues,
                                    sizeof (object_image_t*));

    /* Set all tiles as dirty. */
    mark_dirty_rect (mapview, 0, 0, map->width * TILE_W, map->height * TILE_H);
    render_map (mapview);
    
    return mapview;
  }
}


/* Add an object sprite to the rendering queue. */

void
add_object_image (mapview_t *mapview,
                  layer_value_t tag,
                  object_t *object)
{<commit_msg>Fixed some scope I didn't notice<commit_after>/*
 * Crystals (working title) 
 *
 * Copyright (c) 2010 <NAME>, <NAME> and Alexander
 *                    Preisinger.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *
 *   * The names of contributors may not be used to endorse or promote
 *     products derived from this software without specific prior
 *     written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AFOREMENTIONED COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @file    src/field/mapview.c
 *  @author  <NAME>
 *  @brief   Map rendering functions.
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <glib-2.0/glib/gstrfuncs.h>
#include <glib-2.0/glib/gslist.h>

#include "mapview.h"
#include "map.h"
#include "object.h"
#include "object-image.h"
#include "../util.h"
#include "../module.h"
#include "../graphics.h"


/* -- CONSTANTS -- */

const uint16_t TILE_W = 32;
const uint16_t TILE_H = 32;


/* -- STATIC DECLARATIONS -- */

/**
 * Handles a dirty map rectangle.
 * 
 * @param rectangle  A pointer to the dirty rectangle to handle.
 * @param mapview    A pointer to the mapview to enqueue changes to.
 */
static void
handle_dirty_rectangle (gpointer rectangle,
                        gpointer mapview);

/** 
 * Renders a given layer on a map.
 *
 * @param mapview    Pointer to the map view to render.
 *
 * @param layer      The layer to render.
 */
static void
render_map_layer (mapview_t *mapview,
                  layer_index_t layer);

/**
 * Renders a given rectangular layer "slice" on a map.
 * 
 * @param rectangle  A pointer to the dirty rectangle to render.
 * @param data       A pointer to the data required to perform the render.
 */
static void
render_rect_layer (gpointer rectangle,
                   gpointer data);

/** 
 * Render any map objects to be placed on top of this layer.
 *
 * This will, if this layer is the first defined with its tag, blit
 * all dirty objects of the same tag on top of this map layer in
 * z-order.
 *
 * @param mapview  Pointer to the map view to render.
 *
 * @param layer    The layer to render.
 */

static void
render_map_objects (mapview_t *mapview, layer_index_t layer);


/** Structure of data needed during a rectangle layer render pass. */
typedef struct render_rect_layer_data
{
  mapview_t *mapview;
  image_t *tileset;
  layer_index_t layer;
} render_rect_layer_data_t;


/* -- DEFINITIONS -- */

mapview_t *
init_mapview (map_t *map)
{
  mapview_t *mapview = xcalloc (1, sizeof (mapview_t));
  g_assert (map != NULL);
  g_assert (map->width > 0 && map->height > 0);
  
  mapview->map = map;

  /* Get the number of object queues to reserve, by finding the
     highest tag number in the map. */
  mapview->num_object_queues = get_max_tag (mapview->map);
  
  /* There should be at least one tag! */
  g_assert (mapview->num_object_queues != 0);
  
  mapview->object_queue = xcalloc (mapview->num_object_queues,
                                   sizeof (object_image_t*));
  
  /* Set all tiles as dirty. */
  mark_dirty_rect (mapview, 0, 0, map->width * TILE_W, map->height * TILE_H);
  render_map (mapview);
  
  return mapview;
}


/* Add an object sprite to the rendering queue. */

void
add_object_image (mapview_t *mapview,
                  layer_value_t tag,
                  object_t *object)
{
{'fraction_non_alphanumeric': 0.09093911703181769, 'fraction_numerical': 0.00484421446658593, 'mean_word_length': 3.0481283422459895, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1160138', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2657, 'n_words': 1074}