diff options
| author | Magnus Auvinen <magnus.auvinen@gmail.com> | 2010-05-29 07:25:38 +0000 |
|---|---|---|
| committer | Magnus Auvinen <magnus.auvinen@gmail.com> | 2010-05-29 07:25:38 +0000 |
| commit | 72c06a258940696093f255fb1061beb58e1cdd0b (patch) | |
| tree | 36b9a7712eec2d4f07837eab9c38ef1c5af85319 /src/game/editor/ed_layer_tiles.cpp | |
| parent | e56feb597bc743677633432f77513b02907fd169 (diff) | |
| download | zcatch-72c06a258940696093f255fb1061beb58e1cdd0b.tar.gz zcatch-72c06a258940696093f255fb1061beb58e1cdd0b.zip | |
copied refactor to trunk
Diffstat (limited to 'src/game/editor/ed_layer_tiles.cpp')
| -rw-r--r-- | src/game/editor/ed_layer_tiles.cpp | 354 |
1 files changed, 194 insertions, 160 deletions
diff --git a/src/game/editor/ed_layer_tiles.cpp b/src/game/editor/ed_layer_tiles.cpp index 0d42cb78..ecd7c62c 100644 --- a/src/game/editor/ed_layer_tiles.cpp +++ b/src/game/editor/ed_layer_tiles.cpp @@ -1,247 +1,281 @@ -#include <base/math.hpp> +#include <base/math.h> -#include <engine/client/graphics.h> +#include <engine/graphics.h> +#include <engine/textrender.h> -#include <game/generated/gc_data.hpp> -#include <game/client/render.hpp> -#include "ed_editor.hpp" +#include <game/generated/client_data.h> +#include <game/client/render.h> +#include "ed_editor.h" -LAYER_TILES::LAYER_TILES(int w, int h) +CLayerTiles::CLayerTiles(int w, int h) { - type = LAYERTYPE_TILES; - type_name = "Tiles"; - width = w; - height = h; - image = -1; - tex_id = -1; - game = 0; + m_Type = LAYERTYPE_TILES; + m_pTypeName = "Tiles"; + m_Width = w; + m_Height = h; + m_Image = -1; + m_TexId = -1; + m_Game = 0; - tiles = new TILE[width*height]; - mem_zero(tiles, width*height*sizeof(TILE)); + m_pTiles = new CTile[m_Width*m_Height]; + mem_zero(m_pTiles, m_Width*m_Height*sizeof(CTile)); } -LAYER_TILES::~LAYER_TILES() +CLayerTiles::~CLayerTiles() { - delete [] tiles; + delete [] m_pTiles; } -void LAYER_TILES::prepare_for_save() +void CLayerTiles::PrepareForSave() { - for(int y = 0; y < height; y++) - for(int x = 0; x < width; x++) - tiles[y*width+x].flags &= TILEFLAG_VFLIP|TILEFLAG_HFLIP; + for(int y = 0; y < m_Height; y++) + for(int x = 0; x < m_Width; x++) + m_pTiles[y*m_Width+x].m_Flags &= TILEFLAG_VFLIP|TILEFLAG_HFLIP; - if(image != -1) + if(m_Image != -1) { - for(int y = 0; y < height; y++) - for(int x = 0; x < width; x++) - tiles[y*width+x].flags |= editor->map.images[image]->tileflags[tiles[y*width+x].index]; + for(int y = 0; y < m_Height; y++) + for(int x = 0; x < m_Width; x++) + m_pTiles[y*m_Width+x].m_Flags |= m_pEditor->m_Map.m_lImages[m_Image]->m_aTileFlags[m_pTiles[y*m_Width+x].m_Index]; } } -void LAYER_TILES::make_palette() +void CLayerTiles::MakePalette() { - for(int y = 0; y < height; y++) - for(int x = 0; x < width; x++) - tiles[y*width+x].index = y*16+x; + for(int y = 0; y < m_Height; y++) + for(int x = 0; x < m_Width; x++) + m_pTiles[y*m_Width+x].m_Index = y*16+x; } -void LAYER_TILES::render() +void CLayerTiles::Render() { - if(image >= 0 && image < editor->map.images.len()) - tex_id = editor->map.images[image]->tex_id; - Graphics()->TextureSet(tex_id); - editor->RenderTools()->render_tilemap(tiles, width, height, 32.0f, vec4(1,1,1,1), LAYERRENDERFLAG_OPAQUE|LAYERRENDERFLAG_TRANSPARENT); + if(m_Image >= 0 && m_Image < m_pEditor->m_Map.m_lImages.size()) + m_TexId = m_pEditor->m_Map.m_lImages[m_Image]->m_TexId; + Graphics()->TextureSet(m_TexId); + m_pEditor->RenderTools()->RenderTilemap(m_pTiles, m_Width, m_Height, 32.0f, vec4(1,1,1,1), LAYERRENDERFLAG_OPAQUE|LAYERRENDERFLAG_TRANSPARENT); } -int LAYER_TILES::convert_x(float x) const { return (int)(x/32.0f); } -int LAYER_TILES::convert_y(float y) const { return (int)(y/32.0f); } +int CLayerTiles::ConvertX(float x) const { return (int)(x/32.0f); } +int CLayerTiles::ConvertY(float y) const { return (int)(y/32.0f); } -void LAYER_TILES::convert(CUIRect rect, RECTi *out) +void CLayerTiles::Convert(CUIRect Rect, RECTi *pOut) { - out->x = convert_x(rect.x); - out->y = convert_y(rect.y); - out->w = convert_x(rect.x+rect.w+31) - out->x; - out->h = convert_y(rect.y+rect.h+31) - out->y; + pOut->x = ConvertX(Rect.x); + pOut->y = ConvertY(Rect.y); + pOut->w = ConvertX(Rect.x+Rect.w+31) - pOut->x; + pOut->h = ConvertY(Rect.y+Rect.h+31) - pOut->y; } -void LAYER_TILES::snap(CUIRect *rect) +void CLayerTiles::Snap(CUIRect *pRect) { - RECTi out; - convert(*rect, &out); - rect->x = out.x*32.0f; - rect->y = out.y*32.0f; - rect->w = out.w*32.0f; - rect->h = out.h*32.0f; + RECTi Out; + Convert(*pRect, &Out); + pRect->x = Out.x*32.0f; + pRect->y = Out.y*32.0f; + pRect->w = Out.w*32.0f; + pRect->h = Out.h*32.0f; } -void LAYER_TILES::clamp(RECTi *rect) +void CLayerTiles::Clamp(RECTi *pRect) { - if(rect->x < 0) + if(pRect->x < 0) { - rect->w += rect->x; - rect->x = 0; + pRect->w += pRect->x; + pRect->x = 0; } - if(rect->y < 0) + if(pRect->y < 0) { - rect->h += rect->y; - rect->y = 0; + pRect->h += pRect->y; + pRect->y = 0; } - if(rect->x+rect->w > width) - rect->w = width-rect->x; + if(pRect->x+pRect->w > m_Width) + pRect->w = m_Width - pRect->x; - if(rect->y+rect->h > height) - rect->h = height-rect->y; + if(pRect->y+pRect->h > m_Height) + pRect->h = m_Height - pRect->y; - if(rect->h < 0) - rect->h = 0; - if(rect->w < 0) - rect->w = 0; + if(pRect->h < 0) + pRect->h = 0; + if(pRect->w < 0) + pRect->w = 0; } -void LAYER_TILES::brush_selecting(CUIRect rect) +void CLayerTiles::BrushSelecting(CUIRect Rect) { Graphics()->TextureSet(-1); - editor->Graphics()->QuadsBegin(); - editor->Graphics()->SetColor(1,1,1,0.4f); - snap(&rect); - editor->Graphics()->QuadsDrawTL(rect.x, rect.y, rect.w, rect.h); - editor->Graphics()->QuadsEnd(); - char buf[16]; - str_format(buf, sizeof(buf), "%d,%d", convert_x(rect.w), convert_y(rect.h)); - gfx_text(0, rect.x+3.0f, rect.y+3.0f, 15.0f*editor->world_zoom, buf, -1); + m_pEditor->Graphics()->QuadsBegin(); + m_pEditor->Graphics()->SetColor(1.0f, 1.0f, 1.0f, 0.4f); + Snap(&Rect); + IGraphics::CQuadItem QuadItem(Rect.x, Rect.y, Rect.w, Rect.h); + m_pEditor->Graphics()->QuadsDrawTL(&QuadItem, 1); + m_pEditor->Graphics()->QuadsEnd(); + char aBuf[16]; + str_format(aBuf, sizeof(aBuf), "%d,%d", ConvertX(Rect.w), ConvertY(Rect.h)); + TextRender()->Text(0, Rect.x+3.0f, Rect.y+3.0f, 15.0f*m_pEditor->m_WorldZoom, aBuf, -1); } -int LAYER_TILES::brush_grab(LAYERGROUP *brush, CUIRect rect) +int CLayerTiles::BrushGrab(CLayerGroup *pBrush, CUIRect Rect) { RECTi r; - convert(rect, &r); - clamp(&r); + Convert(Rect, &r); + Clamp(&r); if(!r.w || !r.h) return 0; // create new layers - LAYER_TILES *grabbed = new LAYER_TILES(r.w, r.h); - grabbed->tex_id = tex_id; - grabbed->image = image; - brush->add_layer(grabbed); + CLayerTiles *pGrabbed = new CLayerTiles(r.w, r.h); + pGrabbed->m_pEditor = m_pEditor; + pGrabbed->m_TexId = m_TexId; + pGrabbed->m_Image = m_Image; + pBrush->AddLayer(pGrabbed); // copy the tiles for(int y = 0; y < r.h; y++) for(int x = 0; x < r.w; x++) - grabbed->tiles[y*grabbed->width+x] = tiles[(r.y+y)*width+(r.x+x)]; + pGrabbed->m_pTiles[y*pGrabbed->m_Width+x] = m_pTiles[(r.y+y)*m_Width+(r.x+x)]; return 1; } -void LAYER_TILES::brush_draw(LAYER *brush, float wx, float wy) +void CLayerTiles::FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect) { - if(readonly) + if(m_Readonly) + return; + + int sx = ConvertX(Rect.x); + int sy = ConvertY(Rect.y); + int w = ConvertX(Rect.w); + int h = ConvertY(Rect.h); + + CLayerTiles *pLt = static_cast<CLayerTiles*>(pBrush); + + for(int y = 0; y <= h; y++) + { + for(int x = 0; x <= w; x++) + { + int fx = x+sx; + int fy = y+sy; + + if(fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height) + continue; + + if(Empty) + m_pTiles[fy*m_Width+fx].m_Index = 1; + else + m_pTiles[fy*m_Width+fx] = pLt->m_pTiles[(y*pLt->m_Width + x%pLt->m_Width) % (pLt->m_Width*pLt->m_Height)]; + } + } +} + +void CLayerTiles::BrushDraw(CLayer *pBrush, float wx, float wy) +{ + if(m_Readonly) return; // - LAYER_TILES *l = (LAYER_TILES *)brush; - int sx = convert_x(wx); - int sy = convert_y(wy); + CLayerTiles *l = (CLayerTiles *)pBrush; + int sx = ConvertX(wx); + int sy = ConvertY(wy); - for(int y = 0; y < l->height; y++) - for(int x = 0; x < l->width; x++) + for(int y = 0; y < l->m_Height; y++) + for(int x = 0; x < l->m_Width; x++) { int fx = x+sx; int fy = y+sy; - if(fx<0 || fx >= width || fy < 0 || fy >= height) + if(fx<0 || fx >= m_Width || fy < 0 || fy >= m_Height) continue; - tiles[fy*width+fx] = l->tiles[y*l->width+x]; + m_pTiles[fy*m_Width+fx] = l->m_pTiles[y*l->m_Width+x]; } } -void LAYER_TILES::brush_flip_x() +void CLayerTiles::BrushFlipX() { - for(int y = 0; y < height; y++) - for(int x = 0; x < width/2; x++) + for(int y = 0; y < m_Height; y++) + for(int x = 0; x < m_Width/2; x++) { - TILE tmp = tiles[y*width+x]; - tiles[y*width+x] = tiles[y*width+width-1-x]; - tiles[y*width+width-1-x] = tmp; + CTile Tmp = m_pTiles[y*m_Width+x]; + m_pTiles[y*m_Width+x] = m_pTiles[y*m_Width+m_Width-1-x]; + m_pTiles[y*m_Width+m_Width-1-x] = Tmp; } - for(int y = 0; y < height; y++) - for(int x = 0; x < width; x++) - tiles[y*width+x].flags ^= TILEFLAG_VFLIP; + for(int y = 0; y < m_Height; y++) + for(int x = 0; x < m_Width; x++) + m_pTiles[y*m_Width+x].m_Flags ^= TILEFLAG_VFLIP; } -void LAYER_TILES::brush_flip_y() +void CLayerTiles::BrushFlipY() { - for(int y = 0; y < height/2; y++) - for(int x = 0; x < width; x++) + for(int y = 0; y < m_Height/2; y++) + for(int x = 0; x < m_Width; x++) { - TILE tmp = tiles[y*width+x]; - tiles[y*width+x] = tiles[(height-1-y)*width+x]; - tiles[(height-1-y)*width+x] = tmp; + CTile Tmp = m_pTiles[y*m_Width+x]; + m_pTiles[y*m_Width+x] = m_pTiles[(m_Height-1-y)*m_Width+x]; + m_pTiles[(m_Height-1-y)*m_Width+x] = Tmp; } - for(int y = 0; y < height; y++) - for(int x = 0; x < width; x++) - tiles[y*width+x].flags ^= TILEFLAG_HFLIP; + for(int y = 0; y < m_Height; y++) + for(int x = 0; x < m_Width; x++) + m_pTiles[y*m_Width+x].m_Flags ^= TILEFLAG_HFLIP; } -void LAYER_TILES::resize(int new_w, int new_h) +void CLayerTiles::Resize(int NewW, int NewH) { - TILE *new_data = new TILE[new_w*new_h]; - mem_zero(new_data, new_w*new_h*sizeof(TILE)); + CTile *pNewData = new CTile[NewW*NewH]; + mem_zero(pNewData, NewW*NewH*sizeof(CTile)); // copy old data - for(int y = 0; y < min(new_h, height); y++) - mem_copy(&new_data[y*new_w], &tiles[y*width], min(width, new_w)*sizeof(TILE)); + for(int y = 0; y < min(NewH, m_Height); y++) + mem_copy(&pNewData[y*NewW], &m_pTiles[y*m_Width], min(m_Width, NewW)*sizeof(CTile)); // replace old - delete [] tiles; - tiles = new_data; - width = new_w; - height = new_h; + delete [] m_pTiles; + m_pTiles = pNewData; + m_Width = NewW; + m_Height = NewH; } -int LAYER_TILES::render_properties(CUIRect *toolbox) +int CLayerTiles::RenderProperties(CUIRect *pToolBox) { - CUIRect button; - toolbox->HSplitBottom(12.0f, toolbox, &button); - bool in_gamegroup = editor->map.game_group->layers.find(this) != -1; - if(editor->map.game_layer == this) - in_gamegroup = false; - static int colcl_button = 0; - if(editor->DoButton_Editor(&colcl_button, "Clear Collision", in_gamegroup?0:-1, &button, 0, "Removes collision from this layer")) + CUIRect Button; + pToolBox->HSplitBottom(12.0f, pToolBox, &Button); + + bool InGameGroup = !find_linear(m_pEditor->m_Map.m_pGameGroup->m_lLayers.all(), this).empty(); + if(m_pEditor->m_Map.m_pGameLayer == this) + InGameGroup = false; + static int s_ColclButton = 0; + if(m_pEditor->DoButton_Editor(&s_ColclButton, "Clear Collision", InGameGroup?0:-1, &Button, 0, "Removes collision from this layer")) { - LAYER_TILES *gl = editor->map.game_layer; - int w = min(gl->width, width); - int h = min(gl->height, height); + CLayerTiles *gl = m_pEditor->m_Map.m_pGameLayer; + int w = min(gl->m_Width, m_Width); + int h = min(gl->m_Height, m_Height); for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) { - if(gl->tiles[y*gl->width+x].index <= TILE_SOLID) - if(tiles[y*width+x].index) - gl->tiles[y*gl->width+x].index = TILE_AIR; + if(gl->m_pTiles[y*gl->m_Width+x].m_Index <= TILE_SOLID) + if(m_pTiles[y*m_Width+x].m_Index) + gl->m_pTiles[y*gl->m_Width+x].m_Index = TILE_AIR; } return 1; } - static int col_button = 0; - toolbox->HSplitBottom(5.0f, toolbox, &button); - toolbox->HSplitBottom(12.0f, toolbox, &button); - if(editor->DoButton_Editor(&col_button, "Make Collision", in_gamegroup?0:-1, &button, 0, "Constructs collision from this layer")) + static int s_ColButton = 0; + pToolBox->HSplitBottom(5.0f, pToolBox, &Button); + pToolBox->HSplitBottom(12.0f, pToolBox, &Button); + if(m_pEditor->DoButton_Editor(&s_ColButton, "Make Collision", InGameGroup?0:-1, &Button, 0, "Constructs collision from this layer")) { - LAYER_TILES *gl = editor->map.game_layer; - int w = min(gl->width, width); - int h = min(gl->height, height); + CLayerTiles *gl = m_pEditor->m_Map.m_pGameLayer; + int w = min(gl->m_Width, m_Width); + int h = min(gl->m_Height, m_Height); for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) { - if(gl->tiles[y*gl->width+x].index <= TILE_SOLID) - gl->tiles[y*gl->width+x].index = tiles[y*width+x].index?TILE_SOLID:TILE_AIR; + if(gl->m_pTiles[y*gl->m_Width+x].m_Index <= TILE_SOLID) + gl->m_pTiles[y*gl->m_Width+x].m_Index = m_pTiles[y*m_Width+x].m_Index?TILE_SOLID:TILE_AIR; } return 1; @@ -255,44 +289,44 @@ int LAYER_TILES::render_properties(CUIRect *toolbox) NUM_PROPS, }; - PROPERTY props[] = { - {"Width", width, PROPTYPE_INT_STEP, 1, 1000000000}, - {"Height", height, PROPTYPE_INT_STEP, 1, 1000000000}, - {"Image", image, PROPTYPE_IMAGE, 0, 0}, + CProperty aProps[] = { + {"Width", m_Width, PROPTYPE_INT_STEP, 1, 1000000000}, + {"Height", m_Height, PROPTYPE_INT_STEP, 1, 1000000000}, + {"Image", m_Image, PROPTYPE_IMAGE, 0, 0}, {0}, }; - if(editor->map.game_layer == this) // remove the image from the selection if this is the game layer - props[2].name = 0; + if(m_pEditor->m_Map.m_pGameLayer == this) // remove the image from the selection if this is the game layer + aProps[2].m_pName = 0; - static int ids[NUM_PROPS] = {0}; - int new_val = 0; - int prop = editor->do_properties(toolbox, props, ids, &new_val); + static int s_aIds[NUM_PROPS] = {0}; + int NewVal = 0; + int Prop = m_pEditor->DoProperties(pToolBox, aProps, s_aIds, &NewVal); - if(prop == PROP_WIDTH && new_val > 1) - resize(new_val, height); - else if(prop == PROP_HEIGHT && new_val > 1) - resize(width, new_val); - else if(prop == PROP_IMAGE) + if(Prop == PROP_WIDTH && NewVal > 1) + Resize(NewVal, m_Height); + else if(Prop == PROP_HEIGHT && NewVal > 1) + Resize(m_Width, NewVal); + else if(Prop == PROP_IMAGE) { - if (new_val == -1) + if (NewVal == -1) { - tex_id = -1; - image = -1; + m_TexId = -1; + m_Image = -1; } else - image = new_val%editor->map.images.len(); + m_Image = NewVal%m_pEditor->m_Map.m_lImages.size(); } return 0; } -void LAYER_TILES::modify_image_index(INDEX_MODIFY_FUNC func) +void CLayerTiles::ModifyImageIndex(INDEX_MODIFY_FUNC Func) { - func(&image); + Func(&m_Image); } -void LAYER_TILES::modify_envelope_index(INDEX_MODIFY_FUNC func) +void CLayerTiles::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func) { } |