Fixed stupid syntax error.
[luagame.git] / funcs_video.cpp
blob9c2484a3b4a2c1974dc734ff2ce675200b02d1d5
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 (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());
22 SDL_Surface *tmp;
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);
28 }else{
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);
36 }else{
37 lua_pushlightuserdata(L, im->second);
38 lua_pushinteger(L, (lua_Integer)im->second->w);
39 lua_pushinteger(L, (lua_Integer)im->second->h);
41 return 3;
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);
54 return 0;
57 //function for updating the display surface
58 int l_flip(lua_State *L){
59 SDL_Flip(screen);
60 return 0;
63 //function for simple blits (non-animated)
64 int l_blit(lua_State *L){
65 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
66 SDL_Surface *rot;
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;
76 dest->x = x;
77 dest->y = y;
78 dest->w = tmp->w;
79 dest->h = tmp->h;
81 //rotate the surface, adjusting the center if needed.
82 if(r != 0.0){
83 rot = rotozoomSurface(tmp, r, 1.0, (smooth ? SMOOTHING_ON : SMOOTHING_OFF));
84 if(rc){
85 dest->x += (dest->w - rot->w)/2;
86 dest->y += (dest->h - rot->h)/2;
87 dest->w = rot->w;
88 dest->h = rot->h;
90 tmp = rot;
93 SDL_BlitSurface(tmp, &tmp->clip_rect, screen, dest);
94 delete dest;
95 if(r != 0){SDL_FreeSurface(rot);}
96 return 0;
99 //function for blitting a frame of an animation
100 //frames are laid out horizontally
101 //frames start at 0
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);}
111 //make the dest rect
112 SDL_Rect *dest = new SDL_Rect;
113 dest->x = x;
114 dest->y = y;
115 dest->w = tmp->w/numframes;
116 dest->h = tmp->h;
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;
123 src->h = tmp->h;
125 SDL_BlitSurface(tmp, src, screen, dest);
126 delete dest;
127 delete src;
128 return 0;
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);
134 SDL_Surface *rot;
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;
144 dest->x = x;
145 dest->y = y;
146 dest->w = tmp->w;
147 dest->h = tmp->h;
149 //rotate the surface, adjusting the center if needed.
150 if(r != 0.0){
151 rot = rotozoomSurface(tmp, r, 1.0, (smooth ? SMOOTHING_ON : SMOOTHING_OFF));
152 if(rc){
153 dest->x += (dest->w - rot->w)/2;
154 dest->y += (dest->h - rot->h)/2;
155 dest->w = rot->w;
156 dest->h = rot->h;
158 tmp = rot;
161 SDL_BlitSurface(tmp, &tmp->clip_rect, (SDL_Surface*)lua_touserdata(L,1), dest);
162 delete dest;
163 if(r != 0){SDL_FreeSurface(rot);}
164 return 0;
167 //function for intermediate blitting a frame of an animation
168 // does not do rotation or scaling (yet)
169 //frames are laid out horizontally
170 //frames start at 0
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);}
180 //make the dest rect
181 SDL_Rect *dest = new SDL_Rect;
182 dest->x = x;
183 dest->y = y;
184 dest->w = tmp->w/numframes;
185 dest->h = tmp->h;
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;
192 src->h = tmp->h;
194 SDL_BlitSurface(tmp, src, (SDL_Surface*)lua_touserdata(L,1), dest);
195 delete dest;
196 delete src;
197 return 0;
201 //cursor toggler
202 int l_show_cursor(lua_State *L){
203 bool show = lua_toboolean(L, 1);
205 SDL_ShowCursor((show ? SDL_ENABLE : SDL_DISABLE));
207 return 0;
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)));
213 return 0;
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);
222 return 1;
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);
232 lua_pushnil(L);
233 return 1;
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);
250 return 3;