From 2bacdcdab78016f67cdde4e6fb36445ac1d464b8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 6 Apr 2014 03:38:58 +1000 Subject: [PATCH] The basic server map is being displayed in the client --- client/scenes/in_world.cpp | 40 ++++++++++++++++++++++++++++------ client/scenes/in_world.hpp | 9 +++++++- common/graphics/makefile | 2 +- common/graphics/tile_sheet.cpp | 26 +++++++++++++++++++--- common/graphics/tile_sheet.hpp | 5 ++++- editor/editor_scene.cpp | 13 ++++++++--- rsc/scripts/setup_server.lua | 10 +++++++++ 7 files changed, 89 insertions(+), 16 deletions(-) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index aee7d6b..92eacc1 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -60,6 +60,9 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet disconnectButton.SetText("Disconnect"); shutDownButton.SetText("Shut Down"); + //load the tilesheet + tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15); + //setup the map object mapPager.SetRegionWidth(REGION_WIDTH); mapPager.SetRegionHeight(REGION_HEIGHT); @@ -132,7 +135,9 @@ void InWorld::FrameEnd() { void InWorld::Render(SDL_Surface* const screen) { //draw the map - //TODO + ForNearbyRegions([&](Region* const region) { + tileSheet.DrawRegionTo(screen, region, camera.x, camera.y); + }); //draw characters for (auto& it : playerCharacters) { @@ -291,6 +296,9 @@ void InWorld::HandlePlayerNew(NetworkPacket packet) { if (packet.playerInfo.clientIndex == clientIndex && !localCharacter) { playerIndex = packet.playerInfo.playerIndex; localCharacter = &playerCharacters[playerIndex]; + //setup the camera + camera.width = GetScreen()->w; + camera.height = GetScreen()->h; //center on the player's character camera.marginX = (GetScreen()->w / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2); camera.marginY = (GetScreen()->h / 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2); @@ -355,8 +363,6 @@ void InWorld::SendState() { packet.meta.type = NetworkPacket::Type::PLAYER_UPDATE; packet.playerInfo.clientIndex = clientIndex; packet.playerInfo.playerIndex = playerIndex; -// snprintf(packet.playerInfo.handle, PACKET_STRING_SIZE, "%s", config["player.handle"].c_str()); -// snprintf(packet.playerInfo.avatar, PACKET_STRING_SIZE, "%s", config["player.avatar"].c_str()); packet.playerInfo.position = localCharacter->GetPosition(); packet.playerInfo.motion = localCharacter->GetMotion(); @@ -386,10 +392,6 @@ void InWorld::RequestShutDown() { network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); } -void InWorld::UpdateMap() { - //TODO -} - void InWorld::RequestRegion(int x, int y) { NetworkPacket packet; char buffer[PACKET_BUFFER_SIZE]; @@ -400,4 +402,28 @@ void InWorld::RequestRegion(int x, int y) { packet.regionInfo.y = y; serialize(&packet, buffer); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); +} + +//------------------------- +//Utilities +//------------------------- + +void InWorld::UpdateMap() { + //TODO +} + +void InWorld::ForNearbyRegions(std::function func) { + //TODO: switch this to "is nearby" + //this is ugly + for (int i = snapToBase(mapPager.GetRegionWidth() * tileSheet.GetTileW(), camera.x); + i < camera.x + camera.width; + i += mapPager.GetRegionWidth() * tileSheet.GetTileW() + ) { + for (int j = snapToBase(mapPager.GetRegionHeight() * tileSheet.GetTileH(), camera.y); + j < camera.y + camera.height; + j += mapPager.GetRegionHeight() * tileSheet.GetTileH() + ) { + func(mapPager.GetRegion(i, j)); + } + } } \ No newline at end of file diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index c1cd032..b7aa50d 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -36,6 +36,7 @@ #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" +#include "tile_sheet.hpp" //common #include "config_utility.hpp" @@ -45,6 +46,7 @@ #include "player_character.hpp" //STL +#include #include class InWorld : public BaseScene { @@ -80,9 +82,12 @@ protected: void SendState(); void RequestDisconnect(); void RequestShutDown(); - void UpdateMap(); void RequestRegion(int x, int y); + //utilities + void UpdateMap(); + void ForNearbyRegions(std::function func); + //globals ConfigUtility& config; UDPNetworkUtility& network; @@ -91,6 +96,7 @@ protected: //graphics Image buttonImage; RasterFont font; + TileSheet tileSheet; //map RegionPager mapPager; @@ -100,6 +106,7 @@ protected: Button shutDownButton; struct { int x = 0, y = 0; + int width = 0, height = 0; int marginX = 0, marginY = 0; } camera; diff --git a/common/graphics/makefile b/common/graphics/makefile index fb50dee..c1b2927 100644 --- a/common/graphics/makefile +++ b/common/graphics/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. .. +INCLUDES+=. .. ../map LIBS+= CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES)) diff --git a/common/graphics/tile_sheet.cpp b/common/graphics/tile_sheet.cpp index ef3153d..cc96ec5 100644 --- a/common/graphics/tile_sheet.cpp +++ b/common/graphics/tile_sheet.cpp @@ -34,8 +34,28 @@ void TileSheet::Unload() { XCount = YCount = 0; } -void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, int tile) { - image.SetClipX(tile % XCount * image.GetClipW()); - image.SetClipY(tile / XCount * image.GetClipH()); +void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t tile) { + //0 is invisible + if (tile == 0) return; + image.SetClipX((tile-1) % XCount * image.GetClipW()); + image.SetClipY((tile-1) / XCount * image.GetClipH()); image.DrawTo(dest, x, y); } + +void TileSheet::DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY) { + Region::type_t tile = 0; + for (register int i = 0; i < region->GetWidth(); ++i) { + for (register int j = 0; j < region->GetHeight(); ++j) { + for (register int k = 0; k < region->GetDepth(); ++k) { + tile = region->GetTile(i, j, k); + //0 is invisible + if (tile == 0) continue; + image.SetClipX((tile-1) % XCount * image.GetClipW()); + image.SetClipY((tile-1) / XCount * image.GetClipH()); + image.DrawTo(dest, + region->GetX() + i * image.GetClipW() - camX, + region->GetY() + j * image.GetClipH() - camY); + } + } + } +} \ No newline at end of file diff --git a/common/graphics/tile_sheet.hpp b/common/graphics/tile_sheet.hpp index ce60989..368fcec 100644 --- a/common/graphics/tile_sheet.hpp +++ b/common/graphics/tile_sheet.hpp @@ -22,6 +22,8 @@ #ifndef TILESHEET_HPP_ #define TILESHEET_HPP_ +#include "region.hpp" + #include "image.hpp" #include @@ -35,7 +37,8 @@ public: void Load(std::string fname, int XCount, int YCount); void Unload(); - void DrawTo(SDL_Surface* const dest, int x, int y, int tile); + void DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t tile); + void DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY); //accessors Image* GetImage() { return ℑ } diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 19d414b..af3a42c 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -61,7 +61,13 @@ EditorScene::EditorScene(ConfigUtility* const arg1): pager.SetRegionDepth(REGION_DEPTH); //debug - tsheet.Load(config["dir.tilesets"] + "sand.bmp", 12, 3); + tsheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15); + for (int i = 0; i < REGION_WIDTH; i++) { + for (int j = 0; j < REGION_HEIGHT; j++) { + pager.SetTile(i, j, 0, 14); + } + } + pager.SetTile(5, 10, 1, 48); } EditorScene::~EditorScene() { @@ -85,7 +91,8 @@ void EditorScene::FrameEnd() { } void EditorScene::Render(SDL_Surface* const screen) { - //debug + tsheet.DrawRegionTo(screen, pager.GetRegion(0, 0), camera.x, camera.y); +/* //debug for (int i = 0; i < pager.GetRegionWidth()*2; i++) { for (int j = 0; j < pager.GetRegionHeight()*2; j++) { for (int k = 0; k < pager.GetRegionDepth(); k++) { @@ -99,7 +106,7 @@ void EditorScene::Render(SDL_Surface* const screen) { } } } - +*/ //draw a big bar across the top (hackish) buttonImage.SetClipY(0); for (int i = 0; i < screen->w; i += buttonImage.GetClipW()) { diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 4daf1f4..c201054 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -2,6 +2,16 @@ print("Lua script check OK (./rsc)") function Region.Create(r) print("Region:Create(r", Region.GetX(r), Region.GetY(r), ")") + for i = 1, Region.GetWidth(r) do + for j = 1, Region.GetHeight(r) do + if math.abs(i) == math.abs(j) then + Region.SetTile(r, i, j, 1, 50) + else + Region.SetTile(r, i, j, 1, 14) + end + end + end + print("done") end function Region.Unload(r)