Exemple #1
0
int main(int argc, char* argv[])
{
    SDL_Surface *screen = NULL;
    SDL_Surface *image = NULL;
    const SDL_VideoInfo *videoInfo = NULL;
    FILE* file;

    /*-----------------------------------------------------------------*/

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        fprintf(stderr, "SDL_Init failed - %s\n", SDL_GetError());
        return 1;
    }

    /*-----------------------------------------------------------------*/

    videoInfo = SDL_GetVideoInfo();

    if (videoInfo == 0)
    {
        fprintf(stderr, "SDL_GetVideoInfo failed - %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    /*-----------------------------------------------------------------*/

    screen = SDL_SetVideoMode(image->w,
                              image->h,
                              videoInfo->vfmt->BitsPerPixel,
                              SDL_SWSURFACE);

    if (!screen)
    {
        fprintf(stderr, "SetVideoMode failed - %s\n", SDL_GetError());
        SDL_FreeSurface(image);
        SDL_Quit();
        return 1;
    }

    /*-----------------------------------------------------------------*/

	while(1) {
	    if((file = popen("adb shell \"screencap -p | busybox uuencode -\" |uudecode","r"))==NULL)
	    {
		fprintf(stderr, "can't open dump.png\n");
		return 0;
	    }
	    
	    SDL_RWops *rw = SDL_RWFromFP(file, 0);

	    image = IMG_LoadPNG_RW(rw);
	    
	    SDL_FreeRW(rw);
	    close(file);

	    if (!image)
	    {
		fprintf(stderr, "IMG_LoadBMP_RW failed - %s\n", IMG_GetError());
		SDL_Quit();
		return 1;
	    }

	    /*-----------------------------------------------------------------*/

	    SDL_BlitSurface(image, 0, screen, 0);

	    SDL_Flip(screen);
	    
	    //SDL_Delay(5000);

	    SDL_FreeSurface(image);
	}

    SDL_Quit();

    return 0;
}
Exemple #2
0
/* Load a music file */
Mix_Music *Mix_LoadMUS(const char *file)
{
	SDL_RWops *rw;
	Mix_Music *music;
	Mix_MusicType type;
	char *ext = strrchr(file, '.');

#ifdef CMD_MUSIC
	if ( music_cmd ) {
		/* Allocate memory for the music structure */
		music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
		if ( music == NULL ) {
			Mix_SetError("Out of memory");
			return(NULL);
		}
		music->error = 0;
		music->type = MUS_CMD;
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
		if ( music->data.cmd == NULL ) {
			SDL_free(music);
			music == NULL;
		}
		return music;
	}
#endif

	rw = SDL_RWFromFile(file, "rb");
	if ( rw == NULL ) {
		Mix_SetError("Couldn't open '%s'", file);
		return NULL;
	}

	/* Use the extension as a first guess on the file type */
	type = MUS_NONE;
	ext = strrchr(file, '.');
	/* No need to guard these with #ifdef *_MUSIC stuff,
	 * since we simply call Mix_LoadMUSType_RW() later */
	if ( ext ) {
		++ext; /* skip the dot in the extension */
		if ( MIX_string_equals(ext, "WAV") ) {
			type = MUS_WAV;
		} else if ( MIX_string_equals(ext, "MID") ||
		            MIX_string_equals(ext, "MIDI") ||
		            MIX_string_equals(ext, "KAR") ) {
			type = MUS_MID;
		} else if ( MIX_string_equals(ext, "OGG") ) {
			type = MUS_OGG;
		} else if ( MIX_string_equals(ext, "FLAC") ) {
			type = MUS_FLAC;
		} else 	if ( MIX_string_equals(ext, "MPG") ||
		             MIX_string_equals(ext, "MPEG") ||
		             MIX_string_equals(ext, "MP3") ||
		             MIX_string_equals(ext, "MAD") ) {
			type = MUS_MP3;
		}
	}
	if ( type == MUS_NONE ) {
		type = detect_music_type(rw);
	}

	/* We need to know if a specific error occurs; if not, we'll set a
	 * generic one, so we clear the current one. */
	Mix_SetError("");
	music = Mix_LoadMUSType_RW(rw, type, SDL_TRUE);
	if ( music == NULL && Mix_GetError()[0] == '\0' ) {
		SDL_FreeRW(rw);
		Mix_SetError("Couldn't open '%s'", file);
	}
	return music;
}
Exemple #3
0
SDL_RWops *
SDL_RWFromFile(const char *file, const char *mode)
{
    SDL_RWops *rwops = NULL;
    if (!file || !*file || !mode || !*mode) {
        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
        return NULL;
    }
#if defined(ANDROID)
#ifdef HAVE_STDIO_H
    /* Try to open the file on the filesystem first */
    if (*file == '/') {
        FILE *fp = fopen(file, mode);
        if (fp) {
            return SDL_RWFromFP(fp, 1);
        }
    } else {
        /* Try opening it from internal storage if it's a relative path */
        char *path;
        FILE *fp;

        path = SDL_stack_alloc(char, PATH_MAX);
        if (path) {
            SDL_snprintf(path, PATH_MAX, "%s/%s",
                         SDL_AndroidGetInternalStoragePath(), file);
            fp = fopen(path, mode);
            SDL_stack_free(path);
            if (fp) {
                return SDL_RWFromFP(fp, 1);
            }
        }
    }
#endif /* HAVE_STDIO_H */

    /* Try to open the file from the asset system */
    rwops = SDL_AllocRW();
    if (!rwops)
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
    if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
        SDL_FreeRW(rwops);
        return NULL;
    }
    rwops->size = Android_JNI_FileSize;
    rwops->seek = Android_JNI_FileSeek;
    rwops->read = Android_JNI_FileRead;
    rwops->write = Android_JNI_FileWrite;
    rwops->close = Android_JNI_FileClose;

#elif defined(__WIN32__)
    rwops = SDL_AllocRW();
    if (!rwops)
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
    if (windows_file_open(rwops, file, mode) < 0) {
        SDL_FreeRW(rwops);
        return NULL;
    }
    rwops->size = windows_file_size;
    rwops->seek = windows_file_seek;
    rwops->read = windows_file_read;
    rwops->write = windows_file_write;
    rwops->close = windows_file_close;

#elif HAVE_STDIO_H
    {
    	#ifdef __APPLE__
    	FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
        #else
    	FILE *fp = fopen(file, mode);
    	#endif
    	if (fp == NULL) {
            SDL_SetError("Couldn't open %s", file);
        } else {
            rwops = SDL_RWFromFP(fp, 1);
        }
    }
#else
    SDL_SetError("SDL not compiled with stdio support");
#endif /* !HAVE_STDIO_H */

    return (rwops);
}
Exemple #4
0
static int l_load_music_async_callback(lua_State *L)
{
    load_music_async_t *async = (load_music_async_t*)lua_touserdata(L, 1);

    // Replace light UD with full UD
    lua_pushvalue(L, 1);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_insert(L, 1);
    lua_pushnil(L);
    lua_settable(L, LUA_REGISTRYINDEX);

    // Get CB state and function
    lua_pushvalue(L, 1);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_rawgeti(L, -1, 1);
    lua_State *cbL = lua_tothread(L, -1);
    // NB: cbL may equal L, or it may not
    lua_pop(L, 1);
    lua_rawgeti(L, -1, 2);
    if(L != cbL)
        lua_xmove(L, cbL, 1);

    // Push CB arg
    int nargs = 1;
    if(async->music == NULL)
    {
        lua_pushnil(cbL);
        if(async->err)
        {
            if(*async->err)
            {
                lua_pushstring(cbL, async->err);
                nargs = 2;
            }
            free(async->err);
        }
    }
    else
    {
        lua_rawgeti(L, 2, 3);
        if(L != cbL)
            lua_xmove(L, cbL, 1);
        music_t* pLMusic = (music_t*)lua_touserdata(cbL, -1);
        pLMusic->pMusic = async->music;
        pLMusic->pRWop = async->rwop;
        async->music = NULL;
        async->rwop = NULL;
    }

    // Finish cleanup
    if(async->rwop)
        SDL_FreeRW(async->rwop);
    lua_pushvalue(L, 1);
    lua_pushnil(L);
    lua_settable(L, LUA_REGISTRYINDEX);

    // Callback
    if(cbL == L)
    {
        lua_call(cbL, nargs, 0);
        return 0;
    }
    if(lua_pcall(cbL, nargs, 0, 0) != 0)
    {
        lua_pushliteral(L, "Error in async music load callback: ");
        lua_xmove(cbL, L, 1);
        lua_tostring(L, -1);
        lua_concat(L, 2);
        lua_error(L);
    }
    return 0;
}
Exemple #5
0
static SDL_RWops*
PyRWops_NewRW_Threaded (PyObject *obj, int *canautoclose)
{
#ifndef WITH_THREAD
    /* Fall back to the non-threaded implementation */
    return PyRWops_NewRW (obj, canautoclose);
#else
    _RWWrapper *wrapper;
    SDL_RWops *ops;
    PyInterpreterState* interp;
    PyThreadState* thread;

    if (!obj || !canautoclose)
    {
        PyErr_SetString (PyExc_TypeError, "argument is NULL");
        return NULL;
    }
    
    /* If we have a text object, assume it is a file, which is automatically
     * closed. */
    if (IsTextObj (obj))
    {
        PyObject *tmp;
        char *filename;
        if (!UTF8FromObj (obj, &filename, &tmp))
            return NULL;
        Py_XDECREF (tmp);
        *canautoclose = 1;
        ops = SDL_RWFromFile ((const char *)filename, "wb");
        if (!ops)
            PyErr_SetString (PyExc_PyGameError, SDL_GetError ());
        return ops;

    }

    /* No text object, so its a buffer or something like that. Try to get the
     * necessary information. */
    ops = SDL_AllocRW ();
    if (!ops)
    {
        PyErr_SetString (PyExc_PyGameError, SDL_GetError ());
        return NULL;
    }
    wrapper = PyMem_New (_RWWrapper, 1);
    if (!wrapper)
    {
        SDL_FreeRW (ops);
        return NULL;
    }
    _bind_python_methods (wrapper, obj);
    ops->read = _pyobj_read_threaded;
    ops->write = _pyobj_write_threaded;
    ops->seek = _pyobj_seek_threaded;
    ops->close = _pyobj_close_threaded;
    ops->hidden.unknown.data1 = (void*) wrapper;
    
    PyEval_InitThreads ();
    thread = PyThreadState_Get ();
    interp = thread->interp;
    wrapper->thread = PyThreadState_New (interp);
    
    *canautoclose = 0;
    return ops;
#endif
}
Exemple #6
0
int close_sdl_rwops(SDL_RWops *rw)
{
	RELEASE(rw->hidden.mem.base);
	SDL_FreeRW(rw);
	return 0;
}
Exemple #7
0
CAMLprim value
caml_SDL_FreeRW(value rwo)
{
    SDL_FreeRW(SDL_RWops_val(rwo));
    return Val_unit;
}
Exemple #8
0
int main(int argc, char* argv[])
{
    FILE *fp;
    int archive_flag = 0;
    int framecount = 0;
    char filename[20];
    int index = 0;
    int i1, size;
    int result, ready, timeout;
    int imageReady = 0;
    char imageBuf[MTU*10];
    char buf[MTU], *cp;
    char msg[2] = {'I', 0};
    char msg1[5] = { 'M', 0x01, 0x01, 0x01, 0};
    IPaddress ipaddress;
    TCPsocket tcpsock;
    SDLNet_SocketSet socSet;
    unsigned int flags = SDL_DOUBLEBUF|SDL_HWSURFACE;
    SDL_Surface *screen;
    SDL_Surface *image;
    unsigned int nrSocketsReady;

    // initialize SDL and SDL_net
    if((SDL_Init(SDL_INIT_VIDEO) == -1)) { 
        printf("Could not initialize SDL: %s.\n", SDL_GetError());
        return 1;
    }
    if(SDLNet_Init() == -1) {
        printf("SDLNet_Init: %s\n", SDLNet_GetError());
        return 2;
    }
    if(TTF_Init() == -1) {
        printf("TTF_Init: %s\n", TTF_GetError());
        return 3;
    }
    // initialize the screen
    screen = SDL_SetVideoMode(320, 260, 32, flags);
    SDLNet_ResolveHost(&ipaddress, IP_ADDRESS, 10001);
    tcpsock = SDLNet_TCP_Open(&ipaddress);
    if(!tcpsock) {
        printf("SDLNet_TCP_Open 1: %s\n", SDLNet_GetError());
        return 2;
    }
    socSet = SDLNet_AllocSocketSet(1);
    SDLNet_TCP_AddSocket(socSet, tcpsock);
    msg1[0] = msg1[1] = msg1[2] = msg1[3] = 0;
    
    SDL_Event event;
    int quit = 0;

    imageReady = 0;
    timeout = 0;

    // main loop
    for (; !quit;) {
        // check keyboard events
        *msg1 = 0;
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
				case SDL_MOUSEBUTTONUP:
					if(event.button.button == SDL_BUTTON_LEFT){
						printf("X: %d Y: %d\n", event.button.x, event.button.y);
					}
					break;
				case SDL_KEYDOWN:
					switch( event.key.keysym.sym ) {
						case SDLK_ESCAPE:
							quit = 1;
							break;
						case SDLK_a:
							archive_flag = 1;
							framecount = 0;
							printf(" archiving enabled - files saved in ./archives/ directory\n");
							break;
						case SDLK_RETURN:
							printf("Entering input mode...\n");
							sendCmd(tcpsock,socSet, screen);
							break;
					}
					break;
			}
        }

        index = 0;
        imageBuf[0] = 0;
        imageReady = 0;

		// send 'I' command
		result = SDLNet_TCP_Send(tcpsock, msg, 1);
		if(result < 1)
			printf("SDLNet_TCP_Send 1: %s\n", SDLNet_GetError());

		// look for frames
		while (!imageReady) {	
			if (!imageReady) {
				nrSocketsReady = SDLNet_CheckSockets(socSet, TIMEOUT);
				if (nrSocketsReady == -1) {
					printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
					perror("SDLNet_CheckSockets");
				}
				else if (nrSocketsReady > 0) {
					if (SDLNet_CheckSockets(socSet, TIMEOUT)) {
						if (SDLNet_SocketReady(socSet)) {
							result = SDLNet_TCP_Recv(tcpsock, buf, MTU);
							memcpy(imageBuf+index, buf, result);
							index += result;
							if ((buf[result-2] == -1) && (buf[result-1] == -39))
								imageReady = 1;
						}
					}
				}
				else {
					printf("\n\rNo sockets ready.\n\r");
					break;
				}
			}
		}

		// make certain that captured frames are valid
		if (!imageReady || imageBuf[0]!='#' || imageBuf[1]!='#' || imageBuf[2]!='I') {
			imageReady = 0;
			printf("bad image, or Checksockets() timed out!\n");
			continue;
		}
		size = (unsigned int)imageBuf[6] + (unsigned int)imageBuf[7]*256 + (unsigned int)imageBuf[8]*65536;
		if (size > index-10) {
			printf("bad image size:  %d %d %d\n", size, index, framecount);
			imageReady = 0;
			continue;
		}
		imageReady = 0;
		timeout = 0;
		SDL_RWops *rwop;
		rwop = SDL_RWFromMem(&(imageBuf[10]), index-10);
		image  = IMG_LoadJPG_RW(rwop);
		cp = (char *)image->pixels;

		if (archive_flag) {
			sprintf(filename, "archives/%.4d.ppm", framecount);
			fp = fopen(filename, "w");
			fprintf(fp,"P6\n320\n240\n255\n", 15);
			fwrite(cp, 1, 230400, fp);
			fclose(fp);
		}
		framecount++;
		SDL_BlitSurface(image, NULL, screen, NULL);
		SDL_Flip(screen);
		SDL_FreeRW(rwop);
		SDL_FreeSurface(image);
	}

	TTF_Quit();
    SDLNet_TCP_Close(tcpsock);
    SDLNet_Quit();
    SDL_Quit();
    return 0;
}