mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-02-01 11:15:13 +01:00
Add function for checking wether file exists to osinterface, BMP screenshots can now be saved without WIN32 specific methods.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*****************************************************************************/
|
||||
#pragma pack(1)
|
||||
|
||||
#include <lodepng.h>
|
||||
#include <stdio.h>
|
||||
#include <windows.h>
|
||||
#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;
|
||||
|
||||
Reference in New Issue
Block a user