author | unc0rr |
Wed, 24 Jun 2009 15:59:32 +0000 | |
changeset 2191 | 20c62f787a4d |
parent 2163 | 12730f5e79b9 |
child 2248 | 26e11cb27c61 |
permissions | -rw-r--r-- |
1806 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2009 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
19 |
unit uLandTexture; |
|
20 |
interface |
|
1807 | 21 |
uses SDLh; |
1806 | 22 |
|
1807 | 23 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt); |
24 |
procedure DrawLand(dX, dY: LongInt); |
|
25 |
procedure FreeLand; |
|
1806 | 26 |
|
27 |
implementation |
|
1906 | 28 |
uses uMisc, uLand, uStore, |
2152 | 29 |
{$IFDEF GLES11} |
1906 | 30 |
gles11, |
31 |
{$ELSE} |
|
32 |
GL, |
|
33 |
{$ENDIF} |
|
34 |
uConsts; |
|
1807 | 35 |
|
36 |
const TEXSIZE = 256; |
|
37 |
LANDTEXARW = LAND_WIDTH div TEXSIZE; |
|
38 |
LANDTEXARH = LAND_HEIGHT div TEXSIZE; |
|
1806 | 39 |
|
1807 | 40 |
var |
41 |
LandTextures: array[0..LANDTEXARW - 1, 0..LANDTEXARH - 1] of |
|
42 |
record |
|
43 |
shouldUpdate: boolean; |
|
44 |
tex: PTexture; |
|
45 |
end; |
|
46 |
||
47 |
tmpPixels: array [0..TEXSIZE - 1, 0..TEXSIZE - 1] of LongWord; |
|
1806 | 48 |
|
1807 | 49 |
function Pixels(x, y: Longword): Pointer; |
50 |
var ty: Longword; |
|
1806 | 51 |
begin |
1807 | 52 |
for ty:= 0 to TEXSIZE - 1 do |
53 |
Move(LandPixels[y * TEXSIZE + ty, x * TEXSIZE], tmpPixels[ty, 0], sizeof(Longword) * TEXSIZE); |
|
54 |
||
55 |
Pixels:= @tmpPixels |
|
56 |
end; |
|
1806 | 57 |
|
1859
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
58 |
function Pixels2(x, y: Longword): Pointer; |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
59 |
var tx, ty: Longword; |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
60 |
begin |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
61 |
for ty:= 0 to TEXSIZE - 1 do |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
62 |
for tx:= 0 to TEXSIZE - 1 do |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
63 |
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
|
64 |
|
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
65 |
Pixels2:= @tmpPixels |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
66 |
end; |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
67 |
|
1807 | 68 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt); |
69 |
var tx, ty: Longword; |
|
70 |
begin |
|
1852
fa04a27005c3
Fix crash when explosion is outside of the map (check was lost while refactoring)
unc0rr
parents:
1808
diff
changeset
|
71 |
if (Width <= 0) or (Height <= 0) then exit; |
1807 | 72 |
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true); |
73 |
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true); |
|
1806 | 74 |
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true); |
75 |
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true); |
|
76 |
||
1807 | 77 |
for ty:= Y div TEXSIZE to (Y + Height - 1) div TEXSIZE do |
78 |
for tx:= X div TEXSIZE to (X + Width - 1) div TEXSIZE do |
|
79 |
LandTextures[tx, ty].shouldUpdate:= true |
|
1806 | 80 |
end; |
81 |
||
82 |
procedure RealLandTexUpdate; |
|
1807 | 83 |
var x, y: LongWord; |
1806 | 84 |
begin |
1807 | 85 |
if LandTextures[0, 0].tex = nil then |
86 |
for x:= 0 to LANDTEXARW -1 do |
|
87 |
for y:= 0 to LANDTEXARH - 1 do |
|
88 |
with LandTextures[x, y] do |
|
89 |
tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y)) |
|
1806 | 90 |
else |
1807 | 91 |
for x:= 0 to LANDTEXARW -1 do |
92 |
for y:= 0 to LANDTEXARH - 1 do |
|
93 |
with LandTextures[x, y] do |
|
94 |
if shouldUpdate then |
|
95 |
begin |
|
1808 | 96 |
shouldUpdate:= false; |
1807 | 97 |
glBindTexture(GL_TEXTURE_2D, tex^.id); |
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2152
diff
changeset
|
98 |
{$IFDEF IPHONEOS} |
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2152
diff
changeset
|
99 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_BGRA, GL_UNSIGNED_BYTE, Pixels(x, y)); |
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2152
diff
changeset
|
100 |
{$ELSE} |
1807 | 101 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x, y)); |
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2152
diff
changeset
|
102 |
{$ENDIF} |
1807 | 103 |
end |
1806 | 104 |
end; |
105 |
||
1807 | 106 |
procedure DrawLand(dX, dY: LongInt); |
107 |
var x, y: LongInt; |
|
1806 | 108 |
begin |
109 |
RealLandTexUpdate; |
|
1807 | 110 |
|
111 |
for x:= 0 to LANDTEXARW -1 do |
|
112 |
for y:= 0 to LANDTEXARH - 1 do |
|
113 |
with LandTextures[x, y] do |
|
114 |
DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex) |
|
115 |
end; |
|
116 |
||
117 |
procedure FreeLand; |
|
118 |
var x, y: LongInt; |
|
119 |
begin |
|
120 |
for x:= 0 to LANDTEXARW -1 do |
|
121 |
for y:= 0 to LANDTEXARH - 1 do |
|
122 |
with LandTextures[x, y] do |
|
123 |
FreeTexture(tex) |
|
1806 | 124 |
end; |
125 |
||
126 |
end. |