Implement sdl selection sprite

This commit is contained in:
Nathan Fulton 2016-03-14 13:06:52 -04:00
parent b3827e22e4
commit 017ff13af6
6 changed files with 120 additions and 38 deletions

View File

@ -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

View File

@ -12,6 +12,7 @@ struct SurfaceProperties {
int cyHeight;
int cbxPitch;
int cbyPitch;
float density;
unsigned short ffFormat;
};

View File

@ -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

View File

@ -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();
}

72
game/sdl/sdlspritemgr.cpp Normal file
View File

@ -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

View File

@ -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