summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/menuwindow.cpp25
-rw-r--r--src/gui/register.cpp1
-rw-r--r--src/gui/updatewindow.cpp131
-rw-r--r--src/gui/updatewindow.h64
-rw-r--r--src/gui/viewport.cpp14
5 files changed, 125 insertions, 110 deletions
diff --git a/src/gui/menuwindow.cpp b/src/gui/menuwindow.cpp
index a5b5c99e..943cc6f0 100644
--- a/src/gui/menuwindow.cpp
+++ b/src/gui/menuwindow.cpp
@@ -30,8 +30,11 @@
#include "button.h"
#include "windowcontainer.h"
-extern Window *setupWindow, *inventoryWindow, *equipmentWindow,
- *skillDialog, *statusWindow;
+extern Window *setupWindow;
+extern Window *inventoryWindow;
+extern Window *equipmentWindow;
+extern Window *skillDialog;
+extern Window *statusWindow;
namespace {
struct MenuWindowListener : public gcn::ActionListener
@@ -52,9 +55,14 @@ MenuWindow::MenuWindow():
setTitleBarHeight(0);
// Buttons
- // ------------
- const char *buttonNames[] = {
- "Status", "Equipment", "Inventory", "Skills", "Setup", 0
+ const char *buttonNames[] =
+ {
+ "Status",
+ "Equipment",
+ "Inventory",
+ "Skills",
+ "Setup",
+ 0
};
int x = 0, y = 3, h = 0;
@@ -67,7 +75,11 @@ MenuWindow::MenuWindow():
h = btn->getHeight();
}
- setDefaultSize((windowContainer->getWidth() - x - 2), 0, x, (y + h));
+ setContentSize(x - 3, h);
+ setDefaultSize(windowContainer->getWidth() - getWidth() - 1,
+ 0,
+ x - 3,
+ y + h);
}
void MenuWindow::draw(gcn::Graphics *graphics)
@@ -79,6 +91,7 @@ void MenuWindow::draw(gcn::Graphics *graphics)
void MenuWindowListener::action(const gcn::ActionEvent &event)
{
Window *window = NULL;
+
if (event.getId() == "Status")
{
window = statusWindow;
diff --git a/src/gui/register.cpp b/src/gui/register.cpp
index 4539e48e..be15747d 100644
--- a/src/gui/register.cpp
+++ b/src/gui/register.cpp
@@ -195,6 +195,7 @@ RegisterDialog::action(const gcn::ActionEvent &event)
mLoginData->username = mUserField->getText();
mLoginData->password = mPasswordField->getText();
mLoginData->email = mEmailField->getText();
+ mLoginData->registerLogin = true;
state = STATE_REGISTER_ATTEMPT;
}
diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp
index fe78a27b..d8130cd3 100644
--- a/src/gui/updatewindow.cpp
+++ b/src/gui/updatewindow.cpp
@@ -46,13 +46,41 @@
#include "../resources/resourcemanager.h"
+/**
+ * Calculates the Alder-32 checksum for the given file.
+ */
+unsigned long fadler32(FILE *file)
+{
+ // Obtain file size
+ fseek(file, 0, SEEK_END);
+ long fileSize = ftell(file);
+ rewind(file);
+
+ // Calculate Adler-32 checksum
+ char *buffer = (char*) malloc(fileSize);
+ fread(buffer, 1, fileSize, file);
+ unsigned long adler = adler32(0L, Z_NULL, 0);
+ adler = adler32(adler, (Bytef*) buffer, fileSize);
+ free(buffer);
+
+ return adler;
+}
+
UpdaterWindow::UpdaterWindow():
Window("Updating..."),
- mThread(NULL), mMutex(NULL), mDownloadStatus(UPDATE_NEWS),
- mUpdateHost(""), mCurrentFile("news.txt"), mBasePath(""),
- mStoreInMemory(true), mDownloadComplete(true), mUserCancel(false),
- mDownloadedBytes(0), mMemoryBuffer(NULL),
- mCurlError(new char[CURL_ERROR_SIZE]), mLineIndex(0)
+ mThread(NULL),
+ mDownloadStatus(UPDATE_NEWS),
+ mUpdateHost(""),
+ mCurrentFile("news.txt"),
+ mCurrentChecksum(0),
+ mBasePath(""),
+ mStoreInMemory(true),
+ mDownloadComplete(true),
+ mUserCancel(false),
+ mDownloadedBytes(0),
+ mMemoryBuffer(NULL),
+ mCurlError(new char[CURL_ERROR_SIZE]),
+ mLineIndex(0)
{
mCurlError[0] = 0;
@@ -151,7 +179,7 @@ void UpdaterWindow::action(const gcn::ActionEvent &event)
}
else if (event.getId() == "play")
{
- state = STATE_LOGIN;
+ state = STATE_LOADDATA;
}
}
@@ -206,22 +234,17 @@ int UpdaterWindow::updateProgress(void *ptr,
return 0;
}
-size_t UpdaterWindow::memoryWrite(void *ptr,
- size_t size, size_t nmemb, FILE *stream)
+size_t
+UpdaterWindow::memoryWrite(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
UpdaterWindow *uw = reinterpret_cast<UpdaterWindow *>(stream);
size_t totalMem = size * nmemb;
- uw->mMemoryBuffer = (char*)realloc(uw->mMemoryBuffer,
- uw->mDownloadedBytes + totalMem + 1);
+ uw->mMemoryBuffer = (char*) realloc(uw->mMemoryBuffer,
+ uw->mDownloadedBytes + totalMem);
if (uw->mMemoryBuffer)
{
memcpy(&(uw->mMemoryBuffer[uw->mDownloadedBytes]), ptr, totalMem);
uw->mDownloadedBytes += totalMem;
-
- // Make sure the memory buffer is NULL terminated, because this
- // function is used to download text files that are later parsed as a
- // string.
- uw->mMemoryBuffer[uw->mDownloadedBytes] = 0;
}
return totalMem;
@@ -236,8 +259,10 @@ int UpdaterWindow::downloadThread(void *ptr)
std::string outFilename;
std::string url(uw->mUpdateHost + "/" + uw->mCurrentFile);
- while (attempts < 3 && !uw->mDownloadComplete) {
+ while (attempts < 3 && !uw->mDownloadComplete)
+ {
FILE *outfile = NULL;
+ FILE *newfile = NULL;
uw->setLabel(uw->mCurrentFile + " (0%)");
curl = curl_easy_init();
@@ -282,61 +307,67 @@ int UpdaterWindow::downloadThread(void *ptr)
uw->mDownloadStatus = UPDATE_ERROR;
switch (res)
{
- case CURLE_COULDNT_CONNECT: // give more debug info on that error
- std::cerr << "curl error " << res << " : " << uw->mCurlError << " " << url.c_str()
- << std::endl;
+ case CURLE_COULDNT_CONNECT:
+ // give more debug info on that error
+ std::cerr << "curl error " << res << ": "
+ << uw->mCurlError << " " << url.c_str()
+ << std::endl;
break;
default:
- std::cerr << "curl error " << res << " : " << uw->mCurlError << " host: " << url.c_str()
- << std::endl;
+ std::cerr << "curl error " << res << ": "
+ << uw->mCurlError << " host: " << url.c_str()
+ << std::endl;
}
}
curl_easy_cleanup(curl);
- uw->mDownloadComplete = true;
-
if (!uw->mStoreInMemory)
{
- long fileSize;
- char *buffer;
- // Obtain file size.
- fseek(outfile, 0, SEEK_END);
- fileSize = ftell(outfile);
- rewind(outfile);
- buffer = (char*)malloc(fileSize);
- fread(buffer, 1, fileSize, outfile);
- fclose(outfile);
-
- // Give the file the proper name
- std::string newName(uw->mBasePath + "/updates/" +
- uw->mCurrentFile.c_str());
-
- // Any existing file with this name is deleted first, otherwise the
- // rename will fail on Windows.
- ::remove(newName.c_str());
- ::rename(outFilename.c_str(), newName.c_str());
-
// Don't check resources2.txt checksum
if (uw->mDownloadStatus == UPDATE_RESOURCES)
{
- // Calculate Adler-32 checksum
- unsigned long adler = adler32(0L, Z_NULL, 0);
- adler = adler32(adler, (Bytef *)buffer, fileSize);
- free(buffer);
+ unsigned long adler = fadler32(outfile);
+
+ if (uw->mCurrentChecksum != adler)
+ {
+ fclose(outfile);
- if (uw->mCurrentChecksum != adler) {
- uw->mDownloadComplete = false;
// Remove the corrupted file
- ::remove(newName.c_str());
+ ::remove(outFilename.c_str());
logger->log(
"Checksum for file %s failed: (%lx/%lx)",
uw->mCurrentFile.c_str(),
adler, uw->mCurrentChecksum);
+ attempts++;
+ continue; // Bail out here to avoid the renaming
}
}
+ fclose(outfile);
+
+ // Give the file the proper name
+ std::string newName(uw->mBasePath + "/updates/" +
+ uw->mCurrentFile.c_str());
+ // Any existing file with this name is deleted first, otherwise
+ // the rename will fail on Windows.
+ ::remove(newName.c_str());
+ ::rename(outFilename.c_str(), newName.c_str());
+
+ // Check if we can open it and no errors were encountered
+ // during renaming
+ newfile = fopen(newName.c_str(), "rb");
+ if (newfile)
+ {
+ fclose(newfile);
+ uw->mDownloadComplete = true;
+ }
+ }
+ else
+ {
+ // It's stored in memory, we're done
+ uw->mDownloadComplete = true;
}
}
attempts++;
@@ -398,8 +429,8 @@ void UpdaterWindow::logic()
mCurrentFile = "resources2.txt";
mStoreInMemory = false;
- download();
mDownloadStatus = UPDATE_LIST;
+ download(); // download() changes mDownloadComplete to false
}
break;
case UPDATE_LIST:
diff --git a/src/gui/updatewindow.h b/src/gui/updatewindow.h
index 8c54be27..b5f6a6df 100644
--- a/src/gui/updatewindow.h
+++ b/src/gui/updatewindow.h
@@ -89,7 +89,7 @@ class UpdaterWindow : public Window, public gcn::ActionListener
void download();
/**
- * The tread function that download the files.
+ * The thread function that download the files.
*/
static int downloadThread(void *ptr);
@@ -115,80 +115,46 @@ class UpdaterWindow : public Window, public gcn::ActionListener
UPDATE_RESOURCES
};
- /**
- * A thread that use libcurl to download updates.
- */
+ /** A thread that use libcurl to download updates. */
SDL_Thread *mThread;
- /**
- * A mutex to protect shared data between the threads.
- */
- SDL_mutex *mMutex;
-
- /**
- * Status of the current download.
- */
+ /** Status of the current download. */
DownloadStatus mDownloadStatus;
- /**
- * Host where we get the updated files.
- */
+ /** Host where we get the updated files. */
std::string mUpdateHost;
- /**
- * The file currently downloading.
- */
+ /** The file currently downloading. */
std::string mCurrentFile;
- /**
- * The Adler32 checksum of the file currently downloading.
- */
+ /** The Adler32 checksum of the file currently downloading. */
unsigned long mCurrentChecksum;
- /**
- * Absolute path to locally save downloaded files.
- */
+ /** Absolute path to locally save downloaded files. */
std::string mBasePath;
- /**
- * A flag to know if we must write the downloaded file to a memory buffer
- * instead of a regular file.
- */
+ /** A flag to indicate whether to use a memory buffer or a regular file. */
bool mStoreInMemory;
- /**
- * Flag that show if current download is complete.
- */
+ /** Flag that show if current download is complete. */
bool mDownloadComplete;
- /**
- * Flag that show if the user has canceled the update
- */
+ /** Flag that show if the user has canceled the update. */
bool mUserCancel;
- /**
- * Byte count currently downloaded in mMemoryBuffer.
- */
+ /** Byte count currently downloaded in mMemoryBuffer. */
int mDownloadedBytes;
- /**
- * Buffer where to put downloaded file which are not stored in file system.
- */
+ /** Buffer for files downloaded to memory. */
char *mMemoryBuffer;
- /**
- * Buffer to handler human readable error provided by curl.
- */
+ /** Buffer to handler human readable error provided by curl. */
char *mCurlError;
- /**
- * List of files to download
- */
+ /** List of files to download. */
std::vector<std::string> mLines;
- /**
- * Index of the file to be downloaded
- */
+ /** Index of the file to be downloaded. */
unsigned int mLineIndex;
gcn::Label *mLabel; /**< Progress bar caption. */
diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp
index 5f316aea..bc635cce 100644
--- a/src/gui/viewport.cpp
+++ b/src/gui/viewport.cpp
@@ -132,18 +132,22 @@ Viewport::draw(gcn::Graphics *gcnGraphics)
mViewY = player_y;
};
- if (mMap) {
+ // Don't move camera so that the end of the map is on screen
+ int viewXmax = mMap->getWidth() * 32 - graphics->getWidth();
+ int viewYmax = mMap->getHeight() * 32 - graphics->getHeight();
+ if (mMap)
+ {
if (mViewX < 0) {
mViewX = 0;
}
if (mViewY < 0) {
mViewY = 0;
}
- if (mViewX > mMap->getWidth() * 32 - midTileX) {
- mViewX = mMap->getWidth() * 32 - midTileX;
+ if (mViewX > viewXmax) {
+ mViewX = viewXmax;
}
- if (mViewY > mMap->getHeight() * 32 - midTileY) {
- mViewY = mMap->getHeight() * 32 - midTileY;
+ if (mViewY > viewYmax) {
+ mViewY = viewYmax;
}
}