Fix thumbnail crash, turns out SaveButton was storing a Thumbnail pointer, not a Thumbnail as I thought

This commit is contained in:
Simon Robertshaw 2012-01-21 18:51:28 +00:00
parent 8ec6aae617
commit 1cee908c16
17 changed files with 239 additions and 34 deletions

View File

@ -92,9 +92,21 @@ int main(int argc, char * argv[])
engine->onMouseMove(event.motion.x, event.motion.y); engine->onMouseMove(event.motion.x, event.motion.y);
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
if(event.button.button == SDL_BUTTON_WHEELUP)
{
engine->onMouseWheel(event.motion.x, event.motion.y, 1);
}
else if (event.button.button == SDL_BUTTON_WHEELDOWN)
{
engine->onMouseWheel(event.motion.x, event.motion.y, -1);
}
else
{
engine->onMouseClick(event.motion.x, event.motion.y, event.button.button); engine->onMouseClick(event.motion.x, event.motion.y, event.button.button);
}
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if(event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN)
engine->onMouseUnclick(event.motion.x, event.motion.y, event.button.button); engine->onMouseUnclick(event.motion.x, event.motion.y, event.button.button);
break; break;
} }

View File

@ -39,9 +39,10 @@ Client::~Client()
http_done(); http_done();
} }
std::vector<Save*> * Client::SearchSaves(int start, int count, string query, string sort) std::vector<Save*> * Client::SearchSaves(int start, int count, string query, string sort, int & resultCount)
{ {
lastError = ""; lastError = "";
resultCount = 0;
std::vector<Save*> * saveArray = new std::vector<Save*>(); std::vector<Save*> * saveArray = new std::vector<Save*>();
std::stringstream urlStream; std::stringstream urlStream;
char * data; char * data;
@ -52,7 +53,7 @@ std::vector<Save*> * Client::SearchSaves(int start, int count, string query, str
urlStream << "&Search_Query="; urlStream << "&Search_Query=";
if(query.length()) if(query.length())
urlStream << query; urlStream << query;
if(sort.length()) if(sort == "date")
{ {
if(query.length()) if(query.length())
urlStream << " "; urlStream << " ";
@ -69,6 +70,8 @@ std::vector<Save*> * Client::SearchSaves(int start, int count, string query, str
json::Object objDocument; json::Object objDocument;
json::Reader::Read(objDocument, dataStream); json::Reader::Read(objDocument, dataStream);
json::Number tempCount = objDocument["Count"];
resultCount = tempCount.Value();
json::Array savesArray = objDocument["Saves"]; json::Array savesArray = objDocument["Saves"];
for(int j = 0; j < savesArray.Size(); j++) for(int j = 0; j < savesArray.Size(); j++)
{ {
@ -132,7 +135,7 @@ Thumbnail * Client::GetThumbnail(int saveID, int saveDate)
{ {
activeThumbRequestCompleteTimes[i] = time(NULL); activeThumbRequestCompleteTimes[i] = time(NULL);
} }
else if(activeThumbRequestCompleteTimes[i] < (currentTime-20)) //Otherwise, if it completed more than 10 seconds ago, destroy it. else if(activeThumbRequestCompleteTimes[i] < (currentTime-2)) //Otherwise, if it completed more than 2 seconds ago, destroy it.
{ {
http_async_req_close(activeThumbRequests[i]); http_async_req_close(activeThumbRequests[i]);
activeThumbRequests[i] = NULL; activeThumbRequests[i] = NULL;
@ -165,6 +168,7 @@ Thumbnail * Client::GetThumbnail(int saveID, int saveDate)
char * data; char * data;
int status, data_size, imgw, imgh; int status, data_size, imgw, imgh;
data = http_async_req_stop(activeThumbRequests[i], &status, &data_size); data = http_async_req_stop(activeThumbRequests[i], &status, &data_size);
free(activeThumbRequests[i]);
activeThumbRequests[i] = NULL; activeThumbRequests[i] = NULL;
if (status == 200 && data) if (status == 200 && data)
{ {
@ -203,10 +207,6 @@ Thumbnail * Client::GetThumbnail(int saveID, int saveDate)
return thumbnailCache[thumbnailCacheNextID++]; return thumbnailCache[thumbnailCacheNextID++];
} }
} }
else if(activeThumbRequestTimes[i] < currentTime-HTTP_TIMEOUT)
{
//
}
} }
} }
if(!found) if(!found)

View File

@ -24,7 +24,7 @@ public:
Client(); Client();
~Client(); ~Client();
void ClearThumbnailRequests(); void ClearThumbnailRequests();
std::vector<Save*> * SearchSaves(int start, int count, string query, string sort); std::vector<Save*> * SearchSaves(int start, int count, string query, string sort, int & resultCount);
Thumbnail * GetThumbnail(int saveID, int saveDate); Thumbnail * GetThumbnail(int saveID, int saveDate);
std::string GetLastError() { return lastError; } std::string GetLastError() { return lastError; }
}; };

View File

@ -9,7 +9,8 @@
using namespace std; using namespace std;
GameController::GameController() GameController::GameController():
search(NULL)
{ {
gameView = new GameView(); gameView = new GameView();
gameModel = new GameModel(); gameModel = new GameModel();
@ -22,6 +23,11 @@ GameController::GameController()
GameController::~GameController() GameController::~GameController()
{ {
if(search)
{
ui::Engine::Ref().CloseWindow();
delete search;
}
delete gameView; delete gameView;
delete gameModel; delete gameModel;
} }
@ -70,6 +76,6 @@ void GameController::SetPaused(bool pauseState)
void GameController::OpenSearch() void GameController::OpenSearch()
{ {
SearchController * search = new SearchController(); search = new SearchController();
ui::Engine::Ref().ShowWindow(search->GetView()); ui::Engine::Ref().ShowWindow(search->GetView());
} }

View File

@ -6,6 +6,7 @@
#include "GameModel.h" #include "GameModel.h"
#include "interface/Point.h" #include "interface/Point.h"
#include "simulation/Simulation.h" #include "simulation/Simulation.h"
#include "search/SearchController.h"
using namespace std; using namespace std;
@ -17,6 +18,7 @@ private:
//Simulation * sim; //Simulation * sim;
GameView * gameView; GameView * gameView;
GameModel * gameModel; GameModel * gameModel;
SearchController * search;
public: public:
GameController(); GameController();
~GameController(); ~GameController();

View File

@ -19,6 +19,7 @@ class ButtonAction
{ {
public: public:
virtual void ActionCallback(ui::Button * sender) {} virtual void ActionCallback(ui::Button * sender) {}
virtual ~ButtonAction() {}
}; };
class Button : public Component class Button : public Component

View File

@ -61,8 +61,7 @@ void SaveButton::Tick(float dt)
tempThumb = Client::Ref().GetThumbnail(save->GetID(), 0); tempThumb = Client::Ref().GetThumbnail(save->GetID(), 0);
if(tempThumb) if(tempThumb)
{ {
thumbnail = tempThumb; //Store a local copy of the thumbnail thumbnail = new Thumbnail(*tempThumb); //Store a local copy of the thumbnail
cout << (void *)(tempThumb) << " " << (void *)(&thumbnail) << endl;
if(thumbnail->Data) if(thumbnail->Data)
{ {
if(thumbnail->Size.Y > (Size.Y-25)) if(thumbnail->Size.Y > (Size.Y-25))

View File

@ -15,6 +15,7 @@ class SaveButtonAction
{ {
public: public:
virtual void ActionCallback(ui::SaveButton * sender) {} virtual void ActionCallback(ui::SaveButton * sender) {}
virtual ~SaveButtonAction() {}
}; };
class SaveButton : public Component class SaveButton : public Component

View File

@ -162,13 +162,14 @@ void Textbox::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
cursor++; cursor++;
changed = true; changed = true;
} }
break;
} }
if(changed && actionCallback) if(changed && actionCallback)
{ {
actionCallback->TextChangedCallback(this); actionCallback->TextChangedCallback(this);
} }
} }
catch(std::out_of_range e) catch(std::out_of_range &e)
{ {
cursor = 0; cursor = 0;
text = ""; text = "";

View File

@ -13,6 +13,7 @@ class TextboxAction
{ {
public: public:
virtual void TextChangedCallback(ui::Textbox * sender) {} virtual void TextChangedCallback(ui::Textbox * sender) {}
virtual ~TextboxAction() {}
}; };
class Textbox : public Component class Textbox : public Component
{ {

View File

@ -11,7 +11,7 @@ SearchController::SearchController()
searchModel->AddObserver(searchView); searchModel->AddObserver(searchView);
searchView->AttachController(this); searchView->AttachController(this);
searchModel->UpdateSaveList(""); searchModel->UpdateSaveList(1, "");
//Set up interface //Set up interface
//windowPanel.AddChild(); //windowPanel.AddChild();
@ -25,5 +25,34 @@ SearchController::~SearchController()
void SearchController::DoSearch(std::string query) void SearchController::DoSearch(std::string query)
{ {
searchModel->UpdateSaveList(query); searchModel->UpdateSaveList(1, query);
}
void SearchController::PrevPage()
{
if(searchModel->GetPageNum()>1)
searchModel->UpdateSaveList(searchModel->GetPageNum()-1, searchModel->GetLastQuery());
}
void SearchController::NextPage()
{
if(searchModel->GetPageNum() <= searchModel->GetPageCount())
searchModel->UpdateSaveList(searchModel->GetPageNum()+1, searchModel->GetLastQuery());
}
void SearchController::ChangeSort()
{
if(searchModel->GetSort() == "date")
{
searchModel->SetSort("votes");
}
else
{
searchModel->SetSort("date");
}
}
void SearchController::ShowOwn(bool show)
{
} }

View File

@ -16,6 +16,10 @@ public:
~SearchController(); ~SearchController();
SearchView * GetView() { return searchView; } SearchView * GetView() { return searchView; }
void DoSearch(std::string query); void DoSearch(std::string query);
void NextPage();
void PrevPage();
void ChangeSort();
void ShowOwn(bool show);
}; };
#endif // SEARCHCONTROLLER_H #endif // SEARCHCONTROLLER_H

View File

@ -3,26 +3,30 @@
#include "client/Client.h" #include "client/Client.h"
SearchModel::SearchModel() SearchModel::SearchModel():
currentSort("votes"),
showOwn(false)
{ {
} }
void SearchModel::UpdateSaveList(std::string query) void SearchModel::UpdateSaveList(int pageNumber, std::string query)
{ {
lastQuery = query;
lastError = ""; lastError = "";
saveList.clear(); saveList.clear();
currentPage = 1;
resultCount = 0;
notifySaveListChanged(); notifySaveListChanged();
vector<Save*> * tempSaveList = Client::Ref().SearchSaves(0, 12, query, ""); notifyPageChanged();
vector<Save*> * tempSaveList = Client::Ref().SearchSaves((pageNumber-1)*12, 12, query, currentSort, resultCount);
saveList = *tempSaveList; saveList = *tempSaveList;
delete tempSaveList; delete tempSaveList;
if(!saveList.size()) if(!saveList.size())
{ {
lastError = Client::Ref().GetLastError(); lastError = Client::Ref().GetLastError();
} }
/*for(int i = 0; i < 16; i++) currentPage = pageNumber;
{ notifyPageChanged();
saveList.push_back(Save(2198, 2333, 315, "dima-gord", "Destroyable city 5 (wth metro)"));
}*/
notifySaveListChanged(); notifySaveListChanged();
} }
@ -35,6 +39,9 @@ void SearchModel::AddObserver(SearchView * observer)
{ {
observers.push_back(observer); observers.push_back(observer);
observer->NotifySaveListChanged(this); observer->NotifySaveListChanged(this);
observer->NotifyPageChanged(this);
observer->NotifySortChanged(this);
observer->NotifyShowOwnChanged(this);
} }
void SearchModel::notifySaveListChanged() void SearchModel::notifySaveListChanged()
@ -45,3 +52,30 @@ void SearchModel::notifySaveListChanged()
cObserver->NotifySaveListChanged(this); cObserver->NotifySaveListChanged(this);
} }
} }
void SearchModel::notifyPageChanged()
{
for(int i = 0; i < observers.size(); i++)
{
SearchView* cObserver = observers[i];
cObserver->NotifyPageChanged(this);
}
}
void SearchModel::notifySortChanged()
{
for(int i = 0; i < observers.size(); i++)
{
SearchView* cObserver = observers[i];
cObserver->NotifySortChanged(this);
}
}
void SearchModel::notifyShowOwnChanged()
{
for(int i = 0; i < observers.size(); i++)
{
SearchView* cObserver = observers[i];
cObserver->NotifyShowOwnChanged(this);
}
}

View File

@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <math.h>
#include "Save.h" #include "Save.h"
#include "SearchView.h" #include "SearchView.h"
@ -12,16 +13,31 @@ class SearchView;
class SearchModel class SearchModel
{ {
private: private:
string currentSort;
string lastQuery;
string lastError; string lastError;
vector<SearchView*> observers; vector<SearchView*> observers;
vector<Save*> saveList; vector<Save*> saveList;
int currentPage;
int resultCount;
bool showOwn;
void notifySaveListChanged(); void notifySaveListChanged();
void notifyPageChanged();
void notifySortChanged();
void notifyShowOwnChanged();
public: public:
SearchModel(); SearchModel();
void AddObserver(SearchView * observer); void AddObserver(SearchView * observer);
void UpdateSaveList(std::string query); void UpdateSaveList(int pageNumber, std::string query);
vector<Save*> GetSaveList(); vector<Save*> GetSaveList();
string GetLastError() { return lastError; } string GetLastError() { return lastError; }
int GetPageCount() { return max(1, (int)(ceil(resultCount/16))); }
int GetPageNum() { return currentPage; }
std::string GetLastQuery() { return lastQuery; }
void SetSort(string sort) { currentSort = sort; UpdateSaveList(currentPage, lastQuery); notifySortChanged(); }
string GetSort() { return currentSort; }
void SetShowOwn(bool show) { showOwn = show; UpdateSaveList(currentPage, lastQuery); notifyShowOwnChanged(); }
bool GetShowOwn() { return showOwn; }
}; };
#endif // SEARCHMODEL_H #endif // SEARCHMODEL_H

View File

@ -1,4 +1,7 @@
#include <sstream>
#include "SearchView.h" #include "SearchView.h"
#include "client/Client.h"
#include "interface/SaveButton.h" #include "interface/SaveButton.h"
#include "interface/Label.h" #include "interface/Label.h"
#include "interface/Textbox.h" #include "interface/Textbox.h"
@ -9,8 +12,10 @@ SearchView::SearchView():
saveButtons(vector<ui::SaveButton*>()), saveButtons(vector<ui::SaveButton*>()),
errorLabel(NULL) errorLabel(NULL)
{ {
nextButton = new ui::Button(ui::Point(XRES+BARSIZE-52, YRES+MENUSIZE-18), ui::Point(50, 16), "Next \x95"); nextButton = new ui::Button(ui::Point(XRES+BARSIZE-52, YRES+MENUSIZE-18), ui::Point(50, 16), "Next \x95");
previousButton = new ui::Button(ui::Point(1, YRES+MENUSIZE-18), ui::Point(50, 16), "\x96 Prev"); previousButton = new ui::Button(ui::Point(1, YRES+MENUSIZE-18), ui::Point(50, 16), "\x96 Prev");
infoLabel = new ui::Label(ui::Point(51, YRES+MENUSIZE-18), ui::Point(XRES+BARSIZE-102, 16), "Loading...");
class SearchAction : public ui::TextboxAction class SearchAction : public ui::TextboxAction
{ {
@ -26,11 +31,65 @@ SearchView::SearchView():
searchField->SetAlignment(AlignLeft, AlignBottom); searchField->SetAlignment(AlignLeft, AlignBottom);
searchField->SetActionCallback(new SearchAction(this)); searchField->SetActionCallback(new SearchAction(this));
class SortAction : public ui::ButtonAction
{
SearchView * v;
public:
SortAction(SearchView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
v->c->ChangeSort();
}
};
sortButton = new ui::Button(ui::Point(XRES+BARSIZE-50-50-16-10, 10), ui::Point(50, 16), "Sort");
sortButton->SetActionCallback(new SortAction(this));
sortButton->SetAlignment(AlignLeft, AlignBottom);
AddComponent(sortButton);
class MyOwnAction : public ui::ButtonAction
{
SearchView * v;
public:
MyOwnAction(SearchView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
v->c->ShowOwn(sender->GetToggleState());
}
};
ownButton = new ui::Button(ui::Point(XRES+BARSIZE-50-16-10, 10), ui::Point(50, 16), "My Own");
ownButton->SetTogglable(true);
ownButton->SetActionCallback(new MyOwnAction(this));
ownButton->SetAlignment(AlignLeft, AlignBottom);
AddComponent(ownButton);
class NextPageAction : public ui::ButtonAction
{
SearchView * v;
public:
NextPageAction(SearchView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
v->c->NextPage();
}
};
nextButton->SetActionCallback(new NextPageAction(this));
nextButton->SetAlignment(AlignRight, AlignBottom); nextButton->SetAlignment(AlignRight, AlignBottom);
class PrevPageAction : public ui::ButtonAction
{
SearchView * v;
public:
PrevPageAction(SearchView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
v->c->PrevPage();
}
};
previousButton->SetActionCallback(new PrevPageAction(this));
previousButton->SetAlignment(AlignLeft, AlignBottom); previousButton->SetAlignment(AlignLeft, AlignBottom);
AddComponent(nextButton); AddComponent(nextButton);
AddComponent(previousButton); AddComponent(previousButton);
AddComponent(searchField); AddComponent(searchField);
AddComponent(infoLabel);
ui::Label * searchPrompt = new ui::Label(ui::Point(10, 10), ui::Point(50, 16), "Search:"); ui::Label * searchPrompt = new ui::Label(ui::Point(10, 10), ui::Point(50, 16), "Search:");
searchPrompt->SetAlignment(AlignLeft, AlignBottom); searchPrompt->SetAlignment(AlignLeft, AlignBottom);
@ -46,6 +105,39 @@ SearchView::~SearchView()
{ {
} }
void SearchView::NotifySortChanged(SearchModel * sender)
{
sortButton->SetText("Sort: "+sender->GetSort());
}
void SearchView::NotifyShowOwnChanged(SearchModel * sender)
{
sortButton->SetToggleState(sender->GetShowOwn());
}
void SearchView::NotifyPageChanged(SearchModel * sender)
{
std::stringstream pageInfo;
pageInfo << "Page " << sender->GetPageNum() << " of " << sender->GetPageCount();
infoLabel->SetText(pageInfo.str());
if(sender->GetPageNum() == 1)
{
previousButton->Visible = false;
}
else
{
previousButton->Visible = true;
}
if(sender->GetPageNum() == sender->GetPageCount())
{
nextButton->Visible = false;
}
else
{
nextButton->Visible = true;
}
}
void SearchView::NotifySaveListChanged(SearchModel * sender) void SearchView::NotifySaveListChanged(SearchModel * sender)
{ {
int i = 0; int i = 0;
@ -53,6 +145,13 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
int buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset; int buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset;
vector<Save*> saves = sender->GetSaveList(); vector<Save*> saves = sender->GetSaveList();
Client::Ref().ClearThumbnailRequests();
for(i = 0; i < saveButtons.size(); i++)
{
RemoveComponent(saveButtons[i]);
delete saveButtons[i];
}
saveButtons.clear();
if(!saves.size()) if(!saves.size())
{ {
if(!errorLabel) if(!errorLabel)
@ -61,9 +160,9 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
AddComponent(errorLabel); AddComponent(errorLabel);
} }
if(sender->GetLastError().length()) if(sender->GetLastError().length())
errorLabel->SetText(sender->GetLastError()); errorLabel->SetText("\bo" + sender->GetLastError());
else else
errorLabel->SetText("No saves found"); errorLabel->SetText("\boNo saves found");
} }
else else
{ {
@ -79,13 +178,6 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
buttonAreaHeight = Size.Y - buttonYOffset - 18; buttonAreaHeight = Size.Y - buttonYOffset - 18;
buttonWidth = (buttonAreaWidth/savesX) - buttonPadding*2; buttonWidth = (buttonAreaWidth/savesX) - buttonPadding*2;
buttonHeight = (buttonAreaHeight/savesY) - buttonPadding*2; buttonHeight = (buttonAreaHeight/savesY) - buttonPadding*2;
for(i = 0; i < saveButtons.size(); i++)
{
RemoveComponent(saveButtons[i]);
delete saveButtons[i];
}
saveButtons.clear();
for(i = 0; i < saves.size(); i++) for(i = 0; i < saves.size(); i++)
{ {
if(saveX == savesX) if(saveX == savesX)

View File

@ -22,9 +22,15 @@ private:
ui::Button * previousButton; ui::Button * previousButton;
ui::Label * errorLabel; ui::Label * errorLabel;
ui::Textbox * searchField; ui::Textbox * searchField;
ui::Label * infoLabel;
ui::Button * sortButton;
ui::Button * ownButton;
void doSearch(); void doSearch();
public: public:
void NotifySaveListChanged(SearchModel * sender); void NotifySaveListChanged(SearchModel * sender);
void NotifyPageChanged(SearchModel * sender);
void NotifySortChanged(SearchModel * sender);
void NotifyShowOwnChanged(SearchModel * sender);
SearchView(); SearchView();
virtual ~SearchView(); virtual ~SearchView();
void AttachController(SearchController * _c) { c = _c; } void AttachController(SearchController * _c) { c = _c; }

View File

@ -1,6 +1,7 @@
#ifndef THUMBNAIL_H #ifndef THUMBNAIL_H
#define THUMBNAIL_H #define THUMBNAIL_H
#include <iostream>
#include "Graphics.h" #include "Graphics.h"
#include "interface/Point.h" #include "interface/Point.h"