From 017ff13af67a033ad25885f73a63e3ffdd935943 Mon Sep 17 00:00:00 2001 From: Nathan Fulton Date: Mon, 14 Mar 2016 13:06:52 -0400 Subject: [PATCH] Implement sdl selection sprite --- game/sdl/display.cpp | 13 ++++++ game/sdl/hosthelpers.h | 1 + game/sdl/htplatform.h | 2 + game/sdl/sdlselectionsprite.cpp | 45 +++++++++------------ game/sdl/sdlspritemgr.cpp | 72 +++++++++++++++++++++++++++++++++ game/sdl/sdlspritemgr.h | 25 ++++++------ 6 files changed, 120 insertions(+), 38 deletions(-) create mode 100644 game/sdl/sdlspritemgr.cpp diff --git a/game/sdl/display.cpp b/game/sdl/display.cpp index 06a3ab1..040c154 100644 --- a/game/sdl/display.cpp +++ b/game/sdl/display.cpp @@ -6,6 +6,7 @@ namespace wi { static SdlSpriteManager *s_psprm; +static Size s_siz; Display *HostCreateDisplay() { // Create a display @@ -79,6 +80,7 @@ bool Display::Init() HostHelpers::GetSurfaceProperties(&props); cxScreen = props.cxWidth; cyScreen = props.cyHeight; + m_density = props.density; // Create window if ((window = SDL_CreateWindow("Hostile Takeover", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, cxScreen, cyScreen, videoflags)) == NULL) { @@ -88,6 +90,10 @@ bool Display::Init() renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_TARGETTEXTURE); + // Keep the screen size around + s_siz.cx = cxScreen; + s_siz.cy = cyScreen; + m_gameSurface = SDL_CreateRGBSurface(videoflags, cxScreen, cyScreen, 32, 0, 0, 0, 0); m_pixelCount = cxScreen * cyScreen; m_gameDisplay = (byte *)malloc(m_pixelCount); @@ -270,6 +276,9 @@ void Display::RenderGameSurface() { SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); + // Draw on any sprites + s_psprm->DrawSprites(renderer, s_siz); + // Present the renderer SDL_RenderPresent(renderer); @@ -298,4 +307,8 @@ void Display::SetFormMgrs(FormMgr *pfrmmSimUI, FormMgr *pfrmmInput) #endif } +float Display::Density() { + return m_density; +} + } // namespace wi diff --git a/game/sdl/hosthelpers.h b/game/sdl/hosthelpers.h index 8245728..ffae748 100644 --- a/game/sdl/hosthelpers.h +++ b/game/sdl/hosthelpers.h @@ -12,6 +12,7 @@ struct SurfaceProperties { int cyHeight; int cbxPitch; int cbyPitch; + float density; unsigned short ffFormat; }; diff --git a/game/sdl/htplatform.h b/game/sdl/htplatform.h index 2d54a24..3a2c1c3 100644 --- a/game/sdl/htplatform.h +++ b/game/sdl/htplatform.h @@ -122,6 +122,7 @@ public: SpriteManager *GetSpriteManager(); void SetFormMgrs(FormMgr *pfrmmSim, FormMgr *pfrmmInput); void RenderGameSurface(); + float Density(); private: int m_imode; @@ -143,6 +144,7 @@ private: Uint32 *m_gameSurfacePixels; Uint32 m_32bppColors[256]; int m_pixelCount; + float m_density; }; #define kfDtClearLine 1 diff --git a/game/sdl/sdlselectionsprite.cpp b/game/sdl/sdlselectionsprite.cpp index 0cec0f9..c62ab3d 100644 --- a/game/sdl/sdlselectionsprite.cpp +++ b/game/sdl/sdlselectionsprite.cpp @@ -38,37 +38,32 @@ void SdlSelectionSprite::Draw(void *pv, Size *psiz) { crit_.Enter(); -#if 0 // TODO(darrinm) DPoint apt[4]; drc_.GetPoints(apt); - // x/y are swapped, and y needs to be origin adjusted - - CGContextRef ctx = (CGContextRef)pv; - CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1.0); - CGContextSetLineWidth(ctx, 2.0); - CGContextBeginPath(ctx); - CGContextMoveToPoint(ctx, apt[0].y, psiz->cy - apt[0].x); - for (int i = 1; i < ARRAYSIZE(apt); i++) { - CGContextAddLineToPoint(ctx, apt[i].y, psiz->cy - apt[i].x); - } - CGContextAddLineToPoint(ctx, apt[0].y, psiz->cy - apt[0].x); - CGContextStrokePath(ctx); - - // Draw circles at rect corners for "grabbies". White stroked, - // black filled. - CGContextSetRGBFillColor(ctx, 0.0, 0.0, 0.0, 1.0); - -#define kcpRectHalf 4 + float density = gpdisp->Density(); + #define renderer (SDL_Renderer *)pv + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); for (int i = 0; i < ARRAYSIZE(apt); i++) { - CGRect rc = CGRectMake(apt[i].y - kcpRectHalf, - psiz->cy - (apt[i].x + kcpRectHalf), - kcpRectHalf * 2, kcpRectHalf * 2); - CGContextFillEllipseInRect(ctx, rc); - CGContextStrokeEllipseInRect(ctx, rc); + int next = i + 1 == ARRAYSIZE(apt) ? 0 : i + 1; + + // x/y points for point a/b of the selection sprite line + int x1 = roundf(apt[i].y * density); + int y1 = roundf(apt[i].x * density); + int x2 = roundf(apt[next].y * density); + int y2 = roundf(apt[next].x * density); + + // draw the selection sprite line + SDL_RenderDrawLine(renderer, x1, y1, x2, y2); + + int gs = 20; // grabbie size + int gsh = gs / 2; // grabbie size half + + // create and draw a grabbie rect + SDL_Rect rect = {x1 - gsh, y1 - gsh , gs, gs}; + SDL_RenderFillRect(renderer, &rect); } -#endif crit_.Leave(); } diff --git a/game/sdl/sdlspritemgr.cpp b/game/sdl/sdlspritemgr.cpp new file mode 100644 index 0000000..29cf5c1 --- /dev/null +++ b/game/sdl/sdlspritemgr.cpp @@ -0,0 +1,72 @@ +#include "game/ht.h" +#include "game/sdl/sdlspritemgr.h" +#include "SDL.h" + +namespace wi { + +SdlSpriteManager::SdlSpriteManager() { + pcrit_ = new base::CriticalSection(); + cpspr_ = 0; + fSpriteDirty_ = false; +} + +SdlSpriteManager::~SdlSpriteManager() { + +} + +void SdlSpriteManager::Add(wi::Sprite *pspr) { + base::CritScope cs(pcrit_); + + // If already added, just recreate the layer + + bool fFound = false; + for (int i = 0; i < cpspr_; i++) { + if (pspr == apspr_[i]) { + fFound = true; + } + } + if (!fFound && cpspr_ < ARRAYSIZE(apspr_) - 1) { + apspr_[cpspr_] = pspr; + cpspr_++; + } + + fSpriteDirty_ = true; +} + +void SdlSpriteManager::Remove(wi::Sprite *pspr) { + base::CritScope cs(pcrit_); + + bool fFound = false; + for (int i = 0; i < cpspr_; i++) { + if (pspr == apspr_[i]) { + cpspr_--; + if (i < ARRAYSIZE(apspr_) - 1) { + memmove(&apspr_[i], &apspr_[i + 1], + (ARRAYSIZE(apspr_) - 1 - i) * ELEMENTSIZE(apspr_)); + } + fFound = true; + break; + } + } + + fSpriteDirty_ = true; +} + +void SdlSpriteManager::Update(wi::Sprite *pspr) { + fSpriteDirty_ = true; +} + +void SdlSpriteManager::DrawSprites(SDL_Renderer *renderer, Size siz) { + base::CritScope cs(pcrit_); + + if (cpspr_ == 0) { + return; + } + + for (int i = 0; i < cpspr_; i++) { + apspr_[i]->Draw(renderer, &siz); + } +} + + +} // namespace wi diff --git a/game/sdl/sdlspritemgr.h b/game/sdl/sdlspritemgr.h index c2acfce..710a83b 100644 --- a/game/sdl/sdlspritemgr.h +++ b/game/sdl/sdlspritemgr.h @@ -8,7 +8,8 @@ namespace wi { class SdlSpriteManager : public SpriteManager { public: - SdlSpriteManager() {} + SdlSpriteManager(); + ~SdlSpriteManager(); virtual void SetClipRects(wi::Rect *prc1, wi::Rect *prc2) { LOG() << "SdlSpriteManager::SetClipRects not implemented yet"; @@ -21,18 +22,16 @@ public: virtual wi::SelectionSprite *CreateSelectionSprite() { return new SdlSelectionSprite(this); } - virtual void Add(wi::Sprite *pspr) { - LOG() << "SdlSpriteManager::Add not implemented yet"; - return; - } - virtual void Remove(wi::Sprite *pspr) { - LOG() << "SdlSpriteManager::Remove not implemented yet"; - return; - } - virtual void Update(wi::Sprite *pspr) { - LOG() << "SdlSpriteManager::Update not implemented yet"; - return; - } + virtual void Add(wi::Sprite *pspr); + virtual void Remove(wi::Sprite *pspr); + virtual void Update(wi::Sprite *pspr); + virtual void DrawSprites(SDL_Renderer *renderer, Size siz); + +private: + base::CriticalSection *pcrit_; + int cpspr_; + wi::Sprite *apspr_[16]; + bool fSpriteDirty_; }; } // namespace wi