-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelscript.c
More file actions
159 lines (127 loc) · 4.1 KB
/
levelscript.c
File metadata and controls
159 lines (127 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdlib.h>
#include <string.h>
#include <cairo/cairo.h>
#include "warning.h"
#include "X.h"
#include "window.h"
#include "script.h"
#include "object.h"
#include "rules.h"
#include "layers.h"
#include "gsaves.h"
#include "levelscript.h"
#include "main.h"
#include "draw.h"
#include "keyboard.h"
#include "gmoves.h"
#include "moves.h"
#include "menudraw.h"
char *room_codename = NULL;
int script_set_codename(lua_State *L) // nastavi room_codename
{
const char *codename = luaL_checkstring(L, 1);
if(room_codename) free(room_codename);
room_codename = strdup(codename);
update_window_title();
return 0;
}
int script_openpng(lua_State *L) // otevre PNG soubor a skriptu preda jeho pixely
{
const char *filename = luaL_checkstring(L, 1);
cairo_surface_t *surf;
unsigned char *data;
int datalength;
int width, height;
cairo_surface_t *tmpsurf; // kdyby nevysel format
cairo_t *cr;
surf = cairo_image_surface_create_from_png (filename); // nacteni obrazku
if(cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS)
error("Opening map \"%s\" failed: %s",
filename, cairo_status_to_string(cairo_surface_status(surf)));
width = cairo_image_surface_get_width(surf); // ziskani velikosti
height = cairo_image_surface_get_height(surf);
if(cairo_image_surface_get_format(surf) != CAIRO_FORMAT_ARGB32){
/*
Jejda, format surface ziskany z PNG souboru neodpovida formatu, v jakem
chci pixely predat. Je treba vyrobit tedy surface o spravnem formatu
(CAIRO_FORMAT_ARGB32), do ktere obrazek zkopiruji.
*/
tmpsurf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cr = cairo_create(tmpsurf); // kopiruji
cairo_set_source_surface (cr, surf, 0, 0);
cairo_paint(cr);
cairo_destroy(cr); // rusim puvodni surface
cairo_surface_destroy(surf);
surf = tmpsurf;
}
data = cairo_image_surface_get_data(surf); // ziskam pixely
datalength = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width) * height; // a delku pole
lua_pushlstring(L, (char *)data, datalength); // predam lue
cairo_surface_destroy(surf); // spolu se surface se z pameti uvolni i pole data, lua ma nastesi kopii
lua_pushinteger(L, width); // predam vysku a sirku
lua_pushinteger(L, height);
return 3; // vracim 3 parametry
}
static void run_level() // spusti level nezavisle na tom, zda je uzivatelsky nebo ne
{
level_anim_init();
level_keys_init();
level_gmoves_init();
level_gsaves_init();
finish_objects();
init_rules();
init_moves();
unanim_fish_rectangle();
calculatewin();
if(!userlev) menu_create_icon(); // kdyby mi chybel nahled v mape mistnosti, tak ho vytvorim
}
void open_level(char *codename) /* Otevre mistnost z menu. Jedine, co vlastne dela je,
ze spusti stejnojmennou lua funkci. */
{
room_codename = NULL;
lua_getglobal(luastat, "script_open_level");
lua_pushstring(luastat, codename);
lua_call(luastat, 1, 0);
run_level();
}
void open_user_level() /* Otevre mistnost z menu. Jedine, co vlastne dela je, ze spusti stejnojmennou
lua funkci s parametrem userlev (tam je cesta, kterou uzivatel zadal). */
{
room_codename = NULL;
lua_getglobal(luastat, "script_open_user_level");
lua_pushstring(luastat, userlev);
lua_call(luastat, 1, 0);
run_level();
}
void end_level() // ukonci level
{
lua_getglobal(luastat, "script_end_level"); // zavola stejnojmennou lua funkci
lua_call(luastat, 0, 0);
delete_layers(); // a rusi dalsi struktury
free_rules();
free_moves();
delete_objects();
delete_gsaves();
if(room_codename){ // Z titulku okna zas zmizi "level '$codename'"
free(room_codename);
room_codename = NULL;
update_window_title();
}
}
void refresh_user_level() // obnovi uzivatelsky level
{
if(!userlev) return;
end_level();
open_user_level();
}
int script_flip_object(lua_State *L) // zmeni flip u zadaneho objektu
{
ff_object *obj = (ff_object *)my_luaL_checkludata(L, 1);
obj->gflip = obj->flip = 1-obj->flip;
return 0;
}
int script_start_active_fish(lua_State *L) // nastavi rybu aktivni na zacatku
{
start_active_fish = (ff_object *)my_luaL_checkludata(L, 1);
return 0;
}