More stuff, need to fix memory leak

This commit is contained in:
Simon Robertshaw 2012-01-20 22:07:49 +00:00
parent c5e8b34521
commit c8073657fc
18 changed files with 384 additions and 84 deletions

View File

@ -1,36 +1,45 @@
HEADERS := $(wildcard src/*.h) $(wildcard src/*/*.h)
SOURCES := $(wildcard src/*.cpp) $(wildcard src/*/*.cpp)
OBJS += $(patsubst src/%.cpp,build/obj/powder.exe/%.o,$(SOURCES))
OBJS := $(patsubst src/%.cpp,build/obj/%.o,$(SOURCES))
FOLDERS := $(sort $(dir $(OBJS)))
FOLDERS :=
CFLAGS := -w -Isrc/ -Idata/ -DWIN32 -DWINCONSOLE
CFLAGS := -w -Isrc/ -Idata/
OFLAGS := -fkeep-inline-functions #-O3 -ffast-math -ftree-vectorize -funsafe-math-optimizations -msse2
LFLAGS := -lmingw32 -lregex -lws2_32 -lSDLmain -lpthread -lSDL -lm -lbz2 # -mwindows
CFLAGS += $(OFLAGS)
CPPC := g++
CPPC_WIN := i686-w64-mingw32-gcc
WIN_RES := i686-w64-mingw32-windres
all: build/powder.exe
all: build/powder
powder.exe: build/powder.exe
powder: build/powder
build/powder.exe: CFLAGS += -DWIN32 -DWINCONSOLE
build/powder.exe: LFLAGS := -lmingw32 -lregex -lws2_32 -lSDLmain -lpthread -lSDL -lm -lbz2 #-mwindows
build/powder: CFLAGS += -DLIN32
build/powder: LFLAGS := -lSDL -lm -lbz2
build/powder.exe: buildpaths $(OBJS)
echo $(OBJS)
$(CPPC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LFLAGS) -o $@ -ggdb
build/powder.exe: buildpaths-powder.exe $(patsubst build/obj/%.o,build/obj/powder.exe/%.o,$(OBJS))
$(CPPC) $(CFLAGS) $(OFLAGS) $(LDFLAGS) $(patsubst build/obj/%.o,build/obj/powder.exe/%.o,$(OBJS)) $(LFLAGS) -o $@ -ggdb
build/obj/powder.exe/%.o: src/%.cpp $(HEADERS)
$(CPPC) -c $(CFLAGS) -o $@ $< -ggdb
buildpaths:
$(CPPC) -c $(CFLAGS) $(OFLAGS) -o $@ $< -ggdb
buildpaths-powder.exe:
$(shell mkdir build/obj/powder.exe/)
$(shell mkdir $(FOLDERS))
$(shell mkdir $(sort $(dir $(OBJS))))
build/powder: buildpaths-powder $(patsubst build/obj/%.o,build/obj/powder/%.o,$(OBJS))
$(CPPC) $(CFLAGS) $(OFLAGS) $(LDFLAGS) $(patsubst build/obj/%.o,build/obj/powder/%.o,$(OBJS)) $(LFLAGS) -o $@ -ggdb
build/obj/powder/%.o: src/%.cpp $(HEADERS)
$(CPPC) -c $(CFLAGS) $(OFLAGS) -o $@ $< -ggdb
buildpaths-powder:
$(shell mkdir build/obj/powder/)
$(shell mkdir $(sort $(dir $(OBJS))))
clean:
rm build/obj/core/*.o
rm build/obj/ui/*.o
rm build/obj/elements/*.o
rm build/obj/*.o
rm -r build/obj/*
rm build/*.exe

View File

@ -333,3 +333,6 @@ src/cajun/elements.h
src/cajun/writer.inl
src/cajun/reader.inl
src/cajun/elements.inl
src/interface/Textbox.h
src/interface/Textbox.cpp
src/interface/Keys.h

0
build/obj/.empty Normal file
View File

View File

@ -63,28 +63,14 @@ int main(int argc, char * argv[])
int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
float fps = 0, fpsLimit = 30, delta = 1.0f;
//Renderer * ren;
//Simulation * sim = new Simulation();
//ren = new Renderer(g, sim);
ui::Engine::Ref().g = new Graphics();
ui::Engine::Ref().g->AttachSDLSurface(SDLOpen());
ui::Engine * engine = &ui::Engine::Ref();//new ui::Engine();
//ui::State * engineState = new ui::State();
ui::Label * fpsLabel = new ui::Label(ui::Point(2, 2), ui::Point(200, 14), std::string("FPS: 0"));
//engineState->AddComponent(fpsLabel);
ui::Engine * engine = &ui::Engine::Ref();
engine->Begin(XRES, YRES);
// engine->SetState(engineState);
GameController * gameController = new GameController();
engine->ShowWindow(gameController->GetView());
/*ui::Sandbox * sandbox = new ui::Sandbox();
ui::Button * button = new ui::Button(ui::Point(100, 100), ui::Point(100, 100), std::string("poP"));
engineState->AddComponent(fpsLabel);
engineState->AddComponent(sandbox);
engineState->AddComponent(button);
engineState->AddComponent(ControlFactory::MainMenu(0, YRES+MENUSIZE-17, XRES+BARSIZE, 16));*/
SDL_Event event;
while(engine->Running())
@ -98,6 +84,7 @@ int main(int argc, char * argv[])
engine->Exit();
break;
case SDL_KEYDOWN:
engine->onKeyPress(event.key.keysym.sym, false, false, false);
break;
case SDL_KEYUP:
break;
@ -113,11 +100,6 @@ int main(int argc, char * argv[])
}
event.type = 0; //Clear last event
}
//mouseButton = SDL_GetMouseState(&mouseX, &mouseY);
fpsLabel->LabelText = "";
stringstream fpsText;
fpsText << "FPS: " << fps;
fpsLabel->LabelText = fpsText.str();
engine->Tick(delta);
engine->Draw();

View File

@ -217,7 +217,6 @@ Thumbnail * Client::GetThumbnail(int saveID, int saveDate)
activeThumbRequests[i] = http_async_req_start(NULL, (char *)urlStream.str().c_str(), NULL, 0, 1);
activeThumbRequestTimes[i] = currentTime;
activeThumbRequestCompleteTimes[i] = 0;
std::cout << "ThumbCache: Requesting " << urlStream.str() << " : " << idString << std::endl;
activeThumbRequestIDs[i] = idString;
return NULL;
}

View File

@ -130,34 +130,6 @@ void Button::Draw(const Point& screenPos)
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 255, 255, 255, 255);
}
/*sf::RenderWindow* rw = reinterpret_cast<sf::RenderWindow*>(userdata); //it better be a RenderWindow or so help your god
//Draw component here
sf::Text textGraphic(ButtonText);
textGraphic.SetCharacterSize(11);
if(isButtonDown)
textGraphic.SetColor(sf::Color::Black);
else
textGraphic.SetColor(sf::Color::White);
sf::FloatRect tempRect = textGraphic.GetRect();
textGraphic.SetPosition(ceil(X + Width/2 - tempRect.Width/2), ceil(Y + Height/2 - tempRect.Height/2));
if(isMouseInside)
{
if(isButtonDown)
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
else
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::Black, 2.f, sf::Color::White));
}
else
{
if(isButtonDown)
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
else
rw->Draw(sf::Shape::Rectangle(X+1, Y+1, Width-2, Width-2, sf::Color::Black, 1.f, sf::Color::White));
}
rw->Draw(textGraphic);*/
}
void Button::OnMouseUnclick(int x, int y, unsigned int button)
@ -218,6 +190,8 @@ void Button::SetActionCallback(ButtonAction * action)
Button::~Button()
{
if(actionCallback)
delete actionCallback;
}
} /* namespace ui */

8
src/interface/Keys.h Normal file
View File

@ -0,0 +1,8 @@
#define KEY_UP SDLK_UP
#define KEY_DOWN SDLK_DOWN
#define KEY_RIGHT SDLK_RIGHT
#define KEY_LEFT SDLK_LEFT
#define KEY_HOME SDLK_HOME
#define KEY_END SDLK_END
#define KEY_BACKSPACE SDLK_BACKSPACE
#define KEY_DELETE SDLK_DELETE

View File

@ -1,30 +1,39 @@
#include <string>
#include "Config.h"
#include "Global.h"
#include "interface/Point.h"
#include "interface/Label.h"
#include "Point.h"
#include "Label.h"
using namespace ui;
Label::Label(Window* parent_state, std::string labelText):
Component(parent_state),
LabelText(labelText)
text(labelText),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre)
{
TextPosition();
}
Label::Label(Point position, Point size, std::string labelText):
Component(position, size),
LabelText(labelText)
text(labelText),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre)
{
TextPosition();
}
Label::Label(std::string labelText):
Component(),
LabelText(labelText)
text(labelText),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre)
{
TextPosition();
}
Label::~Label()
@ -32,9 +41,45 @@ Label::~Label()
}
void Label::TextPosition()
{
//Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2
switch(textVAlign)
{
case AlignTop:
textPosition.Y = 3;
break;
case AlignMiddle:
textPosition.Y = (Size.Y-10)/2;
break;
case AlignBottom:
textPosition.Y = Size.Y-11;
break;
}
switch(textHAlign)
{
case AlignLeft:
textPosition.X = 3;
break;
case AlignCentre:
textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))/2;
break;
case AlignRight:
textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))-2;
break;
}
}
void Label::SetText(std::string text)
{
this->text = text;
TextPosition();
}
void Label::Draw(const Point& screenPos)
{
Graphics * g = Engine::Ref().g;
g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)LabelText.c_str()))/2, Position.Y+(Size.Y-10)/2, LabelText, 255, 255, 255, 255);
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, 255, 255, 255, 255);
}

View File

@ -4,20 +4,28 @@
#include <string>
#include "Component.h"
#include "Misc.h"
namespace ui
{
class Label : public Component
{
std::string text;
ui::Point textPosition;
HorizontalAlignment textHAlign;
VerticalAlignment textVAlign;
public:
Label(Window* parent_state, std::string labelText);
Label(Point position, Point size, std::string labelText);
Label(std::string labelText);
virtual ~Label();
std::string LabelText;
void TextPosition();
void SetText(std::string text);
HorizontalAlignment GetHAlignment() { return textHAlign; }
VerticalAlignment GetVAlignment() { return textVAlign; }
void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); }
virtual void Draw(const Point& screenPos);
};

View File

@ -58,6 +58,7 @@ void SaveButton::Tick(float dt)
if(tempThumb)
{
thumbnail = tempThumb; //Store a local copy of the thumbnail
cout << (void *)(tempThumb) << " " << (void *)(&thumbnail) << endl;
if(thumbnail->Data)
{
if(thumbnail->Size.Y > (Size.Y-25))

192
src/interface/Textbox.cpp Normal file
View File

@ -0,0 +1,192 @@
#include <string>
#include <stdexcept>
#include "Config.h"
#include "Global.h"
#include "interface/Point.h"
#include "interface/Textbox.h"
#include "interface/Keys.h"
using namespace ui;
Textbox::Textbox(Window* parent_state, std::string textboxText):
Component(parent_state),
text(textboxText),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
actionCallback(NULL)
{
TextPosition();
cursor = text.length();
}
Textbox::Textbox(Point position, Point size, std::string textboxText):
Component(position, size),
text(textboxText),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
actionCallback(NULL)
{
TextPosition();
cursor = text.length();
}
Textbox::Textbox(std::string textboxText):
Component(),
text(textboxText),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
actionCallback(NULL)
{
TextPosition();
cursor = text.length();
}
Textbox::~Textbox()
{
if(actionCallback)
delete actionCallback;
}
void Textbox::TextPosition()
{
std::string tempText = text;
if(tempText.length() && cursor)
{
tempText.erase(cursor, tempText.length()-cursor);
cursorPosition = Graphics::textwidth((char *)tempText.c_str());
}
else
{
cursorPosition = 0;
}
//Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2
switch(textVAlign)
{
case AlignTop:
textPosition.Y = 3;
break;
case AlignMiddle:
textPosition.Y = (Size.Y-10)/2;
break;
case AlignBottom:
textPosition.Y = Size.Y-11;
break;
}
switch(textHAlign)
{
case AlignLeft:
textPosition.X = 3;
break;
case AlignCentre:
textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))/2;
break;
case AlignRight:
textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))-2;
break;
}
}
void Textbox::SetText(std::string text)
{
this->text = text;
TextPosition();
}
std::string Textbox::GetText()
{
return text;
}
void Textbox::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
{
bool changed = false;
try
{
switch(key)
{
case KEY_HOME:
cursor = 0;
break;
case KEY_END:
cursor = text.length();
break;
case KEY_LEFT:
if(cursor > 0)
cursor--;
break;
case KEY_RIGHT:
if(cursor < text.length())
cursor++;
break;
case KEY_DELETE:
if(text.length() && cursor < text.length())
{
if(ctrl)
text.erase(cursor, text.length()-cursor);
else
text.erase(cursor, 1);
changed = true;
}
break;
case KEY_BACKSPACE:
if(text.length() && cursor > 0)
{
if(ctrl)
{
text.erase(0, cursor);
cursor = 0;
}
else
{
text.erase(cursor-1, 1);
cursor--;
}
changed = true;
}
break;
default:
if(key >= ' ' && key < 127)
{
if(cursor == text.length())
{
text += key;
}
else
{
text.insert(cursor, 1, (char)key);
}
cursor++;
changed = true;
}
}
if(changed && actionCallback)
{
actionCallback->TextChangedCallback(this);
}
}
catch(std::out_of_range e)
{
cursor = 0;
text = "";
}
TextPosition();
}
void Textbox::Draw(const Point& screenPos)
{
Graphics * g = Engine::Ref().g;
if(IsFocused())
{
g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->draw_line(screenPos.X+textPosition.X+cursorPosition, screenPos.Y+3, screenPos.X+textPosition.X+cursorPosition, screenPos.Y+12, 255, 255, 255, XRES+BARSIZE);
}
else
{
g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 160, 160, 160, 255);
}
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, 255, 255, 255, 255);
}

44
src/interface/Textbox.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef TEXTBOX_H
#define TEXTBOX_H
#include <string>
#include "Component.h"
#include "Misc.h"
namespace ui
{
class Textbox;
class TextboxAction
{
public:
virtual void TextChangedCallback(ui::Textbox * sender) {}
};
class Textbox : public Component
{
std::string text;
ui::Point textPosition;
HorizontalAlignment textHAlign;
VerticalAlignment textVAlign;
int cursor, cursorPosition;
TextboxAction *actionCallback;
public:
Textbox(Window* parent_state, std::string textboxText);
Textbox(Point position, Point size, std::string textboxText);
Textbox(std::string textboxText);
virtual ~Textbox();
void TextPosition();
void SetText(std::string text);
std::string GetText();
HorizontalAlignment GetHAlignment() { return textHAlign; }
VerticalAlignment GetVAlignment() { return textVAlign; }
void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); }
void SetActionCallback(TextboxAction * action) { actionCallback = action; }
virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
virtual void Draw(const Point& screenPos);
};
}
#endif // TEXTBOX_H

View File

@ -1,3 +1,4 @@
#include <string>
#include "SearchController.h"
#include "SearchModel.h"
#include "SearchView.h"
@ -10,8 +11,13 @@ SearchController::SearchController()
searchModel->AddObserver(searchView);
searchView->AttachController(this);
searchModel->UpdateSaveList();
searchModel->UpdateSaveList("");
//Set up interface
//windowPanel.AddChild();
}
void SearchController::DoSearch(std::string query)
{
searchModel->UpdateSaveList(query);
}

View File

@ -14,6 +14,7 @@ private:
public:
SearchController();
SearchView * GetView() { return searchView; }
void DoSearch(std::string query);
};
#endif // SEARCHCONTROLLER_H

View File

@ -7,12 +7,12 @@ SearchModel::SearchModel()
{
}
void SearchModel::UpdateSaveList()
void SearchModel::UpdateSaveList(std::string query)
{
lastError = "";
saveList.clear();
notifySaveListChanged();
saveList = Client::Ref().SearchSaves(0, 12, "", "");
saveList = Client::Ref().SearchSaves(0, 12, query, "");
if(!saveList.size())
{
lastError = Client::Ref().GetLastError();

View File

@ -19,7 +19,7 @@ private:
public:
SearchModel();
void AddObserver(SearchView * observer);
void UpdateSaveList();
void UpdateSaveList(std::string query);
vector<Save> GetSaveList();
string GetLastError() { return lastError; }
};

View File

@ -1,6 +1,7 @@
#include "SearchView.h"
#include "interface/SaveButton.h"
#include "interface/Label.h"
#include "interface/Textbox.h"
#include "Misc.h"
SearchView::SearchView():
@ -11,10 +12,34 @@ SearchView::SearchView():
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");
class SearchAction : public ui::TextboxAction
{
SearchView * v;
public:
SearchAction(SearchView * _v) { v = _v; }
void TextChangedCallback(ui::Textbox * sender)
{
v->doSearch();
}
};
searchField = new ui::Textbox(ui::Point(60, 10), ui::Point((XRES+BARSIZE)-((50*2)+16+10+50+10), 16), "");
searchField->SetAlignment(AlignLeft, AlignBottom);
searchField->SetActionCallback(new SearchAction(this));
nextButton->SetAlignment(AlignRight, AlignBottom);
previousButton->SetAlignment(AlignLeft, AlignBottom);
AddComponent(nextButton);
AddComponent(previousButton);
AddComponent(searchField);
ui::Label * searchPrompt = new ui::Label(ui::Point(10, 10), ui::Point(50, 16), "Search:");
searchPrompt->SetAlignment(AlignLeft, AlignBottom);
AddComponent(searchPrompt);
}
void SearchView::doSearch()
{
c->DoSearch(searchField->GetText());
}
SearchView::~SearchView()
@ -36,9 +61,9 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
AddComponent(errorLabel);
}
if(sender->GetLastError().length())
errorLabel->LabelText = sender->GetLastError();
errorLabel->SetText(sender->GetLastError());
else
errorLabel->LabelText = "No saves found";
errorLabel->SetText("No saves found");
}
else
{

View File

@ -6,6 +6,7 @@
#include "interface/SaveButton.h"
#include "interface/Button.h"
#include "interface/Label.h"
#include "interface/Textbox.h"
using namespace std;
@ -20,6 +21,8 @@ private:
ui::Button * nextButton;
ui::Button * previousButton;
ui::Label * errorLabel;
ui::Textbox * searchField;
void doSearch();
public:
void NotifySaveListChanged(SearchModel * sender);
SearchView();