2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
10 #include "SDL/SDL_rotozoom.h"
11 #include "SDL/SDL_image.h"
15 //function for loading a surface from many different formats (except TIFF)
16 int l_getimage(lua_State
*L
){
17 std::string
filename(luaL_checkstring(L
,1));
18 std::map
<std::string
, SDL_Surface
*>::iterator im
= image_store
.find(filename
);
20 if(im
== image_store
.end()){
21 SDL_Surface
*temp
= IMG_Load(filename
.c_str());
24 if(tmp
== 0){std::cerr
<< "ERROR: Can't find image file: "<<filename
<<".\n"; exit(1);}
26 if(temp
->flags
& SDL_SRCALPHA
){
27 tmp
= SDL_DisplayFormatAlpha(temp
);
29 tmp
= SDL_DisplayFormat(temp
);
31 SDL_FreeSurface(temp
);
32 image_store
[filename
] = tmp
;
33 lua_pushlightuserdata(L
, tmp
);
34 lua_pushinteger(L
, (lua_Integer
)tmp
->w
);
35 lua_pushinteger(L
, (lua_Integer
)tmp
->h
);
37 lua_pushlightuserdata(L
, im
->second
);
38 lua_pushinteger(L
, (lua_Integer
)im
->second
->w
);
39 lua_pushinteger(L
, (lua_Integer
)im
->second
->h
);
44 //forces an image to be unloaded from memory
45 int l_releaseimage(lua_State
*L
){
46 std::string
filename(luaL_checkstring(L
,1));
47 std::map
<std::string
, SDL_Surface
*>::iterator im
= image_store
.find(filename
);
49 if(im
!= image_store
.end()){
50 SDL_FreeSurface(im
->second
);
51 image_store
.erase(im
);
57 //function for updating the display surface
58 int l_flip(lua_State
*L
){
63 //function for simple blits (non-animated)
64 int l_blit(lua_State
*L
){
65 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,1);
67 int x
= lua_tointeger(L
,2);
68 int y
= lua_tointeger(L
,3);
69 double r
= lua_tonumber(L
,4);
70 bool rc
= lua_toboolean(L
,5);
71 bool smooth
= lua_toboolean(L
,6);
73 if(tmp
== 0){std::cerr
<< "ERROR: Can't blit. Image is null.\n"; exit(1);}
75 SDL_Rect
*dest
= new SDL_Rect
;
81 //rotate the surface, adjusting the center if needed.
83 rot
= rotozoomSurface(tmp
, r
, 1.0, (smooth
? SMOOTHING_ON
: SMOOTHING_OFF
));
85 dest
->x
+= (dest
->w
- rot
->w
)/2;
86 dest
->y
+= (dest
->h
- rot
->h
)/2;
93 SDL_BlitSurface(tmp
, &tmp
->clip_rect
, screen
, dest
);
95 if(r
!= 0){SDL_FreeSurface(rot
);}
99 //function for blitting a frame of an animation
100 //frames are laid out horizontally
102 int l_blitframe(lua_State
*L
){
103 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,1);
104 int x
= lua_tointeger(L
,2);
105 int y
= lua_tointeger(L
,3);
106 int numframes
= lua_tointeger(L
,4);
107 int frame
= lua_tointeger(L
,5);
109 if(tmp
== 0){std::cerr
<< "ERROR: Can't blit. Image is null.\n"; exit(1);}
112 SDL_Rect
*dest
= new SDL_Rect
;
115 dest
->w
= tmp
->w
/numframes
;
118 //make the src rect based on the frame info
119 SDL_Rect
*src
= new SDL_Rect
;
120 src
->x
= frame
* (tmp
->w
/numframes
);
121 src
->y
= tmp
->clip_rect
.y
;
122 src
->w
= tmp
->w
/numframes
;
125 SDL_BlitSurface(tmp
, src
, screen
, dest
);
131 //function for simple intermediate blits (non-animated)
132 int l_blit_i(lua_State
*L
){
133 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,2);
135 int x
= lua_tointeger(L
,3);
136 int y
= lua_tointeger(L
,4);
137 double r
= lua_tonumber(L
,5);
138 bool rc
= lua_toboolean(L
,6);
139 bool smooth
= lua_toboolean(L
,7);
141 if(tmp
== 0){std::cerr
<< "ERROR: Can't blit. Image is null.\n"; exit(1);}
143 SDL_Rect
*dest
= new SDL_Rect
;
149 //rotate the surface, adjusting the center if needed.
151 rot
= rotozoomSurface(tmp
, r
, 1.0, (smooth
? SMOOTHING_ON
: SMOOTHING_OFF
));
153 dest
->x
+= (dest
->w
- rot
->w
)/2;
154 dest
->y
+= (dest
->h
- rot
->h
)/2;
161 SDL_BlitSurface(tmp
, &tmp
->clip_rect
, (SDL_Surface
*)lua_touserdata(L
,1), dest
);
163 if(r
!= 0){SDL_FreeSurface(rot
);}
167 //function for intermediate blitting a frame of an animation
168 // does not do rotation or scaling (yet)
169 //frames are laid out horizontally
171 int l_blitframe_i(lua_State
*L
){
172 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,2);
173 int x
= lua_tointeger(L
,3);
174 int y
= lua_tointeger(L
,4);
175 int numframes
= lua_tointeger(L
,5);
176 int frame
= lua_tointeger(L
,6);
178 if(tmp
== 0){std::cerr
<< "ERROR: Can't blit. Image is null.\n"; exit(1);}
181 SDL_Rect
*dest
= new SDL_Rect
;
184 dest
->w
= tmp
->w
/numframes
;
187 //make the src rect based on the frame info
188 SDL_Rect
*src
= new SDL_Rect
;
189 src
->x
= frame
* (tmp
->w
/numframes
);
190 src
->y
= tmp
->clip_rect
.y
;
191 src
->w
= tmp
->w
/numframes
;
194 SDL_BlitSurface(tmp
, src
, (SDL_Surface
*)lua_touserdata(L
,1), dest
);
202 int l_show_cursor(lua_State
*L
){
203 bool show
= lua_toboolean(L
, 1);
205 SDL_ShowCursor((show
? SDL_ENABLE
: SDL_DISABLE
));
210 //fills screen with a color
211 int l_fill_screen(lua_State
*L
){
212 SDL_FillRect(screen
, NULL
, SDL_MapRGB(screen
->format
, lua_tointeger(L
,1),lua_tointeger(L
,2),lua_tointeger(L
,3)));
216 //creates a 32-bpp surface
217 int l_create_intermediate(lua_State
*L
){
218 SDL_Surface
*tmp
= SDL_CreateRGBSurface(SDL_SWSURFACE
|SDL_SRCALPHA
,(int)lua_tointeger(L
,1),(int)lua_tointeger(L
,2), 32, 0, 0, 0, 0);
219 SDL_SetAlpha(tmp
, SDL_SRCALPHA
, SDL_ALPHA_OPAQUE
);
221 lua_pushlightuserdata(L
, tmp
);
225 //free surface interface
226 int l_delete_image(lua_State
*L
){
227 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,1);
229 if(tmp
== 0){std::cerr
<< "ERROR: Can't free() null image.\n"; exit(1);}
231 SDL_FreeSurface(tmp
);
237 //rotozoomer interface
238 int l_rotzoom(lua_State
*L
){
239 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,1);
240 double angle
= lua_tonumber(L
,2);
241 double zoom
= lua_tonumber(L
,3);
243 if(tmp
== 0){std::cerr
<< "ERROR: Can't rotate/zoom. Source image is null.\n"; exit(1);}
245 SDL_Surface
*zoomed
= rotozoomSurface(tmp
, angle
, zoom
, SMOOTHING_OFF
);
247 lua_pushlightuserdata(L
, zoomed
);
248 lua_pushinteger(L
, (lua_Integer
)zoomed
->w
);
249 lua_pushinteger(L
, (lua_Integer
)zoomed
->h
);