From ce2ae296b70ae6c799c7989d3d8d02b5dfef6b54 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 8 Jun 2008 18:08:01 +0100 Subject: [PATCH] Remove legacy files --- src/Bitmap.cpp | 98 ---------------------------------- src/Bitmap.hpp | 87 ------------------------------ src/File.cpp | 129 --------------------------------------------- src/File.hpp | 47 ----------------- src/HighScores.cpp | 8 ++- src/Main.cpp | 43 ++++++++++++++- src/Makefile.am | 5 +- src/Platform.hpp | 1 + 8 files changed, 47 insertions(+), 371 deletions(-) delete mode 100644 src/Bitmap.cpp delete mode 100644 src/Bitmap.hpp delete mode 100644 src/File.cpp delete mode 100644 src/File.hpp diff --git a/src/Bitmap.cpp b/src/Bitmap.cpp deleted file mode 100644 index 69c8a03..0000000 --- a/src/Bitmap.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Bitmap.cpp - Implementation of Bitmap wrapper class. - * Copyright (C) 2006 Nick Gasson - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "Bitmap.hpp" - -/* - * Constructs a new bitmap object. - * f -> Stream to read data from. - * Throws std::runtime_error on failure. - */ -Bitmap::Bitmap(File *f) - : loaded(false), data(NULL) -{ - // Read file header - f->Read(&fileh, sizeof(BitmapFileHeader)); - LittleEndian16(fileh.magic); - if (fileh.magic != BITMAP_MAGIC_NUMBER) - throw std::runtime_error("Bitmap has invalid magic number"); - - // Read info header - f->Read(&infoh, sizeof(BitmapInfoHeader)); - - // Bitmaps use big endian - LittleEndian32(infoh.width); - LittleEndian32(infoh.height); - LittleEndian16(infoh.planes); - LittleEndian16(infoh.bitcount); - LittleEndian32(infoh.imagesz); - LittleEndian32(infoh.clrused); - LittleEndian32(infoh.clrimp); - - width = infoh.width; - height = infoh.height; - depth = infoh.bitcount; - - if (depth != 24) - throw std::runtime_error("Only 24-bit bitmaps are supported"); - - // Calculate the size of the image with padding - int datasize = width * height * (infoh.bitcount / 8); - - // Allocate memory - unsigned char *tempdata = new unsigned char[datasize]; - - // Read entire image - f->Read(tempdata, sizeof(unsigned char) * datasize); - - // Calculate final width - int bytewidth = (width * depth) / 8; - int padwidth = bytewidth; - while (padwidth % 4 != 0) - padwidth++; - - // Convert data to useable format - int diff = width * height * RGB_BYTE_SIZE; - data = new unsigned char[diff]; - - int offset = padwidth - bytewidth; - - for (int i = 0; i < datasize; i += RGB_BYTE_SIZE) { - if ((i+1) % padwidth == 0) - i += offset; - - *(data+i+2) = *(tempdata+i); - *(data+i+1) = *(tempdata+i+1); - *(data+i) = *(tempdata+i+2); - } - - // Free temporary storage - delete[] tempdata; - - loaded = true; -} - -/* - * Destroys a bitmap object. - */ -Bitmap::~Bitmap() -{ - if (data) - delete[] data; -} diff --git a/src/Bitmap.hpp b/src/Bitmap.hpp deleted file mode 100644 index 1d24264..0000000 --- a/src/Bitmap.hpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Bitmap.hpp - Definition of Bitmap wrapper class. - * Copyright (C) 2006 Nick Gasson - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef INC_BITMAP_HPP -#define INC_BITMAP_HPP - -#include "Platform.hpp" -#include "File.hpp" - -/* - * A cross platform loader for Windows bitmap files. This is used by - * the DataFile class to load images. - */ -class Bitmap -{ -public: - Bitmap(File *f); - ~Bitmap(); - - bool HasLoaded() const { return loaded; } - int GetWidth() const { return width; } - int GetHeight() const { return height; } - int GetDepth() const { return depth; } - unsigned char *GetData() const { return data; } - -private: - - // Constants - static const short BITMAP_MAGIC_NUMBER = 19778; - static const short RGB_BYTE_SIZE = 3; - -#pragma pack(2) - - // The header present in every bitmap file - struct BitmapFileHeader - { - uint16_t magic; // Should be BITMAP_MAGIC_NUMBER - uint32_t filesz; // Length of file in bytes - uint16_t reserved1; // Not used - uint16_t reserved2; // Not used - uint32_t dataoff; // Start of bitmap data - }; - - // Defines colours and dimensions - struct BitmapInfoHeader - { - uint32_t size; // Size of this header - uint32_t width; // Width of image in pixels - uint32_t height; // Height of image in pixels - uint16_t planes; // Must be 1 - uint16_t bitcount; // Must be 1, 4, 8, or 24 - uint32_t compress; // Ignored - uint32_t imagesz; // Size of image in bytes - uint32_t xpelspm; // Horizontal resolution - uint32_t ypelspm; // Vertical resolution - uint32_t clrused; // Colour indexes used - uint32_t clrimp; // Size of colour table - }; - -#pragma pack() - - BitmapFileHeader fileh; - BitmapInfoHeader infoh; - - bool loaded; - int width, height, depth; - - unsigned char *data; -}; - -#endif diff --git a/src/File.cpp b/src/File.cpp deleted file mode 100644 index 9b9cc64..0000000 --- a/src/File.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "File.hpp" - -// Include CoreFoundation here to prevent namespace collisions -#ifdef MACOSX -#include -#endif - - -/* - * Creates a new File object. Opens the file pointed to by path. - * path -> Path of file to open relative to DATADIR. - * readonly -> If true, do not open file for writing. - * Throw std::runtime_error if file cannot be opened. - */ -File::File(const char *path, bool readonly) - : stream(NULL), path(path) -{ - const char *mode = (readonly ? "rb" : "w+b"); - -#ifdef USE_FOPEN_S - if (fopen_s(&stream, path, mode) != 0) -#else - if ((stream = fopen(path, mode)) == NULL) -#endif - throw std::runtime_error(string("Failed to open ") + path); -} - - -/* - * Closes the file. - */ -File::~File() -{ - if (stream != NULL) - fclose(stream); -} - -/* - * Reads a number of bytes from the file. - * buf -> Buffer to store data. - * bytes -> Number of bytes to read. - * Throws an exception if all the requested bytes could not be read. - */ -void File::Read(void *buf, size_t bytes) -{ - size_t n = fread(buf, 1, bytes, stream); - if (n != bytes) - throw std::runtime_error("Failed to read from file: " + path); -} - - -/* - * Writes a number of bytes to the file. - * buf -> Buffer containing data. - * bytes -> Number of bytes to writes. - * Throws an exception if all the requested bytes could not be written. - */ -void File::Write(const void *buf, size_t bytes) -{ - size_t n = fwrite(buf, 1, bytes, stream); - if (n != bytes) - throw std::runtime_error("Failed to write to file: " + path); -} - - -/* - * Sets the absolute read offset in the file. - */ -void File::Seek(size_t offset) -{ - fseek(stream, static_cast(offset), SEEK_SET); -} - - -/* - * Returns true if specified path points to a valid file. - * path -> File to check. - */ -bool File::Exists(const char *path) -{ -#ifdef WIN32 - OFSTRUCT of; - HFILE hFile; - - hFile = OpenFile(path, &of, OF_EXIST); - return hFile != HFILE_ERROR; -#else - struct stat dummy; - return stat(path, &dummy) == 0; -#endif -} - -/* - * Locate a resource file in a platform independant manner. - * base -> Base of file name. - * ext -> Extension of file name. - * Note that the returned pointer is only valid until the next call - * to this function. - */ -const char *File::LocateResource(const char *base, const char *ext) -{ - static char path[MAX_RES_PATH]; - -#ifdef MACOSX - CFURLRef resURL; - CFBundleRef mainBundle; - CFStringRef cfBase, cfExt, cfPath; - - cfBase = CFStringCreateWithCString(NULL, base, kCFStringEncodingASCII); - cfExt = CFStringCreateWithCString(NULL, ext, kCFStringEncodingASCII); - - mainBundle = CFBundleGetMainBundle(); - - resURL = CFBundleCopyResourceURL(mainBundle, cfBase, cfExt, NULL); - - if (resURL == NULL) - throw runtime_error("Failed to locate " + string(base) + "." + string(ext)); - - cfPath = CFURLCopyPath(resURL); - - CFStringGetCString(cfPath, path, MAX_RES_PATH, kCFStringEncodingASCII); -#else - snprintf(path, MAX_RES_PATH, "%s/%s.%s", DATADIR, base, ext); -#endif - - return path; -} - - diff --git a/src/File.hpp b/src/File.hpp deleted file mode 100644 index 9311dcc..0000000 --- a/src/File.hpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * File.hpp - Definition of File wrapper class. - * Copyright (C) 2006 Nick Gasson - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef INC_FILE_HPP -#define INC_FILE_HPP - -#include "Platform.hpp" - -/* - * A wrapper around file I/O operations. - */ -class File -{ -public: - File(const char *path, bool readonly=true); - ~File(); - - void Read(void *buf, size_t bytes); - void Write(const void *buf, size_t bytes); - void Seek(size_t offset); - - static bool Exists(const char *path); - static const char *LocateResource(const char *base, const char *ext); -private: - static const int MAX_RES_PATH = 256; - - FILE *stream; - string path; -}; - -#endif diff --git a/src/HighScores.cpp b/src/HighScores.cpp index 1331c55..0bffc19 100644 --- a/src/HighScores.cpp +++ b/src/HighScores.cpp @@ -19,8 +19,6 @@ #include "HighScores.hpp" #include "Input.hpp" -#include "File.hpp" - HighScores::HighScores() : hscoreImage("images/hscore.png"), @@ -298,13 +296,13 @@ void ScoreFile::Sort() void ScoreFile::Load() { // Check for file's existence - if (!File::Exists(File::LocateResource("Highscores", "dat"))) { + if (!FileExists(LocateResource("Highscores.dat"))) { // Write a dummy score file Save(); } else { // Open highscores file - const char *fname = File::LocateResource("Highscores", "dat"); + const char *fname = LocateResource("Highscores.dat"); ifstream fin(fname); for (ScoreEntryVecIt it = scores.begin(); it != scores.end(); ++it) (*it).ReadFromStream(fin); @@ -318,7 +316,7 @@ void ScoreFile::Save() if (!needsWrite) return; - const char *fname = File::LocateResource("Highscores", "dat"); + const char *fname = LocateResource("Highscores.dat"); ofstream fout(fname); for (ScoreEntryVecIt it = scores.begin(); it != scores.end(); ++it) (*it).WriteOnStream(fout); diff --git a/src/Main.cpp b/src/Main.cpp index 5afe678..d4f969e 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -18,7 +18,6 @@ */ #include "Lander.hpp" -#include "File.hpp" #define DEBUG_WINDOW #define DEBUG_WIN_X 800 @@ -101,11 +100,51 @@ int main(int argc, char **argv) */ const char *LocateResource(const char *file) { +#ifdef MACOSX + static char path[MAX_RES_PATH]; + + CFURLRef resURL; + CFBundleRef mainBundle; + CFStringRef cfBase, cfExt, cfPath; + + cfBase = CFStringCreateWithCString(NULL, base, kCFStringEncodingASCII); + cfExt = CFStringCreateWithCString(NULL, ext, kCFStringEncodingASCII); + + mainBundle = CFBundleGetMainBundle(); + + resURL = CFBundleCopyResourceURL(mainBundle, cfBase, cfExt, NULL); + + if (resURL == NULL) + throw runtime_error("Failed to locate " + string(base) + "." + string(ext)); + + cfPath = CFURLCopyPath(resURL); + + CFStringGetCString(cfPath, path, MAX_RES_PATH, kCFStringEncodingASCII); + + return patch; +#endif + #ifdef DATADIR static char path[PATH_MAX]; snprintf(path, PATH_MAX, "%s/%s", DATADIR, file); return path; +#else + return file; #endif +} - return file; +bool FileExists(const char *file) +{ +#ifdef UNIX + struct stat buf; + return stat(file, &buf) == 0; +#else + FILE *f = fopen(file, "r"); + if (NULL == f) + return false; + else { + fclose(f); + return true; + } +#endif } diff --git a/src/Makefile.am b/src/Makefile.am index 39600b9..a8c170a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,9 +3,8 @@ bin_PROGRAMS = lander AM_CXXFLAGS=$(LANDER_CFLAGS) $(SDL_CFLAGS) -Wall AM_LDFLAGS=$(LANDER_LIBS) $(SDL_LIBS) -lGLU $(LTLIBINTL) lander_SOURCES = Main.cpp Lander.hpp Game.cpp \ - Bitmap.cpp Bitmap.hpp Font.cpp Font.hpp \ - HighScores.cpp HighScores.hpp Input.cpp Input.hpp \ - File.cpp File.hpp Platform.hpp LoadOnce.hpp \ + Font.cpp Font.hpp HighScores.cpp HighScores.hpp Input.cpp \ + Input.hpp Platform.hpp LoadOnce.hpp \ OpenGL.cpp OpenGL.hpp Menu.cpp Menu.hpp Emitter.cpp \ Emitter.hpp ScreenManager.cpp ScreenManager.hpp \ ObjectGrid.hpp ObjectGrid.cpp Asteroid.hpp Asteroid.cpp \ diff --git a/src/Platform.hpp b/src/Platform.hpp index ec234cc..7c6b3f4 100644 --- a/src/Platform.hpp +++ b/src/Platform.hpp @@ -151,5 +151,6 @@ using namespace std; #endif /* #ifdef LANDER_BIG_ENDIAN */ const char *LocateResource(const char *file); +bool FileExists(const char *file); #endif /* #ifdef INC_PLATFORM_HPP */ -- 2.39.2