author | nemo |
Wed, 31 Aug 2011 11:08:03 -0400 | |
changeset 5716 | 9505a6f3fe05 |
parent 5441 | 39962b855540 |
child 6303 | 3edb3c857995 |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2004-2011 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 |
||
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; |
|
26 |
function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
|
27 |
procedure FreeTexture(tex: PTexture); |
|
28 |
||
29 |
procedure initModule; |
|
30 |
procedure freeModule; |
|
31 |
||
32 |
implementation |
|
4403 | 33 |
uses GLunit, uUtils, uVariables, uConsts, uDebug; |
4375 | 34 |
|
35 |
var TextureList: PTexture; |
|
36 |
||
37 |
||
38 |
procedure SetTextureParameters(enableClamp: Boolean); |
|
39 |
begin |
|
40 |
if enableClamp and ((cReducedQuality and rqClampLess) = 0) then |
|
41 |
begin |
|
42 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|
43 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) |
|
44 |
end; |
|
45 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
46 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
|
47 |
end; |
|
48 |
||
49 |
procedure ResetVertexArrays(texture: PTexture); |
|
50 |
begin |
|
51 |
with texture^ do |
|
52 |
begin |
|
53 |
vb[0].X:= 0; |
|
54 |
vb[0].Y:= 0; |
|
55 |
vb[1].X:= w; |
|
56 |
vb[1].Y:= 0; |
|
57 |
vb[2].X:= w; |
|
58 |
vb[2].Y:= h; |
|
59 |
vb[3].X:= 0; |
|
60 |
vb[3].Y:= h; |
|
61 |
||
62 |
tb[0].X:= 0; |
|
63 |
tb[0].Y:= 0; |
|
64 |
tb[1].X:= rx; |
|
65 |
tb[1].Y:= 0; |
|
66 |
tb[2].X:= rx; |
|
67 |
tb[2].Y:= ry; |
|
68 |
tb[3].X:= 0; |
|
69 |
tb[3].Y:= ry |
|
70 |
end; |
|
71 |
end; |
|
72 |
||
73 |
function NewTexture(width, height: Longword; buf: Pointer): PTexture; |
|
74 |
begin |
|
75 |
new(NewTexture); |
|
76 |
NewTexture^.PrevTexture:= nil; |
|
77 |
NewTexture^.NextTexture:= nil; |
|
78 |
NewTexture^.Scale:= 1; |
|
79 |
if TextureList <> nil then |
|
80 |
begin |
|
81 |
TextureList^.PrevTexture:= NewTexture; |
|
82 |
NewTexture^.NextTexture:= TextureList |
|
83 |
end; |
|
84 |
TextureList:= NewTexture; |
|
85 |
||
86 |
NewTexture^.w:= width; |
|
87 |
NewTexture^.h:= height; |
|
88 |
NewTexture^.rx:= 1.0; |
|
89 |
NewTexture^.ry:= 1.0; |
|
90 |
||
91 |
ResetVertexArrays(NewTexture); |
|
92 |
||
93 |
glGenTextures(1, @NewTexture^.id); |
|
94 |
||
95 |
glBindTexture(GL_TEXTURE_2D, NewTexture^.id); |
|
96 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); |
|
97 |
||
98 |
SetTextureParameters(true); |
|
99 |
end; |
|
100 |
||
101 |
function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
|
102 |
var tw, th, x, y: Longword; |
|
103 |
tmpp: pointer; |
|
104 |
fromP4, toP4: PLongWordArray; |
|
105 |
begin |
|
106 |
new(Surface2Tex); |
|
107 |
Surface2Tex^.PrevTexture:= nil; |
|
108 |
Surface2Tex^.NextTexture:= nil; |
|
109 |
if TextureList <> nil then |
|
110 |
begin |
|
111 |
TextureList^.PrevTexture:= Surface2Tex; |
|
112 |
Surface2Tex^.NextTexture:= TextureList |
|
113 |
end; |
|
114 |
TextureList:= Surface2Tex; |
|
115 |
||
116 |
Surface2Tex^.w:= surf^.w; |
|
117 |
Surface2Tex^.h:= surf^.h; |
|
118 |
||
119 |
if (surf^.format^.BytesPerPixel <> 4) then |
|
120 |
begin |
|
121 |
TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true); |
|
122 |
Surface2Tex^.id:= 0; |
|
123 |
exit |
|
124 |
end; |
|
125 |
||
126 |
||
127 |
glGenTextures(1, @Surface2Tex^.id); |
|
128 |
||
129 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
|
130 |
||
131 |
if SDL_MustLock(surf) then |
|
132 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
133 |
||
5441
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
134 |
fromP4:= Surf^.pixels; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
135 |
|
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
136 |
if cGrayScale then |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
137 |
for y:= 0 to Pred(Surf^.h) do |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
138 |
begin |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
139 |
for x:= 0 to Pred(Surf^.w) do |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
140 |
begin |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
141 |
tw:= fromP4^[x]; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
142 |
tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED + |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
143 |
(tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
144 |
(tw shr BShift and $FF) * RGB_LUMINANCE_BLUE); |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
145 |
if tw > 255 then tw:= 255; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
146 |
tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask); |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
147 |
fromP4^[x]:= tw; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
148 |
end; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
149 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
150 |
end; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
151 |
|
4375 | 152 |
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
153 |
begin |
|
154 |
tw:= toPowerOf2(Surf^.w); |
|
155 |
th:= toPowerOf2(Surf^.h); |
|
156 |
||
157 |
Surface2Tex^.rx:= Surf^.w / tw; |
|
158 |
Surface2Tex^.ry:= Surf^.h / th; |
|
159 |
||
160 |
GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
|
161 |
||
162 |
fromP4:= Surf^.pixels; |
|
163 |
toP4:= tmpp; |
|
164 |
||
165 |
for y:= 0 to Pred(Surf^.h) do |
|
166 |
begin |
|
167 |
for x:= 0 to Pred(Surf^.w) do toP4^[x]:= fromP4^[x]; |
|
168 |
for x:= Surf^.w to Pred(tw) do toP4^[x]:= 0; |
|
169 |
toP4:= @(toP4^[tw]); |
|
170 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
171 |
end; |
|
172 |
||
173 |
for y:= Surf^.h to Pred(th) do |
|
174 |
begin |
|
175 |
for x:= 0 to Pred(tw) do toP4^[x]:= 0; |
|
176 |
toP4:= @(toP4^[tw]) |
|
177 |
end; |
|
178 |
||
179 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
|
180 |
||
181 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
182 |
end |
|
183 |
else |
|
184 |
begin |
|
185 |
Surface2Tex^.rx:= 1.0; |
|
186 |
Surface2Tex^.ry:= 1.0; |
|
187 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
|
188 |
end; |
|
189 |
||
190 |
ResetVertexArrays(Surface2Tex); |
|
191 |
||
192 |
if SDL_MustLock(surf) then |
|
193 |
SDL_UnlockSurface(surf); |
|
194 |
||
195 |
SetTextureParameters(enableClamp); |
|
196 |
end; |
|
197 |
||
4901 | 198 |
// deletes texture and frees the memory allocated for it. |
199 |
// if nil is passed nothing is done |
|
4375 | 200 |
procedure FreeTexture(tex: PTexture); |
201 |
begin |
|
202 |
if tex <> nil then |
|
203 |
begin |
|
204 |
if tex^.NextTexture <> nil then |
|
205 |
tex^.NextTexture^.PrevTexture:= tex^.PrevTexture; |
|
206 |
if tex^.PrevTexture <> nil then |
|
207 |
tex^.PrevTexture^.NextTexture:= tex^.NextTexture |
|
208 |
else |
|
209 |
TextureList:= tex^.NextTexture; |
|
210 |
glDeleteTextures(1, @tex^.id); |
|
211 |
Dispose(tex); |
|
212 |
end |
|
213 |
end; |
|
214 |
||
215 |
procedure initModule; |
|
216 |
begin |
|
217 |
TextureList:= nil; |
|
218 |
end; |
|
219 |
||
220 |
procedure freeModule; |
|
221 |
begin |
|
222 |
while TextureList <> nil do FreeTexture(TextureList); |
|
223 |
end; |
|
224 |
||
4901 | 225 |
end. |