hedgewars/uLandObjects.pas
author unc0rr
Sun, 15 Jan 2006 23:32:58 +0000
changeset 51 b6e3ae05857f
parent 35 9367f246fb5f
child 54 839fd258ae6f
permissions -rw-r--r--
- Get rid of hwserv and runhelper - Rename hw -> hwengine - Updated copyright info - Fixed graves not reacting on explosion - Fixed fpc warning - Fill Land by another ( hope better) way - One more land template
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     1
(*
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     2
 * Hedgewars, a worms-like game
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     3
 * Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     4
 *
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     6
 *
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
     9
 * with the Software without restriction, including without limitation the
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    12
 * furnished to do so, subject to the following conditions:
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    13
 *
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    15
 *    this list of conditions and the following disclaimer.
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    18
 *    and/or other materials provided with the distribution.
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    20
 *    derived from this software without specific prior written permission.
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    21
 *
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    32
 *)
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents: 35
diff changeset
    33
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    34
unit uLandObjects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    35
interface
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    36
uses SDLh;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    37
{$include options.inc}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    38
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    39
procedure AddObjects(Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    40
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    41
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    42
implementation
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    43
uses uLand, uStore, uConsts, uMisc, uConsole, uRandom;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
    44
const MaxRects = 256;
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
    45
      MAXOBJECTRECTS = 16;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    46
type  PRectArray = ^TRectsArray;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    47
      TRectsArray = array[0..MaxRects] of TSDL_rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    48
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    49
type TThemeObject = record
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    50
                    Surf: PSDL_Surface;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    51
                    inland: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    52
                    outland: array[1..MAXOBJECTRECTS] of TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    53
                    rectcnt: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    54
                    Width, Height: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    55
                    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    56
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    57
var Rects: PRectArray;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    58
    RectCount: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    59
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    60
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    61
var i, p: LongWord;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    62
    x, y: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    63
    bpp: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    64
    r: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    65
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    66
r.x:= cpX;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    67
r.y:= cpY;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    68
SDL_UpperBlit(Image, nil, Surface, @r);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    69
WriteToConsole('Generating collision info... ');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    70
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    71
if SDL_MustLock(Image) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    72
   SDLTry(SDL_LockSurface(Image) >= 0, true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    73
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    74
bpp:= Image.format.BytesPerPixel;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    75
WriteToConsole('('+inttostr(bpp)+') ');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    76
p:= LongWord(Image.pixels);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    77
case bpp of
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    78
     1: OutError('We don''t work with 8 bit surfaces', true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    79
     2: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    80
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    81
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    82
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    83
                if PWord(p + x * 2)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    84
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    85
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    86
     3: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    87
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    88
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    89
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    90
                if  (PByte(p + x * 3 + 0)^ <> 0)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    91
                 or (PByte(p + x * 3 + 1)^ <> 0)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    92
                 or (PByte(p + x * 3 + 2)^ <> 0) then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    93
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    94
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    95
     4: for y:= 0 to Pred(Image.h) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    96
            begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    97
            i:= Longword(@Land[cpY + y, cpX]);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    98
            for x:= 0 to Pred(Image.w) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
    99
                if PLongword(p + x * 4)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   100
            inc(p, Image.pitch);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   101
            end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   102
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   103
if SDL_MustLock(Image) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   104
   SDL_UnlockSurface(Image);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   105
WriteLnToConsole(msgOK)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   106
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   107
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   108
procedure AddRect(x1, y1, w1, h1: integer);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   109
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   110
with Rects[RectCount] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   111
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   112
     x:= x1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   113
     y:= y1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   114
     w:= w1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   115
     h:= h1
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   116
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   117
inc(RectCount);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   118
TryDo(RectCount < MaxRects, 'AddRect: overflow', true)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   119
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   120
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   121
procedure InitRects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   122
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   123
RectCount:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   124
New(Rects)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   125
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   126
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   127
procedure FreeRects;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   128
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   129
Dispose(rects)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   130
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   131
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   132
function CheckIntersect(x1, y1, w1, h1: integer): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   133
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   134
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   135
Result:= false;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   136
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   137
if RectCount > 0 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   138
   repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   139
   with Rects[i] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   140
        Result:= (x < x1 + w1) and (x1 < x + w) and
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   141
                 (y < y1 + h1) and (y1 < y + h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   142
   inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   143
   until (i = RectCount) or (Result)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   144
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   145
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   146
function AddGirder(gX: integer; Surface: PSDL_Surface): boolean;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   147
var tmpsurf: PSDL_Surface;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   148
    x1, x2, y, k, i: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   149
    r, rr: TSDL_Rect;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   150
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   151
    function CountNonZeroz(x, y: integer): Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   152
    var i: integer;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   153
    begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   154
    Result:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   155
    for i:= y to y + 15 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   156
        if Land[i, x] <> 0 then inc(Result)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   157
    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   158
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   159
begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   160
y:= 150;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   161
repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   162
  inc(y, 24);
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   163
  x1:= gX;
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   164
  x2:= gX;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   165
  while (x1 > 100) and (CountNonZeroz(x1, y) = 0) do dec(x1, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   166
  i:= x1 - 12;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   167
  repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   168
    k:= CountNonZeroz(x1, y);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   169
    dec(x1, 2)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   170
  until (x1 < 100) or (k = 0) or (k = 16) or (x1 < i);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   171
  inc(x1, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   172
  if k = 16 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   173
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   174
     while (x2 < 1900) and (CountNonZeroz(x2, y) = 0) do inc(x2, 2);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   175
     i:= x2 + 12;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   176
     repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   177
       k:= CountNonZeroz(x2, y);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   178
       inc(x2, 2)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   179
     until (x2 > 1900) or (k = 0) or (k = 16) or (x2 > i);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   180
     if (x2 < 1900) and (k = 16) and (x2 - x1 > 250)
30
794e98e11b66 - Fixed slow sprite blt
unc0rr
parents: 27
diff changeset
   181
        and not CheckIntersect(x1 - 32, y - 64, x2 - x1 + 64, 144) then break;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   182
     end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   183
x1:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   184
until y > 900;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   185
if x1 > 0 then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   186
   begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   187
   Result:= true;
35
9367f246fb5f - New rope
unc0rr
parents: 30
diff changeset
   188
   tmpsurf:= LoadImage(Pathz[ptGraphics] + 'Girder.png', false);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   189
   rr.x:= x1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   190
   rr.y:= y;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   191
   while rr.x + 100 < x2 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   192
         begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   193
         SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   194
         inc(rr.x, 100);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   195
         end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   196
   r.x:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   197
   r.y:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   198
   r.w:= x2 - rr.x;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   199
   r.h:= 16;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   200
   SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   201
   SDL_FreeSurface(tmpsurf);
30
794e98e11b66 - Fixed slow sprite blt
unc0rr
parents: 27
diff changeset
   202
   AddRect(x1 - 8, y - 32, x2 - x1 + 16, 80);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   203
   for k:= y to y + 15 do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   204
       for i:= x1 to x2 do Land[k, i]:= $FFFFFF
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   205
   end else Result:= false
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   206
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   207
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   208
function CheckLand(rect: TSDL_Rect; dX, dY, Color: Longword): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   209
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   210
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   211
Result:= true;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   212
inc(rect.x, dX);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   213
inc(rect.y, dY);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   214
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   215
{$WARNINGS OFF}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   216
while (i <= rect.w) and Result do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   217
      begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   218
      Result:= (Land[rect.y, rect.x + i] = Color) and (Land[rect.y + rect.h, rect.x + i] = Color);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   219
      inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   220
      end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   221
i:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   222
while (i <= rect.h) and Result do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   223
      begin
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   224
      Result:= (Land[rect.y + i, rect.x] = Color) and (Land[rect.y + i, rect.x + rect.w] = Color);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   225
      inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   226
      end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   227
{$WARNINGS ON}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   228
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   229
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   230
function CheckCanPlace(x, y: Longword; var Obj: TThemeObject): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   231
var i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   232
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   233
with Obj do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   234
     if CheckLand(inland, x, y, $FFFFFF) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   235
        begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   236
        Result:= true;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   237
        i:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   238
        while Result and (i <= rectcnt) do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   239
              begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   240
              Result:= CheckLand(outland[i], x, y, 0);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   241
              inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   242
              end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   243
        if Result then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   244
           Result:= not CheckIntersect(x, y, Width, Height)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   245
        end else
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   246
        Result:= false
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   247
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   248
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   249
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   250
const MaxPointsIndex = 2047;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   251
var x, y: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   252
    ar: array[0..MaxPointsIndex] of TPoint;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   253
    cnt, i: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   254
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   255
cnt:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   256
with Obj do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   257
     begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   258
     x:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   259
     repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   260
         y:= 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   261
         repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   262
             if CheckCanPlace(x, y, Obj) then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   263
                begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   264
                ar[cnt].x:= x;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   265
                ar[cnt].y:= y;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   266
                inc(cnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   267
                if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   268
                   begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   269
                   y:= 5000;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   270
                   x:= 5000;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   271
                   end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   272
                end;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   273
             inc(y, 3);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   274
         until y > 1023 - Height;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   275
         inc(x, getrandom(6) + 3)
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   276
     until x > 2047 - Width;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   277
     Result:= cnt <> 0;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   278
     if Result then
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   279
        begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   280
        i:= getrandom(cnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   281
        BlitImageAndGenerateCollisionInfo(ar[i].x, ar[i].y, Obj.Surf, Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   282
        AddRect(ar[i].x, ar[i].y, Width, Height);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   283
        end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   284
     end
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   285
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   286
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   287
procedure AddThemeObjects(Surface: PSDL_Surface; MaxCount: Longword);
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   288
const MAXTHEMEOBJECTS = 32;
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   289
var f: textfile;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   290
    s: string;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   291
    ThemeObjects: array[1..MAXTHEMEOBJECTS] of TThemeObject;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   292
    i, ii, t, n: Longword;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   293
    b: boolean;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   294
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   295
s:= Pathz[ptThemeCurrent] + cThemeCFGFilename;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   296
WriteLnToConsole('Adding objects...');
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   297
AssignFile(f, s);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   298
{$I-}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   299
Reset(f);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   300
Readln(f, s); // skip color
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   301
Readln(f, n);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   302
for i:= 1 to n do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   303
    begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   304
    Readln(f, s); // filename
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   305
    with ThemeObjects[i] do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   306
         begin
35
9367f246fb5f - New rope
unc0rr
parents: 30
diff changeset
   307
         Surf:= LoadImage(Pathz[ptThemeCurrent] + s + '.png', false);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   308
         Read(f, Width, Height);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   309
         with inland do Read(f, x, y, w, h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   310
         Read(f, rectcnt);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   311
         for ii:= 1 to rectcnt do
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   312
             with outland[ii] do Read(f, x, y, w, h);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   313
         ReadLn(f)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   314
         end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   315
    end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   316
Closefile(f);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   317
{$I+}
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   318
TryDo(IOResult = 0, 'Bad data or cannot access file', true);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   319
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   320
// loaded objects, try to put on land
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   321
if n = 0 then exit;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   322
i:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   323
repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   324
    t:= getrandom(n) + 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   325
    ii:= t;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   326
    repeat
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   327
      inc(ii);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   328
      if ii > n then ii:= 1;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   329
      b:= TryPut(ThemeObjects[ii], Surface)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   330
    until b or (ii = t);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   331
inc(i)
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   332
until (i > MaxCount) or not b
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   333
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   334
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   335
procedure AddObjects(Surface: PSDL_Surface);
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   336
begin
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   337
InitRects;
27
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   338
AddGirder(512, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   339
AddGirder(1024, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   340
AddGirder(1300, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   341
AddGirder(1536, Surface);
c374fe590272 - improve land generation
unc0rr
parents: 24
diff changeset
   342
AddThemeObjects(Surface, 8);
24
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   343
FreeRects
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   344
end;
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   345
79c411363184 Add theme objects to land
unc0rr
parents:
diff changeset
   346
end.