start changing colors

This commit is contained in:
Eric House 2021-02-02 13:06:50 -08:00
parent de98a70b9f
commit a047a9c865

View file

@ -16,6 +16,8 @@ typedef struct _WasmDrawCtx {
TTF_Font* font20; TTF_Font* font20;
TTF_Font* font36; TTF_Font* font36;
TTF_Font* font48; TTF_Font* font48;
int trayOwner;
} WasmDrawCtx; } WasmDrawCtx;
static int sBonusColors[4][3] = { static int sBonusColors[4][3] = {
@ -25,6 +27,13 @@ static int sBonusColors[4][3] = {
{0xFF, 0x80, 0x00}, {0xFF, 0x80, 0x00},
}; };
static SDL_Color sPlayerColors[4] = {
{0x00, 0x00, 0x00, 0xFF},
{0xFF, 0x00, 0x00, 0xFF},
{0x80, 0x00, 0xFF, 0xFF},
{0xFF, 0x80, 0x00, 0xFF},
};
static void static void
rectXPToSDL( SDL_Rect* sdlr, const XP_Rect* rect ) rectXPToSDL( SDL_Rect* sdlr, const XP_Rect* rect )
{ {
@ -37,10 +46,23 @@ rectXPToSDL( SDL_Rect* sdlr, const XP_Rect* rect )
static void static void
clearRect( WasmDrawCtx* wdctx, const XP_Rect* rect ) clearRect( WasmDrawCtx* wdctx, const XP_Rect* rect )
{ {
Uint8 red, green, blue, alpha;
SDL_GetRenderDrawColor( wdctx->renderer, &red, &green, &blue, &alpha );
SDL_SetRenderDrawColor( wdctx->renderer, COLOR_BACK, 255 );
SDL_Rect sdlr; SDL_Rect sdlr;
rectXPToSDL( &sdlr, rect ); rectXPToSDL( &sdlr, rect );
SDL_SetRenderDrawColor( wdctx->renderer, COLOR_BACK, 255 );
SDL_RenderFillRect( wdctx->renderer, &sdlr ); SDL_RenderFillRect( wdctx->renderer, &sdlr );
SDL_SetRenderDrawColor( wdctx->renderer, red, green, blue, alpha );
}
static void
setPlayerColor( WasmDrawCtx* wdctx, int owner )
{
SDL_Color colorParts = sPlayerColors[owner];
SDL_SetRenderDrawColor( wdctx->renderer, colorParts.r, colorParts.g,
colorParts.b, colorParts.a );
} }
static void static void
@ -48,9 +70,13 @@ fillRect( WasmDrawCtx* wdctx, const XP_Rect* rect, int colorParts[] )
{ {
SDL_Rect sdlr; SDL_Rect sdlr;
rectXPToSDL( &sdlr, rect ); rectXPToSDL( &sdlr, rect );
Uint8 red, green, blue, alpha;
SDL_GetRenderDrawColor( wdctx->renderer, &red, &green, &blue, &alpha );
SDL_SetRenderDrawColor( wdctx->renderer, colorParts[0], colorParts[1], SDL_SetRenderDrawColor( wdctx->renderer, colorParts[0], colorParts[1],
colorParts[2], 255 ); colorParts[2], 255 );
SDL_RenderFillRect( wdctx->renderer, &sdlr ); SDL_RenderFillRect( wdctx->renderer, &sdlr );
SDL_SetRenderDrawColor( wdctx->renderer, red, green, blue, alpha );
} }
static void static void
@ -58,12 +84,13 @@ frameRect( WasmDrawCtx* wdctx, const XP_Rect* rect )
{ {
SDL_Rect sdlr; SDL_Rect sdlr;
rectXPToSDL( &sdlr, rect ); rectXPToSDL( &sdlr, rect );
SDL_SetRenderDrawColor( wdctx->renderer, COLOR_BLACK, 255 ); // SDL_SetRenderDrawColor( wdctx->renderer, COLOR_BLACK, 255 );
SDL_RenderDrawRect( wdctx->renderer, &sdlr ); SDL_RenderDrawRect( wdctx->renderer, &sdlr );
} }
static void static void
textInRect( WasmDrawCtx* wdctx, const XP_UCHAR* text, const XP_Rect* rect ) textInRect( WasmDrawCtx* wdctx, const XP_UCHAR* text, const XP_Rect* rect,
const SDL_Color* color )
{ {
TTF_Font* font; TTF_Font* font;
if ( rect->height <= 12 ) { if ( rect->height <= 12 ) {
@ -77,24 +104,36 @@ textInRect( WasmDrawCtx* wdctx, const XP_UCHAR* text, const XP_Rect* rect )
font = wdctx->font48; font = wdctx->font48;
} }
SDL_Color color = { 0, 0, 0, 255 }; XP_Rect tmpR = *rect;
SDL_Surface* surface = TTF_RenderText_Blended( font, text, color );
SDL_Color black = { 0, 0, 0, 255 };
if ( NULL == color ) {
color = &black;
}
SDL_Surface* surface = TTF_RenderText_Blended( font, text, *color );
SDL_Texture* texture = SDL_CreateTextureFromSurface( wdctx->renderer, surface ); SDL_Texture* texture = SDL_CreateTextureFromSurface( wdctx->renderer, surface );
SDL_FreeSurface( surface ); SDL_FreeSurface( surface );
int width, height;
SDL_QueryTexture( texture, NULL, NULL, &width, &height );
XP_LOGFF( "have w: %d; h: %d; got w: %d; h: %d",
tmpR.width, tmpR.height, width, height );
tmpR.width = width;
SDL_Rect sdlr; SDL_Rect sdlr;
rectXPToSDL( &sdlr, rect ); rectXPToSDL( &sdlr, &tmpR );
// SDL_QueryTexture( texture, NULL, NULL, &sdlr.w, &sdlr.h );
SDL_RenderCopy( wdctx->renderer, texture, NULL, &sdlr ); SDL_RenderCopy( wdctx->renderer, texture, NULL, &sdlr );
SDL_DestroyTexture( texture ); SDL_DestroyTexture( texture );
} }
static void static void
drawTile( WasmDrawCtx* wdctx, const XP_UCHAR* face, XP_U16 val, const XP_Rect* rect ) drawTile( WasmDrawCtx* wdctx, const XP_UCHAR* face, XP_U16 val,
int owner, const XP_Rect* rect )
{ {
clearRect( wdctx, rect ); clearRect( wdctx, rect );
// setPlayerColor( wdctx, owner );
frameRect( wdctx, rect ); frameRect( wdctx, rect );
textInRect( wdctx, face, rect ); textInRect( wdctx, face, rect, &sPlayerColors[owner] );
} }
static void static void
@ -131,9 +170,7 @@ wasm_draw_boardBegin( DrawCtx* dctx, XWEnv xwe,
XP_U16 hScale, XP_U16 vScale, XP_U16 hScale, XP_U16 vScale,
DrawFocusState dfs ) DrawFocusState dfs )
{ {
LOG_FUNC(); // LOG_FUNC();
/* WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx; */
/* SDL_SetRenderDrawColor( wdctx->renderer, 255, 0, 0, 0 ); */
return XP_TRUE; return XP_TRUE;
} }
@ -155,7 +192,9 @@ wasm_draw_trayBegin( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
XP_U16 owner, XP_S16 score, XP_U16 owner, XP_S16 score,
DrawFocusState dfs ) DrawFocusState dfs )
{ {
LOG_FUNC(); XP_LOGFF( "(owner=%d)", owner );
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
wdctx->trayOwner = owner;
return XP_TRUE; return XP_TRUE;
} }
@ -166,16 +205,27 @@ wasm_draw_scoreBegin( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
XP_S16 remCount, DrawFocusState dfs ) XP_S16 remCount, DrawFocusState dfs )
{ {
LOG_FUNC(); LOG_FUNC();
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
clearRect( wdctx, rect );
return XP_TRUE; return XP_TRUE;
} }
static XP_Bool static XP_Bool
wasm_draw_measureRemText( DrawCtx* dctx, XWEnv xwe, const XP_Rect* r, wasm_draw_measureRemText( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
XP_S16 nTilesLeft, XP_S16 nTilesLeft,
XP_U16* width, XP_U16* height ) XP_U16* width, XP_U16* height )
{ {
LOG_FUNC(); LOG_FUNC();
return XP_FALSE; XP_Bool drawIt = 0 <= nTilesLeft;
if ( drawIt ) {
XP_UCHAR buf[4];
XP_SNPRINTF( buf, VSIZE(buf), "%d", nTilesLeft );
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
textInRect( wdctx, buf, rect, NULL );
*width = *height = rect->height;
}
return drawIt;
} }
static void static void
@ -188,15 +238,26 @@ wasm_draw_drawRemText( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rInner,
static void static void
wasm_draw_measureScoreText( DrawCtx* dctx, XWEnv xwe, wasm_draw_measureScoreText( DrawCtx* dctx, XWEnv xwe,
const XP_Rect* r, const XP_Rect* rect,
const DrawScoreInfo* dsi, const DrawScoreInfo* dsi,
XP_U16* width, XP_U16* height ){ LOG_FUNC(); } XP_U16* width, XP_U16* height )
{
LOG_FUNC();
*width = rect->width / 2;
*height = rect->height;
}
static void static void
wasm_draw_score_drawPlayer( DrawCtx* dctx, XWEnv xwe, wasm_draw_score_drawPlayer( DrawCtx* dctx, XWEnv xwe,
const XP_Rect* rInner, const XP_Rect* rInner,
const XP_Rect* rOuter, const XP_Rect* rOuter,
XP_U16 gotPct, XP_U16 gotPct,
const DrawScoreInfo* dsi ){ LOG_FUNC(); } const DrawScoreInfo* dsi )
{
XP_LOGFF( "(playerNum: %d)", dsi->playerNum );
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
textInRect( wdctx, dsi->name, rInner, &sPlayerColors[dsi->playerNum] );
}
static void static void
wasm_draw_score_pendingScore( DrawCtx* dctx, XWEnv xwe, wasm_draw_score_pendingScore( DrawCtx* dctx, XWEnv xwe,
@ -209,7 +270,7 @@ wasm_draw_score_pendingScore( DrawCtx* dctx, XWEnv xwe,
XP_Rect tmp = *rect; XP_Rect tmp = *rect;
tmp.height /= 2; tmp.height /= 2;
textInRect( wdctx, "Pts:", &tmp ); textInRect( wdctx, "Pts:", &tmp, NULL );
tmp.top += tmp.height; tmp.top += tmp.height;
XP_UCHAR buf[16]; XP_UCHAR buf[16];
if ( score >= 0 ) { if ( score >= 0 ) {
@ -217,7 +278,7 @@ wasm_draw_score_pendingScore( DrawCtx* dctx, XWEnv xwe,
} else { } else {
XP_STRNCPY( buf, "???", VSIZE(buf) ); XP_STRNCPY( buf, "???", VSIZE(buf) );
} }
textInRect( wdctx, buf, &tmp ); textInRect( wdctx, buf, &tmp, NULL );
} }
static void static void
@ -246,7 +307,7 @@ wasm_draw_drawCell( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
fillRect( wdctx, rect, sBonusColors[bonus-1] ); fillRect( wdctx, rect, sBonusColors[bonus-1] );
} }
} else if ( !!text ) { } else if ( !!text ) {
textInRect( wdctx, text, rect ); textInRect( wdctx, text, rect, NULL );
} }
frameRect( wdctx, rect ); frameRect( wdctx, rect );
@ -270,7 +331,7 @@ wasm_draw_drawTile( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
{ {
XP_LOGFF( "(text=%s)", text ); XP_LOGFF( "(text=%s)", text );
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx; WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
drawTile( wdctx, text, val, rect ); drawTile( wdctx, text, val, wdctx->trayOwner, rect );
return XP_TRUE; return XP_TRUE;
} }
@ -287,7 +348,7 @@ wasm_draw_drawTileMidDrag( DrawCtx* dctx, XWEnv xwe,
{ {
LOG_FUNC(); LOG_FUNC();
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx; WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
drawTile( wdctx, text, val, rect ); drawTile( wdctx, text, val, wdctx->trayOwner, rect );
return XP_TRUE; return XP_TRUE;
} }
#endif #endif
@ -298,7 +359,7 @@ wasm_draw_drawTileBack( DrawCtx* dctx, XWEnv xwe, const XP_Rect* rect,
{ {
LOG_FUNC(); LOG_FUNC();
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx; WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
drawTile( wdctx, "?", -1, rect ); drawTile( wdctx, "?", -1, wdctx->trayOwner, rect );
return XP_TRUE; return XP_TRUE;
} }
@ -325,7 +386,7 @@ wasm_draw_drawBoardArrow ( DrawCtx* dctx, XWEnv xwe,
LOG_FUNC(); LOG_FUNC();
WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx; WasmDrawCtx* wdctx = (WasmDrawCtx*)dctx;
const XP_UCHAR* str = vert ? "|" : "-"; const XP_UCHAR* str = vert ? "|" : "-";
textInRect( wdctx, str, rect ); textInRect( wdctx, str, rect, NULL );
} }
static void static void