author | koda |
Thu, 17 Jun 2010 16:28:42 +0200 | |
changeset 3510 | 23145a950eae |
parent 3491 | 4619b1ae99b5 |
child 3509 | d72c2219595d |
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 |
||
2599 | 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 | 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 |
|
3165
3ec07a7d8456
just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents:
3045
diff
changeset
|
30 |
uses uMisc, uLand, uStore, uConsts, GLunit; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2599
diff
changeset
|
31 |
|
1807 | 32 |
|
33 |
const TEXSIZE = 256; |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
34 |
LANDTEXARW = LAND_WIDTH div TEXSIZE; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
35 |
LANDTEXARH = LAND_HEIGHT div TEXSIZE; |
1806 | 36 |
|
1807 | 37 |
var |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
38 |
LandTextures: array[0..LANDTEXARW - 1, 0..LANDTEXARH - 1] of |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
39 |
record |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
40 |
shouldUpdate: boolean; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
41 |
tex: PTexture; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
42 |
end; |
1807 | 43 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
44 |
tmpPixels: array [0..TEXSIZE - 1, 0..TEXSIZE - 1] of LongWord; |
1806 | 45 |
|
1807 | 46 |
function Pixels(x, y: Longword): Pointer; |
47 |
var ty: Longword; |
|
1806 | 48 |
begin |
1807 | 49 |
for ty:= 0 to TEXSIZE - 1 do |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
50 |
Move(LandPixels[y * TEXSIZE + ty, x * TEXSIZE], tmpPixels[ty, 0], sizeof(Longword) * TEXSIZE); |
2376 | 51 |
|
1807 | 52 |
Pixels:= @tmpPixels |
53 |
end; |
|
1806 | 54 |
|
1859
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
55 |
function Pixels2(x, y: Longword): Pointer; |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
56 |
var tx, ty: Longword; |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
57 |
begin |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
58 |
for ty:= 0 to TEXSIZE - 1 do |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
59 |
for tx:= 0 to TEXSIZE - 1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
60 |
tmpPixels[ty, tx]:= Land[y * TEXSIZE + ty, x * TEXSIZE + tx] or AMask; |
2376 | 61 |
|
1859
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
62 |
Pixels2:= @tmpPixels |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
63 |
end; |
e071284b118e
Pixels2 proc, which uses Land array when updating textures
unc0rr
parents:
1852
diff
changeset
|
64 |
|
1807 | 65 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt); |
66 |
var tx, ty: Longword; |
|
67 |
begin |
|
1852
fa04a27005c3
Fix crash when explosion is outside of the map (check was lost while refactoring)
unc0rr
parents:
1808
diff
changeset
|
68 |
if (Width <= 0) or (Height <= 0) then exit; |
1807 | 69 |
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true); |
70 |
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true); |
|
1806 | 71 |
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true); |
72 |
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true); |
|
73 |
||
1807 | 74 |
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
|
75 |
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
|
76 |
LandTextures[tx, ty].shouldUpdate:= true |
1806 | 77 |
end; |
78 |
||
79 |
procedure RealLandTexUpdate; |
|
1807 | 80 |
var x, y: LongWord; |
1806 | 81 |
begin |
1807 | 82 |
if LandTextures[0, 0].tex = nil then |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
83 |
for x:= 0 to LANDTEXARW -1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
84 |
for y:= 0 to LANDTEXARH - 1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
85 |
with LandTextures[x, y] do |
3491 | 86 |
begin |
87 |
tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y)); |
|
88 |
glBindTexture(GL_TEXTURE_2D, tex^.id); |
|
89 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, tpHigh); |
|
90 |
end |
|
1806 | 91 |
else |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
92 |
for x:= 0 to LANDTEXARW -1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
93 |
for y:= 0 to LANDTEXARH - 1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
94 |
with LandTextures[x, y] do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
95 |
if shouldUpdate then |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
96 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
97 |
shouldUpdate:= false; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
98 |
glBindTexture(GL_TEXTURE_2D, tex^.id); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
99 |
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
|
100 |
end |
1806 | 101 |
end; |
102 |
||
1807 | 103 |
procedure DrawLand(dX, dY: LongInt); |
104 |
var x, y: LongInt; |
|
1806 | 105 |
begin |
106 |
RealLandTexUpdate; |
|
1807 | 107 |
|
108 |
for x:= 0 to LANDTEXARW -1 do |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
109 |
for y:= 0 to LANDTEXARH - 1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
110 |
with LandTextures[x, y] do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
111 |
DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex) |
1807 | 112 |
end; |
113 |
||
114 |
procedure FreeLand; |
|
115 |
var x, y: LongInt; |
|
116 |
begin |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
117 |
for x:= 0 to LANDTEXARW -1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
118 |
for y:= 0 to LANDTEXARH - 1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
119 |
with LandTextures[x, y] do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
120 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
121 |
FreeTexture(tex); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
122 |
tex:= nil; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
123 |
end; |
3045 | 124 |
|
125 |
if LandBackSurface <> nil then |
|
126 |
SDL_FreeSurface(LandBackSurface); |
|
1806 | 127 |
end; |
128 |
||
129 |
end. |