author | unc0rr |
Wed, 04 Mar 2009 16:35:36 +0000 | |
changeset 1860 | ce140b2b928a |
parent 1859 | e071284b118e |
child 1906 | 644f93d8f148 |
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 |
|
1807 | 28 |
uses uMisc, uLand, uStore, GL, uConsts; |
29 |
||
30 |
const TEXSIZE = 256; |
|
31 |
LANDTEXARW = LAND_WIDTH div TEXSIZE; |
|
32 |
LANDTEXARH = LAND_HEIGHT div TEXSIZE; |
|
1806 | 33 |
|
1807 | 34 |
var |
35 |
LandTextures: array[0..LANDTEXARW - 1, 0..LANDTEXARH - 1] of |
|
36 |
record |
|
37 |
shouldUpdate: boolean; |
|
38 |
tex: PTexture; |
|
39 |
end; |
|
40 |
||
41 |
tmpPixels: array [0..TEXSIZE - 1, 0..TEXSIZE - 1] of LongWord; |
|
1806 | 42 |
|
1807 | 43 |
function Pixels(x, y: Longword): Pointer; |
44 |
var ty: Longword; |
|
1806 | 45 |
begin |
1807 | 46 |
for ty:= 0 to TEXSIZE - 1 do |
47 |
Move(LandPixels[y * TEXSIZE + ty, x * TEXSIZE], tmpPixels[ty, 0], sizeof(Longword) * TEXSIZE); |
|
48 |
||
49 |
Pixels:= @tmpPixels |
|
50 |
end; |
|
1806 | 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 | 62 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt); |
63 |
var tx, ty: Longword; |
|
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 | 66 |
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true); |
67 |
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true); |
|
1806 | 68 |
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true); |
69 |
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true); |
|
70 |
||
1807 | 71 |
for ty:= Y div TEXSIZE to (Y + Height - 1) div TEXSIZE do |
72 |
for tx:= X div TEXSIZE to (X + Width - 1) div TEXSIZE do |
|
73 |
LandTextures[tx, ty].shouldUpdate:= true |
|
1806 | 74 |
end; |
75 |
||
76 |
procedure RealLandTexUpdate; |
|
1807 | 77 |
var x, y: LongWord; |
1806 | 78 |
begin |
1807 | 79 |
if LandTextures[0, 0].tex = nil then |
80 |
for x:= 0 to LANDTEXARW -1 do |
|
81 |
for y:= 0 to LANDTEXARH - 1 do |
|
82 |
with LandTextures[x, y] do |
|
83 |
tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y)) |
|
1806 | 84 |
else |
1807 | 85 |
for x:= 0 to LANDTEXARW -1 do |
86 |
for y:= 0 to LANDTEXARH - 1 do |
|
87 |
with LandTextures[x, y] do |
|
88 |
if shouldUpdate then |
|
89 |
begin |
|
1808 | 90 |
shouldUpdate:= false; |
1807 | 91 |
glBindTexture(GL_TEXTURE_2D, tex^.id); |
92 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x, y)); |
|
93 |
end |
|
1806 | 94 |
end; |
95 |
||
1807 | 96 |
procedure DrawLand(dX, dY: LongInt); |
97 |
var x, y: LongInt; |
|
1806 | 98 |
begin |
99 |
RealLandTexUpdate; |
|
1807 | 100 |
|
101 |
for x:= 0 to LANDTEXARW -1 do |
|
102 |
for y:= 0 to LANDTEXARH - 1 do |
|
103 |
with LandTextures[x, y] do |
|
104 |
DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex) |
|
105 |
end; |
|
106 |
||
107 |
procedure FreeLand; |
|
108 |
var x, y: LongInt; |
|
109 |
begin |
|
110 |
for x:= 0 to LANDTEXARW -1 do |
|
111 |
for y:= 0 to LANDTEXARH - 1 do |
|
112 |
with LandTextures[x, y] do |
|
113 |
FreeTexture(tex) |
|
1806 | 114 |
end; |
115 |
||
116 |
end. |