hedgewars/uLandPainted.pas
author unc0rr
Sat, 04 Dec 2010 23:29:59 +0300
changeset 4458 7351e6f1ee28
parent 4457 ffb766e85150
child 4490 f6840f7e2f60
permissions -rw-r--r--
Halfplement decode/drawing routine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4457
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     1
(*
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2010 Andrey Korotaev <unC0Rr@gmail.com>
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     4
 *
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     8
 *
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    13
 *
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    17
 *)
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    18
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    19
{$INCLUDE "options.inc"}
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    20
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    21
unit uLandPainted;
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    22
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    23
interface
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    24
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    25
procedure LoadFromFile(fileName: shortstring);
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    26
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    27
implementation
4458
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    28
uses uLandGraphics, uConsts, uUtils, SDLh;
4457
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    29
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    30
type PointRec = packed record
4458
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    31
    X, Y: SmallInt;
4457
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    32
    flags: byte;
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    33
    end;
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    34
4458
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    35
procedure DrawLineOnLand(X1, Y1, X2, Y2: LongInt);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    36
var  eX, eY, dX, dY: LongInt;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    37
    i, sX, sY, x, y, d: LongInt;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    38
    b: boolean;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    39
    len: LongWord;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    40
begin
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    41
    len:= 0;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    42
    if (X1 = X2) and (Y1 = Y2) then
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    43
        begin
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    44
        exit
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    45
        end;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    46
    eX:= 0;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    47
    eY:= 0;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    48
    dX:= X2 - X1;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    49
    dY:= Y2 - Y1;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    50
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    51
    if (dX > 0) then sX:= 1
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    52
    else
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    53
    if (dX < 0) then
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    54
        begin
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    55
        sX:= -1;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    56
        dX:= -dX
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    57
        end else sX:= dX;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    58
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    59
    if (dY > 0) then sY:= 1
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    60
    else
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    61
    if (dY < 0) then
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    62
        begin
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    63
        sY:= -1;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    64
        dY:= -dY
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    65
        end else sY:= dY;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    66
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    67
        if (dX > dY) then d:= dX
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    68
                    else d:= dY;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    69
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    70
        x:= X1;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    71
        y:= Y1;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    72
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    73
        for i:= 0 to d do
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    74
            begin
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    75
            inc(eX, dX);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    76
            inc(eY, dY);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    77
            b:= false;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    78
            if (eX > d) then
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    79
                begin
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    80
                dec(eX, d);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    81
                inc(x, sX);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    82
                b:= true
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    83
                end;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    84
            if (eY > d) then
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    85
                begin
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    86
                dec(eY, d);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    87
                inc(y, sY);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    88
                b:= true
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    89
                end;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    90
            if b then
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    91
                begin
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    92
                inc(len);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    93
                if (len mod 4) = 0 then FillRoundInLand(X, Y, 34, lfBasic)
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    94
                end
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    95
        end
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    96
end;
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    97
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
    98
4457
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
    99
procedure LoadFromFile(fileName: shortstring);
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   100
var
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   101
    f: file of PointRec;
4458
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   102
    rec, prevRec: PointRec;
4457
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   103
begin
4458
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   104
    fileMode:= 0;
4457
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   105
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   106
    assignFile(f, fileName);
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   107
    reset(f);
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   108
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   109
    while not eof(f) do
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   110
        begin
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   111
        read(f, rec);
4458
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   112
        rec.X:= SDLNet_Read16(@rec.X);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   113
        rec.Y:= SDLNet_Read16(@rec.Y);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   114
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   115
        // FIXME: handle single point
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   116
        if eof(f) or (rec.flags and $80 <> 0) then
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   117
            else
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   118
            DrawLineOnLand(prevRec.X, prevRec.Y, rec.X, rec.Y);
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   119
7351e6f1ee28 Halfplement decode/drawing routine
unc0rr
parents: 4457
diff changeset
   120
        prevRec:= rec;
4457
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   121
        end;
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   122
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   123
    closeFile(f);
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   124
end;
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   125
ffb766e85150 - Change painted map file format
unc0rr
parents:
diff changeset
   126
end.