Updated common/graphics, client/base_scene.*pp to SDL2

This commit is contained in:
2015-07-07 19:51:27 +10:00
parent 24eb730c72
commit af17bd2800
12 changed files with 501 additions and 315 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ This game is inspired by classic 2D RPGs (Final Fantasy, The Legend of Zelda), a
## External Dependencies ## External Dependencies
* [SDL 1.2](http://www.libsdl.org/) - Simple DirectMedia Layer API * [SDL 2.0](http://www.libsdl.org/) - Simple DirectMedia Layer API
* [SDL_net 2.0](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension * [SDL_net 2.0](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension
* [lua 5.2](http://www.lua.org/) - The lua programming language * [lua 5.2](http://www.lua.org/) - The lua programming language
* [SQLite3](http://www.sqlite.org/) - A lightweight SQL database engine * [SQLite3](http://www.sqlite.org/) - A lightweight SQL database engine
+76 -71
View File
@@ -21,91 +21,59 @@
*/ */
#include "base_scene.hpp" #include "base_scene.hpp"
#include <stdexcept> SDL_Renderer* BaseScene::rendererHandle = nullptr;
//-------------------------
//Static declarations
//-------------------------
SDL_Surface* BaseScene::screen = nullptr;
//-------------------------
//Public access members
//-------------------------
BaseScene::BaseScene() { BaseScene::BaseScene() {
// //EMPTY
} }
BaseScene::~BaseScene() { BaseScene::~BaseScene() {
// //EMPTY
} }
//-------------------------
//Program control
//-------------------------
SDL_Surface* BaseScene::SetScreen(int w, int h, int bpp, Uint32 flags) {
if (!bpp) {
bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
}
screen = SDL_SetVideoMode(w, h, bpp, flags);
if (!screen) {
throw(std::runtime_error("Failed to create the screen surface"));
}
return screen;
}
SDL_Surface* BaseScene::GetScreen() {
return screen;
}
SceneList BaseScene::SetNextScene(SceneList sceneIndex) {
return nextScene = sceneIndex;
}
SceneList BaseScene::GetNextScene() const {
return nextScene;
}
//-------------------------
//Frame loop
//-------------------------
void BaseScene::RunFrame() { void BaseScene::RunFrame() {
FrameStart(); FrameStart();
HandleEvents(); ProcessEvents();
Update(); Update();
FrameEnd(); FrameEnd();
} }
void BaseScene::RenderFrame() { void BaseScene::RenderFrame(SDL_Renderer* renderer) {
SDL_FillRect(screen, 0, 0); //EMPTY
Render(screen); }
SDL_Flip(screen);
SDL_Delay(10); void BaseScene::SetRenderer(SDL_Renderer* r) {
rendererHandle = r;
}
SDL_Renderer* BaseScene::GetRenderer() {
return rendererHandle;
}
void BaseScene::SetSceneSignal(SceneSignal signal) {
sceneSignal = signal;
}
SceneSignal BaseScene::GetSceneSignal() {
return sceneSignal;
} }
//------------------------- //-------------------------
//Event handlers //frame phases
//------------------------- //-------------------------
void BaseScene::HandleEvents() { void BaseScene::FrameStart() {
SDL_Event event; //EMPTY
}
void BaseScene::ProcessEvents() {
SDL_Event event;
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
switch(event.type) { switch(event.type) {
case SDL_QUIT: case SDL_QUIT:
QuitEvent(); QuitEvent();
break; break;
case SDL_VIDEORESIZE:
SetScreen(event.resize.w, event.resize.h, 0, screen->flags);
break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
MouseMotion(event.motion); MouseMotion(event.motion);
break; break;
@@ -126,15 +94,52 @@ void BaseScene::HandleEvents() {
KeyUp(event.key); KeyUp(event.key);
break; break;
#ifdef USE_EVENT_JOYSTICK //TODO: joystick and controller events
//EMPTY }
#endif }
}
#ifdef USE_EVENT_UNKNOWN
default: void BaseScene::Update() {
UnknownEvent(event); //EMPTY
break; }
#endif
}//switch void BaseScene::FrameEnd() {
}//while //EMPTY
}
//-------------------------
//input events
//-------------------------
void BaseScene::QuitEvent() {
sceneSignal = SceneSignal::QUIT;
}
void BaseScene::MouseMotion(SDL_MouseMotionEvent const& event) {
//EMPTY
}
void BaseScene::MouseButtonDown(SDL_MouseButtonEvent const& event) {
//EMPTY
}
void BaseScene::MouseButtonUp(SDL_MouseButtonEvent const& event) {
//EMPTY
}
void BaseScene::MouseWheel(SDL_MouseWheelEvent const& event) {
//EMPTY
}
void BaseScene::KeyDown(SDL_KeyboardEvent const& event) {
//preference as a default
switch(event.keysym.sym) {
case SDLK_ESCAPE:
QuitEvent();
break;
}
}
void BaseScene::KeyUp(SDL_KeyboardEvent const& event) {
//EMPTY
} }
+25 -32
View File
@@ -21,51 +21,44 @@
*/ */
#pragma once #pragma once
#include "scene_list.hpp" #include "scene_signal.hpp"
#include "SDL/SDL.h" #include "SDL2/SDL.h"
class BaseScene { class BaseScene {
public: public:
//Public access members
BaseScene(); BaseScene();
virtual ~BaseScene(); virtual ~BaseScene();
//Program control
static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF);
static SDL_Surface* GetScreen();
SceneList SetNextScene(SceneList sceneIndex);
SceneList GetNextScene() const;
//Frame loop
virtual void RunFrame(); virtual void RunFrame();
virtual void RenderFrame(); virtual void RenderFrame(SDL_Renderer*);
static void SetRenderer(SDL_Renderer*);
SceneSignal GetSceneSignal();
protected: protected:
virtual void FrameStart() {} //control
virtual void HandleEvents(); static SDL_Renderer* GetRenderer();
virtual void Update() {} void SetSceneSignal(SceneSignal);
virtual void FrameEnd() {}
virtual void Render(SDL_Surface* const screen) {}
//Event handlers //frame phases
virtual void QuitEvent() { SetNextScene(SceneList::QUIT); } virtual void FrameStart();
virtual void MouseMotion(SDL_MouseMotionEvent const&) {} virtual void ProcessEvents();
virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {} virtual void Update();
virtual void MouseButtonUp(SDL_MouseButtonEvent const&) {} virtual void FrameEnd();
virtual void KeyDown(SDL_KeyboardEvent const&) {}
virtual void KeyUp(SDL_KeyboardEvent const&) {}
#ifdef USE_EVENT_JOYSTICK //input events
//EMPTY virtual void QuitEvent();
#endif virtual void MouseMotion(SDL_MouseMotionEvent const& event);
virtual void MouseButtonDown(SDL_MouseButtonEvent const& event);
virtual void MouseButtonUp(SDL_MouseButtonEvent const& event);
virtual void MouseWheel(SDL_MouseWheelEvent const& event);
virtual void KeyDown(SDL_KeyboardEvent const& event);
virtual void KeyUp(SDL_KeyboardEvent const& event);
#ifdef USE_EVENT_UNKNOWN //TODO: joystick and controller events
virtual void UnknownEvent(SDL_Event const&) {}
#endif
private: private:
static SDL_Surface* screen; static SDL_Renderer* rendererHandle;
SceneList nextScene = SceneList::CONTINUE; SceneSignal sceneSignal = SceneSignal::CONTINUE;
}; };
@@ -21,17 +21,12 @@
*/ */
#pragma once #pragma once
enum class SceneList { enum SceneSignal {
//these are reserved //reserved members for internal use
QUIT, QUIT = -1,
CONTINUE, CONTINUE = 0,
FIRST, FIRST = 1,
//custom indexes //custom scenes
SPLASHSCREEN, EXAMPLE_SCENE
MAINMENU,
OPTIONSMENU,
LOBBYMENU,
WORLD,
DISCONNECTEDSCREEN,
}; };
+115 -76
View File
@@ -21,8 +21,10 @@
*/ */
#include "image.hpp" #include "image.hpp"
#include <stdexcept> #include "SDL2/SDL_image.h"
#include <sstream> #include <sstream>
#include <stdexcept>
Image& Image::operator=(Image const& rhs) { Image& Image::operator=(Image const& rhs) {
//don't screw yourself //don't screw yourself
@@ -30,10 +32,10 @@ Image& Image::operator=(Image const& rhs) {
return *this; return *this;
} }
FreeSurface(); Free();
//Copy the other Image's stuff //Copy the other Image's stuff
surface = rhs.surface; texture = rhs.texture;
clip = rhs.clip; clip = rhs.clip;
local = false; local = false;
} }
@@ -44,102 +46,139 @@ Image& Image::operator=(Image&& rhs) {
return *this; return *this;
} }
FreeSurface(); Free();
//Steal the other Image's stuff //Steal the other Image's stuff
surface = rhs.surface; texture = rhs.texture;
clip = rhs.clip; clip = rhs.clip;
local = rhs.local; local = rhs.local;
rhs.surface = nullptr; rhs.texture = nullptr;
rhs.clip = {0, 0, 0, 0}; rhs.clip = {0, 0, 0, 0};
rhs.local = false; rhs.local = false;
} }
SDL_Surface* Image::LoadSurface(std::string fname) { SDL_Texture* Image::Load(SDL_Renderer* renderer, std::string fname) {
FreeSurface(); Free();
SDL_Surface* p = SDL_LoadBMP(fname.c_str());
if (!p) { //load the file into a surface
std::ostringstream os; SDL_Surface* surface = IMG_Load(fname.c_str());
os << "Failed to load file: " << fname; if (!surface) {
throw(std::runtime_error(os.str())); std::ostringstream msg;
msg << "Failed to load an image file: " << fname;
msg << "; " << IMG_GetError();
throw(std::runtime_error(msg.str()));
}
//create a texture from this surface
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
std::ostringstream msg;
msg << "Failed to convert a newly loaded image file: " << fname;
msg << "; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
//set the metadata
clip.x = 0;
clip.y = 0;
if (SDL_QueryTexture(texture, nullptr, nullptr, &clip.w, &clip.h)) {
std::ostringstream msg;
msg << "Failed to record metadata for a newly loaded image file: " << fname;
msg << "; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
} }
surface = p;
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
local = true; local = true;
SetTransparentColor(255, 0, 255); //default
return surface;
}
SDL_Surface* Image::CreateSurface(Uint16 w, Uint16 h) { //free the surface & return
FreeSurface();
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface* p = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, rmask, gmask, bmask, amask);
if (!p) {
throw(std::runtime_error("Failed to create Image surface"));
}
surface = p;
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
local = true;
SetTransparentColor(255, 0, 255); //default
return surface;
}
SDL_Surface* Image::SetSurface(SDL_Surface* p) {
FreeSurface();
if (!p) {
throw(std::invalid_argument("No surface pointer provided"));
}
surface = p;
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
local = false;
return surface;
}
void Image::FreeSurface() {
if (local) {
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
return texture;
}
SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h) {
Free();
//make the texture
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STATIC,
w, h);
if (!texture) {
std::ostringstream msg;
msg << "Failed to create a texture; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
//set the metadata
clip.x = 0;
clip.y = 0;
if (SDL_QueryTexture(texture, nullptr, nullptr, &clip.w, &clip.h)) {
std::ostringstream msg;
msg << "Failed to record metadata for a newly created image";
msg << "; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
local = true;
return texture;
}
SDL_Texture* Image::SetTexture(SDL_Texture* ptr) {
Free();
texture = ptr;
//set the metadata
clip.x = 0;
clip.y = 0;
if (SDL_QueryTexture(texture, nullptr, nullptr, &clip.w, &clip.h)) {
std::ostringstream msg;
msg << "Failed to record metadata for a newly image image";
msg << "; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
local = false;
return texture;
}
SDL_Texture* Image::GetTexture() const {
return texture;
}
void Image::Free() {
if (local) {
SDL_DestroyTexture(texture);
local = false; local = false;
} }
surface = nullptr; texture = nullptr;
clip = {0, 0, 0, 0}; clip = {0, 0, 0, 0};
} }
void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) { void Image::DrawTo(SDL_Renderer* const renderer, Sint16 x, Sint16 y, double scaleX, double scaleY) {
if (!surface) { if (!texture) {
throw(std::logic_error("No image surface to draw")); throw(std::logic_error("No image texture to draw"));
} }
SDL_Rect sclip = clip, dclip = {x,y}; SDL_Rect sclip = clip;
SDL_BlitSurface(surface, &sclip, dest, &dclip); SDL_Rect dclip = {x, y, Uint16(clip.w * scaleX), Uint16(clip.h * scaleY)};
SDL_RenderCopy(renderer, texture, &sclip, &dclip);
} }
void Image::SetTransparentColor(Uint8 r, Uint8 g, Uint8 b) { void Image::SetAlpha(Uint8 a) {
if (!surface) { if (SDL_SetTextureAlphaMod(texture, a)) {
throw(std::logic_error("Failed to set the transparent color")); std::ostringstream msg;
msg << "Failed to set alpha; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
} }
if (!local) {
throw(std::logic_error("Cannot set the transparent color of a non-local surface"));
}
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, r, g, b));
} }
void Image::ClearTransparentColor() { Uint8 Image::GetAlpha() {
if (!surface) { Uint8 ret = 0;
throw(std::logic_error("Failed to clear the transparent color")); if (SDL_GetTextureAlphaMod(texture, &ret)) {
std::ostringstream msg;
msg << "Failed to get alpha; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
} }
if (!local) { return ret;
throw(std::logic_error("Cannot clear the transparent color of a non-local surface"));
}
SDL_SetColorKey(surface, 0, 0);
} }
+16 -14
View File
@@ -21,7 +21,8 @@
*/ */
#pragma once #pragma once
#include "SDL/SDL.h" #include "SDL2/SDL.h"
#include <string> #include <string>
class Image { class Image {
@@ -29,21 +30,24 @@ public:
Image() = default; Image() = default;
Image(Image const& rhs) { *this = rhs; } Image(Image const& rhs) { *this = rhs; }
Image(Image&& rhs) { *this = std::move(rhs); } Image(Image&& rhs) { *this = std::move(rhs); }
Image(std::string fname) { LoadSurface(fname); } Image(SDL_Renderer* r, std::string fname) { Load(r, fname); }
Image(Uint16 w, Uint16 h) { CreateSurface(w, h); } Image(SDL_Renderer* r, Uint16 w, Uint16 h) { Create(r, w, h); }
Image(SDL_Surface* p) { SetSurface(p); } Image(SDL_Texture* p) { SetTexture(p); }
~Image() { FreeSurface(); } virtual ~Image() { Free(); }
Image& operator=(Image const&); Image& operator=(Image const&);
Image& operator=(Image&&); Image& operator=(Image&&);
SDL_Surface* LoadSurface(std::string fname); SDL_Texture* Load(SDL_Renderer* renderer, std::string fname);
SDL_Surface* CreateSurface(Uint16 w, Uint16 h); SDL_Texture* Create(SDL_Renderer* renderer, Uint16 w, Uint16 h);
SDL_Surface* SetSurface(SDL_Surface*); SDL_Texture* SetTexture(SDL_Texture*);
SDL_Surface* GetSurface() const { return surface; } SDL_Texture* GetTexture() const;
void FreeSurface(); virtual void Free();
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); void DrawTo(SDL_Renderer* const, Sint16 x, Sint16 y, double scaleX = 1.0, double scaleY = 1.0);
void SetAlpha(Uint8 a);
Uint8 GetAlpha();
//Clip handlers //Clip handlers
SDL_Rect SetClip(SDL_Rect r) { return clip = r; } SDL_Rect SetClip(SDL_Rect r) { return clip = r; }
@@ -61,10 +65,8 @@ public:
bool GetLocal() const { return local; } bool GetLocal() const { return local; }
void SetTransparentColor(Uint8 r, Uint8 g, Uint8 b);
void ClearTransparentColor();
protected: protected:
SDL_Surface* surface = nullptr; SDL_Texture* texture = nullptr;
SDL_Rect clip = {0, 0, 0, 0}; SDL_Rect clip = {0, 0, 0, 0};
bool local = false; bool local = false;
}; };
+134 -50
View File
@@ -21,79 +21,163 @@
*/ */
#include "sprite_sheet.hpp" #include "sprite_sheet.hpp"
#include <stdexcept>
#include <sstream> #include <sstream>
#include <stdexcept>
SpriteSheet& SpriteSheet::operator=(SpriteSheet const& rhs) {
//don't screw yourself
if (this == &rhs) {
return *this;
}
Free();
//Copy the other SpriteSheet's stuff
texture = rhs.texture;
clip = rhs.clip;
local = false;
countX = rhs.countX;
countY = rhs.countY;
indexX = rhs.indexX;
indexY = rhs.indexY;
delay = rhs.delay;
tick = rhs.tick;
}
SpriteSheet& SpriteSheet::operator=(SpriteSheet&& rhs) {
//don't screw yourself
if (this == &rhs) {
return *this;
}
Free();
//Steal the other SpriteSheet's stuff
texture = rhs.texture;
clip = rhs.clip;
local = rhs.local;
countX = rhs.countX;
countY = rhs.countY;
indexX = rhs.indexX;
indexY = rhs.indexY;
delay = rhs.delay;
tick = rhs.tick;
rhs.texture = nullptr;
rhs.clip = {0, 0, 0, 0};
rhs.local = false;
rhs.countX = 0;
rhs.countY = 0;
rhs.indexX = 0;
rhs.indexY = 0;
rhs.delay = 0.0;
rhs.tick = 0.0;
}
void SpriteSheet::Update(double delta) { void SpriteSheet::Update(double delta) {
//if the delay has passed
if (delay && (tick += delta) >= delay) { if (delay && (tick += delta) >= delay) {
if (++xIndex >= xCount) { //if the index is out of bounds
xIndex = 0; if (++indexX >= countX) {
indexX = 0;
} }
tick = 0; tick = 0;
} }
image.SetClipX(xIndex * image.GetClipW()); //modify area drawn
image.SetClipY(yIndex * image.GetClipH()); clip.x = indexX * clip.w;
clip.y = indexX * clip.y;
} }
SDL_Surface* SpriteSheet::LoadSurface(std::string fname, Uint16 xCellCount, Uint16 yCellCount) { SDL_Texture* SpriteSheet::Load(SDL_Renderer* r, std::string fname, Uint16 cx, Uint16 cy) {
image.LoadSurface(fname); //call the base function
Image::Load(r, fname);
xCount = xCellCount; //set the metadata
yCount = yCellCount; countX = cx;
countY = cy;
image.SetClipW(image.GetSurface()->w / xCount); //assume clip.x and clip.y were set to the size of the texture
image.SetClipH(image.GetSurface()->h / yCount); //reduce the w & h to the size of one cell
clip.w = clip.w / countX;
clip.h = clip.h / countY;
xIndex = yIndex = 0; indexX = indexY = 0;
delay = tick = 0.0;
return texture;
}
SDL_Texture* SpriteSheet::Create(SDL_Renderer* r, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy) {
//call the base function
Image::Create(r, w, h);
//set the metadata
countX = cx;
countY = cy;
//assume clip.x and clip.y were set to the size of the texture
//reduce the w & h to the size of one cell
clip.w = clip.w / countX;
clip.h = clip.h / countY;
indexX = indexY = 0;
delay = tick = 0.0;
return texture;
}
SDL_Texture* SpriteSheet::SetTexture(SDL_Texture* ptr, Uint16 cx, Uint16 cy) {
//call the base function
Image::SetTexture(ptr);
//set the metadata
countX = cx;
countY = cy;
//assume clip.x and clip.y were set to the size of the texture
//reduce the w & h to the size of one cell
clip.w = clip.w / countX;
clip.h = clip.h / countY;
indexX = indexY = 0;
delay = tick = 0.0;
return texture;
}
void SpriteSheet::Free() {
Image::Free();
countX = countY = 0;
indexX = indexY = 0;
delay = tick = 0.0; delay = tick = 0.0;
} }
SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount) { Uint16 SpriteSheet::SetCountX(Uint16 i) {
image.SetSurface(surface); indexX = 0;
return countX = i;
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() { Uint16 SpriteSheet::SetCountY(Uint16 i) {
image.FreeSurface(); indexY = 0;
xCount = yCount = 0; return countY = i;
xIndex = yIndex = 0;
delay = tick = 0.0;
} }
Uint16 SpriteSheet::SetXCount(Uint16 i) { Uint16 SpriteSheet::SetIndexX(Uint16 i) {
xIndex = 0; if (i > countX) {
return xCount = i; std::ostringstream msg;
} msg << "Cannot set index 'x' to " << i;
throw(std::out_of_range(msg.str()));
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; return indexX = i;
} }
Uint16 SpriteSheet::SetYIndex(Uint16 i) { Uint16 SpriteSheet::SetIndexY(Uint16 i) {
if (i > yCount) { if (i > countY) {
std::ostringstream os; std::ostringstream msg;
os << "Cannot set y index to " << i; msg << "Cannot set index 'y' to " << i;
throw(std::invalid_argument(os.str())); throw(std::invalid_argument(msg.str()));
} }
return yIndex = i; return indexY = i;
} }
double SpriteSheet::SetDelay(double d) { double SpriteSheet::SetDelay(double d) {
+31 -24
View File
@@ -23,41 +23,48 @@
#include "image.hpp" #include "image.hpp"
class SpriteSheet { class SpriteSheet : public Image {
public: public:
SpriteSheet() = default; SpriteSheet() = default;
SpriteSheet(std::string fname, Uint16 xCellCount, Uint16 yCellCount) { LoadSurface(fname, xCellCount, yCellCount); } SpriteSheet(SpriteSheet const& rhs) { *this = rhs; }
SpriteSheet(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount) { SetSurface(surface, xCellCount, yCellCount); } SpriteSheet(SpriteSheet&& rhs) { *this = std::move(rhs); }
~SpriteSheet() { FreeSurface(); }; SpriteSheet(SDL_Renderer* r, std::string fname, Uint16 cx, Uint16 cy)
{ Load(r, fname, cx, cy); }
SpriteSheet(SDL_Renderer* r, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy)
{ Create(r, w, h, cx, cy); }
SpriteSheet(SDL_Texture* p, Uint16 cx, Uint16 cy)
{ SetTexture(p, cx, cy); }
~SpriteSheet() = default;
SpriteSheet& operator=(SpriteSheet const&);
SpriteSheet& operator=(SpriteSheet&&);
void Update(double delta); void Update(double delta);
SDL_Surface* LoadSurface(std::string fname, Uint16 xCellCount, Uint16 yCellCount); SDL_Texture* Load(SDL_Renderer*, std::string fname, Uint16 cx, Uint16 cy);
SDL_Surface* SetSurface(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount); SDL_Texture* Create(SDL_Renderer*, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy);
SDL_Surface* GetSurface() { return image.GetSurface(); } SDL_Texture* SetTexture(SDL_Texture*, Uint16 cx, Uint16 cy);
void FreeSurface(); void Free() override;
void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) { image.DrawTo(dest, x, y); } Uint16 SetCountX(Uint16);
Uint16 SetCountY(Uint16);
Uint16 SetIndexX(Uint16);
Uint16 SetIndexY(Uint16);
//accessors and mutators Uint16 GetCountX() const { return countX; }
Image* GetImage() { return &image; } //OO breaker Uint16 GetCountY() const { return countY; }
Uint16 GetIndexX() const { return indexX; }
Uint16 SetXCount(Uint16); Uint16 GetIndexY() const { return indexY; }
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 SetDelay(double d);
double GetDelay() const { return delay; } double GetDelay() const { return delay; }
private: private:
Image image; Uint16 countX = 0, countY = 0, indexX = 0, indexY = 0;
Uint16 xCount = 0, yCount = 0; //number of cells
Uint16 xIndex = 0, yIndex = 0; //current cell being drawn
double delay = 0.0, tick = 0.0; double delay = 0.0, tick = 0.0;
//disable access
using Image::Load;
using Image::Create;
using Image::SetTexture;
}; };
+66 -21
View File
@@ -21,28 +21,71 @@
*/ */
#include "tile_sheet.hpp" #include "tile_sheet.hpp"
void TileSheet::Load(std::string fname, int tileWidth, int tileHeight) { TileSheet& TileSheet::operator=(TileSheet const& rhs) {
image.LoadSurface(fname); //don't screw yourself
image.SetClipW(tileWidth); if (this == &rhs) {
image.SetClipH(tileHeight); return *this;
xCount = image.GetSurface()->w / image.GetClipW(); }
yCount = image.GetSurface()->h / image.GetClipH();
Free();
//Copy the other TileSheet's stuff
texture = rhs.texture;
clip = rhs.clip;
local = false;
countX = rhs.countX;
countY = rhs.countY;
} }
void TileSheet::Unload() { TileSheet& TileSheet::operator=(TileSheet&& rhs) {
image.FreeSurface(); //don't screw yourself
xCount = yCount = 0; if (this == &rhs) {
return *this;
}
Free();
//Copy the other TileSheet's stuff
texture = rhs.texture;
clip = rhs.clip;
local = false;
countX = rhs.countX;
countY = rhs.countY;
rhs.texture = nullptr;
rhs.clip = {0, 0, 0, 0};
rhs.local = false;
rhs.countX = 0;
rhs.countY = 0;
} }
void TileSheet::DrawTileTo(SDL_Surface* const dest, int x, int y, Region::type_t tile) { void TileSheet::Load(SDL_Renderer* renderer, std::string fname, int tileWidth, int tileHeight) {
//0 is invisible Image::Load(renderer, fname);
if (tile == 0) return; countX = clip.w / tileWidth;
image.SetClipX((tile-1) % xCount * image.GetClipW()); countY = clip.h / tileHeight;
image.SetClipY((tile-1) / xCount * image.GetClipH()); clip.w = tileWidth;
image.DrawTo(dest, x, y); clip.h = tileHeight;
} }
void TileSheet::DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY) { SDL_Texture* TileSheet::SetTexture(SDL_Texture* ptr, int tileWidth, int tileHeight) {
Image::SetTexture(ptr);
countX = clip.w / tileWidth;
countY = clip.h / tileHeight;
clip.w = tileWidth;
clip.h = tileHeight;
}
void TileSheet::Free() {
Image::Free();
countX = countY = 0;
}
void TileSheet::DrawLayerTo(SDL_Renderer* const renderer, Region* const region, int layer, int camX, int camY, double scaleX, double scaleY) {
//TODO: empty
}
void TileSheet::DrawRegionTo(SDL_Renderer* const renderer, Region* const region, int camX, int camY, double scaleX, double scaleY) {
//TODO: (2) make TileSheet a friend class of Region
Region::type_t tile = 0; Region::type_t tile = 0;
for (register int i = 0; i < REGION_WIDTH; ++i) { for (register int i = 0; i < REGION_WIDTH; ++i) {
for (register int j = 0; j < REGION_HEIGHT; ++j) { for (register int j = 0; j < REGION_HEIGHT; ++j) {
@@ -50,11 +93,13 @@ void TileSheet::DrawRegionTo(SDL_Surface* const dest, Region* const region, int
tile = region->GetTile(i, j, k); tile = region->GetTile(i, j, k);
//0 is invisible //0 is invisible
if (tile == 0) continue; if (tile == 0) continue;
image.SetClipX((tile-1) % xCount * image.GetClipW()); clip.x = (tile-1) % countX * clip.h;
image.SetClipY((tile-1) / xCount * image.GetClipH()); clip.y = (tile-1) / countX * clip.w;
image.DrawTo(dest, //TODO: (2) raw rendering; improve preformance
(region->GetX() + i) * image.GetClipW() - camX, Image::DrawTo(renderer,
(region->GetY() + j) * image.GetClipH() - camY); (region->GetX() + i) * clip.w - camX,
(region->GetY() + j) * clip.h - camY,
scaleX, scaleY);
} }
} }
} }
+30 -14
View File
@@ -27,25 +27,41 @@
#include <string> #include <string>
class TileSheet { class TileSheet : public Image {
public: public:
TileSheet() = default; TileSheet() = default;
TileSheet(std::string f, int w, int h) { Load(f, w, h); } TileSheet(TileSheet const& rhs) { *this = rhs; }
TileSheet(TileSheet&& rhs) { *this = std::move(rhs); }
TileSheet(SDL_Renderer* r, std::string fn, int tw, int th) { Load(r, fn, tw, th); }
TileSheet(SDL_Texture* p, int tw, int th) { SetTexture(p, tw, th); }
~TileSheet() = default; ~TileSheet() = default;
void Load(std::string fname, int tileWidth, int tileHeight); TileSheet& operator=(TileSheet const&);
void Unload(); TileSheet& operator=(TileSheet&&);
void DrawTileTo(SDL_Surface* const dest, int x, int y, Region::type_t tile); void Load(SDL_Renderer*, std::string fname, int tileWidth, int tileHeight);
void DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY); SDL_Texture* SetTexture(SDL_Texture*, int tileWidth, int tileHeight);
void Free() override;
void DrawLayerTo(SDL_Renderer* const renderer, Region* const region, int layer, int camX, int camY, double scaleX = 1.0, double scaleY = 1.0);
void DrawRegionTo(SDL_Renderer* const renderer, Region* const region, int camX, int camY, double scaleX = 1.0, double scaleY = 1.0);
//accessors //accessors
Image* GetImage() { return &image; } //DOCS: reuse Image::clip for tile sizes
int GetXCount() { return xCount; } int GetCountX() { return countX; }
int GetYCount() { return yCount; } int GetCountY() { return countY; }
int GetTileW() { return image.GetClipW(); } int GetTileW() { return clip.w; }
int GetTileH() { return image.GetClipH(); } int GetTileH() { return clip.h; }
private:
Image image; protected:
int xCount = 0, yCount = 0; int countX = 0, countY = 0;
using Image::Load;
using Image::Create;
using Image::SetTexture;
using Image::SetClip;
using Image::SetClipX;
using Image::SetClipY;
using Image::SetClipW;
using Image::SetClipH;
}; };
+1 -1
View File
@@ -4,5 +4,5 @@ all:
$(MAKE) -C graphics $(MAKE) -C graphics
$(MAKE) -C map $(MAKE) -C map
$(MAKE) -C network $(MAKE) -C network
$(MAKE) -C ui # $(MAKE) -C ui #TODO: reenable this
$(MAKE) -C utilities $(MAKE) -C utilities
+1 -1
View File
@@ -61,7 +61,7 @@ void ConfigUtility::Load(std::string fname, bool skipMissingFile, int argc, char
memset(key, 0, 256); memset(key, 0, 256);
//read the key-value pair //read the key-value pair
if (sscanf(argv[i], "-%[^=]=%[^\0]", key, val) != 2) { if (sscanf(argv[i], "-%[^=]=%[^\\0]", key, val) != 2) {
std::ostringstream os; std::ostringstream os;
os << "Failed to read a command line config argument (expected -%s=%s):" << std::endl; os << "Failed to read a command line config argument (expected -%s=%s):" << std::endl;
os << "\targv[" << i << "]: " << argv[i] << std::endl; os << "\targv[" << i << "]: " << argv[i] << std::endl;