From a70021d53ac6f9ae7ce040bb0950fb27b81fc18f Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Tue, 27 May 2014 16:48:15 +0200 Subject: [PATCH] Add function for checking wether file exists to osinterface, BMP screenshots can now be saved without WIN32 specific methods. --- src/config.c | 5 ++--- src/osinterface.c | 5 +++++ src/osinterface.h | 1 + src/rct2.c | 3 +-- src/screenshot.c | 44 +++++++++++++++++++++++--------------------- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/config.c b/src/config.c index 11fdf54b97..8d99760217 100644 --- a/src/config.c +++ b/src/config.c @@ -254,7 +254,7 @@ void config_init() static int config_find_rct2_path(char *resultPath) { int i; - DWORD dwAttrib; + const char *searchLocations[] = { "C:\\Program Files\\Infogrames\\RollerCoaster Tycoon 2", "C:\\Program Files (x86)\\Infogrames\\RollerCoaster Tycoon 2", @@ -266,8 +266,7 @@ static int config_find_rct2_path(char *resultPath) }; for (i = 0; i < countof(searchLocations); i++) { - dwAttrib = GetFileAttributes(searchLocations[i]); - if (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { + if ( osinterface_directory_exists(searchLocations[i]) ) { strcpy(resultPath, searchLocations[i]); return 1; } diff --git a/src/osinterface.c b/src/osinterface.c index 843b37f712..274c2a5f3a 100644 --- a/src/osinterface.c +++ b/src/osinterface.c @@ -454,6 +454,11 @@ char *osinterface_get_orct2_homesubfolder(const char *subFolder) return path; } +int osinterface_file_exists(const char *path) +{ + return !(GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND); +} + int osinterface_directory_exists(const char *path) { DWORD dwAttrib = GetFileAttributes(path); diff --git a/src/osinterface.h b/src/osinterface.h index 88576a980a..59691835c6 100644 --- a/src/osinterface.h +++ b/src/osinterface.h @@ -52,6 +52,7 @@ char* osinterface_open_directory_browser(char *title); char* osinterface_get_orct2_homefolder(); char *osinterface_get_orct2_homesubfolder(const char *subFolder); +int osinterface_file_exists(const char *path); int osinterface_directory_exists(const char *path); int osinterface_ensure_directory_exists(const char *path); diff --git a/src/rct2.c b/src/rct2.c index 76b81e5e6e..7ab6254c34 100644 --- a/src/rct2.c +++ b/src/rct2.c @@ -189,8 +189,7 @@ void rct2_init() void rct2_init_directories() { // check install directory - DWORD dwAttrib = GetFileAttributes(gGeneral_config.game_path); - if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { + if ( !osinterface_directory_exists(gGeneral_config.game_path) ) { osinterface_show_messagebox("Invalid RCT2 installation path. Please correct in config.ini."); exit(-1); } diff --git a/src/screenshot.c b/src/screenshot.c index 7e74ed32f1..10350f39da 100644 --- a/src/screenshot.c +++ b/src/screenshot.c @@ -17,10 +17,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . *****************************************************************************/ +#pragma pack(1) #include #include -#include #include "osinterface.h" #include "addresses.h" #include "config.h" @@ -75,8 +75,9 @@ static int screenshot_get_next_path(char *path, char *extension) // Glue together path and filename sprintf(path, "%s%cSCR%d%s", screenshotPath, osinterface_get_path_separator(), i, extension); - if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND) + if (!osinterface_file_exists(path)) { return i; + } } free(screenshotPath); @@ -129,22 +130,23 @@ int screenshot_dump_bmp() int i, y, index, width, height, stride; char *buffer, path[MAX_PATH], *row; - HANDLE hFile; - DWORD bytesWritten; + FILE *fp; + unsigned int bytesWritten; // Get a free screenshot path if ((index = screenshot_get_next_path(path, ".bmp")) == -1) return -1; - // Open file for writing - hFile = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (hFile == INVALID_HANDLE_VALUE) + // Open binary file for writing + if ((fp = fopen(path, "wb")) == NULL){ return -1; + } // Allocate buffer buffer = malloc(0xFFFF); if (buffer == NULL) { - CloseHandle(hFile); + //CloseHandle(hFile); + fclose(fp); return -1; } @@ -159,9 +161,9 @@ int screenshot_dump_bmp() header.bfSize = height * stride + 1038; header.bfOffBits = 1038; - WriteFile(hFile, &header, sizeof(header), &bytesWritten, NULL); - if (bytesWritten != sizeof(header)) { - CloseHandle(hFile); + bytesWritten = fwrite(&header, sizeof(BitmapFileHeader), 1, fp); + if (bytesWritten != 1) { + fclose(fp); free(buffer); } @@ -176,9 +178,9 @@ int screenshot_dump_bmp() info.biYPelsPerMeter = 2520; info.biClrUsed = 246; - WriteFile(hFile, &info, sizeof(info), &bytesWritten, NULL); - if (bytesWritten != sizeof(info)) { - CloseHandle(hFile); + bytesWritten=fwrite(&info, sizeof(BitmapInfoHeader), 1, fp); + if (bytesWritten != 1) { + fclose(fp); free(buffer); } @@ -190,9 +192,9 @@ int screenshot_dump_bmp() buffer[i * 4 + 2] = RCT2_ADDRESS(0x01424680, uint8)[i * 4 + 2]; } - WriteFile(hFile, buffer, 246 * 4, &bytesWritten, NULL); - if (bytesWritten != 246 * 4) { - CloseHandle(hFile); + bytesWritten = fwrite(buffer, sizeof(char), 246*4, fp); + if (bytesWritten != 246*4){ + fclose(fp); free(buffer); } @@ -204,14 +206,14 @@ int screenshot_dump_bmp() memset(buffer, 0, stride); memcpy(buffer, row, dpi->width); - WriteFile(hFile, buffer, stride, &bytesWritten, NULL); - if (bytesWritten != stride) { - CloseHandle(hFile); + bytesWritten=fwrite(buffer, sizeof(char), stride, fp); + if (bytesWritten != stride){ + fclose(fp); free(buffer); } } - CloseHandle(hFile); + fclose(fp); free(buffer); return index;