hedgewars/uLandTexture.pas
author unc0rr
Wed, 04 Mar 2009 16:35:36 +0000
changeset 1860 ce140b2b928a
parent 1859 e071284b118e
child 1906 644f93d8f148
permissions -rw-r--r--
Some work on players lists context menus
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
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    19
unit uLandTexture;
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    20
interface
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    21
uses SDLh;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    22
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    23
procedure UpdateLandTexture(X, Width, Y, Height: LongInt);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    24
procedure DrawLand(dX, dY: LongInt);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    25
procedure FreeLand;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    26
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    27
implementation
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    28
uses uMisc, uLand, uStore, GL, uConsts;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    29
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    30
const TEXSIZE = 256;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    31
	LANDTEXARW = LAND_WIDTH div TEXSIZE;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    32
	LANDTEXARH = LAND_HEIGHT div TEXSIZE;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    33
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    34
var
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    35
	LandTextures: array[0..LANDTEXARW - 1, 0..LANDTEXARH - 1] of
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    36
			record
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    37
			shouldUpdate: boolean;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    38
			tex: PTexture;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    39
			end;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    40
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    41
	tmpPixels: array [0..TEXSIZE - 1, 0..TEXSIZE - 1] of LongWord;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    42
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    43
function Pixels(x, y: Longword): Pointer;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    44
var ty: Longword;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    45
begin
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    46
for ty:= 0 to TEXSIZE - 1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    47
	Move(LandPixels[y * TEXSIZE + ty, x * TEXSIZE], tmpPixels[ty, 0], sizeof(Longword) * TEXSIZE);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    48
	
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    49
Pixels:= @tmpPixels
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    50
end;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    51
1859
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    52
function Pixels2(x, y: Longword): Pointer;
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    53
var tx, ty: Longword;
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    54
begin
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    55
for ty:= 0 to TEXSIZE - 1 do
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    56
	for tx:= 0 to TEXSIZE - 1 do
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    57
		tmpPixels[ty, tx]:= Land[y * TEXSIZE + ty, x * TEXSIZE + tx] or $FF000000;
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    58
	
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    59
Pixels2:= @tmpPixels
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    60
end;
e071284b118e Pixels2 proc, which uses Land array when updating textures
unc0rr
parents: 1852
diff changeset
    61
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    62
procedure UpdateLandTexture(X, Width, Y, Height: LongInt);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    63
var tx, ty: Longword;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    64
begin
1852
fa04a27005c3 Fix crash when explosion is outside of the map (check was lost while refactoring)
unc0rr
parents: 1808
diff changeset
    65
if (Width <= 0) or (Height <= 0) then exit;
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    66
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    67
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true);
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    68
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true);
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    69
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true);
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    70
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    71
for ty:= Y div TEXSIZE to (Y + Height - 1) div TEXSIZE do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    72
	for tx:= X div TEXSIZE to (X + Width - 1) div TEXSIZE do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    73
		LandTextures[tx, ty].shouldUpdate:= true
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    74
end;
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    75
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    76
procedure RealLandTexUpdate;
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    77
var x, y: LongWord;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    78
begin
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    79
if LandTextures[0, 0].tex = nil then
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    80
	for x:= 0 to LANDTEXARW -1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    81
		for y:= 0 to LANDTEXARH - 1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    82
			with LandTextures[x, y] do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    83
				tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y))
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    84
else
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    85
	for x:= 0 to LANDTEXARW -1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    86
		for y:= 0 to LANDTEXARH - 1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    87
			with LandTextures[x, y] do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    88
				if shouldUpdate then
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    89
					begin
1808
2fc248766d57 Fix a bug with updating
unc0rr
parents: 1807
diff changeset
    90
					shouldUpdate:= false;
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    91
					glBindTexture(GL_TEXTURE_2D, tex^.id);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    92
					glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x, y));
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    93
					end
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    94
end;
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    95
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    96
procedure DrawLand(dX, dY: LongInt);
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
    97
var x, y: LongInt;
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    98
begin
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
    99
RealLandTexUpdate;
1807
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   100
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   101
for x:= 0 to LANDTEXARW -1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   102
	for y:= 0 to LANDTEXARH - 1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   103
		with LandTextures[x, y] do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   104
			DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex)
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   105
end;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   106
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   107
procedure FreeLand;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   108
var x, y: LongInt;
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   109
begin
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   110
for x:= 0 to LANDTEXARW -1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   111
	for y:= 0 to LANDTEXARH - 1 do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   112
		with LandTextures[x, y] do
795f97007833 Split land texture into small ones:
unc0rr
parents: 1806
diff changeset
   113
			FreeTexture(tex)
1806
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   114
end;
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   115
3c4f0886c123 More reorganization
unc0rr
parents:
diff changeset
   116
end.