author | Wolfgang Steffens <WolfgangSteff@gmail.com> |
Mon, 25 Jun 2012 16:22:03 +0200 | |
changeset 7301 | bea42438a2ec |
parent 7297 | af64b509725c |
child 7304 | 8b3575750cd2 |
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); |
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
27 |
function Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
4375 | 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 |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
35 |
uses GLunit, uUtils, uVariables, uConsts, uDebug, uConsole, uAtlas; |
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); |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
52 |
var x0, y0, x1, y1, tmp: Real; |
7080
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; |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
58 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
59 |
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
|
60 |
begin |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
end |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
64 |
else |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
65 |
begin |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
66 |
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
|
67 |
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
|
68 |
end; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
69 |
|
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
70 |
x0:= (texture^.x + r^.x + texelOffset)/aw; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
71 |
x1:= (texture^.x + r^.x + w - texelOffset)/aw; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
72 |
y0:= (texture^.y + r^.y + texelOffset)/ah; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
73 |
y1:= (texture^.y + r^.y + h - texelOffset)/ah; |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
74 |
|
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
75 |
if (texture^.isRotated) then |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
76 |
begin |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
77 |
tb^[0].X:= x0; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
78 |
tb^[0].Y:= y0; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
79 |
tb^[3].X:= x1; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
80 |
tb^[3].Y:= y0; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
81 |
tb^[2].X:= x1; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
82 |
tb^[2].Y:= y1; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
83 |
tb^[1].X:= x0; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
84 |
tb^[1].Y:= y1 |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
85 |
end else |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
86 |
begin |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
87 |
tb^[0].X:= x0; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
88 |
tb^[0].Y:= y0; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
89 |
tb^[1].X:= x1; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
90 |
tb^[1].Y:= y0; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
91 |
tb^[2].X:= x1; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
92 |
tb^[2].Y:= y1; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
93 |
tb^[3].X:= x0; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
94 |
tb^[3].Y:= y1; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
95 |
end; |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
96 |
end; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
97 |
|
4375 | 98 |
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
|
99 |
var r: TSDL_Rect; |
4375 | 100 |
begin |
101 |
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
|
102 |
begin |
4375 | 103 |
vb[0].X:= 0; |
104 |
vb[0].Y:= 0; |
|
105 |
vb[1].X:= w; |
|
106 |
vb[1].Y:= 0; |
|
107 |
vb[2].X:= w; |
|
108 |
vb[2].Y:= h; |
|
109 |
vb[3].X:= 0; |
|
110 |
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
|
111 |
end; |
4375 | 112 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
ComputeTexcoords(texture, @r, @texture^.tb); |
4375 | 118 |
end; |
119 |
||
120 |
function NewTexture(width, height: Longword; buf: Pointer): PTexture; |
|
121 |
begin |
|
122 |
new(NewTexture); |
|
123 |
NewTexture^.PrevTexture:= nil; |
|
124 |
NewTexture^.NextTexture:= nil; |
|
125 |
NewTexture^.Scale:= 1; |
|
126 |
if TextureList <> nil then |
|
127 |
begin |
|
128 |
TextureList^.PrevTexture:= NewTexture; |
|
129 |
NewTexture^.NextTexture:= TextureList |
|
130 |
end; |
|
131 |
TextureList:= NewTexture; |
|
132 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
133 |
|
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
134 |
// 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
|
135 |
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
|
136 |
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
|
137 |
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
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
NewTexture^.isRotated:=false; |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
143 |
NewTexture^.shared:=false; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
144 |
NewTexture^.surface:=nil; |
4375 | 145 |
|
146 |
ResetVertexArrays(NewTexture); |
|
147 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
148 |
glGenTextures(1, @NewTexture^.atlas^.id); |
4375 | 149 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
150 |
glBindTexture(GL_TEXTURE_2D, NewTexture^.atlas^.id); |
4375 | 151 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); |
152 |
||
153 |
SetTextureParameters(true); |
|
154 |
end; |
|
155 |
||
6303 | 156 |
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
|
157 |
var tw, x, y: Longword; |
6303 | 158 |
fromP4: PLongWordArray; |
159 |
begin |
|
160 |
fromP4:= Surf^.pixels; |
|
161 |
for y:= 0 to Pred(Surf^.h) do |
|
162 |
begin |
|
163 |
for x:= 0 to Pred(Surf^.w) do |
|
164 |
begin |
|
165 |
tw:= fromP4^[x]; |
|
166 |
tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED + |
|
167 |
(tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + |
|
168 |
(tw shr BShift and $FF) * RGB_LUMINANCE_BLUE); |
|
169 |
if tw > 255 then tw:= 255; |
|
170 |
tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask); |
|
171 |
fromP4^[x]:= tw; |
|
172 |
end; |
|
173 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
174 |
end; |
|
175 |
end; |
|
6467 | 176 |
|
177 |
||
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
178 |
function Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
4375 | 179 |
var tw, th, x, y: Longword; |
180 |
tmpp: pointer; |
|
181 |
fromP4, toP4: PLongWordArray; |
|
182 |
begin |
|
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
183 |
if (surf^.w <= 256) and (surf^.h <= 256) then |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
184 |
begin |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
185 |
Surface2Atlas:= Surface2Tex_(surf, enableClamp); // run the atlas side by side for debugging |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
186 |
ResetVertexArrays(Surface2Atlas); |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
187 |
exit; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
188 |
end; |
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
189 |
new(Surface2Atlas); |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
190 |
Surface2Atlas^.PrevTexture:= nil; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
191 |
Surface2Atlas^.NextTexture:= nil; |
4375 | 192 |
if TextureList <> nil then |
193 |
begin |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
194 |
TextureList^.PrevTexture:= Surface2Atlas; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
195 |
Surface2Atlas^.NextTexture:= TextureList |
4375 | 196 |
end; |
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
197 |
TextureList:= Surface2Atlas; |
4375 | 198 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
199 |
// Atlas allocation happens here later on. For now we just allocate one exclusive atlas per sprite |
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
200 |
new(Surface2Atlas^.atlas); |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
201 |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
202 |
Surface2Atlas^.w:= surf^.w; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
203 |
Surface2Atlas^.h:= surf^.h; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
204 |
Surface2Atlas^.x:=0; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
205 |
Surface2Atlas^.y:=0; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
206 |
Surface2Atlas^.isRotated:=false; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
207 |
Surface2Atlas^.surface:= surf; |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
208 |
Surface2Atlas^.shared:= false; |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
209 |
|
4375 | 210 |
|
211 |
if (surf^.format^.BytesPerPixel <> 4) then |
|
212 |
begin |
|
213 |
TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true); |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
214 |
Surface2Atlas^.atlas^.id:= 0; |
4375 | 215 |
exit |
216 |
end; |
|
217 |
||
218 |
||
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
219 |
glGenTextures(1, @Surface2Atlas^.atlas^.id); |
4375 | 220 |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
221 |
glBindTexture(GL_TEXTURE_2D, Surface2Atlas^.atlas^.id); |
4375 | 222 |
|
223 |
if SDL_MustLock(surf) then |
|
224 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
225 |
||
5441
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
226 |
fromP4:= Surf^.pixels; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
227 |
|
6982 | 228 |
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
|
229 |
Surface2GrayScale(Surf); |
6303 | 230 |
|
4375 | 231 |
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
232 |
begin |
|
233 |
tw:= toPowerOf2(Surf^.w); |
|
234 |
th:= toPowerOf2(Surf^.h); |
|
235 |
||
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
236 |
Surface2Atlas^.atlas^.w:=tw; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
237 |
Surface2Atlas^.atlas^.h:=th; |
4375 | 238 |
|
7151 | 239 |
tmpp:= GetMem(tw * th * surf^.format^.BytesPerPixel); |
4375 | 240 |
|
241 |
fromP4:= Surf^.pixels; |
|
242 |
toP4:= tmpp; |
|
243 |
||
244 |
for y:= 0 to Pred(Surf^.h) do |
|
245 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
toP4^[x]:= 0; |
4375 | 250 |
toP4:= @(toP4^[tw]); |
251 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
252 |
end; |
|
253 |
||
254 |
for y:= Surf^.h to Pred(th) do |
|
255 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
256 |
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
|
257 |
toP4^[x]:= 0; |
4375 | 258 |
toP4:= @(toP4^[tw]) |
259 |
end; |
|
260 |
||
261 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
|
262 |
||
263 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
264 |
end |
|
265 |
else |
|
266 |
begin |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
267 |
Surface2Atlas^.atlas^.w:=Surf^.w; |
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
268 |
Surface2Atlas^.atlas^.h:=Surf^.h; |
4375 | 269 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
270 |
end; |
|
271 |
||
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
272 |
ResetVertexArrays(Surface2Atlas); |
4375 | 273 |
|
274 |
if SDL_MustLock(surf) then |
|
275 |
SDL_UnlockSurface(surf); |
|
276 |
||
277 |
SetTextureParameters(enableClamp); |
|
278 |
end; |
|
279 |
||
4901 | 280 |
// deletes texture and frees the memory allocated for it. |
281 |
// if nil is passed nothing is done |
|
4375 | 282 |
procedure FreeTexture(tex: PTexture); |
283 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
284 |
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
|
285 |
begin |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
286 |
if tex^.shared then |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
287 |
begin |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
288 |
FreeTexture_(tex); // run atlas side by side for debugging |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
289 |
SDL_FreeSurface(tex^.surface); |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
290 |
exit; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
291 |
end; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
292 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
293 |
// 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
|
294 |
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
|
295 |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
296 |
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
|
297 |
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
|
298 |
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
|
299 |
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
|
300 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
301 |
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
|
302 |
glDeleteTextures(1, @tex^.atlas^.id); |
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
303 |
|
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
304 |
if (tex^.surface <> nil) then |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
305 |
SDL_FreeSurface(tex^.surface); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
306 |
Dispose(tex); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
307 |
end |
4375 | 308 |
end; |
309 |
||
310 |
procedure initModule; |
|
311 |
begin |
|
7295
e70b81854fb9
made surface retaining mandatory
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7292
diff
changeset
|
312 |
uAtlas.initModule; |
4375 | 313 |
TextureList:= nil; |
314 |
end; |
|
315 |
||
316 |
procedure freeModule; |
|
317 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
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
|
321 |
begin |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
322 |
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
|
323 |
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
|
324 |
end |
4375 | 325 |
end; |
326 |
||
4901 | 327 |
end. |