author | RodolfoRG |
Mon, 22 Jul 2013 18:16:01 +0200 | |
changeset 9366 | 4d0128c9cb35 |
parent 9080 | 9b42757d7e71 |
child 9655 | e154ccca4dad |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
9080 | 3 |
* Copyright (c) 2004-2013 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 |
|
8027
e5ba3dd12531
make stats-only mode work headless. also skip a few things to save time/memory.
nemo
parents:
7151
diff
changeset
|
129 |
if cOnlyStats then exit(nil); |
4375 | 130 |
new(Surface2Tex); |
131 |
Surface2Tex^.PrevTexture:= nil; |
|
132 |
Surface2Tex^.NextTexture:= nil; |
|
133 |
if TextureList <> nil then |
|
134 |
begin |
|
135 |
TextureList^.PrevTexture:= Surface2Tex; |
|
136 |
Surface2Tex^.NextTexture:= TextureList |
|
137 |
end; |
|
138 |
TextureList:= Surface2Tex; |
|
139 |
||
140 |
Surface2Tex^.w:= surf^.w; |
|
141 |
Surface2Tex^.h:= surf^.h; |
|
142 |
||
143 |
if (surf^.format^.BytesPerPixel <> 4) then |
|
144 |
begin |
|
145 |
TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true); |
|
146 |
Surface2Tex^.id:= 0; |
|
147 |
exit |
|
148 |
end; |
|
149 |
||
150 |
||
151 |
glGenTextures(1, @Surface2Tex^.id); |
|
152 |
||
153 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
|
154 |
||
155 |
if SDL_MustLock(surf) then |
|
156 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
157 |
||
5441
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
158 |
fromP4:= Surf^.pixels; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
159 |
|
6982 | 160 |
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
|
161 |
Surface2GrayScale(Surf); |
6303 | 162 |
|
4375 | 163 |
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
164 |
begin |
|
165 |
tw:= toPowerOf2(Surf^.w); |
|
166 |
th:= toPowerOf2(Surf^.h); |
|
167 |
||
168 |
Surface2Tex^.rx:= Surf^.w / tw; |
|
169 |
Surface2Tex^.ry:= Surf^.h / th; |
|
170 |
||
7151 | 171 |
tmpp:= GetMem(tw * th * surf^.format^.BytesPerPixel); |
4375 | 172 |
|
173 |
fromP4:= Surf^.pixels; |
|
174 |
toP4:= tmpp; |
|
175 |
||
176 |
for y:= 0 to Pred(Surf^.h) do |
|
177 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
toP4^[x]:= 0; |
4375 | 182 |
toP4:= @(toP4^[tw]); |
183 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
184 |
end; |
|
185 |
||
186 |
for y:= Surf^.h to Pred(th) do |
|
187 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
188 |
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
|
189 |
toP4^[x]:= 0; |
4375 | 190 |
toP4:= @(toP4^[tw]) |
191 |
end; |
|
192 |
||
193 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
|
194 |
||
195 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
196 |
end |
|
197 |
else |
|
198 |
begin |
|
199 |
Surface2Tex^.rx:= 1.0; |
|
200 |
Surface2Tex^.ry:= 1.0; |
|
201 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
|
202 |
end; |
|
203 |
||
204 |
ResetVertexArrays(Surface2Tex); |
|
205 |
||
206 |
if SDL_MustLock(surf) then |
|
207 |
SDL_UnlockSurface(surf); |
|
208 |
||
209 |
SetTextureParameters(enableClamp); |
|
210 |
end; |
|
211 |
||
4901 | 212 |
// deletes texture and frees the memory allocated for it. |
213 |
// if nil is passed nothing is done |
|
4375 | 214 |
procedure FreeTexture(tex: PTexture); |
215 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
216 |
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
|
217 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
218 |
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
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
223 |
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
|
224 |
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
|
225 |
Dispose(tex); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
226 |
end |
4375 | 227 |
end; |
228 |
||
229 |
procedure initModule; |
|
230 |
begin |
|
231 |
TextureList:= nil; |
|
232 |
end; |
|
233 |
||
234 |
procedure freeModule; |
|
235 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
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
|
242 |
end |
4375 | 243 |
end; |
244 |
||
4901 | 245 |
end. |