author | unc0rr |
Wed, 02 May 2012 22:12:58 +0400 | |
changeset 7001 | 89488f5b99ca |
parent 6982 | 8d41d22a291d |
child 7080 | dbf43c07a507 |
child 7151 | ec15d9e1a7e3 |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
6700 | 3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
4976 | 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 |
||
4375 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
21 |
unit uTextures; |
|
22 |
interface |
|
23 |
uses SDLh, uTypes; |
|
24 |
||
25 |
function NewTexture(width, height: Longword; buf: Pointer): PTexture; |
|
6303 | 26 |
procedure Surface2GrayScale(surf: PSDL_Surface); |
4375 | 27 |
function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
28 |
procedure FreeTexture(tex: PTexture); |
|
29 |
||
30 |
procedure initModule; |
|
31 |
procedure freeModule; |
|
32 |
||
33 |
implementation |
|
6394
f0a9042e7387
yay, finally osx (and likely windows) fullscreen switch works like on linux! ALL textures had to be destroyed and recreated only after the new window got created. In other news, the new window must be cleaned with glClear to skip a first frame of garbage and AddProgress is only called the first time.
koda
parents:
6390
diff
changeset
|
34 |
uses GLunit, uUtils, uVariables, uConsts, uDebug, uConsole; |
4375 | 35 |
|
36 |
var TextureList: PTexture; |
|
37 |
||
38 |
||
39 |
procedure SetTextureParameters(enableClamp: Boolean); |
|
40 |
begin |
|
41 |
if enableClamp and ((cReducedQuality and rqClampLess) = 0) then |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
42 |
begin |
4375 | 43 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
44 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
45 |
end; |
4375 | 46 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
47 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
|
48 |
end; |
|
49 |
||
50 |
procedure ResetVertexArrays(texture: PTexture); |
|
51 |
begin |
|
52 |
with texture^ do |
|
53 |
begin |
|
54 |
vb[0].X:= 0; |
|
55 |
vb[0].Y:= 0; |
|
56 |
vb[1].X:= w; |
|
57 |
vb[1].Y:= 0; |
|
58 |
vb[2].X:= w; |
|
59 |
vb[2].Y:= h; |
|
60 |
vb[3].X:= 0; |
|
61 |
vb[3].Y:= h; |
|
62 |
||
63 |
tb[0].X:= 0; |
|
64 |
tb[0].Y:= 0; |
|
65 |
tb[1].X:= rx; |
|
66 |
tb[1].Y:= 0; |
|
67 |
tb[2].X:= rx; |
|
68 |
tb[2].Y:= ry; |
|
69 |
tb[3].X:= 0; |
|
70 |
tb[3].Y:= ry |
|
71 |
end; |
|
72 |
end; |
|
73 |
||
74 |
function NewTexture(width, height: Longword; buf: Pointer): PTexture; |
|
75 |
begin |
|
76 |
new(NewTexture); |
|
77 |
NewTexture^.PrevTexture:= nil; |
|
78 |
NewTexture^.NextTexture:= nil; |
|
79 |
NewTexture^.Scale:= 1; |
|
80 |
if TextureList <> nil then |
|
81 |
begin |
|
82 |
TextureList^.PrevTexture:= NewTexture; |
|
83 |
NewTexture^.NextTexture:= TextureList |
|
84 |
end; |
|
85 |
TextureList:= NewTexture; |
|
86 |
||
87 |
NewTexture^.w:= width; |
|
88 |
NewTexture^.h:= height; |
|
89 |
NewTexture^.rx:= 1.0; |
|
90 |
NewTexture^.ry:= 1.0; |
|
91 |
||
92 |
ResetVertexArrays(NewTexture); |
|
93 |
||
94 |
glGenTextures(1, @NewTexture^.id); |
|
95 |
||
96 |
glBindTexture(GL_TEXTURE_2D, NewTexture^.id); |
|
97 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); |
|
98 |
||
99 |
SetTextureParameters(true); |
|
100 |
end; |
|
101 |
||
6303 | 102 |
procedure Surface2GrayScale(surf: PSDL_Surface); |
6305
5f7480c2a08d
Set default water colours in greyscale mode in case the theme does not define them, decrement piano weapon on use
nemo
parents:
6303
diff
changeset
|
103 |
var tw, x, y: Longword; |
6303 | 104 |
fromP4: PLongWordArray; |
105 |
begin |
|
106 |
fromP4:= Surf^.pixels; |
|
107 |
for y:= 0 to Pred(Surf^.h) do |
|
108 |
begin |
|
109 |
for x:= 0 to Pred(Surf^.w) do |
|
110 |
begin |
|
111 |
tw:= fromP4^[x]; |
|
112 |
tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED + |
|
113 |
(tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + |
|
114 |
(tw shr BShift and $FF) * RGB_LUMINANCE_BLUE); |
|
115 |
if tw > 255 then tw:= 255; |
|
116 |
tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask); |
|
117 |
fromP4^[x]:= tw; |
|
118 |
end; |
|
119 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
120 |
end; |
|
121 |
end; |
|
6467 | 122 |
|
123 |
||
4375 | 124 |
function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
125 |
var tw, th, x, y: Longword; |
|
126 |
tmpp: pointer; |
|
127 |
fromP4, toP4: PLongWordArray; |
|
128 |
begin |
|
129 |
new(Surface2Tex); |
|
130 |
Surface2Tex^.PrevTexture:= nil; |
|
131 |
Surface2Tex^.NextTexture:= nil; |
|
132 |
if TextureList <> nil then |
|
133 |
begin |
|
134 |
TextureList^.PrevTexture:= Surface2Tex; |
|
135 |
Surface2Tex^.NextTexture:= TextureList |
|
136 |
end; |
|
137 |
TextureList:= Surface2Tex; |
|
138 |
||
139 |
Surface2Tex^.w:= surf^.w; |
|
140 |
Surface2Tex^.h:= surf^.h; |
|
141 |
||
142 |
if (surf^.format^.BytesPerPixel <> 4) then |
|
143 |
begin |
|
144 |
TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true); |
|
145 |
Surface2Tex^.id:= 0; |
|
146 |
exit |
|
147 |
end; |
|
148 |
||
149 |
||
150 |
glGenTextures(1, @Surface2Tex^.id); |
|
151 |
||
152 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
|
153 |
||
154 |
if SDL_MustLock(surf) then |
|
155 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
156 |
||
5441
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
157 |
fromP4:= Surf^.pixels; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
158 |
|
6982 | 159 |
if GrayScale then |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
160 |
Surface2GrayScale(Surf); |
6303 | 161 |
|
4375 | 162 |
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
163 |
begin |
|
164 |
tw:= toPowerOf2(Surf^.w); |
|
165 |
th:= toPowerOf2(Surf^.h); |
|
166 |
||
167 |
Surface2Tex^.rx:= Surf^.w / tw; |
|
168 |
Surface2Tex^.ry:= Surf^.h / th; |
|
169 |
||
170 |
GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
|
171 |
||
172 |
fromP4:= Surf^.pixels; |
|
173 |
toP4:= tmpp; |
|
174 |
||
175 |
for y:= 0 to Pred(Surf^.h) do |
|
176 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
177 |
for x:= 0 to Pred(Surf^.w) do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
178 |
toP4^[x]:= fromP4^[x]; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
179 |
for x:= Surf^.w to Pred(tw) do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
180 |
toP4^[x]:= 0; |
4375 | 181 |
toP4:= @(toP4^[tw]); |
182 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
183 |
end; |
|
184 |
||
185 |
for y:= Surf^.h to Pred(th) do |
|
186 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
187 |
for x:= 0 to Pred(tw) do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
188 |
toP4^[x]:= 0; |
4375 | 189 |
toP4:= @(toP4^[tw]) |
190 |
end; |
|
191 |
||
192 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
|
193 |
||
194 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
195 |
end |
|
196 |
else |
|
197 |
begin |
|
198 |
Surface2Tex^.rx:= 1.0; |
|
199 |
Surface2Tex^.ry:= 1.0; |
|
200 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
|
201 |
end; |
|
202 |
||
203 |
ResetVertexArrays(Surface2Tex); |
|
204 |
||
205 |
if SDL_MustLock(surf) then |
|
206 |
SDL_UnlockSurface(surf); |
|
207 |
||
208 |
SetTextureParameters(enableClamp); |
|
209 |
end; |
|
210 |
||
4901 | 211 |
// deletes texture and frees the memory allocated for it. |
212 |
// if nil is passed nothing is done |
|
4375 | 213 |
procedure FreeTexture(tex: PTexture); |
214 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
215 |
if tex <> nil then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
216 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
217 |
if tex^.NextTexture <> nil then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
218 |
tex^.NextTexture^.PrevTexture:= tex^.PrevTexture; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
219 |
if tex^.PrevTexture <> nil then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
220 |
tex^.PrevTexture^.NextTexture:= tex^.NextTexture |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
221 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
222 |
TextureList:= tex^.NextTexture; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
223 |
glDeleteTextures(1, @tex^.id); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
224 |
Dispose(tex); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
225 |
end |
4375 | 226 |
end; |
227 |
||
228 |
procedure initModule; |
|
229 |
begin |
|
230 |
TextureList:= nil; |
|
231 |
end; |
|
232 |
||
233 |
procedure freeModule; |
|
234 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
235 |
if TextureList <> nil then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
236 |
WriteToConsole('FIXME FIXME FIXME. App shutdown without full cleanup of texture list; read game0.log and please report this problem'); |
6390
3807d4cad077
This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents:
6380
diff
changeset
|
237 |
while TextureList <> nil do |
3807d4cad077
This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents:
6380
diff
changeset
|
238 |
begin |
3807d4cad077
This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents:
6380
diff
changeset
|
239 |
AddFileLog('Texture not freed: width='+inttostr(LongInt(TextureList^.w))+' height='+inttostr(LongInt(TextureList^.h))+' priority='+inttostr(round(TextureList^.priority*1000))); |
3807d4cad077
This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents:
6380
diff
changeset
|
240 |
FreeTexture(TextureList); |
3807d4cad077
This should have been added before. add log spew if this ever happens. We should hopefully identify the various circumstances and make sure it is all cleaned up so the list becomes unnecessary.
nemo
parents:
6380
diff
changeset
|
241 |
end |
4375 | 242 |
end; |
243 |
||
4901 | 244 |
end. |