author | unc0rr |
Wed, 04 Mar 2009 13:42:21 +0000 | |
changeset 1858 | a6189dbd16f0 |
parent 1852 | fa04a27005c3 |
child 1859 | e071284b118e |
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 |
|
1807 | 52 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt); |
53 |
var tx, ty: Longword; |
|
54 |
begin |
|
1852
fa04a27005c3
Fix crash when explosion is outside of the map (check was lost while refactoring)
unc0rr
parents:
1808
diff
changeset
|
55 |
if (Width <= 0) or (Height <= 0) then exit; |
1807 | 56 |
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true); |
57 |
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true); |
|
1806 | 58 |
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true); |
59 |
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true); |
|
60 |
||
1807 | 61 |
for ty:= Y div TEXSIZE to (Y + Height - 1) div TEXSIZE do |
62 |
for tx:= X div TEXSIZE to (X + Width - 1) div TEXSIZE do |
|
63 |
LandTextures[tx, ty].shouldUpdate:= true |
|
1806 | 64 |
end; |
65 |
||
66 |
procedure RealLandTexUpdate; |
|
1807 | 67 |
var x, y: LongWord; |
1806 | 68 |
begin |
1807 | 69 |
if LandTextures[0, 0].tex = nil then |
70 |
for x:= 0 to LANDTEXARW -1 do |
|
71 |
for y:= 0 to LANDTEXARH - 1 do |
|
72 |
with LandTextures[x, y] do |
|
73 |
tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y)) |
|
1806 | 74 |
else |
1807 | 75 |
for x:= 0 to LANDTEXARW -1 do |
76 |
for y:= 0 to LANDTEXARH - 1 do |
|
77 |
with LandTextures[x, y] do |
|
78 |
if shouldUpdate then |
|
79 |
begin |
|
1808 | 80 |
shouldUpdate:= false; |
1807 | 81 |
glBindTexture(GL_TEXTURE_2D, tex^.id); |
82 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEXSIZE, TEXSIZE, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x, y)); |
|
83 |
end |
|
1806 | 84 |
end; |
85 |
||
1807 | 86 |
procedure DrawLand(dX, dY: LongInt); |
87 |
var x, y: LongInt; |
|
1806 | 88 |
begin |
89 |
RealLandTexUpdate; |
|
1807 | 90 |
|
91 |
for x:= 0 to LANDTEXARW -1 do |
|
92 |
for y:= 0 to LANDTEXARH - 1 do |
|
93 |
with LandTextures[x, y] do |
|
94 |
DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex) |
|
95 |
end; |
|
96 |
||
97 |
procedure FreeLand; |
|
98 |
var x, y: LongInt; |
|
99 |
begin |
|
100 |
for x:= 0 to LANDTEXARW -1 do |
|
101 |
for y:= 0 to LANDTEXARH - 1 do |
|
102 |
with LandTextures[x, y] do |
|
103 |
FreeTexture(tex) |
|
1806 | 104 |
end; |
105 |
||
106 |
end. |