Пример #1
0
 lua_State *lua_core_init() {
     lua_State *L = lua_open();
     luaL_openlibs(L);
     luaopen_marshal(L);
     luaopen_graphics(L);
     luaopen_font(L);
     luaopen_sound(L);
     luaopen_system(L);
     luaopen_image(L);
     luaopen_window(L);
     luaopen_directory(L);
     luaopen_matrix4x4(L);
     
     return L;
 }
Пример #2
0
/* main - function - entry point */
int main(int argc, char *argv[]) {
	lua_State *L;
	SDL_Event event;
	Uint32 lastTick;	/* Last iteration's tick value */
	Uint32 delta = 0;
	int i, n, narg;
	char *filename = NULL;

	fprintf(stdout, "%s v%s\n", PROG_NAME, VERSION);

	if (argc > 1) {
		n = 1;
		if (strcmp(argv[1], "--") != 0)
			filename = argv[1];			
	} else {
		n = argc - 1;
	}

	L = luaL_newstate();	/* initialize Lua */
	luaL_openlibs(L);	/* load Lua base libraries */

	FS_Init(L, argv, &filename);	/* initialize virtual filesystem */

	/* register Lua functions */
	lua_newtable(L);
	luaopen_main(L, NULL);
	luaopen_fileio(L, NULL);
	luaopen_font(L, NULL);
	luaopen_graphics(L, NULL);
	luaopen_sound(L, NULL);
	luaopen_mouse(L, NULL);
	luaopen_keyboard(L, NULL);
	luaopen_movie(L, NULL);
	lua_setglobal(L, NAMESPACE);
	
	/* put luaopen_oocairo in package.preload["oocairo"] */
	lua_getglobal(L, "package");
	lua_getfield(L, -1, "preload");
	lua_pushcfunction(L, luaopen_oocairo);
	lua_setfield(L, -2, "oocairo");
	
	/* put luaopen_socket_core in package.preload["socket.core"] */
	lua_pushcfunction(L, luaopen_socket_core);
	lua_setfield(L, -2, "socket.core");
	
	/* put luaopen_mime_core in package.preload["mime.core"] */
	lua_pushcfunction(L, luaopen_mime_core);
	lua_setfield(L, -2, "mime.core");
	
	lua_pop(L, 2);	
	
	/* push the error function for the protected calls later on */
	lua_pushcfunction(L, error_function);

	/* load and compile main lua file */
	if (FS_loadFile(L, filename) == FILEIO_ERROR) {
		error(L, "Error loading '%s':\n\t%s", filename, lua_tostring(L, -1));
	}

	/* push all script arguments */
	/* create arg-table for the command-line-arguments */
	/* copied from lua.c of the Lua distribution */
	narg = argc - (n+1); /* number of arguments to the script */
	luaL_checkstack(L, narg+2, "too many arguments to script");
	for (i=n+1; i<argc; i++) {
		lua_pushstring(L, argv[i]);
	}
	lua_createtable(L, narg, n+1);
	for (i=0; i<argc; i++) {
		lua_pushstring(L, argv[i]);
		lua_rawseti(L, -2, i - n);
	}
	lua_setglobal(L, "arg");

	/* run the compiled chunk */
	if ((lua_pcall(L, narg, 0, -(narg+2)) != 0) && !check_for_exit(L)) {
		error(L, "Error running '%s':\n\t%s", filename, lua_tostring(L, -1));
	}

	/* the error function stays on the stack */

	if (SDL_GetVideoSurface() == NULL)
		error(L, "Error: " PROG_NAME " was not initialized by " NAMESPACE ".init()!\n");

	/* main loop */

	/* Wait until SDL_QUIT event type (window closed) or a call to scrupp.exit() occurs */
	while ( !done ) {
		lastTick = SDL_GetTicks();

		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

		/* maybe the 'main' table has changed
		   so get a new reference in every cycle */
		lua_getglobal(L, "main");		

		if (lua_isnil(L, -1)) {
			error(L, "Error: Table 'main' not found!\n");
		}

		/* at this point, the stack always contains 
		   the error function and the 'main' table */

		/* main.render(delta) */
		lua_getfield(L, -1, "render");
		lua_pushinteger(L, delta);
		if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) {
			error(L, "Error running main.render:\n\t%s\n", lua_tostring(L, -1));
		}

		SDL_GL_SwapBuffers();

		while ( SDL_PollEvent( &event ) ) {
			switch ( event.type ) {
			case SDL_QUIT:
				done = 1;
				break;

			case SDL_KEYDOWN:
				lua_getfield(L, -1, "keypressed");
				if (lua_isnil(L, -1)) {
					lua_pop(L, 1); /* pop if it's nil */
					break;
				}
				lua_pushliteral(L, "Scrupp:key_table");
				lua_rawget(L, LUA_REGISTRYINDEX);
				lua_rawgeti(L, -1, event.key.keysym.sym);
				lua_remove(L, -2); /* remove the key_table */
				if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) {
					error(L, "Error running main.keypressed:\n\t%s\n", lua_tostring(L, -1));
				}
				break;

			case SDL_KEYUP:
				lua_getfield(L, -1, "keyreleased");
				if (lua_isnil(L, -1)) {
					lua_pop(L, 1); /* pop if it's nil */
					break;
				}
				lua_pushliteral(L, "Scrupp:key_table");
				lua_rawget(L, LUA_REGISTRYINDEX);
				lua_rawgeti(L, -1, event.key.keysym.sym);
				lua_remove(L, -2); /* remove the key_table */				
				if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) {
					error(L, "Error running main.keyreleased:\n\t%s\n", lua_tostring(L, -1));
				}
				break;

			case SDL_MOUSEBUTTONDOWN:
				lua_getfield(L, -1, "mousepressed");
				if (lua_isnil(L, -1)) {
					lua_pop(L, 1); /* pop if it's nil */
					break;
				}
				lua_pushinteger(L, event.button.x);
				lua_pushinteger(L, event.button.y);
				lua_pushstring(L, buttonNames[event.button.button-1]);
				if ((lua_pcall(L, 3, 0, -6) != 0) && !check_for_exit(L)) {
					error(L, "Error running main.mousepressed:\n\t%s\n", lua_tostring(L, -1));
				}
				break;

			case SDL_MOUSEBUTTONUP:
				lua_getfield(L, -1, "mousereleased");
				if (lua_isnil(L, -1)) {
					lua_pop(L, 1); /* pop if it's nil */
					break;
				}
				lua_pushinteger(L, event.button.x);
				lua_pushinteger(L, event.button.y);
				lua_pushstring(L, buttonNames[event.button.button-1]);
				if ((lua_pcall(L, 3, 0, -6) != 0) && !check_for_exit(L)) {
					error(L, "Error running main.mousereleased:\n\t%s\n", lua_tostring(L, -1));
				}
				break;
				
			case SDL_VIDEORESIZE:
				lua_getfield(L, -1, "resized");
				if (lua_isnil(L, -1)) {
					lua_pop(L, 1); /* pop if it's nil */
					break;
				}
				lua_pushinteger(L, event.resize.w);
				lua_pushinteger(L, event.resize.h);
				if ((lua_pcall(L, 2, 0, -5) != 0) && !check_for_exit(L)) {
					error(L, "Error running main.resized:\n\t%s\n", lua_tostring(L, -1));
				}
				break;
			}
		}

		lua_pop(L, 1);	/* pop 'main' table */

		delta = SDL_GetTicks() - lastTick;

		/*
		if (delta>1) {
			fprintf(stdout, "delta: %d\n", delta);
		}
		*/

		while (delta < minimumDelta) {
			SDL_Delay(1);
			delta = SDL_GetTicks() - lastTick;
		}

	}
	lua_close(L);
	return 0;
}
Пример #3
0
int
sys_start(int argc, char *argv[])
{
	int status;
	const char* f;
	lua_State *L;
	
	if (argc<2) {
		printf("%s: <filename.lua>\n", argv[0]);
		return 1;
	}
	
    s_init();
    log_print("sound> initialized\n");
	
	L = lua_open();
	/*luaopen_base(L);*/
	luaL_openlibs(L);
	luaopen_sys(L);
	luaopen_log(L);
	luaopen_mat4(L);
	luaopen_quat(L);
	luaopen_vec3(L);
	luaopen_event(L);
	luaopen_image(L);
	luaopen_render(L);
	luaopen_shader(L);
	luaopen_sound(L);
	luaopen_net(L);
	luaopen_util(L);
	luaopen_font(L);
	luaopen_md2(L);
	//luaopen_blender(L);
	
	lua_getglobal(L, "package");
	if (LUA_TTABLE != lua_type(L, 1)) {
		log_print("lua> 'package' is not a table\n");
		return 1;
	}
	lua_getfield(L, 1, "path");
	if (LUA_TSTRING != lua_type(L, 2)) {
		log_print("lua> 'package.path' is not a string\n");
		lua_pop(L, 1);
		return 1;
	}
	lua_pop(L, 1);
	
#if defined(__IPHONE__)
	f = full_path();
#else
	f = ".";
#endif

	/* package.path = f .. "/?.lua" */
	lua_pushlstring(L, f, strlen(f));
	lua_pushliteral(L, "/?.lua");
	lua_concat(L, 2);
	lua_setfield(L, 1, "path");

	lua_bootstrap(L, f);

#if defined(__IPHONE__)
	f = full_path_to_file(argv[1]);
#else
	f = argv[1];
#endif

	log_print("lua> initialized\n");
	printf("lua> loading %s\n", f);
	
	if (luaL_loadfile(L,f)==0) { 
		/* function runme = file_content */
		lua_setglobal(L, "runme");
		lua_getglobal(L, "runme");

		status = lua_pcall(L,0,LUA_MULTRET,0);
		assert(0 == report(L, status));
        
        if (lua_gettop(L) > 0) {
            status = lua_toboolean(L, -1);
            if (!status) {
                printf("lua> %s returned false\n", f);
                sys_quit();
            }
        }
		
        log_print("lua> loaded\n");
	} else {
		printf("lua> unable to load %s\n",f);
		report(L, status);
		sys_quit();
	}

	lua_state = L;
	
	/* TODO: Double check this stuff some day when bored. */
	return status > 0 ? false : true;	
}