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
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());
25 return luaL_error(L
, "ERROR: Can't find image file: \"%s\".", filename
.c_str());
28 if(temp
->flags
& SDL_SRCALPHA
){
29 tmp
= SDL_DisplayFormatAlpha(temp
);
31 tmp
= SDL_DisplayFormat(temp
);
33 SDL_FreeSurface(temp
);
34 image_store
[filename
] = tmp
;
35 lua_pushlightuserdata(L
, tmp
);
36 lua_pushinteger(L
, (lua_Integer
)tmp
->w
);
37 lua_pushinteger(L
, (lua_Integer
)tmp
->h
);
39 lua_pushlightuserdata(L
, im
->second
);
40 lua_pushinteger(L
, (lua_Integer
)im
->second
->w
);
41 lua_pushinteger(L
, (lua_Integer
)im
->second
->h
);
46 //forces an image to be unloaded from memory
47 int l_releaseimage(lua_State
*L
){
48 std::string
filename(luaL_checkstring(L
,1));
49 std::map
<std::string
, SDL_Surface
*>::iterator im
= image_store
.find(filename
);
51 if(im
!= image_store
.end()){
52 SDL_FreeSurface(im
->second
);
53 image_store
.erase(im
);
59 //function for updating the display surface
60 int l_flip(lua_State
*L
){
65 //function for simple blits (non-animated)
66 int l_blit(lua_State
*L
){
67 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,1);
69 int x
= lua_tointeger(L
,2);
70 int y
= lua_tointeger(L
,3);
71 double r
= lua_tonumber(L
,4);
72 bool rc
= lua_toboolean(L
,5);
73 bool smooth
= lua_toboolean(L
,6);
76 return luaL_error(L
, "ERROR: Can't blit. Image is null.");
79 SDL_Rect
*dest
= new SDL_Rect
;
85 //rotate the surface, adjusting the center if needed.
87 rot
= rotozoomSurface(tmp
, r
, 1.0, (smooth
? SMOOTHING_ON
: SMOOTHING_OFF
));
89 dest
->x
+= (dest
->w
- rot
->w
)/2;
90 dest
->y
+= (dest
->h
- rot
->h
)/2;
97 SDL_BlitSurface(tmp
, &tmp
->clip_rect
, screen
, dest
);
99 if(r
!= 0){SDL_FreeSurface(rot
);}
103 //function for blitting a frame of an animation
104 //frames are laid out horizontally
106 int l_blitframe(lua_State
*L
){
107 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,1);
108 int x
= lua_tointeger(L
,2);
109 int y
= lua_tointeger(L
,3);
110 int numframes
= lua_tointeger(L
,4);
111 int frame
= lua_tointeger(L
,5);
114 return luaL_error(L
, "ERROR: Can't blit. Image is null.");
118 SDL_Rect
*dest
= new SDL_Rect
;
121 dest
->w
= tmp
->w
/numframes
;
124 //make the src rect based on the frame info
125 SDL_Rect
*src
= new SDL_Rect
;
126 src
->x
= frame
* (tmp
->w
/numframes
);
127 src
->y
= tmp
->clip_rect
.y
;
128 src
->w
= tmp
->w
/numframes
;
131 SDL_BlitSurface(tmp
, src
, screen
, dest
);
137 //function for simple intermediate blits (non-animated)
138 int l_blit_i(lua_State
*L
){
139 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,2);
141 int x
= lua_tointeger(L
,3);
142 int y
= lua_tointeger(L
,4);
143 double r
= lua_tonumber(L
,5);
144 bool rc
= lua_toboolean(L
,6);
145 bool smooth
= lua_toboolean(L
,7);
148 return luaL_error(L
, "ERROR: Can't blit. Image is null.");
151 SDL_Rect
*dest
= new SDL_Rect
;
157 //rotate the surface, adjusting the center if needed.
159 rot
= rotozoomSurface(tmp
, r
, 1.0, (smooth
? SMOOTHING_ON
: SMOOTHING_OFF
));
161 dest
->x
+= (dest
->w
- rot
->w
)/2;
162 dest
->y
+= (dest
->h
- rot
->h
)/2;
169 SDL_BlitSurface(tmp
, &tmp
->clip_rect
, (SDL_Surface
*)lua_touserdata(L
,1), dest
);
171 if(r
!= 0){SDL_FreeSurface(rot
);}
175 //function for intermediate blitting a frame of an animation
176 // does not do rotation or scaling (yet)
177 //frames are laid out horizontally
179 int l_blitframe_i(lua_State
*L
){
180 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,2);
181 int x
= lua_tointeger(L
,3);
182 int y
= lua_tointeger(L
,4);
183 int numframes
= lua_tointeger(L
,5);
184 int frame
= lua_tointeger(L
,6);
187 return luaL_error(L
, "ERROR: Can't blit. Image is null.");
191 SDL_Rect
*dest
= new SDL_Rect
;
194 dest
->w
= tmp
->w
/numframes
;
197 //make the src rect based on the frame info
198 SDL_Rect
*src
= new SDL_Rect
;
199 src
->x
= frame
* (tmp
->w
/numframes
);
200 src
->y
= tmp
->clip_rect
.y
;
201 src
->w
= tmp
->w
/numframes
;
204 SDL_BlitSurface(tmp
, src
, (SDL_Surface
*)lua_touserdata(L
,1), dest
);
212 int l_show_cursor(lua_State
*L
){
213 bool show
= lua_toboolean(L
, 1);
215 SDL_ShowCursor((show
? SDL_ENABLE
: SDL_DISABLE
));
220 //fills screen with a color
221 int l_fill_screen(lua_State
*L
){
222 SDL_FillRect(screen
, NULL
, SDL_MapRGB(screen
->format
, lua_tointeger(L
,1),lua_tointeger(L
,2),lua_tointeger(L
,3)));
226 //creates a 32-bpp surface
227 int l_create_intermediate(lua_State
*L
){
228 SDL_Surface
*tmp
= SDL_CreateRGBSurface(SDL_SWSURFACE
|SDL_SRCALPHA
,(int)lua_tointeger(L
,1),(int)lua_tointeger(L
,2), 32, 0, 0, 0, 0);
229 SDL_SetAlpha(tmp
, SDL_SRCALPHA
, SDL_ALPHA_OPAQUE
);
231 lua_pushlightuserdata(L
, tmp
);
235 //free surface interface
236 int l_delete_image(lua_State
*L
){
237 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,1);
240 return luaL_error(L
, "ERROR: Can't blit. Image is null.");
243 SDL_FreeSurface(tmp
);
249 //rotozoomer interface
250 int l_rotzoom(lua_State
*L
){
251 SDL_Surface
*tmp
= (SDL_Surface
*)lua_touserdata(L
,1);
252 double angle
= lua_tonumber(L
,2);
253 double zoom
= lua_tonumber(L
,3);
254 bool smooth
= lua_toboolean(L
,4);
257 return luaL_error(L
, "ERROR: Can't rotate/zoom. Source image is null.");
260 SDL_Surface
*zoomed
= rotozoomSurface(tmp
, angle
, zoom
, (smooth
? SMOOTHING_ON
: SMOOTHING_OFF
));
262 lua_pushlightuserdata(L
, zoomed
);
263 lua_pushinteger(L
, (lua_Integer
)zoomed
->w
);
264 lua_pushinteger(L
, (lua_Integer
)zoomed
->h
);