author | unc0rr |
Mon, 13 Apr 2009 12:59:48 +0000 | |
changeset 1989 | 129e8c4fe554 |
parent 1906 | 644f93d8f148 |
child 2152 | a2811690da1b |
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, |
29 |
{$IFDEF IPHONE} |
|
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); |
98 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x, y)); |
|
99 |
end |
|
1806 | 100 |
end; |
101 |
||
1807 | 102 |
procedure DrawLand(dX, dY: LongInt); |
103 |
var x, y: LongInt; |
|
1806 | 104 |
begin |
105 |
RealLandTexUpdate; |
|
1807 | 106 |
|
107 |
for x:= 0 to LANDTEXARW -1 do |
|
108 |
for y:= 0 to LANDTEXARH - 1 do |
|
109 |
with LandTextures[x, y] do |
|
110 |
DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex) |
|
111 |
end; |
|
112 |
||
113 |
procedure FreeLand; |
|
114 |
var x, y: LongInt; |
|
115 |
begin |
|
116 |
for x:= 0 to LANDTEXARW -1 do |
|
117 |
for y:= 0 to LANDTEXARH - 1 do |
|
118 |
with LandTextures[x, y] do |
|
119 |
FreeTexture(tex) |
|
1806 | 120 |
end; |
121 |
||
122 |
end. |