From 613d9cec0848f5790c82522b2313e7555313b119 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 15 May 2013 19:49:27 +1000 Subject: [PATCH] Imported updates from Codebase --- client/button.cpp | 96 ++++++++++++++++++++++++++++++++++++++++ client/button.hpp | 78 ++++++++++++++++++++++++++++++++ client/image.cpp | 14 +++++- client/image.hpp | 31 ++++++------- client/main.cpp | 23 +++++++++- client/makefile | 22 ++------- client/network_udp.cpp | 23 ++++++++++ client/raster_font.cpp | 53 ++++++++++++++++++++++ client/raster_font.hpp | 43 ++++++++++++++++++ client/scene_manager.cpp | 2 +- rsc/config.cfg | 8 ++-- server/makefile | 17 ++++--- server/network_udp.cpp | 23 ++++++++++ 13 files changed, 383 insertions(+), 50 deletions(-) create mode 100644 client/button.cpp create mode 100644 client/button.hpp create mode 100644 client/network_udp.cpp create mode 100644 client/raster_font.cpp create mode 100644 client/raster_font.hpp create mode 100644 server/network_udp.cpp diff --git a/client/button.cpp b/client/button.cpp new file mode 100644 index 0000000..3b035b7 --- /dev/null +++ b/client/button.cpp @@ -0,0 +1,96 @@ +/* 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 "button.hpp" + +#include + +Button::Button(): + Button(0,0, nullptr, nullptr) +{ +} + +Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string s) { + x = i; + y = j; + state = State::NORMAL; + + //graphical stuff + image.SetSurface(imageSurface); + image.SetClipH(image.GetClipH() / 3); //3 phases + font.SetSurface(fontSurface); + + SetText(s); +} + +Button::State Button::MouseMotion(SDL_MouseMotionEvent& motion) { + if (motion.state & SDL_BUTTON_LMASK) { + return CalcState(motion.x, motion.y, true); + } + else { + return CalcState(motion.x, motion.y, false); + } + return state; +} + +Button::State Button::MouseButtonDown(SDL_MouseButtonEvent& button) { + if (button.button == SDL_BUTTON_LEFT) { + return CalcState(button.x, button.y, true); + } + return state; +} + +Button::State Button::MouseButtonUp(SDL_MouseButtonEvent& button) { + if (button.button == SDL_BUTTON_LEFT) { + return CalcState(button.x, button.y, false); + } + return state; +} + +void Button::DrawTo(SDL_Surface* const dest) { + image.DrawTo(dest, x, y); + font.DrawStringTo(text, dest, textX, textY); +} + +std::string Button::SetText(std::string s) { + //one line + text = s; + textX = (image.GetClipW() / 2 + x) - (font.GetClipW() * text.size() / 2); + textY = (image.GetClipH() / 2 + y) - (font.GetClipH() / 2); + return text; +} + +Button::State Button::CalcState(Sint16 i, Sint16 j, bool leftPressed) { + if (i < x || i > (x + image.GetClipW()) || + j < y || j > (y + image.GetClipH()) + ) { + image.SetClipY(0); + return state = State::NORMAL; + } + if (leftPressed) { + image.SetClipY(image.GetClipH()*2); + return state = State::PRESSED; + } + else { + image.SetClipY(image.GetClipH()); + return state = State::HOVER; + } +} diff --git a/client/button.hpp b/client/button.hpp new file mode 100644 index 0000000..eaa8579 --- /dev/null +++ b/client/button.hpp @@ -0,0 +1,78 @@ +/* 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 BUTTON_HPP_ +#define BUTTON_HPP_ + +#include "image.hpp" +#include "raster_font.hpp" + +#include + +//3-phases, no toggle, centred text +class Button { +public: + enum class State { + NORMAL, HOVER, PRESSED + }; + + Button(); + Button(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = ""); + + //return the current state + State MouseMotion(SDL_MouseMotionEvent&); + State MouseButtonDown(SDL_MouseButtonEvent&); + State MouseButtonUp(SDL_MouseButtonEvent&); + State GetState() const { + return state; + } + + //yet another draw function + void DrawTo(SDL_Surface* const); + + //simple accessors and mutators + Sint16 SetX(Sint16 i) { return x = i; } + Sint16 SetY(Sint16 i) { return y = i; } + Sint16 GetX() const { return x; } + Sint16 GetY() const { return y; } + + std::string SetText(std::string s); + std::string GetText() const { return text; } + + //raw access, be careful + Image* GetImage() { return ℑ } + RasterFont* GetFont() { return &font; } + + //debug + Sint16 GetTextX() const { return textX; } + Sint16 GetTextY() const { return textY; } +private: + State CalcState(Sint16 x, Sint16 y, bool leftPressed); + + Sint16 x, y; + Sint16 textX, textY; //prevent recalc every loop + Image image; + RasterFont font; + State state; + std::string text; +}; + +#endif diff --git a/client/image.cpp b/client/image.cpp index 3ecc848..466d4d2 100644 --- a/client/image.cpp +++ b/client/image.cpp @@ -21,6 +21,10 @@ */ #include "image.hpp" + Image::Image() { + SetSurface(nullptr); + } + Image::Image(SDL_Surface* p) { SetSurface(p); } @@ -31,7 +35,12 @@ Image::Image(SDL_Surface* p, SDL_Rect r) { SDL_Surface* Image::SetSurface(SDL_Surface* p) { surface = p; - clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; + if (!surface) { + clip = {0, 0, 0, 0}; + } + else { + clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; + } return surface; } @@ -46,6 +55,9 @@ SDL_Surface* Image::GetSurface() const { } void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) { + if (!surface) { + return; + } SDL_Rect sclip = clip, dclip = {x,y}; SDL_BlitSurface(surface, &sclip, dest, &dclip); diff --git a/client/image.hpp b/client/image.hpp index 687576d..de45d2c 100644 --- a/client/image.hpp +++ b/client/image.hpp @@ -26,30 +26,31 @@ class Image { public: + Image(); Image(SDL_Surface*); Image(SDL_Surface*, SDL_Rect); virtual ~Image() {} - SDL_Surface* SetSurface(SDL_Surface*); - SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); - SDL_Surface* GetSurface() const; + virtual SDL_Surface* SetSurface(SDL_Surface*); + virtual SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); + virtual SDL_Surface* GetSurface() const; - void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); + virtual void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); //Clip handlers - SDL_Rect SetClip(SDL_Rect r) { return clip = r; } - SDL_Rect GetClip() const { return clip; } + virtual SDL_Rect SetClip(SDL_Rect r) { return clip = r; } + virtual SDL_Rect GetClip() const { return clip; } - Sint16 SetClipX(Sint16 x) { return clip.x = x; } - Sint16 SetClipY(Sint16 y) { return clip.y = y; } - Uint16 SetClipW(Uint16 w) { return clip.w = w; } - Uint16 SetClipH(Uint16 h) { return clip.h = h; } + virtual Sint16 SetClipX(Sint16 x) { return clip.x = x; } + virtual Sint16 SetClipY(Sint16 y) { return clip.y = y; } + virtual Uint16 SetClipW(Uint16 w) { return clip.w = w; } + virtual Uint16 SetClipH(Uint16 h) { return clip.h = h; } - Sint16 GetClipX() const { return clip.x; } - Sint16 GetClipY() const { return clip.y; } - Uint16 GetClipW() const { return clip.w; } - Uint16 GetClipH() const { return clip.h; } -private: + virtual Sint16 GetClipX() const { return clip.x; } + virtual Sint16 GetClipY() const { return clip.y; } + virtual Uint16 GetClipW() const { return clip.w; } + virtual Uint16 GetClipH() const { return clip.h; } +protected: SDL_Surface* surface; SDL_Rect clip; }; diff --git a/client/main.cpp b/client/main.cpp index 119214a..7a76519 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,3 +1,24 @@ +/* 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 "scene_manager.hpp" #include @@ -9,8 +30,8 @@ int main(int, char**) { #ifdef DEBUG cout << "Beginning program" << endl; #endif - SceneManager app; try { + SceneManager app; app.Init(); app.Proc(); app.Quit(); diff --git a/client/makefile b/client/makefile index 406cede..877c63b 100644 --- a/client/makefile +++ b/client/makefile @@ -3,23 +3,7 @@ CXXFLAGS+=-std=c++11 -DDEBUG LIB=-lmingw32 -lSDLmain -lSDL -lwsock32 -lWS2_32 #source -SRC=base_scene.cpp \ - scene_manager.cpp \ - test_systems.cpp \ - splash.cpp \ - main_menu.cpp \ - in_game.cpp \ - lobby.cpp \ - combat.cpp \ - surface_manager.cpp \ - image.cpp \ - sprite_sheet.cpp \ - player.cpp \ - player_manager.cpp \ - config_utility.cpp \ - network.cpp \ - network_tcp.cpp \ - main.cpp +SRC=$(filter-out unit.cpp, $(wildcard *.cpp)) #objects OBJDIR=obj @@ -27,7 +11,7 @@ OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o)) #output OUTDIR=out -OUT=$(addprefix $(OUTDIR)/,a) +OUT=$(addprefix $(OUTDIR)/,client) #targets all: $(OBJ) $(OUT) @@ -44,7 +28,7 @@ $(OUTDIR): mkdir $(OUTDIR) $(OBJDIR)/%.o: %.cpp - $(CXX) $(CXXFLAGS) -c -o $(@) $< + $(CXX) $(CXXFLAGS) -c -o $@ $< clean: $(RM) *.o *.a *.exe diff --git a/client/network_udp.cpp b/client/network_udp.cpp new file mode 100644 index 0000000..39ce01d --- /dev/null +++ b/client/network_udp.cpp @@ -0,0 +1,23 @@ +/* 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 "network.hpp" + diff --git a/client/raster_font.cpp b/client/raster_font.cpp new file mode 100644 index 0000000..893bc93 --- /dev/null +++ b/client/raster_font.cpp @@ -0,0 +1,53 @@ +/* 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 "raster_font.hpp" + +void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) { + if (!surface) { + return; + } + //character size won't change here + const Sint16 w = surface->w/16; + const Sint16 h = surface->h/16; + SDL_Rect sclip, dclip = {x,y,0,0}; + for (auto c : s) { + //TODO: inefficient + sclip = {Sint16(c%w*w), Sint16(c/h*h), clip.w, clip.h}; +// sclip.x = c % w; +// sclip.h = c / h; +// sclip.w = clip.w; +// sclip.h = clip.h; + SDL_BlitSurface(surface, &sclip, dest, &dclip); + dclip.x += w; + } +} + +SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) { + surface = p; + if (!surface) { + clip = {0, 0, 0, 0}; + } + else { + clip = {0, 0, Uint16(surface->w/16), Uint16(surface->h/16)}; + } + return surface; +} diff --git a/client/raster_font.hpp b/client/raster_font.hpp new file mode 100644 index 0000000..b46c9c3 --- /dev/null +++ b/client/raster_font.hpp @@ -0,0 +1,43 @@ +/* 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 RASTERFONT_HPP_ +#define RASTERFONT_HPP_ + +#include "image.hpp" + +#include + +class RasterFont : protected Image { +public: + RasterFont() {} + RasterFont(SDL_Surface* p) {SetSurface(p);} + virtual ~RasterFont() {} + virtual void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y); + virtual SDL_Surface* SetSurface(SDL_Surface*) override; + + using Image::GetSurface; + using Image::GetClip; + using Image::GetClipW; + using Image::GetClipH; +}; + +#endif diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index 01000d7..62d666e 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -39,7 +39,7 @@ void SceneManager::Init() { if (SDL_Init(SDL_INIT_VIDEO)) throw(std::runtime_error("Failed to initialize SDL")); - GetSingletonPtr()->Load("rsc\\config.cfg"); + GetSingletonPtr()->Load("rsc/config.cfg"); //set the screen from the config file int flags = SDL_HWSURFACE|SDL_DOUBLEBUF; diff --git a/rsc/config.cfg b/rsc/config.cfg index 9fe6af0..c4b3754 100644 --- a/rsc/config.cfg +++ b/rsc/config.cfg @@ -6,10 +6,10 @@ screen.h = 600 screen.f = false #directories -fonts = rsc\graphics\fonts -logos = rsc\graphics\logos -sprites = rsc\graphics\sprites -tilesets = rsc\graphics\tilesets +fonts = rsc/graphics/fonts +logos = rsc/graphics/logos +sprites = rsc/graphics/sprites +tilesets = rsc/graphics/tilesets #debugging debug = true diff --git a/server/makefile b/server/makefile index 7534020..20211b4 100644 --- a/server/makefile +++ b/server/makefile @@ -2,20 +2,19 @@ CXXFLAGS+=-std=c++11 -DDEBUG LIB=-lwsock32 -lWS2_32 +#source +SRC=$(filter-out unit.cpp main.cpp, $(wildcard *.cpp)) + #objects OBJDIR=obj -OBJ=$(addprefix $(OBJDIR)/,config_utility.o network.o network_tcp.o) +OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o)) #output OUTDIR=out -OUT=$(addprefix $(OUTDIR)/,server) - -#source -SRC=server.cpp main.cpp #targets all: $(OBJ) $(OUT) - $(CXX) $(CXXFLAGS) -o $(OUT) $(SRC) $(OBJ) $(LIB) + $(CXX) $(CXXFLAGS) -o $(OUTDIR)/server main.cpp $(OBJ) $(LIB) $(OBJ): | $(OBJDIR) @@ -28,12 +27,12 @@ $(OUTDIR): mkdir $(OUTDIR) $(OBJDIR)/%.o: %.cpp - $(CXX) $(CXXFLAGS) -c -o $(@) $< + $(CXX) $(CXXFLAGS) -c -o $@ $< clean: $(RM) *.o *.a *.exe -rebuild: clean all +rebuild: clean all unit -unit: +unit: $(OBJ) $(OUT) $(CXX) $(CXXFLAGS) -o $(OUTDIR)\unit unit.cpp $(OBJ) $(LIB) \ No newline at end of file diff --git a/server/network_udp.cpp b/server/network_udp.cpp new file mode 100644 index 0000000..39ce01d --- /dev/null +++ b/server/network_udp.cpp @@ -0,0 +1,23 @@ +/* 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 "network.hpp" +