hedgewars/uLandTexture.pas
author nemo
Fri, 26 Mar 2010 15:55:20 +0000
changeset 3078 07d4ca3afce3
parent 3045 41732f986b4f
child 3165 3ec07a7d8456
permissions -rw-r--r--
Remove doubled call of freeEverything
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     1
(*
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2009 Andrey Korotaev <unC0Rr@gmail.com>
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     4
 *
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     8
 *
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    13
 *
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    17
 *)
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    18
2599
c7153d2348f3 move compiler directives to standard pascal
koda
parents: 2592
diff changeset
    19
{$INCLUDE "options.inc"}
2587
0dfa56a8513c fix a segfault in the iphone simulator by moving options.inc at the beginning of the file
koda
parents: 2376
diff changeset
    20
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    21
unit uLandTexture;
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    22
interface
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    23
uses SDLh;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    24
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    25
procedure UpdateLandTexture(X, Width, Y, Height: LongInt);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    26
procedure DrawLand(dX, dY: LongInt);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    27
procedure FreeLand;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    28
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    29
implementation
2699
249adefa9c1c replace initialization/finalization statements with custom init functions
koda
parents: 2599
diff changeset
    30
uses uMisc, uLand, uStore, uConsts,
2152
a2811690da1b Patch by koda:
unc0rr
parents: 1906
diff changeset
    31
{$IFDEF GLES11}
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    32
    gles11;
1906
644f93d8f148 Apply koda's OpenGL ES compatibility patch
unc0rr
parents: 1859
diff changeset
    33
{$ELSE}
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    34
    GL;
1906
644f93d8f148 Apply koda's OpenGL ES compatibility patch
unc0rr
parents: 1859
diff changeset
    35
{$ENDIF}
2699
249adefa9c1c replace initialization/finalization statements with custom init functions
koda
parents: 2599
diff changeset
    36
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    37
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    38
const TEXSIZE = 256;
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    39
    LANDTEXARW = LAND_WIDTH div TEXSIZE;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    40
    LANDTEXARH = LAND_HEIGHT div TEXSIZE;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    41
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    42
var
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    43
    LandTextures: array[0..LANDTEXARW - 1, 0..LANDTEXARH - 1] of
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    44
            record
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    45
            shouldUpdate: boolean;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    46
            tex: PTexture;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    47
            end;
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    48
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    49
    tmpPixels: array [0..TEXSIZE - 1, 0..TEXSIZE - 1] of LongWord;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    50
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    51
function Pixels(x, y: Longword): Pointer;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    52
var ty: Longword;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    53
begin
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    54
for ty:= 0 to TEXSIZE - 1 do
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    55
    Move(LandPixels[y * TEXSIZE + ty, x * TEXSIZE], tmpPixels[ty, 0], sizeof(Longword) * TEXSIZE);
2376
ece7b87f1334 Strip trailing spaces
nemo
parents: 2248
diff changeset
    56
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    57
Pixels:= @tmpPixels
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    58
end;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    59
1859
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    60
function Pixels2(x, y: Longword): Pointer;
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    61
var tx, ty: Longword;
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    62
begin
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    63
for ty:= 0 to TEXSIZE - 1 do
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    64
    for tx:= 0 to TEXSIZE - 1 do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    65
        tmpPixels[ty, tx]:= Land[y * TEXSIZE + ty, x * TEXSIZE + tx] or AMask;
2376
ece7b87f1334 Strip trailing spaces
nemo
parents: 2248
diff changeset
    66
1859
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    67
Pixels2:= @tmpPixels
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    68
end;
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    69
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    70
procedure UpdateLandTexture(X, Width, Y, Height: LongInt);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    71
var tx, ty: Longword;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    72
begin
1852
fa04a27005c3 Fix crash when explosion is outside of the map (check was lost while refactoring)
unc0rr
parents: 1808
diff changeset
    73
if (Width <= 0) or (Height <= 0) then exit;
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    74
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    75
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true);
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    76
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true);
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    77
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true);
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    78
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    79
for ty:= Y div TEXSIZE to (Y + Height - 1) div TEXSIZE do
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    80
    for tx:= X div TEXSIZE to (X + Width - 1) div TEXSIZE do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    81
        LandTextures[tx, ty].shouldUpdate:= true
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    82
end;
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    83
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    84
procedure RealLandTexUpdate;
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    85
var x, y: LongWord;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    86
begin
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    87
if LandTextures[0, 0].tex = nil then
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    88
    for x:= 0 to LANDTEXARW -1 do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    89
        for y:= 0 to LANDTEXARH - 1 do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    90
            with LandTextures[x, y] do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    91
                tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y))
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    92
else
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    93
    for x:= 0 to LANDTEXARW -1 do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    94
        for y:= 0 to LANDTEXARH - 1 do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    95
            with LandTextures[x, y] do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    96
                if shouldUpdate then
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    97
                    begin
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    98
                    shouldUpdate:= false;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
    99
                    glBindTexture(GL_TEXTURE_2D, tex^.id);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   100
                    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x, y));
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   101
                    end
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   102
end;
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   103
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   104
procedure DrawLand(dX, dY: LongInt);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   105
var x, y: LongInt;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   106
begin
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   107
RealLandTexUpdate;
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   108
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   109
for x:= 0 to LANDTEXARW -1 do
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   110
    for y:= 0 to LANDTEXARH - 1 do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   111
        with LandTextures[x, y] do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   112
            DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex)
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   113
end;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   114
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   115
procedure FreeLand;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   116
var x, y: LongInt;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   117
begin
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   118
    for x:= 0 to LANDTEXARW -1 do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   119
        for y:= 0 to LANDTEXARH - 1 do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   120
            with LandTextures[x, y] do
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   121
            begin
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   122
                FreeTexture(tex);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   123
                tex:= nil;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2716
diff changeset
   124
            end;
3045
41732f986b4f Clean Augean stables
unc0rr
parents: 2948
diff changeset
   125
41732f986b4f Clean Augean stables
unc0rr
parents: 2948
diff changeset
   126
if LandBackSurface <> nil then
41732f986b4f Clean Augean stables
unc0rr
parents: 2948
diff changeset
   127
    SDL_FreeSurface(LandBackSurface);
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   128
end;
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   129
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   130
end.