Initial Commit.
[luagame.git] / funcs_video.h
blob4cac78e7331dee5dbdf3afcc016d1e5c5754499c
1 #include "SDL/SDL.h"
2 #include "SDL/SDL_rotozoom.h"
3 #include "SDL/SDL_image.h"
4 #include "lua.hpp"
5 #include "globals.h"
7 //function for loading a surface from many different formats (except TIFF)
8 static int l_getimage(lua_State *L){
9 std::string filename(luaL_checkstring(L,1));
10 std::map<std::string, SDL_Surface *>::iterator im = image_store.find(filename);
12 if(im == image_store.end()){
13 SDL_Surface *tmp = IMG_Load(filename.c_str());
14 image_store[filename] = tmp;
15 lua_pushlightuserdata(L, tmp);
16 lua_pushinteger(L, (lua_Integer)tmp->w);
17 lua_pushinteger(L, (lua_Integer)tmp->h);
18 }else{
19 lua_pushlightuserdata(L, im->second);
20 lua_pushinteger(L, (lua_Integer)im->second->w);
21 lua_pushinteger(L, (lua_Integer)im->second->h);
23 return 3;
26 //forces an image to be unloaded from memory
27 static int l_releaseimage(lua_State *L){
28 std::string filename(luaL_checkstring(L,1));
29 std::map<std::string, SDL_Surface *>::iterator im = image_store.find(filename);
31 if(im != image_store.end()){
32 SDL_FreeSurface(im->second);
33 image_store.erase(im);
36 return 0;
39 //function for updating the display surface
40 static int l_flip(lua_State *L){
41 SDL_Flip(screen);
42 return 0;
45 //function for simple blits (non-animated)
46 static int l_blit(lua_State *L){
47 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
48 int x = lua_tointeger(L,2);
49 int y = lua_tointeger(L,3);
51 SDL_Rect *dest = new SDL_Rect;
52 dest->x = x;
53 dest->y = y;
54 dest->w = tmp->w;
55 dest->h = tmp->h;
57 SDL_BlitSurface(tmp, &tmp->clip_rect, screen, dest);
58 delete dest;
59 return 0;
62 //function for blitting a frame of an animation
63 //frames are laid out horizontally
64 //frames start at 0
65 static int l_blitframe(lua_State *L){
66 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
67 int x = lua_tointeger(L,2);
68 int y = lua_tointeger(L,3);
69 int numframes = lua_tointeger(L,4);
70 int frame = lua_tointeger(L,5);
72 //make the dest rect
73 SDL_Rect *dest = new SDL_Rect;
74 dest->x = x;
75 dest->y = y;
76 dest->w = tmp->w/numframes;
77 dest->h = tmp->h;
79 //make the src rect based on the frame info
80 SDL_Rect *src = new SDL_Rect;
81 src->x = frame * (tmp->w/numframes);
82 src->y = tmp->clip_rect.y;
83 src->w = tmp->w/numframes;
84 src->h = tmp->h;
86 SDL_BlitSurface(tmp, src, screen, dest);
87 delete dest;
88 delete src;
89 return 0;
92 //cursor toggler
93 static int l_show_cursor(lua_State *L){
94 bool show = lua_toboolean(L, 1);
96 SDL_ShowCursor((show ? SDL_ENABLE : SDL_DISABLE));
98 return 0;
101 //fills screen with a color
102 static int l_fill_screen(lua_State *L){
103 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, lua_tointeger(L,1),lua_tointeger(L,2),lua_tointeger(L,3)));
104 return 0;
107 //free surface interface (sad but true (-_-))
108 static int l_delete_image(lua_State *L){
109 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
110 SDL_FreeSurface(tmp);
111 lua_pushnil(L);
112 return 1;
116 //rotozoomer interface
117 static int l_rotzoom(lua_State *L){
118 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
119 double angle = lua_tonumber(L,2);
120 double zoom = lua_tonumber(L,3);
122 SDL_Surface *zoomed = rotozoomSurface(tmp, angle, zoom, SMOOTHING_OFF);
124 lua_pushlightuserdata(L, zoomed);
125 lua_pushinteger(L, (lua_Integer)zoomed->w);
126 lua_pushinteger(L, (lua_Integer)zoomed->h);
127 return 3;