Unify stamp and clipboard placement code, GameSave translation and transoformation working (not particularly well)

This commit is contained in:
Simon Robertshaw
2012-06-09 19:42:07 +01:00
parent c408e035fa
commit 050ad82155
7 changed files with 216 additions and 69 deletions

View File

@@ -98,7 +98,108 @@ char * GameSave::Serialise(int & dataSize)
void GameSave::Transform(matrix2d transform, vector2d translate) void GameSave::Transform(matrix2d transform, vector2d translate)
{ {
void *ndata;
/*unsigned char (*blockMap)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(unsigned char));
unsigned char (*blockMapNew)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(unsigned char));
particle *partst = calloc(sizeof(particle), NPART);
sign *signst = calloc(MAXSIGNS, sizeof(sign));
unsigned (*pmapt)[XRES] = calloc(YRES*XRES, sizeof(unsigned));
float (*fanVelX)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*fanVelY)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*fanVelXNew)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*fanVelYNew)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*vxo)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*vyo)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*vxn)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*vyn)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*pvo)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*pvn)[XRES/CELL] = calloc((YRES/CELL)*(XRES/CELL), sizeof(float));*/
unsigned char (*blockMapNew)[blockWidth] = (unsigned char(*)[blockWidth])new unsigned char[blockHeight*blockWidth];
float (*fanVelXNew)[blockWidth] = (float(*)[blockWidth])new float[blockHeight*blockWidth];
float (*fanVelYNew)[blockWidth] = (float(*)[blockWidth])new float[blockHeight*blockWidth];
int i, x, y, nx, ny, w = blockWidth*CELL, h = blockHeight*CELL, nw, nh;
vector2d pos, tmp, ctl, cbr, vel;
vector2d cornerso[4];
// undo any translation caused by rotation
cornerso[0] = v2d_new(0,0);
cornerso[1] = v2d_new(w-1,0);
cornerso[2] = v2d_new(0,h-1);
cornerso[3] = v2d_new(w-1,h-1);
for (i=0; i<4; i++)
{
tmp = m2d_multiply_v2d(transform,cornerso[i]);
if (i==0) ctl = cbr = tmp; // top left, bottom right corner
if (tmp.x<ctl.x) ctl.x = tmp.x;
if (tmp.y<ctl.y) ctl.y = tmp.y;
if (tmp.x>cbr.x) cbr.x = tmp.x;
if (tmp.y>cbr.y) cbr.y = tmp.y;
}
// casting as int doesn't quite do what we want with negative numbers, so use floor()
tmp = v2d_new(floor(ctl.x+0.5f),floor(ctl.y+0.5f));
translate = v2d_sub(translate,tmp);
nw = floor(cbr.x+0.5f)-floor(ctl.x+0.5f)+1;
nh = floor(cbr.y+0.5f)-floor(ctl.y+0.5f)+1;
if (nw>XRES) nw = XRES;
if (nh>YRES) nh = YRES;
// rotate and translate signs, parts, walls
for (i=0; i < signs.size(); i++)
{
pos = v2d_new(signs[i].x, signs[i].y);
pos = v2d_add(m2d_multiply_v2d(transform,pos),translate);
nx = floor(pos.x+0.5f);
ny = floor(pos.y+0.5f);
if (nx<0 || nx>=nw || ny<0 || ny>=nh)
{
signs[i].text[0] = 0;
continue;
}
signs[i].x = nx;
signs[i].y = ny;
}
for (i=0; i<NPART; i++)
{
if (!particles[i].type) continue;
pos = v2d_new(particles[i].x, particles[i].y);
pos = v2d_add(m2d_multiply_v2d(transform,pos),translate);
nx = floor(pos.x+0.5f);
ny = floor(pos.y+0.5f);
if (nx<0 || nx>=nw || ny<0 || ny>=nh)
{
particles[i].type = PT_NONE;
continue;
}
particles[i].x = nx;
particles[i].y = ny;
vel = v2d_new(particles[i].vx, particles[i].vy);
vel = m2d_multiply_v2d(transform, vel);
particles[i].vx = vel.x;
particles[i].vy = vel.y;
}
for (y=0; y<YRES/CELL; y++)
for (x=0; x<XRES/CELL; x++)
{
pos = v2d_new(x*CELL+CELL*0.4f, y*CELL+CELL*0.4f);
pos = v2d_add(m2d_multiply_v2d(transform,pos),translate);
nx = pos.x/CELL;
ny = pos.y/CELL;
if (nx<0 || nx>=nw/CELL || ny<0 || ny>=nh/CELL)
continue;
if (blockMap[y][x])
{
blockMapNew[ny][nx] = blockMap[y][x];
if (blockMap[y][x]==WL_FAN)
{
vel = v2d_new(fanVelX[y][x], fanVelY[y][x]);
vel = m2d_multiply_v2d(transform, vel);
fanVelXNew[ny][nx] = vel.x;
fanVelYNew[ny][nx] = vel.y;
}
}
}
//ndata = build_save(size,0,0,nw,nh,blockMapNew,vxn,vyn,pvn,fanVelXNew,fanVelYNew,signst,partst);
blockMapPtr = (unsigned char*)blockMapNew;
fanVelXPtr = (float*)fanVelXNew;
fanVelYPtr = (float*)fanVelYNew;
} }
void GameSave::readOPS(char * data, int dataLength) void GameSave::readOPS(char * data, int dataLength)

View File

@@ -107,6 +107,7 @@ public:
if(cc->localBrowser->GetSave()) if(cc->localBrowser->GetSave())
{ {
cc->gameModel->SetStamp(cc->localBrowser->GetSave()->GetGameSave()); cc->gameModel->SetStamp(cc->localBrowser->GetSave()->GetGameSave());
cc->LoadStamp();
} }
else else
cc->gameModel->SetStamp(NULL); cc->gameModel->SetStamp(NULL);
@@ -168,20 +169,11 @@ GameView * GameController::GetView()
return gameView; return gameView;
} }
void GameController::PlaceStamp(ui::Point position) void GameController::PlaceSave(ui::Point position)
{ {
if(gameModel->GetStamp()) if(gameModel->GetPlaceSave())
{ {
gameModel->GetSimulation()->Load(position.X, position.Y, gameModel->GetStamp()); gameModel->GetSimulation()->Load(position.X, position.Y, gameModel->GetPlaceSave());
gameModel->SetPaused(gameModel->GetPaused());
}
}
void GameController::PlaceClipboard(ui::Point position)
{
if(gameModel->GetClipboard())
{
gameModel->GetSimulation()->Load(position.X, position.Y, gameModel->GetClipboard());
gameModel->SetPaused(gameModel->GetPaused()); gameModel->SetPaused(gameModel->GetPaused());
} }
} }
@@ -309,6 +301,31 @@ void GameController::DrawPoints(int toolSelection, queue<ui::Point*> & pointQueu
} }
} }
void GameController::LoadClipboard()
{
gameModel->SetPlaceSave(gameModel->GetClipboard());
}
void GameController::LoadStamp()
{
gameModel->SetPlaceSave(gameModel->GetStamp());
}
void GameController::TranslateSave(ui::Point point)
{
matrix2d transform = m2d_identity;
vector2d translate = v2d_new(point.X, point.Y);
gameModel->GetPlaceSave()->Transform(transform, translate);
gameModel->SetPlaceSave(gameModel->GetPlaceSave());
}
void GameController::TransformSave(matrix2d transform)
{
vector2d translate = v2d_zero;
gameModel->GetPlaceSave()->Transform(transform, translate);
gameModel->SetPlaceSave(gameModel->GetPlaceSave());
}
void GameController::ToolClick(int toolSelection, ui::Point point) void GameController::ToolClick(int toolSelection, ui::Point point)
{ {
Simulation * sim = gameModel->GetSimulation(); Simulation * sim = gameModel->GetSimulation();

View File

@@ -85,17 +85,21 @@ public:
void OpenRenderOptions(); void OpenRenderOptions();
void OpenSaveWindow(); void OpenSaveWindow();
void OpenStamps(); void OpenStamps();
void PlaceStamp(ui::Point position); void PlaceSave(ui::Point position);
void PlaceClipboard(ui::Point position);
void ClearSim(); void ClearSim();
void ReloadSim(); void ReloadSim();
void Vote(int direction); void Vote(int direction);
void ChangeBrush(); void ChangeBrush();
void ShowConsole(); void ShowConsole();
void FrameStep(); void FrameStep();
void TranslateSave(ui::Point point);
void TransformSave(matrix2d transform);
ui::Point PointTranslate(ui::Point point); ui::Point PointTranslate(ui::Point point);
ui::Point NormaliseBlockCoord(ui::Point point); ui::Point NormaliseBlockCoord(ui::Point point);
std::string ElementResolve(int type); std::string ElementResolve(int type);
void LoadClipboard();
void LoadStamp();
}; };
#endif // GAMECONTROLLER_H #endif // GAMECONTROLLER_H

View File

@@ -18,7 +18,8 @@ GameModel::GameModel():
currentSave(NULL), currentSave(NULL),
colourSelector(false), colourSelector(false),
clipboard(NULL), clipboard(NULL),
stamp(NULL) stamp(NULL),
placeSave(NULL)
{ {
sim = new Simulation(); sim = new Simulation();
ren = new Renderer(ui::Engine::Ref().g, sim); ren = new Renderer(ui::Engine::Ref().g, sim);
@@ -430,7 +431,17 @@ void GameModel::SetStamp(GameSave * save)
if(stamp) if(stamp)
delete stamp; delete stamp;
stamp = new GameSave(*save); stamp = new GameSave(*save);
notifyStampChanged(); }
void GameModel::SetPlaceSave(GameSave * save)
{
if(save != placeSave)
delete placeSave;
if(save != placeSave)
placeSave = new GameSave(*save);
else if(!save)
placeSave = NULL;
notifyPlaceSaveChanged();
} }
void GameModel::AddStamp(GameSave * save) void GameModel::AddStamp(GameSave * save)
@@ -439,7 +450,6 @@ void GameModel::AddStamp(GameSave * save)
delete stamp; delete stamp;
stamp = new GameSave(*save); stamp = new GameSave(*save);
Client::Ref().AddStamp(save); Client::Ref().AddStamp(save);
notifyClipboardChanged();
} }
void GameModel::SetClipboard(GameSave * save) void GameModel::SetClipboard(GameSave * save)
@@ -447,7 +457,6 @@ void GameModel::SetClipboard(GameSave * save)
if(clipboard) if(clipboard)
delete clipboard; delete clipboard;
clipboard = save; clipboard = save;
notifyClipboardChanged();
} }
GameSave * GameModel::GetClipboard() GameSave * GameModel::GetClipboard()
@@ -455,6 +464,11 @@ GameSave * GameModel::GetClipboard()
return clipboard; return clipboard;
} }
GameSave * GameModel::GetPlaceSave()
{
return placeSave;
}
GameSave * GameModel::GetStamp() GameSave * GameModel::GetStamp()
{ {
return stamp; return stamp;
@@ -577,19 +591,11 @@ void GameModel::notifyZoomChanged()
} }
} }
void GameModel::notifyStampChanged() void GameModel::notifyPlaceSaveChanged()
{ {
for(int i = 0; i < observers.size(); i++) for(int i = 0; i < observers.size(); i++)
{ {
observers[i]->NotifyStampChanged(this); observers[i]->NotifyPlaceSaveChanged(this);
}
}
void GameModel::notifyClipboardChanged()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifyClipboardChanged(this);
} }
} }

View File

@@ -36,6 +36,7 @@ private:
//unsigned char * clipboardData; //unsigned char * clipboardData;
GameSave * stamp; GameSave * stamp;
GameSave * clipboard; GameSave * clipboard;
GameSave * placeSave;
deque<string> consoleLog; deque<string> consoleLog;
vector<GameView*> observers; vector<GameView*> observers;
vector<Tool*> toolList; vector<Tool*> toolList;
@@ -63,7 +64,7 @@ private:
void notifyUserChanged(); void notifyUserChanged();
void notifyZoomChanged(); void notifyZoomChanged();
void notifyClipboardChanged(); void notifyClipboardChanged();
void notifyStampChanged(); void notifyPlaceSaveChanged();
void notifyColourSelectorColourChanged(); void notifyColourSelectorColourChanged();
void notifyColourSelectorVisibilityChanged(); void notifyColourSelectorVisibilityChanged();
void notifyLogChanged(string entry); void notifyLogChanged(string entry);
@@ -113,10 +114,12 @@ public:
void SetStamp(GameSave * newStamp); void SetStamp(GameSave * newStamp);
void AddStamp(GameSave * save); void AddStamp(GameSave * save);
void SetClipboard(GameSave * save); void SetClipboard(GameSave * save);
void SetPlaceSave(GameSave * save);
void Log(string message); void Log(string message);
deque<string> GetLog(); deque<string> GetLog();
GameSave * GetClipboard(); GameSave * GetClipboard();
GameSave * GetStamp(); GameSave * GetStamp();
GameSave * GetPlaceSave();
}; };
#endif // GAMEMODEL_H #endif // GAMEMODEL_H

View File

@@ -29,8 +29,7 @@ GameView::GameView():
selectMode(SelectNone), selectMode(SelectNone),
selectPoint1(0, 0), selectPoint1(0, 0),
selectPoint2(0, 0), selectPoint2(0, 0),
stampThumb(NULL), placeSaveThumb(NULL),
clipboardThumb(NULL),
mousePosition(0, 0) mousePosition(0, 0)
{ {
int currentX = 1; int currentX = 1;
@@ -493,7 +492,7 @@ void GameView::OnMouseMove(int x, int y, int dx, int dy)
mousePosition = c->PointTranslate(ui::Point(x, y)); mousePosition = c->PointTranslate(ui::Point(x, y));
if(selectMode!=SelectNone) if(selectMode!=SelectNone)
{ {
if(selectMode==PlaceStamp || selectMode==PlaceClipboard) if(selectMode==PlaceSave)
selectPoint1 = ui::Point(x, y); selectPoint1 = ui::Point(x, y);
if(selectPoint1.X!=-1) if(selectPoint1.X!=-1)
selectPoint2 = ui::Point(x, y); selectPoint2 = ui::Point(x, y);
@@ -544,9 +543,9 @@ void GameView::OnMouseUp(int x, int y, unsigned button)
{ {
if(button==BUTTON_LEFT) if(button==BUTTON_LEFT)
{ {
if(selectMode==PlaceStamp || selectMode==PlaceClipboard) if(selectMode==PlaceSave)
{ {
Thumbnail * tempThumb = selectMode==PlaceStamp?stampThumb:clipboardThumb; Thumbnail * tempThumb = placeSaveThumb;
if(tempThumb) if(tempThumb)
{ {
int thumbX = selectPoint2.X - (tempThumb->Size.X/2); int thumbX = selectPoint2.X - (tempThumb->Size.X/2);
@@ -562,10 +561,7 @@ void GameView::OnMouseUp(int x, int y, unsigned button)
if(thumbY+(tempThumb->Size.Y)>=YRES) if(thumbY+(tempThumb->Size.Y)>=YRES)
thumbY = YRES-tempThumb->Size.Y; thumbY = YRES-tempThumb->Size.Y;
if(selectMode==PlaceStamp) c->PlaceSave(ui::Point(thumbX, thumbY));
c->PlaceStamp(ui::Point(thumbX, thumbY));
if(selectMode==PlaceClipboard)
c->PlaceClipboard(ui::Point(thumbX, thumbY));
} }
} }
else else
@@ -646,6 +642,40 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
{ {
if(selectMode!=SelectNone) if(selectMode!=SelectNone)
{ {
if(selectMode==PlaceSave)
{
switch(key)
{
case KEY_RIGHT:
case 'd':
c->TranslateSave(ui::Point(1, 0));
break;
case KEY_LEFT:
case 'a':
c->TranslateSave(ui::Point(-1, 0));
break;
case KEY_UP:
case 'w':
c->TranslateSave(ui::Point(0, -1));
break;
case KEY_DOWN:
case 's':
c->TranslateSave(ui::Point(0, 1));
break;
case 'r':
if(shift)
{
//Flip
c->TransformSave(m2d_new(-1,0,0,1));
}
else
{
//Rotate 90deg
c->TransformSave(m2d_new(0,1,-1,0));
}
break;
}
}
return; return;
} }
switch(key) switch(key)
@@ -703,15 +733,14 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
} }
break; break;
case 'v': case 'v':
if(ctrl && clipboardThumb) if(ctrl)
{ {
selectMode = PlaceClipboard; c->LoadClipboard();
selectPoint2 = ui::Point(-1, -1); selectPoint2 = ui::Point(-1, -1);
selectPoint1 = selectPoint2; selectPoint1 = selectPoint2;
} }
break; break;
case 'l': case 'l':
selectMode = PlaceStamp;
selectPoint2 = ui::Point(-1, -1); selectPoint2 = ui::Point(-1, -1);
selectPoint1 = selectPoint2; selectPoint1 = selectPoint2;
c->OpenStamps(); c->OpenStamps();
@@ -746,9 +775,7 @@ void GameView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bo
void GameView::OnTick(float dt) void GameView::OnTick(float dt)
{ {
if(selectMode==PlaceStamp && !stampThumb) if(selectMode==PlaceSave && !placeSaveThumb)
selectMode = SelectNone;
if(selectMode==PlaceClipboard&& !clipboardThumb)
selectMode = SelectNone; selectMode = SelectNone;
if(zoomEnabled && !zoomCursorFixed) if(zoomEnabled && !zoomCursorFixed)
c->SetZoomPosition(currentMouse); c->SetZoomPosition(currentMouse);
@@ -828,29 +855,20 @@ void GameView::NotifyLogChanged(GameModel * sender, string entry)
logEntries.pop_back(); logEntries.pop_back();
} }
void GameView::NotifyClipboardChanged(GameModel * sender) void GameView::NotifyPlaceSaveChanged(GameModel * sender)
{ {
if(clipboardThumb) if(placeSaveThumb)
delete clipboardThumb; delete placeSaveThumb;
if(sender->GetClipboard()) if(sender->GetPlaceSave())
{ {
clipboardThumb = SaveRenderer::Ref().Render(sender->GetClipboard()); placeSaveThumb = SaveRenderer::Ref().Render(sender->GetPlaceSave());
selectMode = PlaceSave;
} }
else else
clipboardThumb = NULL;
}
void GameView::NotifyStampChanged(GameModel * sender)
{
if(stampThumb)
delete stampThumb;
if(sender->GetStamp())
{ {
stampThumb = SaveRenderer::Ref().Render(sender->GetStamp()); placeSaveThumb = NULL;
selectMode = SelectNone;
} }
else
stampThumb = NULL;
} }
void GameView::changeColour() void GameView::changeColour()
@@ -890,9 +908,9 @@ void GameView::OnDraw()
if(selectMode!=SelectNone) if(selectMode!=SelectNone)
{ {
if(selectMode==PlaceStamp || selectMode==PlaceClipboard) if(selectMode==PlaceSave)
{ {
Thumbnail * tempThumb = selectMode==PlaceStamp?stampThumb:clipboardThumb; Thumbnail * tempThumb = placeSaveThumb;
if(tempThumb && selectPoint2.X!=-1) if(tempThumb && selectPoint2.X!=-1)
{ {
int thumbX = selectPoint2.X - (tempThumb->Size.X/2); int thumbX = selectPoint2.X - (tempThumb->Size.X/2);

View File

@@ -23,7 +23,7 @@ enum DrawMode
enum SelectMode enum SelectMode
{ {
SelectNone, SelectStamp, SelectCopy, PlaceClipboard, PlaceStamp SelectNone, SelectStamp, SelectCopy, PlaceSave
}; };
class GameController; class GameController;
@@ -73,8 +73,7 @@ private:
ui::Point mousePosition; ui::Point mousePosition;
Thumbnail * clipboardThumb; Thumbnail * placeSaveThumb;
Thumbnail * stampThumb;
Particle sample; Particle sample;
@@ -99,8 +98,7 @@ public:
void NotifyZoomChanged(GameModel * sender); void NotifyZoomChanged(GameModel * sender);
void NotifyColourSelectorVisibilityChanged(GameModel * sender); void NotifyColourSelectorVisibilityChanged(GameModel * sender);
void NotifyColourSelectorColourChanged(GameModel * sender); void NotifyColourSelectorColourChanged(GameModel * sender);
void NotifyClipboardChanged(GameModel * sender); void NotifyPlaceSaveChanged(GameModel * sender);
void NotifyStampChanged(GameModel * sender);
void NotifyLogChanged(GameModel * sender, string entry); void NotifyLogChanged(GameModel * sender, string entry);
virtual void OnMouseMove(int x, int y, int dx, int dy); virtual void OnMouseMove(int x, int y, int dx, int dy);
virtual void OnMouseDown(int x, int y, unsigned button); virtual void OnMouseDown(int x, int y, unsigned button);