author | Xeli |
Sun, 22 Apr 2012 15:13:17 +0200 | |
changeset 6909 | 2b9c1228d516 |
parent 6884 | 85e810230372 |
child 6952 | 7f70f37bbf08 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2947 | 2 |
* Hedgewars, a free turn based strategy game |
6700 | 3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
2947 | 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 |
*) |
|
4 | 18 |
|
2630 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
4 | 21 |
unit uMisc; |
22 |
interface |
|
2630 | 23 |
|
5004
2efa6a414518
update some sdl-1.3 bindings (working up to rev 5296)
koda
parents:
4976
diff
changeset
|
24 |
uses SDLh, uConsts, GLunit, uTypes; |
1054 | 25 |
|
3169
c8c6ac44f51b
prophylactic removal of some Integer references, raise a few of the template islands up a bit so they work inverted without triggering border
nemo
parents:
3165
diff
changeset
|
26 |
procedure movecursor(dx, dy: LongInt); |
2670 | 27 |
function doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface; |
5912
d31eba29e706
screenshots: display a msg on failure and log causative error
sheepluva
parents:
5911
diff
changeset
|
28 |
function MakeScreenshot(filename: shortstring): boolean; |
4413 | 29 |
function GetTeamStatString(p: PTeam): shortstring; |
6695
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
30 |
{$IFDEF SDL13} |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
31 |
function SDL_RectMake(x, y, width, height: LongInt): TSDL_Rect; |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
32 |
{$ELSE} |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
33 |
function SDL_RectMake(x, y: SmallInt; width, height: Word): TSDL_Rect; |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
34 |
{$ENDIF} |
3038 | 35 |
procedure initModule; |
36 |
procedure freeModule; |
|
4 | 37 |
|
38 |
implementation |
|
6881 | 39 |
uses typinfo, sysutils, uVariables, uUtils |
40 |
{$IFDEF PNG_SCREENSHOTS}, PNGh, png {$ENDIF} |
|
41 |
{$IFNDEF USE_SDLTHREADS} {$IFDEF UNIX}, cthreads{$ENDIF} {$ENDIF}; |
|
42 |
||
43 |
type PScreenshot = ^TScreenshot; |
|
44 |
TScreenshot = record |
|
45 |
buffer: PByte; |
|
46 |
filename: shortstring; |
|
47 |
width, height: LongInt; |
|
48 |
size: QWord; |
|
49 |
end; |
|
3756 | 50 |
|
3169
c8c6ac44f51b
prophylactic removal of some Integer references, raise a few of the template islands up a bit so they work inverted without triggering border
nemo
parents:
3165
diff
changeset
|
51 |
procedure movecursor(dx, dy: LongInt); |
2428 | 52 |
var x, y: LongInt; |
53 |
begin |
|
54 |
if (dx = 0) and (dy = 0) then exit; |
|
2671
7e0f88013fe8
smaller patches, one missing Sky-lowres, IMG_Init and Mix_Init (might require newer libraries), updates to SDL bindings, code cleanup, new compile flags
koda
parents:
2670
diff
changeset
|
55 |
|
2428 | 56 |
SDL_GetMouseState(@x, @y); |
57 |
Inc(x, dx); |
|
58 |
Inc(y, dy); |
|
59 |
SDL_WarpMouse(x, y); |
|
60 |
end; |
|
61 |
||
6881 | 62 |
{$IFDEF PNG_SCREENSHOTS} |
63 |
// this funtion will be executed in separate thread |
|
64 |
function SaveScreenshot(screenshot: pointer): PtrInt; |
|
65 |
var i: LongInt; |
|
66 |
png_ptr: ^png_struct; |
|
67 |
info_ptr: ^png_info; |
|
2947 | 68 |
f: file; |
6881 | 69 |
image: PScreenshot; |
70 |
begin |
|
71 |
image:= PScreenshot(screenshot); |
|
72 |
||
73 |
png_ptr := png_create_write_struct(png_get_libpng_ver(nil), nil, nil, nil); |
|
74 |
if png_ptr = nil then |
|
75 |
begin |
|
76 |
// AddFileLog('Error: Could not create png write struct.'); |
|
77 |
exit(0); |
|
78 |
end; |
|
79 |
||
80 |
info_ptr := png_create_info_struct(png_ptr); |
|
81 |
if info_ptr = nil then |
|
82 |
begin |
|
83 |
png_destroy_write_struct(@png_ptr, nil); |
|
84 |
// AddFileLog('Error: Could not create png info struct.'); |
|
85 |
exit(0); |
|
86 |
end; |
|
87 |
||
88 |
{$IOCHECKS OFF} |
|
89 |
Assign(f, image^.filename); |
|
90 |
Rewrite(f, 1); |
|
91 |
if IOResult = 0 then |
|
92 |
begin |
|
93 |
png_init_pascal_io(png_ptr,@f); |
|
94 |
png_set_IHDR(png_ptr, info_ptr, image^.width, image^.height, |
|
95 |
8, // bit depth |
|
96 |
PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, |
|
97 |
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
|
98 |
png_write_info(png_ptr, info_ptr); |
|
99 |
// glReadPixels and libpng number rows in different order |
|
100 |
for i:= image^.height-1 downto 0 do |
|
101 |
png_write_row(png_ptr, image^.buffer + i*4*image^.width); |
|
102 |
png_write_end(png_ptr, info_ptr); |
|
103 |
Close(f); |
|
104 |
end; |
|
105 |
{$IOCHECKS ON} |
|
106 |
||
107 |
// free everything |
|
108 |
png_destroy_write_struct(@png_ptr, @info_ptr); |
|
109 |
FreeMem(image^.buffer, image^.size); |
|
110 |
Dispose(image); |
|
111 |
SaveScreenshot:= 0; |
|
112 |
end; |
|
113 |
||
114 |
{$ELSE} // no PNG_SCREENSHOTS |
|
115 |
||
116 |
// this funtion will be executed in separate thread |
|
117 |
function SaveScreenshot(screenshot: pointer): PtrInt; |
|
118 |
var f: file; |
|
2947 | 119 |
// Windows Bitmap Header |
120 |
head: array[0..53] of Byte = ( |
|
6267
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
121 |
$42, $4D, // identifier ("BM") |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
122 |
0, 0, 0, 0, // file size |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
123 |
0, 0, 0, 0, // reserved |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
124 |
54, 0, 0, 0, // starting offset |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
125 |
40, 0, 0, 0, // header size |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
126 |
0, 0, 0, 0, // width |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
127 |
0, 0, 0, 0, // height |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
128 |
1, 0, // color planes |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
129 |
32, 0, // bit depth |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
130 |
0, 0, 0, 0, // compression method (uncompressed) |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
131 |
0, 0, 0, 0, // image size |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
132 |
96, 0, 0, 0, // horizontal resolution |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
133 |
96, 0, 0, 0, // vertical resolution |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
134 |
0, 0, 0, 0, // number of colors (all) |
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
135 |
0, 0, 0, 0 // number of important colors |
2947 | 136 |
); |
6881 | 137 |
image: PScreenshot; |
138 |
size: QWord; |
|
139 |
begin |
|
140 |
image:= PScreenshot(screenshot); |
|
141 |
||
142 |
size:= image^.Width*image^.Height*4; |
|
143 |
||
144 |
head[$02]:= (size + 54) and $ff; |
|
145 |
head[$03]:= ((size + 54) shr 8) and $ff; |
|
146 |
head[$04]:= ((size + 54) shr 16) and $ff; |
|
147 |
head[$05]:= ((size + 54) shr 24) and $ff; |
|
148 |
head[$12]:= image^.Width and $ff; |
|
149 |
head[$13]:= (image^.Width shr 8) and $ff; |
|
150 |
head[$14]:= (image^.Width shr 16) and $ff; |
|
151 |
head[$15]:= (image^.Width shr 24) and $ff; |
|
152 |
head[$16]:= image^.Height and $ff; |
|
153 |
head[$17]:= (image^.Height shr 8) and $ff; |
|
154 |
head[$18]:= (image^.Height shr 16) and $ff; |
|
155 |
head[$19]:= (image^.Height shr 24) and $ff; |
|
156 |
head[$22]:= size and $ff; |
|
157 |
head[$23]:= (size shr 8) and $ff; |
|
158 |
head[$24]:= (size shr 16) and $ff; |
|
159 |
head[$25]:= (size shr 24) and $ff; |
|
160 |
||
161 |
{$IOCHECKS OFF} |
|
162 |
Assign(f, image^.filename); |
|
163 |
Rewrite(f, 1); |
|
164 |
if IOResult = 0 then |
|
165 |
begin |
|
166 |
BlockWrite(f, head, sizeof(head)); |
|
167 |
BlockWrite(f, image^.buffer^, size); |
|
168 |
Close(f); |
|
169 |
end |
|
170 |
else |
|
171 |
begin |
|
172 |
//AddFileLog('Error: Could not write to ' + filename); |
|
173 |
end; |
|
174 |
{$IOCHECKS ON} |
|
175 |
||
176 |
// free everything |
|
177 |
FreeMem(image^.buffer, image^.size); |
|
178 |
Dispose(image); |
|
179 |
SaveScreenshot:= 0; |
|
180 |
end; |
|
181 |
||
182 |
{$ENDIF} // no PNG_SCREENSHOTS |
|
183 |
||
184 |
// captures and saves the screen. returns true on success. |
|
185 |
function MakeScreenshot(filename: shortstring): Boolean; |
|
186 |
var p: Pointer; |
|
187 |
size: QWord; |
|
188 |
image: PScreenshot; |
|
189 |
format: GLenum; |
|
190 |
ext: string[4]; |
|
1080 | 191 |
begin |
3107 | 192 |
// flash |
193 |
ScreenFade:= sfFromWhite; |
|
194 |
ScreenFadeValue:= sfMax; |
|
195 |
ScreenFadeSpeed:= 5; |
|
196 |
||
6881 | 197 |
{$IFDEF PNG_SCREENSHOTS} |
198 |
format:= GL_RGBA; |
|
199 |
ext:= '.png'; |
|
200 |
{$ELSE} |
|
201 |
format:= GL_BGRA; |
|
202 |
ext:= '.bmp'; |
|
203 |
{$ENDIF} |
|
204 |
||
6267
be5d40bb1e86
make screenshots with 32bits of depths, so that they are more opengles friendly
koda
parents:
5912
diff
changeset
|
205 |
size:= toPowerOf2(cScreenWidth) * toPowerOf2(cScreenHeight) * 4; |
6881 | 206 |
p:= GetMem(size); // will be freed in SaveScreenshot() |
1080 | 207 |
|
5910 | 208 |
// memory could not be allocated |
209 |
if p = nil then |
|
5912
d31eba29e706
screenshots: display a msg on failure and log causative error
sheepluva
parents:
5911
diff
changeset
|
210 |
begin |
d31eba29e706
screenshots: display a msg on failure and log causative error
sheepluva
parents:
5911
diff
changeset
|
211 |
AddFileLog('Error: Could not allocate memory for screenshot.'); |
d31eba29e706
screenshots: display a msg on failure and log causative error
sheepluva
parents:
5911
diff
changeset
|
212 |
exit(false); |
d31eba29e706
screenshots: display a msg on failure and log causative error
sheepluva
parents:
5911
diff
changeset
|
213 |
end; |
5910 | 214 |
|
6881 | 215 |
// read pixel from the front buffer |
216 |
glReadPixels(0, 0, cScreenWidth, cScreenHeight, format, GL_UNSIGNED_BYTE, p); |
|
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset
|
217 |
|
6881 | 218 |
// allocate and fill structure that will be passed to new thread |
219 |
New(image); // will be disposed in SaveScreenshot() |
|
220 |
image^.filename:= UserPathPrefix + '/Screenshots/' + filename + ext; |
|
221 |
image^.width:= cScreenWidth; |
|
222 |
image^.height:= cScreenHeight; |
|
223 |
image^.size:= size; |
|
224 |
image^.buffer:= p; |
|
1080 | 225 |
|
6881 | 226 |
{$IFDEF USE_SDLTHREADS} |
227 |
SDL_CreateThread(@SaveScreenshot{$IFDEF SDL13}, nil{$ENDIF}, image); |
|
228 |
{$ELSE} |
|
229 |
BeginThread(@SaveScreenshot, image); |
|
230 |
{$ENDIF} |
|
231 |
MakeScreenshot:= true; // possibly it is not true but we will not wait for thread to terminate |
|
1080 | 232 |
end; |
233 |
||
5004
2efa6a414518
update some sdl-1.3 bindings (working up to rev 5296)
koda
parents:
4976
diff
changeset
|
234 |
// http://www.idevgames.com/forums/thread-5602-post-21860.html#pid21860 |
2619 | 235 |
function doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface; |
5004
2efa6a414518
update some sdl-1.3 bindings (working up to rev 5296)
koda
parents:
4976
diff
changeset
|
236 |
var convertedSurf: PSDL_Surface; |
2619 | 237 |
begin |
5004
2efa6a414518
update some sdl-1.3 bindings (working up to rev 5296)
koda
parents:
4976
diff
changeset
|
238 |
if ((tmpsurf^.format^.bitsperpixel = 32) and (tmpsurf^.format^.rshift > tmpsurf^.format^.bshift)) or |
2efa6a414518
update some sdl-1.3 bindings (working up to rev 5296)
koda
parents:
4976
diff
changeset
|
239 |
(tmpsurf^.format^.bitsperpixel = 24) then |
4578 | 240 |
begin |
5046
fc6639d56799
this brings compatibility up with SDL HEAD (5504), but maybe breaks compatibility with sdl 1.2 so please test! still has problems with keyboard input and rendered ttf textures
koda
parents:
5004
diff
changeset
|
241 |
convertedSurf:= SDL_ConvertSurface(tmpsurf, conversionFormat, SDL_SWSURFACE); |
2947 | 242 |
SDL_FreeSurface(tmpsurf); |
243 |
exit(convertedSurf); |
|
4578 | 244 |
end; |
2705
2b5625c4ec16
fix a nasty 196 bytes memory leak in engine, plus other stuff for iphone frontend
koda
parents:
2699
diff
changeset
|
245 |
|
2947 | 246 |
exit(tmpsurf); |
2619 | 247 |
end; |
248 |
||
6695
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
249 |
{$IFDEF SDL13} |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
250 |
function SDL_RectMake(x, y, width, height: LongInt): TSDL_Rect; |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
251 |
{$ELSE} |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
252 |
function SDL_RectMake(x, y: SmallInt; width, height: Word): TSDL_Rect; |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
253 |
{$ENDIF} |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
254 |
begin |
6857 | 255 |
SDL_RectMake.x:= x; |
256 |
SDL_RectMake.y:= y; |
|
257 |
SDL_RectMake.w:= width; |
|
258 |
SDL_RectMake.h:= height; |
|
6695
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6267
diff
changeset
|
259 |
end; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2698
diff
changeset
|
260 |
|
4413 | 261 |
function GetTeamStatString(p: PTeam): shortstring; |
262 |
var s: ansistring; |
|
2670 | 263 |
begin |
4413 | 264 |
s:= p^.TeamName + ':' + IntToStr(p^.TeamHealth) + ':'; |
265 |
GetTeamStatString:= s; |
|
2670 | 266 |
end; |
2630 | 267 |
|
3038 | 268 |
procedure initModule; |
6843
59da15acb2f2
Finally fix the bug with pointer declarations polluting namespace with bad records
unc0rr
parents:
6700
diff
changeset
|
269 |
const SDL_PIXELFORMAT_ABGR8888 = (1 shl 31) or (6 shl 24) or (7 shl 20) or (6 shl 16) or (32 shl 8) or 4; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2698
diff
changeset
|
270 |
begin |
5052 | 271 |
conversionFormat:= SDL_AllocFormat(SDL_PIXELFORMAT_ABGR8888); |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2698
diff
changeset
|
272 |
end; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2698
diff
changeset
|
273 |
|
3038 | 274 |
procedure freeModule; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2698
diff
changeset
|
275 |
begin |
3626 | 276 |
recordFileName:= ''; |
5046
fc6639d56799
this brings compatibility up with SDL HEAD (5504), but maybe breaks compatibility with sdl 1.2 so please test! still has problems with keyboard input and rendered ttf textures
koda
parents:
5004
diff
changeset
|
277 |
SDL_FreeFormat(conversionFormat); |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2698
diff
changeset
|
278 |
end; |
4 | 279 |
|
280 |
end. |