Skip to content

Commit

Permalink
Added the ability to specify using the software renderer. Added a sim…
Browse files Browse the repository at this point in the history
…ple image scaling routine for full screen with software renderer. This allows it to run well on the PocketCHIP.
  • Loading branch information
dulsi committed Jul 18, 2016
1 parent 4579ca9 commit 346885a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
55 changes: 49 additions & 6 deletions src/display.C
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ BTSound::~BTSound()
}
}

BTDisplay::BTDisplay(BTDisplayConfig *c, bool physfs /*= true*/, int multiplier /*= 0*/)
: fullScreen(false), config(c), expanded(0), xMult(multiplier), yMult(multiplier), lockMult(multiplier), status(*this), textPos(0), p3d(this, 0, 0), mainWindow(0), mainRenderer(0), mainTexture(0), mainScreen(0), mainBackground(0), picture(-1), ttffont(0), sfont(&simple8x8), mapXStart(0), mapYStart(0)
BTDisplay::BTDisplay(BTDisplayConfig *c, bool physfs /*= true*/, int multiplier /*= 0*/, bool full /*= false*/, bool softRender /*= false*/)
: fullScreen(full), softRenderer(softRender), config(c), expanded(0), xMult(multiplier), yMult(multiplier), lockMult(multiplier), status(*this), textPos(0), p3d(this, 0, 0), mainWindow(0), mainRenderer(0), mainTexture(0), mainScreen(0), mainBackground(0), picture(-1), ttffont(0), sfont(&simple8x8), mapXStart(0), mapYStart(0)
{
animation.animation = 0;
animation.frame = 0;
Expand Down Expand Up @@ -109,15 +109,16 @@ BTDisplay::BTDisplay(BTDisplayConfig *c, bool physfs /*= true*/, int multiplier
mainWindow = SDL_CreateWindow("Bt Builder",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
config->width * xMult, config->height * yMult,
((fullScreen && softRenderer) ? xFull : config->width * xMult),
((fullScreen && softRenderer) ? yFull : config->height * yMult),
(fullScreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
if (mainWindow == NULL)
{
printf("Failed - SDL_CreateWindow\n");
exit(0);
}

mainRenderer = SDL_CreateRenderer(mainWindow, -1, 0);
mainRenderer = SDL_CreateRenderer(mainWindow, -1, (softRenderer ? SDL_RENDERER_SOFTWARE : 0));
if (mainRenderer == NULL)
{
printf("Failed - SDL_CreateRenderer\n");
Expand All @@ -126,7 +127,8 @@ BTDisplay::BTDisplay(BTDisplayConfig *c, bool physfs /*= true*/, int multiplier
mainTexture = SDL_CreateTexture(mainRenderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
config->width * xMult, config->height * yMult);
((fullScreen && softRenderer) ? xFull : config->width * xMult),
((fullScreen && softRenderer) ? yFull : config->height * yMult));
if (mainTexture == NULL)
{
printf("Failed - SDL_CreateTexture\n");
Expand Down Expand Up @@ -1757,7 +1759,48 @@ void BTDisplay::loadImageOrAnimation(const char *file, SDL_Surface **img, MNG_Im

void BTDisplay::render()
{
SDL_UpdateTexture(mainTexture, NULL, mainScreen->pixels, config->width * xMult * sizeof (Uint32));
if (fullScreen && softRenderer)
{
void *pixels;
int pitch;
SDL_Rect dest;
dest.x = 0;
dest.y = 0;
dest.w = xFull;
dest.h = yFull;
SDL_LockTexture(mainTexture, &dest, &pixels, &pitch);
double scaleWidth = ((double)(config->width * xMult)) / (double)xFull;
double scaleHeight = ((double)(config->height * yMult)) / (double)yFull;
double curX = 0;
double curY = 0;
Uint8 *realLine;
Uint8 *realPos;
int cy;
int cx;

SDL_LockSurface(mainScreen);
realPos = (Uint8 *)pixels;
Uint8 *pixelSource = (Uint8*)mainScreen->pixels;
for (cy = 0; cy < yFull; cy++, curY += scaleHeight)
{
curX = 0;
realLine = realPos;
for (cx = 0; cx < xFull; cx++, curX += scaleWidth)
{
int pos = ((int)curY) * mainScreen->pitch + (((int)curX) * 4);
realPos[0] = pixelSource[pos];
realPos[1] = pixelSource[pos + 1];
realPos[2] = pixelSource[pos + 2];
realPos[3] = pixelSource[pos + 3];
realPos += 4;
}
realPos = realLine + pitch;
}
SDL_UnlockSurface(mainScreen);
SDL_UnlockTexture(mainTexture);
}
else
SDL_UpdateTexture(mainTexture, NULL, mainScreen->pixels, mainScreen->pitch);
SDL_RenderClear(mainRenderer);
SDL_RenderCopy(mainRenderer, mainTexture, NULL, NULL);
SDL_RenderPresent(mainRenderer);
Expand Down
3 changes: 2 additions & 1 deletion src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class BTAnimation
class BTDisplay : public ImageLoader
{
public:
BTDisplay(BTDisplayConfig *c, bool physfs = true, int multiplier = 0);
BTDisplay(BTDisplayConfig *c, bool physfs = true, int multiplier = 0, bool full = false, bool softRender = false);
~BTDisplay();

enum alignment { left, center, right };
Expand Down Expand Up @@ -173,6 +173,7 @@ class BTDisplay : public ImageLoader
private:
int xFull, yFull;
bool fullScreen;
bool softRenderer;
BTDisplayConfig *config;
BTDisplayExpanded *expanded;
int xMult, yMult, lockMult;
Expand Down
12 changes: 10 additions & 2 deletions src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ int main(int argc, char *argv[])
char *xmlFile = NULL;
int multiplier = 0;
std::string libDir(TOSTRING(BTBUILDERDIR));
while ((opt = getopt_long(argc,argv,"imsap:x:e:l:u:", long_options, NULL)) != -1)
bool full = false;
bool softRender = false;
while ((opt = getopt_long(argc,argv,"imsap:x:e:l:u:fw", long_options, NULL)) != -1)
{
switch (opt)
{
Expand Down Expand Up @@ -152,12 +154,18 @@ int main(int argc, char *argv[])
multiplier = atol(optarg);
}
break;
case 'f':
full = true;
break;
case 'w':
softRender = true;
break;
default:
break;
}
}

BTMainScreen mainScreen(argv[0], libDir, multiplier);
BTMainScreen mainScreen(argv[0], libDir, multiplier, full, softRender);
if (optind >= argc)
{
if (mode != MODE_STANDARD)
Expand Down
10 changes: 5 additions & 5 deletions src/mainscreen.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

namespace fs = boost::filesystem;

BTMainScreen::BTMainScreen(const char *a0, std::string lDir, int mult /*= 0*/)
: argv0(a0), libDir(lDir), mainConfig(0), display(0), multiplier(mult)
BTMainScreen::BTMainScreen(const char *a0, std::string lDir, int mult /*= 0*/, bool full /*= false*/, bool softRender /*= false*/)
: argv0(a0), libDir(lDir), mainConfig(0), display(0), multiplier(mult), fullScreen(full), softRenderer(softRender)
{
}

Expand All @@ -36,7 +36,7 @@ BTMainScreen::~BTMainScreen()
void BTMainScreen::run()
{
loadMainConfig();
display = new BTDisplay(mainConfig, true, multiplier);
display = new BTDisplay(mainConfig, true, multiplier, fullScreen, softRenderer);
std::vector<std::string> fileModule;
XMLVector<BTModule*> module;
fs::directory_iterator end_iter;
Expand Down Expand Up @@ -101,7 +101,7 @@ void BTMainScreen::runModule(std::string moduleFile)
}
else
{
display = new BTDisplay(&config, multiplier);
display = new BTDisplay(&config, multiplier, fullScreen, softRenderer);
}
BTGame::getGame()->run(*display);
if (mainConfig)
Expand All @@ -124,7 +124,7 @@ void BTMainScreen::editModule(std::string moduleFile, std::string mapFile /*= st
if (!display)
{
loadMainConfig();
display = new BTDisplay(mainConfig, true, multiplier);
display = new BTDisplay(mainConfig, true, multiplier, fullScreen, softRenderer);
}
if (mapFile.empty())
editor.edit(*display);
Expand Down
4 changes: 3 additions & 1 deletion src/mainscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class physfsException : public std::exception
class BTMainScreen
{
public:
BTMainScreen(const char *a0, std::string lDir, int mult = 0);
BTMainScreen(const char *a0, std::string lDir, int mult = 0, bool full = false, bool softRender = false);
~BTMainScreen();

void run();
Expand All @@ -36,6 +36,8 @@ class BTMainScreen
BTDisplayConfig *mainConfig;
BTDisplay *display;
int multiplier;
bool fullScreen;
bool softRenderer;
};

#endif
Expand Down

0 comments on commit 346885a

Please sign in to comment.