Пример #1
0
bool mapgen_load_heightmap(const utf8 *path)
{
    const char* extension = path_get_extension(path);
    uint8 *pixels;
    size_t pitch;
    uint32 numChannels;
    uint32 width, height;

    if (strcicmp(extension, ".png") == 0) {
        if (!image_io_png_read(&pixels, &width, &height, path)) {
            log_warning("Error reading PNG");
            window_error_open(STR_HEIGHT_MAP_ERROR, STR_ERROR_READING_PNG);
            return false;
        }

        numChannels = 4;
        pitch = width * numChannels;
    }
    else if (strcicmp(extension, ".bmp") == 0) {
        if (!context_read_bmp((void *)&pixels, &width, &height, path)) {
            // ReadBMP contains window_error_open calls
            return false;
        }

        numChannels = 4;
        pitch = width * numChannels;
    }
    else
    {
        openrct2_assert(false, "A file with an invalid file extension was selected.");
        return false;
    }

    if (width != height) {
        window_error_open(STR_HEIGHT_MAP_ERROR, STR_ERROR_WIDTH_AND_HEIGHT_DO_NOT_MATCH);
        free(pixels);
        return false;
    }

    if (width > MAXIMUM_MAP_SIZE_PRACTICAL) {
        window_error_open(STR_HEIGHT_MAP_ERROR, STR_ERROR_HEIHGT_MAP_TOO_BIG);
        width = height = min(height, MAXIMUM_MAP_SIZE_PRACTICAL);
    }

    // Allocate memory for the height map values, one byte pixel
    free(_heightMapData.mono_bitmap);
    _heightMapData.mono_bitmap = (uint8*)malloc(width * height);
    _heightMapData.width = width;
    _heightMapData.height = height;

    // Copy average RGB value to mono bitmap
    for (uint32 x = 0; x < _heightMapData.width; x++)
    {
        for (uint32 y = 0; y < _heightMapData.height; y++)
        {
            const uint8 red = pixels[x * numChannels + y * pitch];
            const uint8 green = pixels[x * numChannels + y * pitch + 1];
            const uint8 blue = pixels[x * numChannels + y * pitch + 2];
            _heightMapData.mono_bitmap[x + y * _heightMapData.width] = (red + green + blue) / 3;
        }
    }

    free(pixels);
    return true;
}
Пример #2
0
// Call For Socket Client
int socket_client(char *ip, int port)
{
    int sockfd = 0;
    char buffer[1024];
    struct sockaddr_in serv_addr;

    //memset(buffer, '\0',sizeof(buffer)); // memset() use to set all the specific space to a specific char

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {	// Check is connected or not
        printf("\n Error : Could not create socket\n");
        return 1;
    }
    memset(&serv_addr, '0', sizeof(serv_addr)); // Same , initialize the buffer

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port); 		// htons : 通常用於deal with "little-Endian" or "Big-Endian"

    if(inet_pton(AF_INET, ip, &serv_addr.sin_addr)<=0) {	// inet_pton convert IPv4 and IPv6 addresses from text to binary form
        printf("\n inet_pton error occured\n");
        return 1;
    }
    if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("\n Error : Connect Failed \n");
        return 1;
    }
//========================connection complete=====================================//
    char msg[1024];
    while(1) {
    	memset(buffer, '\0',sizeof(buffer));
        if (read(sockfd, buffer, sizeof(buffer)) < 0) {
            printf("\n Error : Read Failed \n");
            return 1;
        }
		
        if(!strcicmp(buffer,"End")) {
            exit(1);
        } else if(!strcicmp(buffer,"Please input the content you want to edit in this file")) {
            printf("From Server say :\n%s\n", buffer);
            //sleep(1);
            //read(sockfd, msg, sizeof(msg));
            while(strcicmp(msg,"OK!Stop~")) { // while not send the specify word
            	printf("Get Input ( \"StopIn\" to Stop input)\n");
                memset(msg, '\0', sizeof(msg));
                scanf("%s",msg);
                printf("Input The \"StopIn\" to Stop input\n");
                if (write(sockfd, msg, sizeof(msg)) < 0) { //準備寫回server端
                    printf("\n Error : Write Failed \n");
                    return 1;
                }
                memset(msg, '\0', sizeof(msg));
                read(sockfd,msg, sizeof(msg));
            }
            memset(buffer, '\0', sizeof(buffer));
        } else if(!strcicmp(buffer,"Show")) { // Do the "ls" instruction
            printf("The File List:\n");
            while(1) {
                char bk[128];
                read(sockfd, bk, sizeof(bk));
                if(!strcicmp(bk,"OK!STOPLS")) {
                    break;
                }
                printf("%s\n",bk);
                write(sockfd,"Y",1);
            }
            printf("========End of List========\n\n");
        } else if(!strcicmp(buffer,"DownLoad")) { // Do the DownLoad Instruction
            char dFile[1024];
            memset(dFile,'\0',sizeof(dFile));
            printf("Which file in \"Server端\" you want to download?\n");
            scanf("%s",dFile);
            write(sockfd,dFile,sizeof(dFile));
            memset(dFile,'\0',sizeof(dFile));
            read(sockfd,dFile,sizeof(dFile));
            printf("%s\n",dFile);
            memset(dFile,'\0',sizeof(dFile));
            char cwd[1024];
            if (getcwd(cwd, sizeof(cwd)) != NULL) // Get Currently File Path
                strcpy(dFile,cwd);
            write(sockfd,dFile,sizeof(dFile));
            printf("End of DownLoad Input\n\n");

        } else {
            printf("========================================\n");
            printf("From Server port's message :\n%s\n", buffer);
            sleep(1);
            //====================Data ready to deliver======================//
            //strcpy(buffer, "I get client message!")
            memset(buffer, '\0', sizeof(buffer));
            scanf("%s",buffer);
            if(!strcicmp(buffer,"End")) {
                exit(1);
            }
            if (write(sockfd, buffer, sizeof(buffer)) < 0) { //準備寫回server端
                printf("\n Error : Write Failed \n");
                return 1;
            }
        }
    }
//===========================End socket===========================================//
    close(sockfd);
    return 0;
}