Lots of changes. Good number of small fixes. Some new stuff.
[luagame.git] / funcs_video.cpp
blob30bb8b849e095e059dba89aeb8cf74e6bbc05c3b
1 /*
2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
5 */
7 #include <iostream>
9 #include "SDL/SDL.h"
10 #include "SDL/SDL_rotozoom.h"
11 #include "SDL/SDL_image.h"
12 #include "lua.hpp"
13 #include "globals.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());
22 SDL_Surface *tmp = 0;
24 if(temp == NULL){
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);
30 }else{
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);
38 }else{
39 lua_pushlightuserdata(L, im->second);
40 lua_pushinteger(L, (lua_Integer)im->second->w);
41 lua_pushinteger(L, (lua_Integer)im->second->h);
43 return 3;
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);
56 return 0;
59 //function for updating the display surface
60 int l_flip(lua_State *L){
61 SDL_Flip(screen);
62 return 0;
65 //function for simple blits (non-animated)
66 int l_blit(lua_State *L){
67 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
68 SDL_Surface *rot;
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);
75 if(tmp == NULL){
76 return luaL_error(L, "ERROR: Can't blit. Image is null.");
79 SDL_Rect *dest = new SDL_Rect;
80 dest->x = x;
81 dest->y = y;
82 dest->w = tmp->w;
83 dest->h = tmp->h;
85 //rotate the surface, adjusting the center if needed.
86 if(r != 0.0){
87 rot = rotozoomSurface(tmp, r, 1.0, (smooth ? SMOOTHING_ON : SMOOTHING_OFF));
88 if(rc){
89 dest->x += (dest->w - rot->w)/2;
90 dest->y += (dest->h - rot->h)/2;
91 dest->w = rot->w;
92 dest->h = rot->h;
94 tmp = rot;
97 SDL_BlitSurface(tmp, &tmp->clip_rect, screen, dest);
98 delete dest;
99 if(r != 0){SDL_FreeSurface(rot);}
100 return 0;
103 //function for blitting a frame of an animation
104 //frames are laid out horizontally
105 //frames start at 0
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);
113 if(tmp == NULL){
114 return luaL_error(L, "ERROR: Can't blit. Image is null.");
117 //make the dest rect
118 SDL_Rect *dest = new SDL_Rect;
119 dest->x = x;
120 dest->y = y;
121 dest->w = tmp->w/numframes;
122 dest->h = tmp->h;
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;
129 src->h = tmp->h;
131 SDL_BlitSurface(tmp, src, screen, dest);
132 delete dest;
133 delete src;
134 return 0;
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);
140 SDL_Surface *rot;
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);
147 if(tmp == NULL){
148 return luaL_error(L, "ERROR: Can't blit. Image is null.");
151 SDL_Rect *dest = new SDL_Rect;
152 dest->x = x;
153 dest->y = y;
154 dest->w = tmp->w;
155 dest->h = tmp->h;
157 //rotate the surface, adjusting the center if needed.
158 if(r != 0.0){
159 rot = rotozoomSurface(tmp, r, 1.0, (smooth ? SMOOTHING_ON : SMOOTHING_OFF));
160 if(rc){
161 dest->x += (dest->w - rot->w)/2;
162 dest->y += (dest->h - rot->h)/2;
163 dest->w = rot->w;
164 dest->h = rot->h;
166 tmp = rot;
169 SDL_BlitSurface(tmp, &tmp->clip_rect, (SDL_Surface*)lua_touserdata(L,1), dest);
170 delete dest;
171 if(r != 0){SDL_FreeSurface(rot);}
172 return 0;
175 //function for intermediate blitting a frame of an animation
176 // does not do rotation or scaling (yet)
177 //frames are laid out horizontally
178 //frames start at 0
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);
186 if(tmp == NULL){
187 return luaL_error(L, "ERROR: Can't blit. Image is null.");
190 //make the dest rect
191 SDL_Rect *dest = new SDL_Rect;
192 dest->x = x;
193 dest->y = y;
194 dest->w = tmp->w/numframes;
195 dest->h = tmp->h;
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;
202 src->h = tmp->h;
204 SDL_BlitSurface(tmp, src, (SDL_Surface*)lua_touserdata(L,1), dest);
205 delete dest;
206 delete src;
207 return 0;
211 //cursor toggler
212 int l_show_cursor(lua_State *L){
213 bool show = lua_toboolean(L, 1);
215 SDL_ShowCursor((show ? SDL_ENABLE : SDL_DISABLE));
217 return 0;
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)));
223 return 0;
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);
232 return 1;
235 //free surface interface
236 int l_delete_image(lua_State *L){
237 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
239 if(tmp == NULL){
240 return luaL_error(L, "ERROR: Can't blit. Image is null.");
243 SDL_FreeSurface(tmp);
244 lua_pushnil(L);
245 return 1;
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);
256 if(tmp == NULL){
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);
265 return 3;