forked from libcg/XtreamLua-Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtream.c
178 lines (132 loc) · 3.93 KB
/
xtream.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* XtreamLua Player - Final Version
* xtream.c
*
* Copyright:
*
* This work is licensed under the Creative Commons BY-SA 3.0 License.
* See http://creativecommons.org/licenses/by-sa/3.0/ or the LICENSE file
* for more details.
*/
#include "xtream.h"
int XT_waitVblankStart(lua_State *L)
{
int argc = lua_gettop(L);
if (argc != 0 && argc != 1)
return luaL_error(L, "Xtream.waitVblankStart([count]): takes zero or one argument");
if ((argc == 1) && (lua_type(L, 1) == LUA_TNUMBER))
{
int i;
for (i = 0; i < luaL_checkint(L, 1); i++)
sceDisplayWaitVblankStart();
}
else
sceDisplayWaitVblankStart();
return 0;
}
int XT_exitGame(lua_State *L)
{
sceKernelExitGame();
return 0;
}
int XT_delay(lua_State *L)
{
if (lua_gettop(L) != 1)
return luaL_error(L, "Xtream.delay(millisec): wrong number of arguments");
sceKernelDelayThread(luaL_checkint(L, 1) * 1000);
return 0;
}
int XT_print(lua_State *L)
{
int argc = lua_gettop(L);
if (argc < 3 || argc > 4)
return luaL_error(L, "Xtream.print(x,y,text [,color]): wrong number of arguments");
int posx = luaL_checkint(L, 1);
int posy = luaL_checkint(L, 2) + 10;
intraFontSetStyle(luaFont, 0.9f, argc == 3 ? 0xFFFFFFFF : luaL_checknumber(L, 4), 0, 0, INTRAFONT_ALIGN_LEFT);
lua_pushnumber(L, intraFontPrint(luaFont, posx, posy, luaL_checkstring(L, 3)));
return 1;
}
int XT_debugPrint(lua_State *L)
{
int argc = lua_gettop(L);
if (argc != 3 && argc != 4)
return luaL_error(L, "Xtream.debugPrint(x,y,text [,color]): wrong number of arguments");
int posx = luaL_checkint(L, 1);
int posy = luaL_checkint(L, 2);
int n;
const char *buff = luaL_checkstring(L, 3);
g2dColor color = (argc == 4) ? luaL_checknumber(L, 4) : 0xFFFFFFFF;
for (n = 0; n < strlen(buff); n++)
pspDebugScreenPutChar(posx + 7*n, posy, color, buff[n]);
return 0;
}
int XT_getFPS(lua_State *L)
{
lua_n_fps++;
sceRtcGetCurrentTick(&lua_tick);
lua_current_time = lua_tick/1000;
if ((lua_current_time - lua_last_time) >= 1000)
{
lua_fps = lua_n_fps;
lua_n_fps = 0;
lua_last_time = lua_current_time;
}
lua_pushnumber(L, lua_fps);
return 1;
}
int XT_screenshot(lua_State *L)
{
int argc = lua_gettop(L);
if (argc != 1) return luaL_error(L, "Xtream.screenshot(path): takes 1 argument.");
FILE *file = fopen(luaL_checkstring(L, 1), "wb");
if (file == NULL)
return luaL_error(L, "Xtream.screenshot(path): Cannot create/open path file.");
png_structp png_ptr;
png_infop info_ptr;
int i, x, y;
u8* line;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) return luaL_error(L, "Xtream.savescreen: Error creating file's write strcture.");;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr, &info_ptr);
return luaL_error(L, "Xtream.savescreen: Error creating file's info strcture.");
}
png_init_io(png_ptr, file);
png_set_IHDR(png_ptr, info_ptr, g2d_disp_buffer.w, g2d_disp_buffer.h, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
line = (u8*) malloc(480 * 3); // 3 = R, G and B. Alpha isn't needed, save screen, not a texture.
for (y = 0; y < g2d_disp_buffer.h; y++)
{
i = 0;
for (x = 0; x < g2d_disp_buffer.w; x++)
{
g2dColor color = g2d_disp_buffer.data[x + y * 512];
line[i++] = G2D_GET_R(color);
line[i++] = G2D_GET_G(color);
line[i++] = G2D_GET_B(color);
}
png_write_row(png_ptr, line);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
free(line);
fclose(file);
return 0;
}
int Xtream_init(lua_State *L)
{
luaFont = intraFontLoad("flash0:/font/ltn8.pgf", INTRAFONT_CACHE_ASCII); //Loads font used for Xtream.print
if (luaFont == NULL)
return 0;
//FPS init
lua_current_time = 0;
lua_last_time = 0;
lua_n_fps = 0;
lua_fps = 60;
luaL_register(L, "Xtream", XT_methods);
return 1;
}
//EOF