The basic server map is being displayed in the client

This commit is contained in:
Kayne Ruse
2014-04-06 03:38:58 +10:00
parent 99aecbfdbb
commit 2bacdcdab7
7 changed files with 89 additions and 16 deletions
+33 -7
View File
@@ -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<void (Region* const)> 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));
}
}
}
+8 -1
View File
@@ -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 <functional>
#include <map>
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<void (Region* const)> func);
//globals
ConfigUtility& config;
UDPNetworkUtility& network;
@@ -91,6 +96,7 @@ protected:
//graphics
Image buttonImage;
RasterFont font;
TileSheet tileSheet;
//map
RegionPager<BlankGenerator, DummyFormat> 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;
+1 -1
View File
@@ -1,5 +1,5 @@
#config
INCLUDES+=. ..
INCLUDES+=. .. ../map
LIBS+=
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
+23 -3
View File
@@ -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);
}
}
}
}
+4 -1
View File
@@ -22,6 +22,8 @@
#ifndef TILESHEET_HPP_
#define TILESHEET_HPP_
#include "region.hpp"
#include "image.hpp"
#include <string>
@@ -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 &image; }
+10 -3
View File
@@ -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()) {
+10
View File
@@ -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)