author | koda |
Sun, 20 Nov 2011 19:07:59 +0100 | |
changeset 6409 | ca93f38a1aab |
parent 6383 | c34a8b98d78c |
child 6580 | 6155187bf599 |
permissions | -rw-r--r-- |
1806 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
4976 | 3 |
* Copyright (c) 2004-2011 Andrey Korotaev <unC0Rr@gmail.com> |
1806 | 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 |
|
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
25 |
procedure initModule; |
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
26 |
procedure freeModule; |
1807 | 27 |
procedure UpdateLandTexture(X, Width, Y, Height: LongInt); |
28 |
procedure DrawLand(dX, dY: LongInt); |
|
5654 | 29 |
procedure ResetLand; |
1806 | 30 |
|
31 |
implementation |
|
4403 | 32 |
uses uConsts, GLunit, uTypes, uVariables, uTextures, uDebug, uRender; |
1807 | 33 |
|
6383
c34a8b98d78c
increase land tex size to 512, which is the current minimum required just to load a hat. On my system max fps rose from 840 to 890 - about 6% change.
nemo
parents:
6382
diff
changeset
|
34 |
const TEXSIZE = 512; |
1806 | 35 |
|
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
36 |
type TLandRecord = record |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
37 |
shouldUpdate: boolean; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
38 |
tex: PTexture; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
39 |
end; |
3615 | 40 |
|
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
41 |
var LandTextures: array of array of TLandRecord; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
42 |
tmpPixels: array [0..TEXSIZE - 1, 0..TEXSIZE - 1] of LongWord; |
3615 | 43 |
LANDTEXARW: LongWord; |
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
44 |
LANDTEXARH: LongWord; |
3697 | 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 |
|
3595
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
68 |
if (Width <= 0) or (Height <= 0) then exit; |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
69 |
TryDo((X >= 0) and (X < LAND_WIDTH), 'UpdateLandTexture: wrong X parameter', true); |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
70 |
TryDo(X + Width <= LAND_WIDTH, 'UpdateLandTexture: wrong Width parameter', true); |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
71 |
TryDo((Y >= 0) and (Y < LAND_HEIGHT), 'UpdateLandTexture: wrong Y parameter', true); |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
72 |
TryDo(Y + Height <= LAND_HEIGHT, 'UpdateLandTexture: wrong Height parameter', true); |
1806 | 73 |
|
3595
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
74 |
if (cReducedQuality and rqBlurryLand) = 0 then |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
75 |
for ty:= Y div TEXSIZE to (Y + Height - 1) div TEXSIZE do |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
76 |
for tx:= X div TEXSIZE to (X + Width - 1) div TEXSIZE do |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
77 |
LandTextures[tx, ty].shouldUpdate:= true |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
78 |
else |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
79 |
for ty:= (Y div TEXSIZE) div 2 to ((Y + Height - 1) div TEXSIZE) div 2 do |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
80 |
for tx:= (X div TEXSIZE) div 2 to ((X + Width - 1) div TEXSIZE) div 2 do |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
81 |
LandTextures[tx, ty].shouldUpdate:= true; |
1806 | 82 |
end; |
83 |
||
84 |
procedure RealLandTexUpdate; |
|
1807 | 85 |
var x, y: LongWord; |
1806 | 86 |
begin |
1807 | 87 |
if LandTextures[0, 0].tex = nil then |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
88 |
for x:= 0 to LANDTEXARW -1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
89 |
for y:= 0 to LANDTEXARH - 1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
90 |
with LandTextures[x, y] do |
3491 | 91 |
begin |
92 |
tex:= NewTexture(TEXSIZE, TEXSIZE, Pixels(x, y)); |
|
93 |
glBindTexture(GL_TEXTURE_2D, tex^.id); |
|
94 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, tpHigh); |
|
95 |
end |
|
1806 | 96 |
else |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
97 |
for x:= 0 to LANDTEXARW -1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
98 |
for y:= 0 to LANDTEXARH - 1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
99 |
with LandTextures[x, y] do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
100 |
if shouldUpdate then |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
101 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
102 |
shouldUpdate:= false; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
103 |
glBindTexture(GL_TEXTURE_2D, tex^.id); |
3697 | 104 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXSIZE, TEXSIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, Pixels(x,y)); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
105 |
end |
1806 | 106 |
end; |
107 |
||
1807 | 108 |
procedure DrawLand(dX, dY: LongInt); |
109 |
var x, y: LongInt; |
|
1806 | 110 |
begin |
111 |
RealLandTexUpdate; |
|
1807 | 112 |
|
113 |
for x:= 0 to LANDTEXARW -1 do |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
114 |
for y:= 0 to LANDTEXARH - 1 do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
115 |
with LandTextures[x, y] do |
3595
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
116 |
if (cReducedQuality and rqBlurryLand) = 0 then |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
117 |
DrawTexture(dX + x * TEXSIZE, dY + y * TEXSIZE, tex) |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
118 |
else |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
119 |
DrawTexture(dX + x * TEXSIZE * 2, dY + y * TEXSIZE * 2, tex, 2.0) |
341e407e3754
partially removing DOWNSCALE ifdef -- only two remain and their removal requires dynamic allocation (btw this breaks low quality mode)
koda
parents:
3513
diff
changeset
|
120 |
|
1807 | 121 |
end; |
122 |
||
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
123 |
procedure initModule; |
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
124 |
begin |
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
125 |
if (cReducedQuality and rqBlurryLand) = 0 then |
5654 | 126 |
begin |
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
127 |
LANDTEXARW:= LAND_WIDTH div TEXSIZE; |
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
128 |
LANDTEXARH:= LAND_HEIGHT div TEXSIZE; |
5654 | 129 |
end |
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
130 |
else |
5654 | 131 |
begin |
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
132 |
LANDTEXARW:= (LAND_WIDTH div TEXSIZE) div 2; |
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
133 |
LANDTEXARH:= (LAND_HEIGHT div TEXSIZE) div 2; |
5654 | 134 |
end; |
3697 | 135 |
|
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
136 |
SetLength(LandTextures, LANDTEXARW, LANDTEXARH); |
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
137 |
end; |
3697 | 138 |
|
5654 | 139 |
procedure ResetLand; |
3615 | 140 |
var x, y: LongInt; |
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
141 |
begin |
6382
0e76c5cd4250
move the order of reloading texture to workaround buggy drivers
koda
parents:
6380
diff
changeset
|
142 |
for x:= 0 to LANDTEXARW - 1 do |
3615 | 143 |
for y:= 0 to LANDTEXARH - 1 do |
144 |
with LandTextures[x, y] do |
|
5654 | 145 |
begin |
6380
1ff5ad1d771b
Remove a bunch of unnecessary nil checks. FreeTexture does its own nil check.
nemo
parents:
5654
diff
changeset
|
146 |
FreeTexture(tex); |
3615 | 147 |
tex:= nil; |
5654 | 148 |
end; |
149 |
end; |
|
150 |
||
151 |
procedure freeModule; |
|
152 |
begin |
|
153 |
ResetLand; |
|
3615 | 154 |
if LandBackSurface <> nil then |
155 |
SDL_FreeSurface(LandBackSurface); |
|
156 |
LandBackSurface:= nil; |
|
3612
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
157 |
LandTextures:= nil; |
b50215a8a43d
land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents:
3595
diff
changeset
|
158 |
end; |
1806 | 159 |
end. |