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 |
|
1807
|
52 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt);
|
|
53 |
var tx, ty: Longword;
|
|
54 |
begin
|
|
55 |
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true);
|
|
56 |
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true);
|
1806
|
57 |
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true);
|
|
58 |
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true);
|
|
59 |
|
1807
|
60 |
for ty:= Y div TEXSIZE to (Y + Height - 1) div TEXSIZE do
|
|
61 |
for tx:= X div TEXSIZE to (X + Width - 1) div TEXSIZE do
|
|
62 |
LandTextures[tx, ty].shouldUpdate:= true
|
1806
|
63 |
end;
|
|
64 |
|
|
65 |
procedure RealLandTexUpdate;
|
1807
|
66 |
var x, y: LongWord;
|
1806
|
67 |
begin
|
1807
|
68 |
if LandTextures[0, 0].tex = nil then
|
|
69 |
for x:= 0 to LANDTEXARW -1 do
|
|
70 |
for y:= 0 to LANDTEXARH - 1 do
|
|
71 |
with LandTextures[x, y] do
|
|
72 |
tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y))
|
1806
|
73 |
else
|
1807
|
74 |
for x:= 0 to LANDTEXARW -1 do
|
|
75 |
for y:= 0 to LANDTEXARH - 1 do
|
|
76 |
with LandTextures[x, y] do
|
|
77 |
if shouldUpdate then
|
|
78 |
begin
|
1808
|
79 |
shouldUpdate:= false;
|
1807
|
80 |
glBindTexture(GL_TEXTURE_2D, tex^.id);
|
|
81 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x, y));
|
|
82 |
end
|
1806
|
83 |
end;
|
|
84 |
|
1807
|
85 |
procedure DrawLand(dX, dY: LongInt);
|
|
86 |
var x, y: LongInt;
|
1806
|
87 |
begin
|
|
88 |
RealLandTexUpdate;
|
1807
|
89 |
|
|
90 |
for x:= 0 to LANDTEXARW -1 do
|
|
91 |
for y:= 0 to LANDTEXARH - 1 do
|
|
92 |
with LandTextures[x, y] do
|
|
93 |
DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex)
|
|
94 |
end;
|
|
95 |
|
|
96 |
procedure FreeLand;
|
|
97 |
var x, y: LongInt;
|
|
98 |
begin
|
|
99 |
for x:= 0 to LANDTEXARW -1 do
|
|
100 |
for y:= 0 to LANDTEXARH - 1 do
|
|
101 |
with LandTextures[x, y] do
|
|
102 |
FreeTexture(tex)
|
1806
|
103 |
end;
|
|
104 |
|
|
105 |
end.
|