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 |
|
|
134 |
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
|
|
135 |
begin
|
|
136 |
tw:= toPowerOf2(Surf^.w);
|
|
137 |
th:= toPowerOf2(Surf^.h);
|
|
138 |
|
|
139 |
Surface2Tex^.rx:= Surf^.w / tw;
|
|
140 |
Surface2Tex^.ry:= Surf^.h / th;
|
|
141 |
|
|
142 |
GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel);
|
|
143 |
|
|
144 |
fromP4:= Surf^.pixels;
|
|
145 |
toP4:= tmpp;
|
|
146 |
|
|
147 |
for y:= 0 to Pred(Surf^.h) do
|
|
148 |
begin
|
|
149 |
for x:= 0 to Pred(Surf^.w) do toP4^[x]:= fromP4^[x];
|
|
150 |
for x:= Surf^.w to Pred(tw) do toP4^[x]:= 0;
|
|
151 |
toP4:= @(toP4^[tw]);
|
|
152 |
fromP4:= @(fromP4^[Surf^.pitch div 4])
|
|
153 |
end;
|
|
154 |
|
|
155 |
for y:= Surf^.h to Pred(th) do
|
|
156 |
begin
|
|
157 |
for x:= 0 to Pred(tw) do toP4^[x]:= 0;
|
|
158 |
toP4:= @(toP4^[tw])
|
|
159 |
end;
|
|
160 |
|
|
161 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp);
|
|
162 |
|
|
163 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
|
|
164 |
end
|
|
165 |
else
|
|
166 |
begin
|
|
167 |
Surface2Tex^.rx:= 1.0;
|
|
168 |
Surface2Tex^.ry:= 1.0;
|
|
169 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels);
|
|
170 |
end;
|
|
171 |
|
|
172 |
ResetVertexArrays(Surface2Tex);
|
|
173 |
|
|
174 |
if SDL_MustLock(surf) then
|
|
175 |
SDL_UnlockSurface(surf);
|
|
176 |
|
|
177 |
SetTextureParameters(enableClamp);
|
|
178 |
end;
|
|
179 |
|
4901
|
180 |
// deletes texture and frees the memory allocated for it.
|
|
181 |
// if nil is passed nothing is done
|
4375
|
182 |
procedure FreeTexture(tex: PTexture);
|
|
183 |
begin
|
|
184 |
if tex <> nil then
|
|
185 |
begin
|
|
186 |
if tex^.NextTexture <> nil then
|
|
187 |
tex^.NextTexture^.PrevTexture:= tex^.PrevTexture;
|
|
188 |
if tex^.PrevTexture <> nil then
|
|
189 |
tex^.PrevTexture^.NextTexture:= tex^.NextTexture
|
|
190 |
else
|
|
191 |
TextureList:= tex^.NextTexture;
|
|
192 |
glDeleteTextures(1, @tex^.id);
|
|
193 |
Dispose(tex);
|
|
194 |
end
|
|
195 |
end;
|
|
196 |
|
|
197 |
procedure initModule;
|
|
198 |
begin
|
|
199 |
TextureList:= nil;
|
|
200 |
end;
|
|
201 |
|
|
202 |
procedure freeModule;
|
|
203 |
begin
|
|
204 |
while TextureList <> nil do FreeTexture(TextureList);
|
|
205 |
end;
|
|
206 |
|
4901
|
207 |
end.
|