author | koda |
Thu, 29 Oct 2009 17:51:54 +0000 | |
changeset 2592 | d86618629e20 |
parent 2587 | 0dfa56a8513c |
child 2599 | c7153d2348f3 |
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 |
||
2587
0dfa56a8513c
fix a segfault in the iphone simulator by moving options.inc at the beginning of the file
koda
parents:
2376
diff
changeset
|
19 |
{$include options.inc} |
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 | 21 |
unit uLandTexture; |
22 |
interface |
|
1807 | 23 |
uses SDLh; |
1806 | 24 |
|
1807 | 25 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt); |
26 |
procedure DrawLand(dX, dY: LongInt); |
|
27 |
procedure FreeLand; |
|
1806 | 28 |
|
29 |
implementation |
|
2376 | 30 |
uses uMisc, uLand, uStore, |
2152 | 31 |
{$IFDEF GLES11} |
1906 | 32 |
gles11, |
33 |
{$ELSE} |
|
34 |
GL, |
|
35 |
{$ENDIF} |
|
36 |
uConsts; |
|
1807 | 37 |
|
38 |
const TEXSIZE = 256; |
|
39 |
LANDTEXARW = LAND_WIDTH div TEXSIZE; |
|
40 |
LANDTEXARH = LAND_HEIGHT div TEXSIZE; |
|
1806 | 41 |
|
1807 | 42 |
var |
43 |
LandTextures: array[0..LANDTEXARW - 1, 0..LANDTEXARH - 1] of |
|
44 |
record |
|
45 |
shouldUpdate: boolean; |
|
46 |
tex: PTexture; |
|
47 |
end; |
|
48 |
||
49 |
tmpPixels: array [0..TEXSIZE - 1, 0..TEXSIZE - 1] of LongWord; |
|
1806 | 50 |
|
1807 | 51 |
function Pixels(x, y: Longword): Pointer; |
52 |
var ty: Longword; |
|
1806 | 53 |
begin |
1807 | 54 |
for ty:= 0 to TEXSIZE - 1 do |
55 |
Move(LandPixels[y * TEXSIZE + ty, x * TEXSIZE], tmpPixels[ty, 0], sizeof(Longword) * TEXSIZE); |
|
2376 | 56 |
|
1807 | 57 |
Pixels:= @tmpPixels |
58 |
end; |
|
1806 | 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 |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
64 |
for tx:= 0 to TEXSIZE - 1 do |
2592 | 65 |
tmpPixels[ty, tx]:= Land[y * TEXSIZE + ty, x * TEXSIZE + tx] or AMask; |
2376 | 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 | 70 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt); |
71 |
var tx, ty: Longword; |
|
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 | 74 |
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true); |
75 |
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true); |
|
1806 | 76 |
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true); |
77 |
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true); |
|
78 |
||
1807 | 79 |
for ty:= Y div TEXSIZE to (Y + Height - 1) div TEXSIZE do |
80 |
for tx:= X div TEXSIZE to (X + Width - 1) div TEXSIZE do |
|
81 |
LandTextures[tx, ty].shouldUpdate:= true |
|
1806 | 82 |
end; |
83 |
||
84 |
procedure RealLandTexUpdate; |
|
1807 | 85 |
var x, y: LongWord; |
1806 | 86 |
begin |
1807 | 87 |
if LandTextures[0, 0].tex = nil then |
88 |
for x:= 0 to LANDTEXARW -1 do |
|
89 |
for y:= 0 to LANDTEXARH - 1 do |
|
90 |
with LandTextures[x, y] do |
|
91 |
tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y)) |
|
1806 | 92 |
else |
1807 | 93 |
for x:= 0 to LANDTEXARW -1 do |
94 |
for y:= 0 to LANDTEXARH - 1 do |
|
95 |
with LandTextures[x, y] do |
|
96 |
if shouldUpdate then |
|
97 |
begin |
|
1808 | 98 |
shouldUpdate:= false; |
1807 | 99 |
glBindTexture(GL_TEXTURE_2D, tex^.id); |
100 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x, y)); |
|
101 |
end |
|
1806 | 102 |
end; |
103 |
||
1807 | 104 |
procedure DrawLand(dX, dY: LongInt); |
105 |
var x, y: LongInt; |
|
1806 | 106 |
begin |
107 |
RealLandTexUpdate; |
|
1807 | 108 |
|
109 |
for x:= 0 to LANDTEXARW -1 do |
|
110 |
for y:= 0 to LANDTEXARH - 1 do |
|
111 |
with LandTextures[x, y] do |
|
112 |
DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex) |
|
113 |
end; |
|
114 |
||
115 |
procedure FreeLand; |
|
116 |
var x, y: LongInt; |
|
117 |
begin |
|
118 |
for x:= 0 to LANDTEXARW -1 do |
|
119 |
for y:= 0 to LANDTEXARH - 1 do |
|
120 |
with LandTextures[x, y] do |
|
121 |
FreeTexture(tex) |
|
1806 | 122 |
end; |
123 |
||
124 |
end. |