author | Wolfgang Steffens <WolfgangSteff@gmail.com> |
Tue, 10 Jul 2012 16:38:50 +0200 | |
changeset 7377 | 1aceade403ba |
parent 7304 | 8b3575750cd2 |
child 11317 | 62287d4044e7 |
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); |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
27 |
function SurfaceSheet2Atlas(surf: PSDL_Surface; spriteWidth: Integer; spriteHeight: Integer): PTexture; |
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
28 |
function Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
4375 | 29 |
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
|
30 |
procedure ComputeTexcoords(texture: PTexture; r: PSDL_Rect; tb: PVertexRect); |
4375 | 31 |
|
32 |
procedure initModule; |
|
33 |
procedure freeModule; |
|
34 |
||
35 |
implementation |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
36 |
uses GLunit, uUtils, uVariables, uConsts, uDebug, uConsole, uAtlas, SysUtils; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
37 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
38 |
var |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
39 |
logFile: TextFile; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
40 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
41 |
function CropSurface(source: PSDL_Surface; rect: PSDL_Rect): PSDL_Surface; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
42 |
var |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
43 |
fmt: PSDL_PixelFormat; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
44 |
srcP, dstP: PByte; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
45 |
copySize: Integer; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
46 |
i: Integer; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
47 |
const |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
48 |
pixelSize = 4; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
49 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
50 |
//writeln(stdout, 'Cropping from ' + IntToStr(source^.w) + 'x' + IntToStr(source^.h) + ' -> ' + IntToStr(rect^.w) + 'x' + IntToStr(rect^.h)); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
51 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
52 |
fmt:= source^.format; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
53 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
54 |
CropSurface:= SDL_CreateRGBSurface(source^.flags, rect^.w, rect^.h, |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
55 |
fmt^.BitsPerPixel, fmt^.Rmask, fmt^.Gmask, fmt^.Bmask, fmt^.Amask); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
56 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
57 |
if SDL_MustLock(source) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
58 |
SDLTry(SDL_LockSurface(source) >= 0, true); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
59 |
if SDL_MustLock(CropSurface) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
60 |
SDLTry(SDL_LockSurface(CropSurface) >= 0, true); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
61 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
62 |
srcP:= source^.pixels; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
63 |
dstP:= CropSurface^.pixels; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
64 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
65 |
inc(srcP, pixelSize * rect^.x); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
66 |
inc(srcP, source^.pitch * rect^.y); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
67 |
copySize:= rect^.w * pixelSize; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
68 |
for i:= 0 to Pred(rect^.h) do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
69 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
70 |
Move(srcP^, dstP^, copySize); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
71 |
inc(srcP, source^.pitch); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
72 |
inc(dstP, CropSurface^.pitch); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
73 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
74 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
75 |
if SDL_MustLock(source) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
76 |
SDL_UnlockSurface(source); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
77 |
if SDL_MustLock(CropSurface) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
78 |
SDL_UnlockSurface(CropSurface); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
79 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
80 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
81 |
function TransparentLine(p: PByte; stride: Integer; length: Integer): boolean; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
82 |
var |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
83 |
i: Integer; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
84 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
85 |
TransparentLine:= false; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
86 |
for i:=0 to pred(length) do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
87 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
88 |
if p^ <> 0 then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
89 |
exit; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
90 |
inc(p, stride); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
91 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
92 |
TransparentLine:= true; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
93 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
94 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
95 |
function AutoCrop(source: PSDL_Surface; var cropinfo: TCropInformation): PSDL_Surface; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
96 |
var |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
97 |
l,r,t,b, i: Integer; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
98 |
pixels, p: PByte; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
99 |
scanlineSize: Integer; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
100 |
rect: TSDL_Rect; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
101 |
const |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
102 |
pixelSize = 4; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
103 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
104 |
l:= source^.w; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
105 |
r:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
106 |
t:= source^.h; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
107 |
b:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
108 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
109 |
if SDL_MustLock(source) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
110 |
SDLTry(SDL_LockSurface(source) >= 0, true); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
111 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
112 |
pixels:= source^.pixels; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
113 |
scanlineSize:= source^.pitch; |
4375 | 114 |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
115 |
inc(pixels, 3); // advance to alpha value |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
116 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
117 |
// check top |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
118 |
p:= pixels; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
119 |
for i:= 0 to Pred(source^.h) do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
120 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
121 |
if not TransparentLine(p, pixelSize, source^.w) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
122 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
123 |
t:= i; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
124 |
break; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
125 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
126 |
inc(p, scanlineSize); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
127 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
128 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
129 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
130 |
// check bottom |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
131 |
p:= pixels; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
132 |
inc(p, scanlineSize * source^.h); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
133 |
for i:= 0 to Pred(source^.h - t) do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
134 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
135 |
dec(p, scanlineSize); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
136 |
if not TransparentLine(p, pixelSize, source^.w) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
137 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
138 |
b:= i; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
139 |
break; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
140 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
141 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
142 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
143 |
// check left |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
144 |
p:= pixels; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
145 |
for i:= 0 to Pred(source^.w) do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
146 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
147 |
if not TransparentLine(p, scanlineSize, source^.h) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
148 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
149 |
l:= i; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
150 |
break; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
151 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
152 |
inc(p, pixelSize); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
153 |
end; |
4375 | 154 |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
155 |
// check right |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
156 |
p:= pixels; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
157 |
inc(p, scanlineSize); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
158 |
for i:= 0 to Pred(source^.w - l) do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
159 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
160 |
dec(p, pixelSize); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
161 |
if not TransparentLine(p, scanlineSize, source^.h) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
162 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
163 |
r:= i; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
164 |
break; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
165 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
166 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
167 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
168 |
if SDL_MustLock(source) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
169 |
SDL_UnlockSurface(source); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
170 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
171 |
rect.x:= l; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
172 |
rect.y:= t; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
173 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
174 |
rect.w:= source^.w - r - l; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
175 |
rect.h:= source^.h - b - t; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
176 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
177 |
cropInfo.l:= l; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
178 |
cropInfo.r:= r; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
179 |
cropInfo.t:= t; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
180 |
cropInfo.b:= b; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
181 |
cropInfo.x:= Trunc(source^.w / 2 - l + r); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
182 |
cropInfo.y:= Trunc(source^.h / 2 - t + b); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
183 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
184 |
if (l = source^.w) or (t = source^.h) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
185 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
186 |
result:= nil; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
187 |
exit; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
188 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
189 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
190 |
if (l <> 0) or (r <> 0) or (t <> 0) or (b <> 0) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
191 |
result:= CropSurface(source, @rect) |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
192 |
else result:= source; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
193 |
end; |
4375 | 194 |
|
195 |
procedure SetTextureParameters(enableClamp: Boolean); |
|
196 |
begin |
|
197 |
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
|
198 |
begin |
4375 | 199 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
200 |
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
|
201 |
end; |
4375 | 202 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
203 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
|
204 |
end; |
|
205 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
206 |
procedure ComputeTexcoords(texture: PTexture; r: PSDL_Rect; tb: PVertexRect); |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
207 |
var |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
208 |
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
|
209 |
w, h, aw, ah: LongInt; |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
210 |
p: PChar; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
211 |
const |
7377 | 212 |
texelOffsetPos = 0.5; |
213 |
texelOffsetNeg = 0.0; |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
214 |
begin |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
215 |
aw:=texture^.atlas^.w; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
216 |
ah:=texture^.atlas^.h; |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
217 |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
218 |
if texture^.isRotated then |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
219 |
begin |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
220 |
w:=r^.h; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
221 |
h:=r^.w; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
222 |
end else |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
223 |
begin |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
224 |
w:=r^.w; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
225 |
h:=r^.h; |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
226 |
end; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
227 |
|
7377 | 228 |
x0:= (texture^.x + {r^.x} + texelOffsetPos)/aw; |
229 |
x1:= (texture^.x + {r^.x} + w + texelOffsetNeg)/aw; |
|
230 |
y0:= (texture^.y + {r^.y} + texelOffsetPos)/ah; |
|
231 |
y1:= (texture^.y + {r^.y} + h + texelOffsetNeg)/ah; |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
232 |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
233 |
if (texture^.isRotated) then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
234 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
235 |
tb^[0].X:= x0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
236 |
tb^[0].Y:= y0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
237 |
tb^[3].X:= x1; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
238 |
tb^[3].Y:= y0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
239 |
tb^[2].X:= x1; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
240 |
tb^[2].Y:= y1; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
241 |
tb^[1].X:= x0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
242 |
tb^[1].Y:= y1 |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
243 |
end else |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
244 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
245 |
tb^[0].X:= x0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
246 |
tb^[0].Y:= y0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
247 |
tb^[1].X:= x1; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
248 |
tb^[1].Y:= y0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
249 |
tb^[2].X:= x1; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
250 |
tb^[2].Y:= y1; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
251 |
tb^[3].X:= x0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
252 |
tb^[3].Y:= y1; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
253 |
end; |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
254 |
end; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
255 |
|
4375 | 256 |
procedure ResetVertexArrays(texture: PTexture); |
7377 | 257 |
var |
258 |
rect: TSDL_Rect; |
|
259 |
l, t, r, b: Real; |
|
260 |
const |
|
261 |
halfTexelOffsetPos = 1.0; |
|
262 |
halfTexelOffsetNeg = -0.0; |
|
4375 | 263 |
begin |
7377 | 264 |
l:= texture^.cropInfo.l + halfTexelOffsetPos; |
265 |
r:= texture^.cropInfo.l + texture^.w + halfTexelOffsetNeg; |
|
266 |
t:= texture^.cropInfo.t + halfTexelOffsetPos; |
|
267 |
b:= texture^.cropInfo.t + texture^.h + halfTexelOffsetNeg; |
|
268 |
||
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
269 |
with texture^ do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
270 |
begin |
7377 | 271 |
vb[0].X:= l; |
272 |
vb[0].Y:= t; |
|
273 |
vb[1].X:= r; |
|
274 |
vb[1].Y:= t; |
|
275 |
vb[2].X:= r; |
|
276 |
vb[2].Y:= b; |
|
277 |
vb[3].X:= l; |
|
278 |
vb[3].Y:= b; |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
279 |
end; |
4375 | 280 |
|
7377 | 281 |
rect.x:= 0; |
282 |
rect.y:= 0; |
|
283 |
rect.w:= texture^.w; |
|
284 |
rect.h:= texture^.h; |
|
285 |
ComputeTexcoords(texture, @rect, @texture^.tb); |
|
4375 | 286 |
end; |
287 |
||
288 |
function NewTexture(width, height: Longword; buf: Pointer): PTexture; |
|
289 |
begin |
|
290 |
new(NewTexture); |
|
291 |
NewTexture^.Scale:= 1; |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
292 |
|
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
293 |
// 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
|
294 |
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
|
295 |
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
|
296 |
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
|
297 |
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
|
298 |
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
|
299 |
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
|
300 |
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
|
301 |
NewTexture^.isRotated:=false; |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
302 |
NewTexture^.shared:=false; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
303 |
NewTexture^.surface:=nil; |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
304 |
NewTexture^.nextFrame:=nil; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
305 |
NewTexture^.cropInfo.l:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
306 |
NewTexture^.cropInfo.r:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
307 |
NewTexture^.cropInfo.t:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
308 |
NewTexture^.cropInfo.b:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
309 |
NewTexture^.cropInfo.x:= width div 2; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
310 |
NewTexture^.cropInfo.y:= height div 2; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
311 |
|
4375 | 312 |
|
313 |
ResetVertexArrays(NewTexture); |
|
314 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
315 |
glGenTextures(1, @NewTexture^.atlas^.id); |
4375 | 316 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
317 |
glBindTexture(GL_TEXTURE_2D, NewTexture^.atlas^.id); |
4375 | 318 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); |
319 |
||
320 |
SetTextureParameters(true); |
|
321 |
end; |
|
322 |
||
6303 | 323 |
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
|
324 |
var tw, x, y: Longword; |
6303 | 325 |
fromP4: PLongWordArray; |
326 |
begin |
|
327 |
fromP4:= Surf^.pixels; |
|
328 |
for y:= 0 to Pred(Surf^.h) do |
|
329 |
begin |
|
330 |
for x:= 0 to Pred(Surf^.w) do |
|
331 |
begin |
|
332 |
tw:= fromP4^[x]; |
|
333 |
tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED + |
|
334 |
(tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + |
|
335 |
(tw shr BShift and $FF) * RGB_LUMINANCE_BLUE); |
|
336 |
if tw > 255 then tw:= 255; |
|
337 |
tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask); |
|
338 |
fromP4^[x]:= tw; |
|
339 |
end; |
|
340 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
341 |
end; |
|
342 |
end; |
|
6467 | 343 |
|
344 |
||
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
345 |
function SurfaceSheet2Atlas(surf: PSDL_Surface; spriteWidth: Integer; spriteHeight: Integer): PTexture; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
346 |
var |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
347 |
subSurface: PSDL_Surface; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
348 |
framesX, framesY: Integer; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
349 |
last, current: PTexture; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
350 |
r: TSDL_Rect; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
351 |
x, y: Integer; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
352 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
353 |
SurfaceSheet2Atlas:= nil; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
354 |
r.x:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
355 |
r.y:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
356 |
r.w:= spriteWidth; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
357 |
r.h:= spriteHeight; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
358 |
last:= nil; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
359 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
360 |
framesX:= surf^.w div spriteWidth; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
361 |
framesY:= surf^.h div spriteHeight; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
362 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
363 |
for x:=0 to Pred(framesX) do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
364 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
365 |
r.y:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
366 |
for y:=0 to Pred(framesY) do |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
367 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
368 |
subSurface:= CropSurface(surf, @r); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
369 |
current:= Surface2Atlas(subSurface, false); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
370 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
371 |
if last = nil then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
372 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
373 |
SurfaceSheet2Atlas:= current; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
374 |
last:= current; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
375 |
end else |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
376 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
377 |
last^.nextFrame:= current; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
378 |
last:= current; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
379 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
380 |
inc(r.y, spriteHeight); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
381 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
382 |
inc(r.x, spriteWidth); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
383 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
384 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
385 |
SDL_FreeSurface(surf); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
386 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
387 |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
388 |
function Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
4375 | 389 |
var tw, th, x, y: Longword; |
390 |
tmpp: pointer; |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
391 |
cropped: PSDL_Surface; |
4375 | 392 |
fromP4, toP4: PLongWordArray; |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
393 |
cropInfo: TCropInformation; |
4375 | 394 |
begin |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
395 |
cropped:= AutoCrop(surf, cropInfo); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
396 |
if cropped <> surf then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
397 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
398 |
SDL_FreeSurface(surf); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
399 |
surf:= cropped; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
400 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
401 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
402 |
if surf = nil then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
403 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
404 |
new(Surface2Atlas); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
405 |
Surface2Atlas^.w:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
406 |
Surface2Atlas^.h:= 0; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
407 |
Surface2Atlas^.x:=0 ; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
408 |
Surface2Atlas^.y:=0 ; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
409 |
Surface2Atlas^.isRotated:= false; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
410 |
Surface2Atlas^.surface:= nil; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
411 |
Surface2Atlas^.shared:= false; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
412 |
Surface2Atlas^.nextFrame:= nil; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
413 |
Surface2Atlas^.cropInfo:= cropInfo; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
414 |
exit; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
415 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
416 |
|
7377 | 417 |
//if (surf^.w <= 512) and (surf^.h <= 512) then |
418 |
// nothing should use the old codepath anymore once we are done! |
|
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
419 |
begin |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
420 |
Surface2Atlas:= Surface2Tex_(surf, enableClamp); // run the atlas side by side for debugging |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
421 |
Surface2Atlas^.cropInfo:= cropInfo; |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
422 |
ResetVertexArrays(Surface2Atlas); |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
423 |
exit; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
424 |
end; |
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
425 |
new(Surface2Atlas); |
4375 | 426 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
427 |
// 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
|
428 |
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
|
429 |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
430 |
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
|
431 |
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
|
432 |
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
|
433 |
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
|
434 |
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
|
435 |
Surface2Atlas^.surface:= surf; |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
436 |
Surface2Atlas^.shared:= false; |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
437 |
Surface2Atlas^.nextFrame:= nil; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
438 |
Surface2Atlas^.cropInfo:= cropInfo; |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
439 |
|
4375 | 440 |
|
441 |
if (surf^.format^.BytesPerPixel <> 4) then |
|
442 |
begin |
|
443 |
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
|
444 |
Surface2Atlas^.atlas^.id:= 0; |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
445 |
exit; |
4375 | 446 |
end; |
447 |
||
448 |
||
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
449 |
glGenTextures(1, @Surface2Atlas^.atlas^.id); |
4375 | 450 |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
451 |
glBindTexture(GL_TEXTURE_2D, Surface2Atlas^.atlas^.id); |
4375 | 452 |
|
453 |
if SDL_MustLock(surf) then |
|
454 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
455 |
||
5441
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
456 |
fromP4:= Surf^.pixels; |
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
4976
diff
changeset
|
457 |
|
6982 | 458 |
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
|
459 |
Surface2GrayScale(Surf); |
6303 | 460 |
|
4375 | 461 |
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
462 |
begin |
|
463 |
tw:= toPowerOf2(Surf^.w); |
|
464 |
th:= toPowerOf2(Surf^.h); |
|
465 |
||
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
466 |
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
|
467 |
Surface2Atlas^.atlas^.h:=th; |
4375 | 468 |
|
7151 | 469 |
tmpp:= GetMem(tw * th * surf^.format^.BytesPerPixel); |
4375 | 470 |
|
471 |
fromP4:= Surf^.pixels; |
|
472 |
toP4:= tmpp; |
|
473 |
||
474 |
for y:= 0 to Pred(Surf^.h) do |
|
475 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
476 |
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
|
477 |
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
|
478 |
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
|
479 |
toP4^[x]:= 0; |
4375 | 480 |
toP4:= @(toP4^[tw]); |
481 |
fromP4:= @(fromP4^[Surf^.pitch div 4]) |
|
482 |
end; |
|
483 |
||
484 |
for y:= Surf^.h to Pred(th) do |
|
485 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
486 |
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
|
487 |
toP4^[x]:= 0; |
4375 | 488 |
toP4:= @(toP4^[tw]) |
489 |
end; |
|
490 |
||
491 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
|
492 |
||
493 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
494 |
end |
|
495 |
else |
|
496 |
begin |
|
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
497 |
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
|
498 |
Surface2Atlas^.atlas^.h:=Surf^.h; |
4375 | 499 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
500 |
end; |
|
501 |
||
7291
ad4b6c2b09e8
retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7186
diff
changeset
|
502 |
ResetVertexArrays(Surface2Atlas); |
4375 | 503 |
|
504 |
if SDL_MustLock(surf) then |
|
505 |
SDL_UnlockSurface(surf); |
|
506 |
||
507 |
SetTextureParameters(enableClamp); |
|
508 |
end; |
|
509 |
||
4901 | 510 |
// deletes texture and frees the memory allocated for it. |
511 |
// if nil is passed nothing is done |
|
4375 | 512 |
procedure FreeTexture(tex: PTexture); |
513 |
begin |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
514 |
if tex <> nil then |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
515 |
begin |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
516 |
FreeTexture(tex^.nextFrame); // free all frames linked to this animation |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
517 |
|
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
518 |
if tex^.surface = nil then |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
519 |
begin |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
520 |
Dispose(tex); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
521 |
exit; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
522 |
end; |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
523 |
|
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
524 |
if tex^.shared then |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
525 |
begin |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
526 |
SDL_FreeSurface(tex^.surface); |
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
527 |
FreeTexture_(tex); // run atlas side by side for debugging |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
528 |
exit; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
529 |
end; |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
530 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
531 |
// Atlas cleanup happens here later on. For now we just free as each sprite has one atlas |
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
532 |
glDeleteTextures(1, @tex^.atlas^.id); |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
6982
diff
changeset
|
533 |
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
|
534 |
|
7297
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
535 |
if (tex^.surface <> nil) then |
af64b509725c
using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7295
diff
changeset
|
536 |
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
|
537 |
Dispose(tex); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6467
diff
changeset
|
538 |
end |
4375 | 539 |
end; |
540 |
||
541 |
procedure initModule; |
|
542 |
begin |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
543 |
assign(logFile, 'out.log'); |
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
544 |
rewrite(logFile); |
7295
e70b81854fb9
made surface retaining mandatory
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7292
diff
changeset
|
545 |
uAtlas.initModule; |
4375 | 546 |
end; |
547 |
||
548 |
procedure freeModule; |
|
549 |
begin |
|
7304
8b3575750cd2
Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7297
diff
changeset
|
550 |
close(logFile); |
4375 | 551 |
end; |
552 |
||
4901 | 553 |
end. |