/*********************/ /*** TileSet Stuph ***/ /*********************/ #include "stdafx.h" #include "gl3d.h" #include #include #include #include "GLEnabledView.h" #include #include "Core.h" #include "TileSet.h" #include "GinTex.h" #include "utils.h" #include "MapEdit.h" #include "MapEditDoc.h" #include "MapEditView.h" #include "MainFrm.h" #include "TileSetDlg.h" /*****************************************************************************/ /*****************************************************************************/ /*** TileBank ****************************************************************/ /*****************************************************************************/ const float TileBrowserGap=0.2f; const float TileBrowserX0=0-TileBrowserGap/2; const float TileBrowserX1=1+TileBrowserGap/2; const float TileBrowserY0=0-TileBrowserGap/2; const float TileBrowserY1=1+TileBrowserGap/2; /*****************************************************************************/ CTileBank::CTileBank() { LoadFlag=FALSE; CurrentSet=0; for (int i=0; iRead(&ListSize,sizeof(int)); File->Read(&CurrentSet,sizeof(int)); File->Read(&ActiveBrush,sizeof(int)); Brush[0].Load(File,Version); Brush[1].Load(File,Version); for (int i=0;iRead(Filename,256+64); AddTileSet(Filename); } } /*****************************************************************************/ void CTileBank::Save(CFile *File) { int ListSize=TileSet.size(); File->Write(&ListSize,sizeof(int)); File->Write(&CurrentSet,sizeof(int)); File->Write(&ActiveBrush,sizeof(int)); Brush[0].Save(File); Brush[1].Save(File); for (int i=0;iWrite(Filename,256+64); } } /*****************************************************************************/ void CTileBank::AddTileSet(char *Filename) { int ListSize=TileSet.size(); TileSet.push_back(CTileSet(Filename,ListSize)); LoadFlag=TRUE; } /*****************************************************************************/ void CTileBank::LoadTileSets(CCore *Core) { int ListSize=TileSet.size(); for (int i=0;iIsGridOn()) TileSet[CurrentSet].RenderGrid(CamPos); } /*****************************************************************************/ void CTileBank::FindCursorPos(CCore *Core,CMapEditView *View,Vec &CamPos,CPoint &MousePos) { if (!TileSet.size()) return; // No tiles, return CursorPos=TileSet[CurrentSet].FindCursorPos(Core,View,CamPos,MousePos); SelEnd=CursorPos; } /*****************************************************************************/ /*** Gui *********************************************************************/ /*****************************************************************************/ void CTileBank::UpdateGUI(CCore *Core,BOOL IsTileView) { CMainFrame *Frm=(CMainFrame*)AfxGetApp()->GetMainWnd(); CTileSetDlg *TileSetDlg=(CTileSetDlg*)Frm->GetDialog(IDD_TILESET_DIALOG); int ListSize=TileSet.size(); ASSERT(TileSetDlg); TileSetDlg->TileSetList.ResetContent(); if (ListSize) { for (int i=0; iTileSetList.AddString(TileSet[i].GetName()); } TileSetDlg->TileSetList.SetCurSel(CurrentSet); } else { IsTileView=FALSE; } TileSetDlg->TileSetList.EnableWindow(IsTileView); } /*****************************************************************************/ /*** Functions ***************************************************************/ /*****************************************************************************/ BOOL CTileBank::Select(int BrushID,BOOL DownFlag) { if (DownFlag && SelStart==-1) { if (CursorPos==-1) return(FALSE); SelStart=CursorPos; TRACE3("Start %i=%i,%i\n",SelStart,SelStart%TileBrowserWidth,SelStart/TileBrowserWidth); } else if (!DownFlag && SelStart!=-1) { if (CursorPos==-1) return(SelectCancel()); SetBrush(GetBrush(BrushID)); SelStart=-1; TRACE0("END SEL\n"); } return(TRUE); } /*****************************************************************************/ void CTileBank::SetBrush(CMap &ThisBrush) { CPoint S=IDToPoint(SelStart,TileBrowserWidth); CPoint E=IDToPoint(SelEnd,TileBrowserWidth); CPoint Start=CPoint(min(S.x,E.x), min(S.y,E.y)); CPoint End=CPoint( max(S.x,E.x), max(S.y,E.y)); int Width=(End.x-Start.x)+1; int Height=(End.y-Start.y)+1; sMapElem ThisElem; int MaxTile=TileSet[CurrentSet].GetTileCount(); if (PointToID(End,TileBrowserWidth)>=MaxTile) SelectCancel(); // Invalid selection ThisElem.Set=CurrentSet; ThisElem.Flags=0; ThisBrush.Delete(); ThisBrush.SetSize(Width,Height); for (int Y=0; YListSize) return; if (SelStart==-1) { Start=IDToPoint(CursorPos,TileBrowserWidth); End=Start; } else { CPoint S=IDToPoint(SelStart,TileBrowserWidth); CPoint E=IDToPoint(SelEnd,TileBrowserWidth); Start=CPoint( min(S.x,E.x), min(S.y,E.y)); End=CPoint( max(S.x,E.x), max(S.y,E.y)); if (PointToID(End,TileBrowserWidth)>=MaxTile) return; // Invalid selection } glMatrixMode(GL_MODELVIEW); glDisable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); for (int Y=Start.y; Y<=End.y; Y++) { for (int X=Start.x; X<=End.x; X++) { // RenderCursorBlock(CamPos,X,Y); glLoadIdentity(); glTranslatef(CamPos.x+X*(1+TileBrowserGap),CamPos.y-Y*(1+TileBrowserGap),CamPos.z); glBegin(GL_QUADS); glNormal3f( 1,1,1); glColor4f(1,1,0,0.5); BuildGLQuad(TileBrowserX0,TileBrowserX1,TileBrowserY0,TileBrowserY1,0); glEnd(); } } glEnable(GL_TEXTURE_2D); glDisable(GL_BLEND); } /*****************************************************************************/ void CTileSet::RenderGrid(Vec &CamPos) { int ListSize=Tile.size(); int TileID=0; glMatrixMode(GL_MODELVIEW); glDisable(GL_TEXTURE_2D); while(TileID!=ListSize) { CPoint Pos=IDToPoint(TileID,TileBrowserWidth); glLoadIdentity(); glTranslatef(CamPos.x+Pos.x*(1+TileBrowserGap),CamPos.y-Pos.y*(1+TileBrowserGap),CamPos.z); glBegin(GL_LINES); glNormal3f( 1,1,1); glColor3ub(255,255,255); glVertex3f( TileBrowserX0,TileBrowserY0,0); glVertex3f( TileBrowserX1,TileBrowserY0,0); glVertex3f( TileBrowserX0,TileBrowserY1,0); glVertex3f( TileBrowserX1,TileBrowserY1,0); glVertex3f( TileBrowserX0,TileBrowserY0,0); glVertex3f( TileBrowserX0,TileBrowserY1,0); glVertex3f( TileBrowserX1,TileBrowserY0,0); glVertex3f( TileBrowserX1,TileBrowserY1,0); glEnd(); TileID++; } glEnable(GL_TEXTURE_2D); } /*****************************************************************************/ int CTileSet::FindCursorPos(CCore *Core,CMapEditView *View,Vec &CamPos,CPoint &MousePos) { int ListSize=Tile.size(); GLint Viewport[4]; GLuint SelectBuffer[SELECT_BUFFER_SIZE]; int HitCount; int TileID=0; glGetIntegerv(GL_VIEWPORT, Viewport); glSelectBuffer (SELECT_BUFFER_SIZE, SelectBuffer ); glRenderMode (GL_SELECT); glInitNames(); glPushName(-1); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix( MousePos.x ,(Viewport[3]-MousePos.y),5.0,5.0,Viewport); View->SetupPersMatrix(); glMatrixMode(GL_MODELVIEW); while(TileID!=ListSize) { CPoint Pos=IDToPoint(TileID,TileBrowserWidth); glLoadIdentity(); glTranslatef(CamPos.x+Pos.x*(1+TileBrowserGap),CamPos.y-Pos.y*(1+TileBrowserGap),CamPos.z); glLoadName (TileID); glBegin (GL_QUADS); BuildGLQuad(TileBrowserX0,TileBrowserX1,TileBrowserY0,TileBrowserY1,0); glEnd(); TileID++; } HitCount= glRenderMode (GL_RENDER); glMatrixMode(GL_PROJECTION); glPopMatrix(); // Process hits GLuint *HitPtr=SelectBuffer; TileID=-1; if (HitCount) // Just take 1st { TileID=HitPtr[3]; } glMatrixMode(GL_MODELVIEW); // <-- Prevent arse GL assert return(TileID); }