diff --git a/client/makefile b/client/makefile index ad32d17..3fcc40e 100644 --- a/client/makefile +++ b/client/makefile @@ -1,5 +1,5 @@ #config -COMMONDIR+=../common ../common/map ../common/network ../common/ui +COMMONDIR+=../common ../common/graphics ../common/map ../common/network ../common/ui COMMON+=../libcommon.a LIB+=$(COMMON) -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR)) diff --git a/common/image.cpp b/common/graphics/image.cpp similarity index 100% rename from common/image.cpp rename to common/graphics/image.cpp diff --git a/common/image.hpp b/common/graphics/image.hpp similarity index 100% rename from common/image.hpp rename to common/graphics/image.hpp diff --git a/common/graphics/makefile b/common/graphics/makefile new file mode 100644 index 0000000..85c3910 --- /dev/null +++ b/common/graphics/makefile @@ -0,0 +1,44 @@ +#config +COMMONDIR+=.. +COMMON+=../../libcommon.a +LIB+= +CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR)) +CFLAGS+=-DDEBUG $(addprefix -I,$(COMMONDIR)) + +#source +CXXSRC=$(wildcard *.cpp) +CSRC=$(wildcard *.c) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) +OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o)) + +#output +OUTDIR=../.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +$(OBJDIR)/%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/common/graphics/sprite_sheet.cpp b/common/graphics/sprite_sheet.cpp new file mode 100644 index 0000000..39a669c --- /dev/null +++ b/common/graphics/sprite_sheet.cpp @@ -0,0 +1,102 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "sprite_sheet.hpp" + +#include +#include + +void SpriteSheet::Update(double delta) { + if (delay && (tick += delta) >= delay) { + if (++xIndex >= xCount) { + xIndex = 0; + } + tick = 0; + } + image.SetClipX(xIndex * image.GetClipW()); + image.SetClipY(yIndex * image.GetClipH()); +} + +SDL_Surface* SpriteSheet::LoadSurface(std::string fname, Uint16 xCellCount, Uint16 yCellCount) { + image.LoadSurface(fname); + + xCount = xCellCount; + yCount = yCellCount; + + image.SetClipW(image.GetSurface()->w / xCount); + image.SetClipH(image.GetSurface()->h / yCount); + + xIndex = yIndex = 0; + delay = tick = 0.0; +} + +SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount) { + image.SetSurface(surface); + + xCount = xCellCount; + yCount = yCellCount; + + image.SetClipW(image.GetSurface()->w / xCount); + image.SetClipH(image.GetSurface()->h / yCount); + + xIndex = yIndex = 0; + delay = tick = 0.0; +} + +void SpriteSheet::FreeSurface() { + image.FreeSurface(); + xCount = yCount = 0; + xIndex = yIndex = 0; + delay = tick = 0.0; +} + +Uint16 SpriteSheet::SetXCount(Uint16 i) { + xIndex = 0; + return xCount = i; +} + +Uint16 SpriteSheet::SetYCount(Uint16 i) { + yIndex = 0; + return yCount = i; +} + +Uint16 SpriteSheet::SetXIndex(Uint16 i) { + if (i > xCount) { + std::ostringstream os; + os << "Cannot set x index to " << i; + throw(std::invalid_argument(os.str())); + } + return xIndex = i; +} + +Uint16 SpriteSheet::SetYIndex(Uint16 i) { + if (i > yCount) { + std::ostringstream os; + os << "Cannot set y index to " << i; + throw(std::invalid_argument(os.str())); + } + return yIndex = i; +} + +double SpriteSheet::SetDelay(double d) { + tick = 0; + return delay = d; +} diff --git a/common/graphics/sprite_sheet.hpp b/common/graphics/sprite_sheet.hpp new file mode 100644 index 0000000..a82148b --- /dev/null +++ b/common/graphics/sprite_sheet.hpp @@ -0,0 +1,66 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef SPRITESHEET_HPP_ +#define SPRITESHEET_HPP_ + +#include "image.hpp" + +class SpriteSheet { +public: + SpriteSheet() = default; + SpriteSheet(std::string fname, Uint16 xCellCount, Uint16 yCellCount) { LoadSurface(fname, xCellCount, yCellCount); } + SpriteSheet(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount) { SetSurface(surface, xCellCount, yCellCount); } + ~SpriteSheet() { FreeSurface(); }; + + void Update(double delta); + + SDL_Surface* LoadSurface(std::string fname, Uint16 xCellCount, Uint16 yCellCount); + SDL_Surface* SetSurface(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount); + SDL_Surface* GetSurface() { return image.GetSurface(); } + void FreeSurface(); + + void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) { image.DrawTo(dest, x, y); } + + //accessors and mutators + Image* GetImage() { return ℑ } //OO breaker + + Uint16 SetXCount(Uint16); + Uint16 SetYCount(Uint16); + Uint16 SetXIndex(Uint16); + Uint16 SetYIndex(Uint16); + + Uint16 GetXCount() const { return xCount; } + Uint16 GetYCount() const { return yCount; } + Uint16 GetXIndex() const { return xIndex; } + Uint16 GetYIndex() const { return yIndex; } + + double SetDelay(double d); + double GetDelay() const { return delay; } + +private: + Image image; + Uint16 xCount = 0, yCount = 0; //number of cells + Uint16 xIndex = 0, yIndex = 0; //current cell being drawn + double delay = 0.0, tick = 0.0; +}; + +#endif diff --git a/common/makefile b/common/makefile index da39ca9..3cc5ece 100644 --- a/common/makefile +++ b/common/makefile @@ -19,6 +19,7 @@ OUT=$(addprefix $(OUTDIR)/,libcommon.a) #targets all: $(OBJ) $(OUT) ar -crs $(OUT) $(OBJ) + $(MAKE) -C graphics $(MAKE) -C map $(MAKE) -C network $(MAKE) -C ui diff --git a/common/map/makefile b/common/map/makefile index 85c3910..1dd5910 100644 --- a/common/map/makefile +++ b/common/map/makefile @@ -1,5 +1,5 @@ #config -COMMONDIR+=.. +COMMONDIR+=.. ../graphics COMMON+=../../libcommon.a LIB+= CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR)) diff --git a/common/ui/makefile b/common/ui/makefile index 85c3910..1dd5910 100644 --- a/common/ui/makefile +++ b/common/ui/makefile @@ -1,5 +1,5 @@ #config -COMMONDIR+=.. +COMMONDIR+=.. ../graphics COMMON+=../../libcommon.a LIB+= CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR)) diff --git a/editor/makefile b/editor/makefile index b787db3..e10bbe5 100644 --- a/editor/makefile +++ b/editor/makefile @@ -1,5 +1,5 @@ #config -COMMONDIR+=../common ../common/map ../common/network ../common/ui +COMMONDIR+=../common ../common/graphics ../common/map ../common/network ../common/ui COMMON+=../libcommon.a LIB+=$(COMMON) -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR)) diff --git a/server/makefile b/server/makefile index 387cf39..809547d 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #config -COMMONDIR+=../common ../common/map ../common/network ../common/ui +COMMONDIR+=../common ../common/map ../common/network COMMON+=../libcommon.a LIB+=$(COMMON) -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR))