author | Wolfgang Steffens <WolfgangSteff@gmail.com> |
Tue, 05 Jun 2012 22:17:06 +0200 | |
changeset 7186 | 013deb83086b |
parent 7080 | dbf43c07a507 |
parent 7151 | ec15d9e1a7e3 |
child 7291 | ad4b6c2b09e8 |
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); |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
29 |
procedure ComputeTexcoords(texture: PTexture; r: PSDL_Rect; tb: PVertexRect); |
4375 | 30 |
|
31 |
procedure initModule; |
|
32 |
procedure freeModule; |
|
33 |
||
34 |
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
|
35 |
uses GLunit, uUtils, uVariables, uConsts, uDebug, uConsole; |
4375 | 36 |
|
37 |
var TextureList: PTexture; |
|
38 |
||
39 |
||
40 |
procedure SetTextureParameters(enableClamp: Boolean); |
|
41 |
begin |
|
42 |
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
|
43 |
begin |
4375 | 44 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
45 |
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
|
46 |
end; |
4375 | 47 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
48 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
|
49 |
end; |
|
50 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
51 |
procedure ComputeTexcoords(texture: PTexture; r: PSDL_Rect; tb: PVertexRect); |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
52 |
var x0, y0, x1, y1: Real; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
53 |
w, h, aw, ah: LongInt; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
54 |
const texelOffset = 0.0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
55 |
begin |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
56 |
aw:=texture^.atlas^.w; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
57 |
ah:=texture^.atlas^.h; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
58 |
if texture^.isRotated then |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
59 |
begin |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
60 |
w:=r^.h; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
61 |
h:=r^.w; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
62 |
end |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
63 |
else |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
64 |
begin |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
65 |
w:=r^.w; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
66 |
h:=r^.h; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
67 |
end; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
68 |
|
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
69 |
x0:= (r^.x + texelOffset)/aw; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
70 |
x1:= (r^.x + w - texelOffset)/aw; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
71 |
y0:= (r^.y + texelOffset)/ah; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
72 |
y1:= (r^.y + h - texelOffset)/ah; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
73 |
|
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
74 |
tb^[0].X:= x0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
75 |
tb^[0].Y:= y0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
76 |
tb^[1].X:= x1; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
77 |
tb^[1].Y:= y0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
78 |
tb^[2].X:= x1; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
79 |
tb^[2].Y:= y1; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
80 |
tb^[3].X:= x0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
81 |
tb^[3].Y:= y1 |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
82 |
end; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
83 |
|
4375 | 84 |
procedure ResetVertexArrays(texture: PTexture); |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
85 |
var r: TSDL_Rect; |
4375 | 86 |
begin |
87 |
with texture^ do |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
88 |
begin |
4375 | 89 |
vb[0].X:= 0; |
90 |
vb[0].Y:= 0; |
|
91 |
vb[1].X:= w; |
|
92 |
vb[1].Y:= 0; |
|
93 |
vb[2].X:= w; |
|
94 |
vb[2].Y:= h; |
|
95 |
vb[3].X:= 0; |
|
96 |
vb[3].Y:= h; |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
97 |
end; |
4375 | 98 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
99 |
r.x:= 0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
100 |
r.y:= 0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
101 |
r.w:= texture^.w; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
102 |
r.h:= texture^.h; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
103 |
ComputeTexcoords(texture, @r, @texture^.tb); |
4375 | 104 |
end; |
105 |
||
106 |
function NewTexture(width, height: Longword; buf: Pointer): PTexture; |
|
107 |
begin |
|
108 |
new(NewTexture); |
|
109 |
NewTexture^.PrevTexture:= nil; |
|
110 |
NewTexture^.NextTexture:= nil; |
|
111 |
NewTexture^.Scale:= 1; |
|
112 |
if TextureList <> nil then |
|
113 |
begin |
|
114 |
TextureList^.PrevTexture:= NewTexture; |
|
115 |
NewTexture^.NextTexture:= TextureList |
|
116 |
end; |
|
117 |
TextureList:= NewTexture; |
|
118 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
119 |
|
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
120 |
// Atlas allocation happens here later on. For now we just allocate one exclusive atlas per sprite |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
121 |
new(NewTexture^.atlas); |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
122 |
NewTexture^.atlas^.w:=width; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
123 |
NewTexture^.atlas^.h:=height; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
124 |
NewTexture^.x:=0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
125 |
NewTexture^.y:=0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
126 |
NewTexture^.w:=width; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
127 |
NewTexture^.h:=height; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
128 |
NewTexture^.isRotated:=false; |
4375 | 129 |
|
130 |
ResetVertexArrays(NewTexture); |
|
131 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
132 |
glGenTextures(1, @NewTexture^.atlas^.id); |
4375 | 133 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
134 |
glBindTexture(GL_TEXTURE_2D, NewTexture^.atlas^.id); |
4375 | 135 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); |
136 |
||
137 |
SetTextureParameters(true); |
|
138 |
end; |
|
139 |
||
6303 | 140 |
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
|
141 |
var tw, x, y: Longword; |
6303 | 142 |
fromP4: PLongWordArray; |
143 |
begin |
|
144 |
fromP4:= Surf^.pixels; |
|
145 |
for y:= 0 to Pred(Surf^.h) do |
|
146 |
begin |
|
147 |
for x:= 0 to Pred(Surf^.w) do |
|
148 |
begin |
|
149 |
tw:= fromP4^[x]; |
|
150 |
tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED + |
|
151 |
(tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + |
|
152 |
(tw shr BShift and $FF) * RGB_LUMINANCE_BLUE); |
|
153 |
if tw > 255 then tw:= 255; |
|
154 |
tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask); |
|
155 |
fromP4^[x]:= tw; |
|
156 |
end; |
|
157 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
158 |
end; |
|
159 |
end; |
|
6467 | 160 |
|
161 |
||
4375 | 162 |
function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
163 |
var tw, th, x, y: Longword; |
|
164 |
tmpp: pointer; |
|
165 |
fromP4, toP4: PLongWordArray; |
|
166 |
begin |
|
167 |
new(Surface2Tex); |
|
168 |
Surface2Tex^.PrevTexture:= nil; |
|
169 |
Surface2Tex^.NextTexture:= nil; |
|
170 |
if TextureList <> nil then |
|
171 |
begin |
|
172 |
TextureList^.PrevTexture:= Surface2Tex; |
|
173 |
Surface2Tex^.NextTexture:= TextureList |
|
174 |
end; |
|
175 |
TextureList:= Surface2Tex; |
|
176 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
177 |
// Atlas allocation happens here later on. For now we just allocate one exclusive atlas per sprite |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
178 |
new(Surface2Tex^.atlas); |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
179 |
|
4375 | 180 |
Surface2Tex^.w:= surf^.w; |
181 |
Surface2Tex^.h:= surf^.h; |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
182 |
Surface2Tex^.x:=0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
183 |
Surface2Tex^.y:=0; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
184 |
Surface2Tex^.isRotated:=false; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
185 |
|
4375 | 186 |
|
187 |
if (surf^.format^.BytesPerPixel <> 4) then |
|
188 |
begin |
|
189 |
TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true); |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
190 |
Surface2Tex^.atlas^.id:= 0; |
4375 | 191 |
exit |
192 |
end; |
|
193 |
||
194 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
195 |
glGenTextures(1, @Surface2Tex^.atlas^.id); |
4375 | 196 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
197 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.atlas^.id); |
4375 | 198 |
|
199 |
if SDL_MustLock(surf) then |
|
200 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
201 |
||
5441
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
202 |
fromP4:= Surf^.pixels; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
203 |
|
6982 | 204 |
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
|
205 |
Surface2GrayScale(Surf); |
6303 | 206 |
|
4375 | 207 |
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
208 |
begin |
|
209 |
tw:= toPowerOf2(Surf^.w); |
|
210 |
th:= toPowerOf2(Surf^.h); |
|
211 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
212 |
Surface2Tex^.atlas^.w:=tw; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
213 |
Surface2Tex^.atlas^.h:=th; |
4375 | 214 |
|
7151 | 215 |
tmpp:= GetMem(tw * th * surf^.format^.BytesPerPixel); |
4375 | 216 |
|
217 |
fromP4:= Surf^.pixels; |
|
218 |
toP4:= tmpp; |
|
219 |
||
220 |
for y:= 0 to Pred(Surf^.h) do |
|
221 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
222 |
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
|
223 |
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
|
224 |
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
|
225 |
toP4^[x]:= 0; |
4375 | 226 |
toP4:= @(toP4^[tw]); |
227 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
228 |
end; |
|
229 |
||
230 |
for y:= Surf^.h to Pred(th) do |
|
231 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
232 |
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
|
233 |
toP4^[x]:= 0; |
4375 | 234 |
toP4:= @(toP4^[tw]) |
235 |
end; |
|
236 |
||
237 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
|
238 |
||
239 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
240 |
end |
|
241 |
else |
|
242 |
begin |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
243 |
Surface2Tex^.atlas^.w:=Surf^.w; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
244 |
Surface2Tex^.atlas^.h:=Surf^.h; |
4375 | 245 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
246 |
end; |
|
247 |
||
248 |
ResetVertexArrays(Surface2Tex); |
|
249 |
||
250 |
if SDL_MustLock(surf) then |
|
251 |
SDL_UnlockSurface(surf); |
|
252 |
||
253 |
SetTextureParameters(enableClamp); |
|
254 |
end; |
|
255 |
||
4901 | 256 |
// deletes texture and frees the memory allocated for it. |
257 |
// if nil is passed nothing is done |
|
4375 | 258 |
procedure FreeTexture(tex: PTexture); |
259 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
260 |
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
|
261 |
begin |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
262 |
// Atlas cleanup happens here later on. For now we just free as each sprite has one atlas |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
263 |
Dispose(tex^.atlas); |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
264 |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
265 |
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
|
266 |
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
|
267 |
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
|
268 |
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
|
269 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
270 |
TextureList:= tex^.NextTexture; |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
271 |
glDeleteTextures(1, @tex^.atlas^.id); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
272 |
Dispose(tex); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
273 |
end |
4375 | 274 |
end; |
275 |
||
276 |
procedure initModule; |
|
277 |
begin |
|
278 |
TextureList:= nil; |
|
279 |
end; |
|
280 |
||
281 |
procedure freeModule; |
|
282 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
begin |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
287 |
AddFileLog('Sprite not freed: width='+inttostr(LongInt(TextureList^.w))+' height='+inttostr(LongInt(TextureList^.h))+' priority='+inttostr(round(TextureList^.atlas^.priority*1000))); |
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
|
288 |
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
|
289 |
end |
4375 | 290 |
end; |
291 |
||
4901 | 292 |
end. |