Finished ParticleSystem and delta-animation support.
[luagame.git] / funcs_video.cpp
blob018f6ff9121ea0bb0d05568e0710e607aa09fab4
1 /*
2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
5 */
7 #include "SDL/SDL.h"
8 #include "SDL/SDL_rotozoom.h"
9 #include "SDL/SDL_image.h"
10 #include "lua.hpp"
11 #include "globals.h"
13 //function for loading a surface from many different formats (except TIFF)
14 int l_getimage(lua_State *L){
15 std::string filename(luaL_checkstring(L,1));
16 std::map<std::string, SDL_Surface *>::iterator im = image_store.find(filename);
18 if(im == image_store.end()){
19 SDL_Surface *tmp = IMG_Load(filename.c_str());
20 image_store[filename] = tmp;
21 lua_pushlightuserdata(L, tmp);
22 lua_pushinteger(L, (lua_Integer)tmp->w);
23 lua_pushinteger(L, (lua_Integer)tmp->h);
24 }else{
25 lua_pushlightuserdata(L, im->second);
26 lua_pushinteger(L, (lua_Integer)im->second->w);
27 lua_pushinteger(L, (lua_Integer)im->second->h);
29 return 3;
32 //forces an image to be unloaded from memory
33 int l_releaseimage(lua_State *L){
34 std::string filename(luaL_checkstring(L,1));
35 std::map<std::string, SDL_Surface *>::iterator im = image_store.find(filename);
37 if(im != image_store.end()){
38 SDL_FreeSurface(im->second);
39 image_store.erase(im);
42 return 0;
45 //function for updating the display surface
46 int l_flip(lua_State *L){
47 SDL_Flip(screen);
48 return 0;
51 //function for simple blits (non-animated)
52 int l_blit(lua_State *L){
53 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
54 int x = lua_tointeger(L,2);
55 int y = lua_tointeger(L,3);
57 SDL_Rect *dest = new SDL_Rect;
58 dest->x = x;
59 dest->y = y;
60 dest->w = tmp->w;
61 dest->h = tmp->h;
63 SDL_BlitSurface(tmp, &tmp->clip_rect, screen, dest);
64 delete dest;
65 return 0;
68 //function for blitting a frame of an animation
69 //frames are laid out horizontally
70 //frames start at 0
71 int l_blitframe(lua_State *L){
72 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
73 int x = lua_tointeger(L,2);
74 int y = lua_tointeger(L,3);
75 int numframes = lua_tointeger(L,4);
76 int frame = lua_tointeger(L,5);
78 //make the dest rect
79 SDL_Rect *dest = new SDL_Rect;
80 dest->x = x;
81 dest->y = y;
82 dest->w = tmp->w/numframes;
83 dest->h = tmp->h;
85 //make the src rect based on the frame info
86 SDL_Rect *src = new SDL_Rect;
87 src->x = frame * (tmp->w/numframes);
88 src->y = tmp->clip_rect.y;
89 src->w = tmp->w/numframes;
90 src->h = tmp->h;
92 SDL_BlitSurface(tmp, src, screen, dest);
93 delete dest;
94 delete src;
95 return 0;
98 //cursor toggler
99 int l_show_cursor(lua_State *L){
100 bool show = lua_toboolean(L, 1);
102 SDL_ShowCursor((show ? SDL_ENABLE : SDL_DISABLE));
104 return 0;
107 //fills screen with a color
108 int l_fill_screen(lua_State *L){
109 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, lua_tointeger(L,1),lua_tointeger(L,2),lua_tointeger(L,3)));
110 return 0;
113 //free surface interface (sad but true (-_-))
114 int l_delete_image(lua_State *L){
115 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
116 SDL_FreeSurface(tmp);
117 lua_pushnil(L);
118 return 1;
122 //rotozoomer interface
123 int l_rotzoom(lua_State *L){
124 SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);
125 double angle = lua_tonumber(L,2);
126 double zoom = lua_tonumber(L,3);
128 SDL_Surface *zoomed = rotozoomSurface(tmp, angle, zoom, SMOOTHING_OFF);
130 lua_pushlightuserdata(L, zoomed);
131 lua_pushinteger(L, (lua_Integer)zoomed->w);
132 lua_pushinteger(L, (lua_Integer)zoomed->h);
133 return 3;