src/heightmap.cpp
author belugas
Tue, 04 Mar 2008 17:07:33 +0000
changeset 9168 a35d94d8501c
parent 8766 c86cfa3a7580
child 10429 1b99254f9607
permissions -rw-r--r--
(svn r12337) -Change: update some documentation.
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     1
/* $Id$ */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     2
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5869
diff changeset
     3
/** @file heightmap.cpp */
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5869
diff changeset
     4
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     5
#include "stdafx.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     6
#include "openttd.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     7
#include "variables.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     8
#include "heightmap.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
     9
#include "clear_map.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    10
#include "void_map.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    11
#include "debug.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    12
#include "gui.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    13
#include "saveload.h"
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    14
#include "bmp.h"
8619
c2434269c3eb (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8617
diff changeset
    15
#include "gfx_func.h"
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
    16
#include "core/alloc_func.hpp"
8647
0709611186e0 (svn r11713) -Codechange: Move some declarations and definitions in a more logical disposition
belugas
parents: 8627
diff changeset
    17
#include "fios.h"
8766
c86cfa3a7580 (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8760
diff changeset
    18
#include "settings_type.h"
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    19
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8647
diff changeset
    20
#include "table/strings.h"
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8647
diff changeset
    21
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    22
/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    23
 * Convert RGB colors to Grayscale using 29.9% Red, 58.7% Green, 11.4% Blue
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    24
 *  (average luminosity formula) -- Dalestan
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    25
 * This in fact is the NTSC Color Space -- TrueLight
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    26
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    27
static inline byte RGBToGrayscale(byte red, byte green, byte blue)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    28
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    29
	/* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    30
	 *  divide by it to normalize the value to a byte again. */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    31
	return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    32
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    33
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    34
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    35
#ifdef WITH_PNG
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    36
6548
ca6b0556e9ae (svn r9019) -Fix: use <> for system-headers
truelight
parents: 6505
diff changeset
    37
#include <png.h>
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    38
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    39
/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    40
 * The PNG Heightmap loader.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    41
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    42
static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop info_ptr)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    43
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    44
	uint x, y;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    45
	byte gray_palette[256];
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    46
	png_bytep *row_pointers = NULL;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    47
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    48
	/* Get palette and convert it to grayscale */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    49
	if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    50
		int i;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    51
		int palette_size;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    52
		png_color *palette;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    53
		bool all_gray = true;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    54
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    55
		png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    56
		for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    57
			all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    58
			gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    59
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    60
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    61
		/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    62
		 * For a non-gray palette of size 16 we assume that
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    63
		 * the order of the palette determines the height;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    64
		 * the first entry is the sea (level 0), the second one
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    65
		 * level 1, etc.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    66
		 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    67
		if (palette_size == 16 && !all_gray) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    68
			for (i = 0; i < palette_size; i++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    69
				gray_palette[i] = 256 * i / palette_size;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    70
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    71
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    72
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    73
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    74
	row_pointers = png_get_rows(png_ptr, info_ptr);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    75
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    76
	/* Read the raw image data and convert in 8-bit grayscale */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    77
	for (x = 0; x < info_ptr->width; x++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    78
		for (y = 0; y < info_ptr->height; y++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    79
			byte *pixel = &map[y * info_ptr->width + x];
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    80
			uint x_offset = x * info_ptr->channels;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    81
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    82
			if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    83
				*pixel = gray_palette[row_pointers[y][x_offset]];
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    84
			} else if (info_ptr->channels == 3) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    85
				*pixel = RGBToGrayscale(row_pointers[y][x_offset + 0],
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    86
						row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    87
			} else {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    88
				*pixel = row_pointers[y][x_offset];
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    89
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    90
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    91
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    92
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    93
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    94
/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    95
 * Reads the heightmap and/or size of the heightmap from a PNG file.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    96
 * If map == NULL only the size of the PNG is read, otherwise a map
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    97
 * with grayscale pixels is allocated and assigned to *map.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    98
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
    99
static bool ReadHeightmapPNG(char *filename, uint *x, uint *y, byte **map)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   100
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   101
	FILE *fp;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   102
	png_structp png_ptr = NULL;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   103
	png_infop info_ptr  = NULL;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   104
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   105
	fp = fopen(filename, "rb");
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   106
	if (fp == NULL) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   107
		ShowErrorMessage(STR_PNGMAP_ERR_FILE_NOT_FOUND, STR_PNGMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   108
		return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   109
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   110
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   111
	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   112
	if (png_ptr == NULL) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   113
		ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_PNGMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   114
		fclose(fp);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   115
		return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   116
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   117
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   118
	info_ptr = png_create_info_struct(png_ptr);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   119
	if (info_ptr == NULL || setjmp(png_jmpbuf(png_ptr))) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   120
		ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_PNGMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   121
		fclose(fp);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   122
		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   123
		return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   124
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   125
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   126
	png_init_io(png_ptr, fp);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   127
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   128
	/* Allocate memory and read image, without alpha or 16-bit samples
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4300
diff changeset
   129
	 * (result is either 8-bit indexed/grayscale or 24-bit RGB) */
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   130
	png_set_packing(png_ptr);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   131
	png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, NULL);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   132
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   133
	/* Maps of wrong color-depth are not used.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   134
	 * (this should have been taken care of by stripping alpha and 16-bit samples on load) */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   135
	if ((info_ptr->channels != 1) && (info_ptr->channels != 3) && (info_ptr->bit_depth != 8)) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   136
		ShowErrorMessage(STR_PNGMAP_ERR_IMAGE_TYPE, STR_PNGMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   137
		fclose(fp);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   138
		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   139
		return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   140
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   141
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   142
	if (map != NULL) {
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5844
diff changeset
   143
		*map = MallocT<byte>(info_ptr->width * info_ptr->height);
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   144
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   145
		if (*map == NULL) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   146
			ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_PNGMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   147
			fclose(fp);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   148
			png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   149
			return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   150
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   151
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   152
		ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   153
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   154
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   155
	*x = info_ptr->width;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   156
	*y = info_ptr->height;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   157
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   158
	fclose(fp);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   159
	png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   160
	return true;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   161
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   162
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   163
#endif /* WITH_PNG */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   164
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   165
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   166
/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   167
 * The BMP Heightmap loader.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   168
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   169
static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   170
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   171
	uint x, y;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   172
	byte gray_palette[256];
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   173
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   174
	if (data->palette != NULL) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   175
		uint i;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   176
		bool all_gray = true;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   177
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   178
		if (info->palette_size != 2) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   179
			for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   180
				all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   181
				gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   182
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   183
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   184
			/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   185
			 * For a non-gray palette of size 16 we assume that
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   186
			 * the order of the palette determines the height;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   187
			 * the first entry is the sea (level 0), the second one
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   188
			 * level 1, etc.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   189
			 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   190
			if (info->palette_size == 16 && !all_gray) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   191
				for (i = 0; i < info->palette_size; i++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   192
					gray_palette[i] = 256 * i / info->palette_size;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   193
				}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   194
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   195
		} else {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   196
			/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   197
			 * For a palette of size 2 we assume that the order of the palette determines the height;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   198
			 * the first entry is the sea (level 0), the second one is the land (level 1)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   199
			 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   200
			gray_palette[0] = 0;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   201
			gray_palette[1] = 16;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   202
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   203
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   204
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   205
	/* Read the raw image data and convert in 8-bit grayscale */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   206
	for (y = 0; y < info->height; y++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   207
		byte *pixel = &map[y * info->width];
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   208
		byte *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)];
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   209
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   210
		for (x = 0; x < info->width; x++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   211
			if (info->bpp != 24) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   212
				*pixel++ = gray_palette[*bitmap++];
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   213
			} else {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   214
				*pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   215
				bitmap += 3;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   216
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   217
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   218
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   219
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   220
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   221
/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   222
 * Reads the heightmap and/or size of the heightmap from a BMP file.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   223
 * If map == NULL only the size of the BMP is read, otherwise a map
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   224
 * with grayscale pixels is allocated and assigned to *map.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   225
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   226
static bool ReadHeightmapBMP(char *filename, uint *x, uint *y, byte **map)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   227
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   228
	FILE *f;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   229
	BmpInfo info;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   230
	BmpData data;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   231
	BmpBuffer buffer;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   232
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5869
diff changeset
   233
	/* Init BmpData */
5864
3f21c4757b8d (svn r8070) -Fix r5815: missing initialisation could cause crash when loading 24bpp BMP heightmap
glx
parents: 5860
diff changeset
   234
	memset(&data, 0, sizeof(data));
3f21c4757b8d (svn r8070) -Fix r5815: missing initialisation could cause crash when loading 24bpp BMP heightmap
glx
parents: 5860
diff changeset
   235
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   236
	f = fopen(filename, "rb");
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   237
	if (f == NULL) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   238
		ShowErrorMessage(STR_PNGMAP_ERR_FILE_NOT_FOUND, STR_BMPMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   239
		return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   240
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   241
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   242
	BmpInitializeBuffer(&buffer, f);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   243
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   244
	if (!BmpReadHeader(&buffer, &info, &data)) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   245
		ShowErrorMessage(STR_BMPMAP_ERR_IMAGE_TYPE, STR_BMPMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   246
		fclose(f);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   247
		BmpDestroyData(&data);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   248
		return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   249
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   250
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   251
	if (map != NULL) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   252
		if (!BmpReadBitmap(&buffer, &info, &data)) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   253
			ShowErrorMessage(STR_BMPMAP_ERR_IMAGE_TYPE, STR_BMPMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   254
			fclose(f);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   255
			BmpDestroyData(&data);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   256
			return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   257
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   258
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5844
diff changeset
   259
		*map = MallocT<byte>(info.width * info.height);
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   260
		if (*map == NULL) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   261
			ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_BMPMAP_ERROR, 0, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   262
			fclose(f);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   263
			BmpDestroyData(&data);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   264
			return false;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   265
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   266
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   267
		ReadHeightmapBMPImageData(*map, &info, &data);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   268
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   269
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   270
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   271
	BmpDestroyData(&data);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   272
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   273
	*x = info.width;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   274
	*y = info.height;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   275
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   276
	fclose(f);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   277
	return true;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   278
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   279
7036
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6573
diff changeset
   280
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6573
diff changeset
   281
 * Converts a given grayscale map to something that fits in OTTD map system
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6573
diff changeset
   282
 * and create a map of that data.
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6573
diff changeset
   283
 * @param img_width  the with of the image in pixels/tiles
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6573
diff changeset
   284
 * @param img_height the height of the image in pixels/tiles
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6573
diff changeset
   285
 * @param map        the input map
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6573
diff changeset
   286
 */
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   287
static void GrayscaleToMapHeights(uint img_width, uint img_height, byte *map)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   288
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   289
	/* Defines the detail of the aspect ratio (to avoid doubles) */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   290
	const uint num_div = 16384;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   291
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   292
	uint width, height;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   293
	uint row, col;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   294
	uint row_pad = 0, col_pad = 0;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   295
	uint img_scale;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   296
	uint img_row, img_col;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   297
	TileIndex tile;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   298
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   299
	/* Get map size and calculate scale and padding values */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   300
	switch (_patches.heightmap_rotation) {
5869
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   301
		default: NOT_REACHED();
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   302
		case HM_COUNTER_CLOCKWISE:
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   303
			width   = MapSizeX();
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   304
			height  = MapSizeY();
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   305
			break;
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   306
		case HM_CLOCKWISE:
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   307
			width   = MapSizeY();
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   308
			height  = MapSizeX();
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   309
			break;
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   310
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   311
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   312
	if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   313
		/* Image is wider than map - center vertically */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   314
		img_scale = (width * num_div) / img_width;
5867
724600f8ae0d (svn r8074) -Fix (FS#537, r7555, r5749): revert r7555 because it was really wrong and fix the off-by-one error due to truncation that was supposedly fixed by r7555.
rubidium
parents: 5864
diff changeset
   315
		row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   316
	} else {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   317
		/* Image is taller than map - center horizontally */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   318
		img_scale = (height * num_div) / img_height;
5867
724600f8ae0d (svn r8074) -Fix (FS#537, r7555, r5749): revert r7555 because it was really wrong and fix the off-by-one error due to truncation that was supposedly fixed by r7555.
rubidium
parents: 5864
diff changeset
   319
		col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   320
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   321
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   322
	/* Form the landscape */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   323
	for (row = 0; row < height - 1; row++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   324
		for (col = 0; col < width - 1; col++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   325
			switch (_patches.heightmap_rotation) {
5869
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   326
				default: NOT_REACHED();
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   327
				case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break;
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   328
				case HM_CLOCKWISE:         tile = TileXY(row, col); break;
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   329
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   330
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   331
			/* Check if current tile is within the 1-pixel map edge or padding regions */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   332
			if ((DistanceFromEdge(tile) <= 1) ||
5867
724600f8ae0d (svn r8074) -Fix (FS#537, r7555, r5749): revert r7555 because it was really wrong and fix the off-by-one error due to truncation that was supposedly fixed by r7555.
rubidium
parents: 5864
diff changeset
   333
					(row < row_pad) || (row >= (height - row_pad - 1)) ||
724600f8ae0d (svn r8074) -Fix (FS#537, r7555, r5749): revert r7555 because it was really wrong and fix the off-by-one error due to truncation that was supposedly fixed by r7555.
rubidium
parents: 5864
diff changeset
   334
					(col < col_pad) || (col >= (width  - col_pad - 1))) {
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   335
				SetTileHeight(tile, 0);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   336
			} else {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   337
				/* Use nearest neighbor resizing to scale map data.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   338
				 *  We rotate the map 45 degrees (counter)clockwise */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   339
				img_row = (((row - row_pad) * num_div) / img_scale);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   340
				switch (_patches.heightmap_rotation) {
5869
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   341
					default: NOT_REACHED();
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   342
					case HM_COUNTER_CLOCKWISE:
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   343
						img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   344
						break;
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   345
					case HM_CLOCKWISE:
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   346
						img_col = (((col - col_pad) * num_div) / img_scale);
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   347
						break;
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   348
				}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   349
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   350
				assert(img_row < img_height);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   351
				assert(img_col < img_width);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   352
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   353
				/* Color scales from 0 to 255, OpenTTD height scales from 0 to 15 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   354
				SetTileHeight(tile, map[img_row * img_width + img_col] / 16);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   355
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   356
			MakeClear(tile, CLEAR_GRASS, 3);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   357
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   358
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   359
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   360
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   361
/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   362
 * This function takes care of the fact that land in OpenTTD can never differ
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   363
 * more than 1 in height
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   364
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6548
diff changeset
   365
static void FixSlopes()
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   366
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   367
	uint width, height;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   368
	uint row, col;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   369
	byte current_tile;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   370
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   371
	/* Adjust height difference to maximum one horizontal/vertical change. */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   372
	width   = MapSizeX();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   373
	height  = MapSizeY();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   374
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   375
	/* Top and left edge */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   376
	for (row = 1; row < height - 2; row++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   377
		for (col = 1; col < width - 2; col++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   378
			/* Find lowest tile; either the top or left one */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   379
			current_tile = TileHeight(TileXY(col - 1, row)); // top edge
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   380
			if (TileHeight(TileXY(col, row - 1)) < current_tile) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   381
				current_tile = TileHeight(TileXY(col, row - 1)); // left edge
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   382
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   383
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   384
			/* Does the height differ more than one? */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   385
			if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   386
				/* Then change the height to be no more than one */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   387
				SetTileHeight(TileXY(col, row), current_tile + 1);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   388
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   389
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   390
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   391
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   392
	/* Bottom and right edge */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   393
	for (row = height - 2; row > 0; row--) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   394
		for (col = width - 2; col > 0; col--) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   395
			/* Find lowest tile; either the bottom and right one */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   396
			current_tile = TileHeight(TileXY(col + 1, row)); // bottom edge
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   397
			if (TileHeight(TileXY(col, row + 1)) < current_tile) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   398
				current_tile = TileHeight(TileXY(col, row + 1)); // right edge
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   399
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   400
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   401
			/* Does the height differ more than one? */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   402
			if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   403
				/* Then change the height to be no more than one */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   404
				SetTileHeight(TileXY(col, row), current_tile + 1);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   405
			}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   406
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   407
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   408
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   409
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   410
/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   411
 * Reads the heightmap with the correct file reader
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   412
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   413
static bool ReadHeightMap(char *filename, uint *x, uint *y, byte **map)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   414
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   415
	switch (_file_to_saveload.mode) {
5869
33d51b7f639d (svn r8077) -Cleanup: Restructure some switch() statements' default case when they're unreachable
Darkvater
parents: 5867
diff changeset
   416
		default: NOT_REACHED();
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   417
#ifdef WITH_PNG
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   418
		case SL_PNG:
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   419
			return ReadHeightmapPNG(filename, x, y, map);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   420
#endif /* WITH_PNG */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   421
		case SL_BMP:
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   422
			return ReadHeightmapBMP(filename, x, y, map);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   423
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   424
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   425
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   426
bool GetHeightmapDimensions(char *filename, uint *x, uint *y)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   427
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   428
	return ReadHeightMap(filename, x, y, NULL);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   429
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   430
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   431
void LoadHeightmap(char *filename)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   432
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   433
	uint x, y;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   434
	byte *map = NULL;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   435
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   436
	if (!ReadHeightMap(filename, &x, &y, &map)) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   437
		free(map);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   438
		return;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   439
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   440
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   441
	GrayscaleToMapHeights(x, y, map);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   442
	free(map);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   443
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   444
	FixSlopes();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   445
	MarkWholeScreenDirty();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   446
}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   447
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   448
void FlatEmptyWorld(byte tile_height)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   449
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   450
	uint width, height;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   451
	uint row, col;
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   452
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   453
	width  = MapSizeX();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   454
	height = MapSizeY();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   455
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   456
	for (row = 2; row < height - 2; row++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   457
		for (col = 2; col < width - 2; col++) {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   458
			SetTileHeight(TileXY(col, row), tile_height);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   459
		}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   460
	}
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   461
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   462
	FixSlopes();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   463
	MarkWholeScreenDirty();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
diff changeset
   464
}