SBSPSS/Utils/MapEdit/LayerTile.cpp

487 lines
11 KiB
C++
Raw Normal View History

2000-11-07 22:15:35 +01:00
/******************/
/*** Layer Tile ***/
/******************/
#include "stdafx.h"
#include "gl3d.h"
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>
#include "GLEnabledView.h"
2000-11-14 16:03:04 +01:00
#include "MapEdit.h"
2000-11-07 22:15:35 +01:00
#include "MapEditDoc.h"
#include "MapEditView.h"
2000-11-14 16:03:04 +01:00
#include "MainFrm.h"
2000-11-07 22:15:35 +01:00
#include "Core.h"
#include "Layer.h"
#include "LayerTile.h"
#include "Utils.h"
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
// New Layer
2000-11-20 17:21:43 +01:00
CLayerTile::CLayerTile(char *_Name,int Width,int Height,float MapDiv,float ZDiv,BOOL Is3d,BOOL Resizable)
2000-11-07 22:15:35 +01:00
{
SetName(_Name);
ZPosDiv=ZDiv;
2000-11-20 17:21:43 +01:00
MapSizeDiv=MapDiv;
ResizeFlag=Resizable;
2000-11-07 22:15:35 +01:00
Render3dFlag=Is3d;
2000-11-21 22:27:55 +01:00
VisibleFlag=TRUE;
2000-11-15 22:22:40 +01:00
Mode=MouseModePaint;
2000-11-20 17:21:43 +01:00
Map.SetSize(Width/MapDiv,Height/MapDiv,TRUE);
2000-11-07 22:15:35 +01:00
}
/*****************************************************************************/
// Load Layer
2000-11-20 21:33:42 +01:00
CLayerTile::CLayerTile(CFile *File,int Version)
2000-11-07 22:15:35 +01:00
{
2000-11-20 21:33:42 +01:00
Load(File,Version);
2000-11-07 22:15:35 +01:00
}
/*****************************************************************************/
CLayerTile::~CLayerTile()
{
}
2000-11-20 21:33:42 +01:00
/*****************************************************************************/
void CLayerTile::Load(CFile *File,float Version)
{
// Version 1
File->Read(Name,256);
File->Read(&Render3dFlag,sizeof(BOOL));
File->Read(&ZPosDiv,sizeof(float));
File->Read(&MapSizeDiv,sizeof(float));
File->Read(&ResizeFlag,sizeof(BOOL));
2000-11-21 22:27:55 +01:00
File->Read(&VisibleFlag,sizeof(BOOL));
2000-11-20 21:33:42 +01:00
File->Read(&Mode,sizeof(MouseMode));
Map.Load(File,Version);
}
/*****************************************************************************/
void CLayerTile::Save(CFile *File)
{
// Always Save current version
File->Write(Name,256);
File->Write(&Render3dFlag,sizeof(BOOL));
File->Write(&ZPosDiv,sizeof(float));
File->Write(&MapSizeDiv,sizeof(float));
File->Write(&ResizeFlag,sizeof(BOOL));
2000-11-21 22:27:55 +01:00
File->Write(&VisibleFlag,sizeof(BOOL));
2000-11-20 21:33:42 +01:00
File->Write(&Mode,sizeof(MouseMode));
Map.Save(File);
}
2000-11-20 17:21:43 +01:00
/*****************************************************************************/
void CLayerTile::Resize(int Width,int Height)
{
if (!ResizeFlag) return; // Its a fixed size, so DONT DO IT!
Map.Resize(Width/MapSizeDiv,Height/MapSizeDiv);
}
2000-11-07 22:15:35 +01:00
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
2000-11-14 16:03:04 +01:00
void CLayerTile::Render(CCore *Core,Vec &CamPos,BOOL Is3d)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
float XYDiv=GetLayerZPosDiv();
Vec ThisCam=CamPos/XYDiv;
if (Is3d && Render3dFlag)
{
glEnable(GL_DEPTH_TEST);
2000-11-22 23:08:47 +01:00
Render(Core,ThisCam,Map,TRUE);
2000-11-17 22:36:13 +01:00
glDisable(GL_DEPTH_TEST);
}
else
{
2000-11-22 23:08:47 +01:00
Render(Core,ThisCam,Map,FALSE);
2000-11-17 22:36:13 +01:00
}
2000-11-07 22:15:35 +01:00
}
/*****************************************************************************/
2000-11-17 22:36:13 +01:00
void CLayerTile::RenderCursorPaint(CCore *Core,Vec &CamPos,BOOL Is3d)
{
CTileBank &TileBank=Core->GetTileBank();
Vec ThisCam=CamPos;
CPoint &CursPos=Core->GetCursorPos();
CMap &Brush=TileBank.GetActiveBrush();
if (!Brush.IsValid()) return;
if (CursPos.x==-1 || CursPos.y==-1) return;
ThisCam.x+=CursPos.x;
ThisCam.y-=CursPos.y;
if (Is3d && Render3dFlag)
{
glEnable(GL_DEPTH_TEST);
2000-11-22 23:08:47 +01:00
Render(Core,ThisCam,Brush,TRUE,0.5);
2000-11-17 22:36:13 +01:00
glDisable(GL_DEPTH_TEST);
}
else
{
2000-11-22 23:08:47 +01:00
Render(Core,ThisCam,Brush,FALSE,0.5);
2000-11-17 22:36:13 +01:00
}
}
/*****************************************************************************/
2000-11-22 23:08:47 +01:00
void CLayerTile::Render(CCore *Core,Vec &CamPos,CMap &ThisMap,BOOL Render3d,float Alpha)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
int Width=ThisMap.GetWidth();
int Height=ThisMap.GetHeight();
if (Alpha<1)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Alpha Blend Style
glColor4f(0.5,0.5,0.5,Alpha);
}
else
{
glColor3f(0.5,0.5,0.5);
}
2000-11-07 22:15:35 +01:00
glMatrixMode(GL_MODELVIEW);
2000-11-17 22:36:13 +01:00
for (int YLoop=0; YLoop<Height; YLoop++)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
for (int XLoop=0; XLoop<Width; XLoop++)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
sMapElem &ThisElem=ThisMap.Get(XLoop,YLoop);
2000-11-15 22:22:40 +01:00
if (ThisElem.Tile)
{ // Render Non Zero Tiles
CTile &ThisTile=Core->GetTile(ThisElem.Set,ThisElem.Tile);
glLoadIdentity(); // Slow way, but good to go for the mo
2000-11-17 22:36:13 +01:00
glTranslatef(CamPos.x+XLoop,CamPos.y-YLoop,CamPos.z);
2000-11-22 23:08:47 +01:00
ThisTile.Render(ThisElem.Flags,Render3d);
2000-11-15 22:22:40 +01:00
}
2000-11-07 22:15:35 +01:00
}
}
2000-11-17 22:36:13 +01:00
glDisable(GL_BLEND);
2000-11-07 22:15:35 +01:00
}
/*****************************************************************************/
2000-11-14 16:03:04 +01:00
void CLayerTile::RenderGrid(CCore *Core,Vec &CamPos)
2000-11-07 22:15:35 +01:00
{
float XYDiv=GetLayerZPosDiv();
2000-11-17 22:36:13 +01:00
int Width=Map.GetWidth();
int Height=Map.GetHeight();
2000-11-14 16:03:04 +01:00
float StartX=CamPos.x/XYDiv;
float StartY=CamPos.y/XYDiv;
2000-11-07 22:15:35 +01:00
float OverVal=0.5;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
2000-11-14 16:03:04 +01:00
glTranslatef(StartX,StartY,CamPos.z);
2000-11-07 22:15:35 +01:00
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES);
glNormal3f( 1,1,1);
glColor3ub(255,255,255);
2000-11-17 22:36:13 +01:00
for (int YLoop=0; YLoop<Height+1; YLoop++)
2000-11-07 22:15:35 +01:00
{
glVertex3f( 0-OverVal, -YLoop+1, 0);
2000-11-17 22:36:13 +01:00
glVertex3f( Width+OverVal, -YLoop+1, 0);
2000-11-07 22:15:35 +01:00
}
2000-11-17 22:36:13 +01:00
for (int XLoop=0; XLoop<Width+1; XLoop++)
2000-11-07 22:15:35 +01:00
{
glVertex3f( XLoop, 0+1+OverVal, 0);
2000-11-17 22:36:13 +01:00
glVertex3f( XLoop, -Height+1-OverVal, 0);
2000-11-07 22:15:35 +01:00
}
glEnd();
glEnable(GL_TEXTURE_2D);
}
/*****************************************************************************/
2000-11-14 16:03:04 +01:00
void CLayerTile::FindCursorPos(CCore *Core,CMapEditView *View,Vec &CamPos,CPoint &MousePos)
2000-11-07 22:15:35 +01:00
{
GLint Viewport[4];
GLuint SelectBuffer[SELECT_BUFFER_SIZE];
int HitCount;
int TileID=0;
CPoint &CursorPos=Core->GetCursorPos();
float XYDiv=GetLayerZPosDiv();
2000-11-17 22:36:13 +01:00
int Width=Map.GetWidth();
int Height=Map.GetHeight();
2000-11-14 16:03:04 +01:00
float StartX=CamPos.x/XYDiv;
float StartY=CamPos.y/XYDiv;
2000-11-07 22:15:35 +01:00
2000-11-08 20:53:57 +01:00
2000-11-07 22:15:35 +01:00
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);
glLoadIdentity();
2000-11-14 16:03:04 +01:00
glTranslatef(StartX,StartY,CamPos.z);
2000-11-07 22:15:35 +01:00
2000-11-17 22:36:13 +01:00
for (int YLoop=0; YLoop<Height; YLoop++)
2000-11-07 22:15:35 +01:00
{
2000-11-17 22:36:13 +01:00
for (int XLoop=0; XLoop<Width; XLoop++)
2000-11-07 22:15:35 +01:00
{
glLoadName (TileID);
glBegin (GL_QUADS);
BuildGLQuad(XLoop,XLoop+1,-YLoop,-YLoop+1,0);
glEnd();
TileID++;
}
}
HitCount= glRenderMode (GL_RENDER);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
CursorPos.x=CursorPos.y=-1;
// Process hits
GLuint *HitPtr=SelectBuffer;
if (HitCount) // Just take 1st
{
int HitID=HitPtr[3];
2000-11-17 22:36:13 +01:00
CursorPos=IDToPoint(HitID,Width);
2000-11-07 22:15:35 +01:00
}
glMatrixMode(GL_MODELVIEW); // <-- Prevent arse GL assert
}
2000-11-14 16:03:04 +01:00
/*****************************************************************************/
/*** Gui *********************************************************************/
/*****************************************************************************/
void CLayerTile::InitGUI(CCore *Core)
{
CMainFrame *Frm=(CMainFrame*)AfxGetApp()->GetMainWnd();
CMultiBar *ParamBar=Frm->GetParamBar();
2000-11-15 22:22:40 +01:00
ParamBar->Add(Frm->GetGfxToolBar(),IDD_GFXTOOLBAR,TRUE);
2000-11-14 16:03:04 +01:00
}
/*****************************************************************************/
void CLayerTile::UpdateGUI(CCore *Core)
{
2000-11-17 00:08:54 +01:00
CMainFrame *Frm=(CMainFrame*)AfxGetApp()->GetMainWnd();
CGfxToolBar *GfxDlg=(CGfxToolBar *)Frm->GetDialog(IDD_GFXTOOLBAR);
if (GfxDlg)
{
GfxDlg->ResetButtons();
switch(Mode)
{
case MouseModePaint:
GfxDlg->SetButtonState(CGfxToolBar::PAINT,TRUE);
break;
case MouseModeSelect:
GfxDlg->SetButtonState(CGfxToolBar::SELECT,TRUE);
break;
case MouseModePicker:
GfxDlg->SetButtonState(CGfxToolBar::PICKER,TRUE);
break;
default:
break;
}
}
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
/*** Functions ***************************************************************/
/*****************************************************************************/
BOOL CLayerTile::SetMode(int NewMode)
{
BOOL Ret=FALSE;
2000-11-17 00:08:54 +01:00
2000-11-15 22:22:40 +01:00
// Clean up last mode
2000-11-17 00:08:54 +01:00
Ret|=ExitMode();
Mode=(MouseMode)NewMode;
Ret|=InitMode();
return(Ret);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
BOOL CLayerTile::InitMode()
{
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
break;
case MouseModeSelect:
break;
case MouseModePicker:
break;
default:
break;
}
return(FALSE);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
BOOL CLayerTile::ExitMode()
{
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
break;
case MouseModeSelect:
break;
case MouseModePicker:
break;
default:
break;
}
return(FALSE);
}
2000-11-15 22:22:40 +01:00
/*****************************************************************************/
BOOL CLayerTile::LButtonControl(CCore *Core,CMapEditView *View,UINT nFlags, CPoint &CursorPos,BOOL DownFlag)
{
BOOL Ret=FALSE;
CTileBank &TileBank=Core->GetTileBank();
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
if (DownFlag)
2000-11-17 22:36:13 +01:00
Ret=Paint(TileBank.GetLBrush(),CursorPos);
2000-11-17 00:08:54 +01:00
break;
case MouseModeSelect:
break;
case MouseModePicker:
break;
default:
break;
}
return(Ret);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
BOOL CLayerTile::RButtonControl(CCore *Core,CMapEditView *View,UINT nFlags, CPoint &CursorPos,BOOL DownFlag)
{
BOOL Ret=FALSE;
CTileBank &TileBank=Core->GetTileBank();
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
if (DownFlag)
2000-11-17 22:36:13 +01:00
Ret=Paint(TileBank.GetRBrush(),CursorPos);
2000-11-17 00:08:54 +01:00
break;
case MouseModeSelect:
break;
case MouseModePicker:
break;
default:
break;
}
return(Ret);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
BOOL CLayerTile::MouseMove(CCore *Core,CMapEditView *View,UINT nFlags, CPoint &CursorPos)
{
BOOL Ret=FALSE;
CTileBank &TileBank=Core->GetTileBank();
2000-11-17 00:08:54 +01:00
switch(Mode)
{
case MouseModePaint:
if (nFlags & MK_LBUTTON)
2000-11-17 22:36:13 +01:00
Ret=Paint(TileBank.GetLBrush(),CursorPos);
2000-11-17 00:08:54 +01:00
else
if (nFlags & MK_RBUTTON)
2000-11-17 22:36:13 +01:00
Ret=Paint(TileBank.GetRBrush(),CursorPos);
2000-11-17 00:08:54 +01:00
break;
case MouseModeSelect:
break;
case MouseModePicker:
break;
default:
break;
}
return(Ret);
}
/*****************************************************************************/
2000-11-17 22:36:13 +01:00
void CLayerTile::RenderCursor(CCore *Core,Vec &CamPos,BOOL Is3d)
2000-11-17 00:08:54 +01:00
{
2000-11-17 22:36:13 +01:00
switch(Mode)
{
case MouseModePaint:
RenderCursorPaint(Core,CamPos,Is3d);
break;
case MouseModeSelect:
break;
case MouseModePicker:
break;
default:
break;
}
}
/*****************************************************************************/
BOOL CLayerTile::MirrorX(CCore *Core)
{
CTileBank &TileBank=Core->GetTileBank();
CMap &LBrush=TileBank.GetLBrush();
CMap &RBrush=TileBank.GetRBrush();
LBrush.MirrorX(MouseFlagMirrorX);
RBrush.MirrorX(MouseFlagMirrorX);
2000-11-17 00:08:54 +01:00
return(TRUE);
}
/*****************************************************************************/
2000-11-17 22:36:13 +01:00
BOOL CLayerTile::MirrorY(CCore *Core)
2000-11-17 00:08:54 +01:00
{
2000-11-17 22:36:13 +01:00
CTileBank &TileBank=Core->GetTileBank();
CMap &LBrush=TileBank.GetLBrush();
CMap &RBrush=TileBank.GetRBrush();
LBrush.MirrorY(MouseFlagMirrorY);
RBrush.MirrorY(MouseFlagMirrorY);
2000-11-17 00:08:54 +01:00
return(TRUE);
2000-11-15 22:22:40 +01:00
}
/*****************************************************************************/
2000-11-17 00:08:54 +01:00
/*****************************************************************************/
/*****************************************************************************/
2000-11-17 22:36:13 +01:00
BOOL CLayerTile::Paint(CMap &Blk,CPoint &CursorPos)
2000-11-15 22:22:40 +01:00
{
if (CursorPos.y==-1 || CursorPos.y==-1) return(FALSE); // Off Map?
2000-11-17 22:36:13 +01:00
if (!Blk.IsValid()) return(FALSE); // Invalid tile?
Map.Set(CursorPos.x,CursorPos.y,Blk);
2000-11-15 22:22:40 +01:00
return(TRUE);
}