Skip to content

Commit

Permalink
Convert to SDL2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamefire committed Feb 21, 2020
1 parent 213729b commit 2d7562e
Show file tree
Hide file tree
Showing 35 changed files with 285 additions and 4,539 deletions.
8 changes: 8 additions & 0 deletions CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ int CGame::Execute()
return 0;
}

void CGame::RenderPresent()
{
SDL_UpdateTexture(displayTexture_.get(), nullptr, Surf_Display->pixels, Surf_Display->w * sizeof(Uint32));
SDL_RenderClear(renderer_.get());
SDL_RenderCopy(renderer_.get(), displayTexture_.get(), nullptr, nullptr);
SDL_RenderPresent(renderer_.get());
}

CMenu* CGame::RegisterMenu(std::unique_ptr<CMenu> Menu)
{
for(auto& i : Menus)
Expand Down
9 changes: 6 additions & 3 deletions CGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class CGame

bool Running;
bool showLoadScreen;
bool useOpenGL = false;
SdlSurface Surf_Display, Surf_DisplayGL;
SdlSurface Surf_Display;
SdlTexture displayTexture_;
SdlRenderer renderer_;
SdlWindow window_;

private:
#ifdef _ADMINMODE
Expand Down Expand Up @@ -75,6 +77,8 @@ class CGame

void Render();

void RenderPresent();

CMenu* RegisterMenu(std::unique_ptr<CMenu> Menu);
bool UnregisterMenu(CMenu* Menu);
CWindow* RegisterWindow(std::unique_ptr<CWindow> Window);
Expand All @@ -85,7 +89,6 @@ class CGame
CMap* getMapObj();
void delMapObj();
SDL_Surface* getDisplaySurface() { return Surf_Display.get(); };
SDL_Surface* getDisplayGLSurface() { return Surf_DisplayGL.get(); };
auto getRes() { return GameResolution; }
};

Expand Down
42 changes: 15 additions & 27 deletions CGame_Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,21 @@

bool CGame::ReCreateWindow()
{
useOpenGL = CSurface::useOpenGL;
static char CENTER_ENV[] = "SDL_VIDEO_CENTERED=center";
SDL_putenv(CENTER_ENV);

if(useOpenGL)
{
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
Surf_DisplayGL.reset(SDL_SetVideoMode(GameResolution.x, GameResolution.y, 32, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0)));
Surf_Display = makeSdlSurface(SDL_SWSURFACE, GameResolution.x, GameResolution.y, 32);
if(!Surf_Display || !Surf_DisplayGL)
return false;
} else
{
Surf_Display.reset(
SDL_SetVideoMode(GameResolution.x, GameResolution.y, 32, SDL_SWSURFACE | SDL_DOUBLEBUF | (fullscreen ? SDL_FULLSCREEN : 0)));
Surf_DisplayGL.reset();
if(!Surf_Display)
return false;
}
displayTexture_.reset();
renderer_.reset();
window_.reset();
window_.reset(SDL_CreateWindow("Return to the Roots Map editor [BETA]", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
GameResolution.x, GameResolution.y, fullscreen ? SDL_WINDOW_FULLSCREEN : 0));
if(!window_)
return false;
renderer_.reset(SDL_CreateRenderer(window_.get(), -1, 0));
if(!renderer_)
return false;
displayTexture_ = makeSdlTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, GameResolution.x, GameResolution.y);
Surf_Display = makeRGBSurface(GameResolution.x, GameResolution.y, true);
if(!displayTexture_ || !Surf_Display)
return false;

SDL_WM_SetCaption("Return to the Roots Map editor [BETA]", nullptr);
SetAppIcon();
return true;
}
Expand All @@ -45,7 +35,6 @@ bool CGame::Init()
{
std::cout << "Return to the Roots Map editor\n";

SDL_EnableKeyRepeat(100, 100);
SDL_ShowCursor(SDL_DISABLE);

std::cout << "Create Window...";
Expand All @@ -54,7 +43,6 @@ bool CGame::Init()
std::cout << "failure";
return false;
}
sge_Update_OFF();
sge_Lock_OFF();
CFile::init();

Expand Down Expand Up @@ -89,7 +77,7 @@ bool CGame::Init()
auto& surfSplash = global::bmpArray[SPLASHSCREEN_LOADING_S2SCREEN].surface;
sge_TexturedRect(Surf_Display.get(), 0, 0, Surf_Display->w - 1, 0, 0, Surf_Display->h - 1, Surf_Display->w - 1, Surf_Display->h - 1,
surfSplash.get(), 0, 0, surfSplash->w - 1, 0, 0, surfSplash->h - 1, surfSplash->w - 1, surfSplash->h - 1);
SDL_Flip(Surf_Display.get());
RenderPresent();

GameDataLoader gdLoader(global::worldDesc);
if(!gdLoader.Load())
Expand Down
26 changes: 7 additions & 19 deletions CGame_Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ void CGame::SetAppIcon()
SDL_SysWMinfo info;
// get window handle from SDL
SDL_VERSION(&info.version);
if(SDL_GetWMInfo(&info) != 1)
if(SDL_GetWindowWMInfo(window_.get(), &info) != 1)
return;
SendMessage(info.window, WM_SETICON, ICON_BIG, icon);
SendMessage(info.window, WM_SETICON, ICON_SMALL, icon);
SendMessage(info.info.win.window, WM_SETICON, ICON_BIG, icon);
SendMessage(info.info.win.window, WM_SETICON, ICON_SMALL, icon);
#endif // _WIN32
}

void CGame::Render()
{
if(Extent(Surf_Display->w, Surf_Display->h) != GameResolution || fullscreen != ((Surf_Display->flags & SDL_FULLSCREEN) != 0)
|| useOpenGL != CSurface::useOpenGL)
if(Extent(Surf_Display->w, Surf_Display->h) != GameResolution
|| fullscreen != ((SDL_GetWindowFlags(window_.get()) & SDL_WINDOW_FULLSCREEN) != 0))
{
ReCreateWindow();
}
Expand All @@ -49,13 +49,7 @@ void CGame::Render()
sge_TexturedRect(Surf_Display.get(), 0, 0, Surf_Display->w - 1, 0, 0, Surf_Display->h - 1, Surf_Display->w - 1, Surf_Display->h - 1,
surfLoadScreen.get(), 0, 0, surfLoadScreen->w - 1, 0, 0, surfLoadScreen->h - 1, surfLoadScreen->w - 1,
surfLoadScreen->h - 1);

if(useOpenGL)
{
SDL_BlitSurface(Surf_Display.get(), nullptr, Surf_DisplayGL.get(), nullptr);
SDL_GL_SwapBuffers();
} else
SDL_Flip(Surf_Display.get());
RenderPresent();
return;
}

Expand Down Expand Up @@ -131,13 +125,7 @@ void CGame::Render()
}
CSurface::Draw(Surf_Display, lastFps.getSurface(), 0, 0);

if(useOpenGL)
{
SDL_BlitSurface(Surf_Display.get(), nullptr, Surf_DisplayGL.get(), nullptr);
SDL_Flip(Surf_DisplayGL.get());
SDL_GL_SwapBuffers();
} else
SDL_Flip(Surf_Display.get());
RenderPresent();

if(msWait)
SDL_Delay(msWait);
Expand Down
2 changes: 1 addition & 1 deletion CIO/CButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool CButton::render()
// if we need a new surface
if(!Surf_Button)
{
if((Surf_Button = makeSdlSurface(SDL_SWSURFACE, w, h, 32)) == nullptr)
if((Surf_Button = makeRGBSurface(w, h)) == nullptr)
return false;
}

Expand Down
28 changes: 13 additions & 15 deletions CIO/CFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,11 @@ bool CFile::open_lbm(const std::string& filename)
CHECK_READ(libendian::be_read_ui(&length, fp));

// now we are ready to read the picture lines and fill the surface, so lets create one
if(!(bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)))
if(!(bmpArray->surface = makePalSurface(bmpArray->w, bmpArray->h, colors)))
{
std::cerr << "Failed to create surface: " << SDL_GetError() << std::endl;
return false;
SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, colors.data(), 0, colors.size());
}

if(compression_flag == 0)
{
Expand Down Expand Up @@ -501,12 +503,12 @@ bool CFile::open_lbm(const std::string& filename)
|| filename.find("TEX7.LBM") != std::string::npos || filename.find("TEXTUR_0.LBM") != std::string::npos
|| filename.find("TEXTUR_3.LBM") != std::string::npos)
{
SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));

bmpArray++;
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, (bmpArray - 1)->w, (bmpArray - 1)->h, 32)))
if((bmpArray->surface = makeRGBSurface((bmpArray - 1)->w, (bmpArray - 1)->h)))
{
SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
CSurface::Draw(bmpArray->surface, (bmpArray - 1)->surface, 0, 0);
} else
bmpArray--;
Expand Down Expand Up @@ -1054,10 +1056,9 @@ bool CFile::read_bob02()
CHECK_READ(libendian::le_read_us(&starts[y], fp));

// now we are ready to read the picture lines and fill the surface, so lets create one
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == nullptr)
if((bmpArray->surface = makePalSurface(bmpArray->w, bmpArray->h, palActual->colors)) == nullptr)
return false;
SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size());
SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
// SDL_SetAlpha(bmpArray->surface, SDL_SRCALPHA, 128);

// main loop for reading picture lines
Expand Down Expand Up @@ -1224,11 +1225,10 @@ bool CFile::read_bob04(int player_color)
CHECK_READ(libendian::le_read_us(&starts[y], fp));

// now we are ready to read the picture lines and fill the surface, so lets create one
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == nullptr)
if((bmpArray->surface = makePalSurface(bmpArray->w, bmpArray->h, palActual->colors)) == nullptr)
return false;

SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size());
SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));

// main loop for reading picture lines
for(int y = 0; y < bmpArray->h; y++)
Expand Down Expand Up @@ -1363,9 +1363,8 @@ bool CFile::read_bob07()
CHECK_READ(libendian::le_read_us(&starts[y], fp));

// now we are ready to read the picture lines and fill the surface, so lets create one
if((shadowArray->surface = makeSdlSurface(SDL_SWSURFACE, shadowArray->w, shadowArray->h, 8)) == nullptr)
if((shadowArray->surface = makePalSurface(shadowArray->w, shadowArray->h, palActual->colors)) == nullptr)
return false;
SDL_SetPalette(shadowArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size());
// SDL_SetAlpha(shadowArray->surface, SDL_SRCALPHA, 128);

// main loop for reading picture lines
Expand Down Expand Up @@ -1472,9 +1471,8 @@ bool CFile::read_bob14()
return true;

// now we are ready to read the picture lines and fill the surface, so lets create one
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == nullptr)
if((bmpArray->surface = makePalSurface(bmpArray->w, bmpArray->h, palActual->colors)) == nullptr)
return false;
SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size());

// set fp to back to the first offset of data block
fseek(fp, data_start, SEEK_SET);
Expand Down
4 changes: 2 additions & 2 deletions CIO/CFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ void CFont::writeText()
pixel_ctr_w = pixel_ctr_w_tmp;
w = pixel_ctr_w;
h = pixel_ctr_h;
Surf_Font = makeSdlSurface(SDL_SWSURFACE, w, h, 32);
Surf_Font = makeRGBSurface(w, h);
if(!Surf_Font)
return;
SDL_SetColorKey(Surf_Font.get(), SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Font->format, 0, 0, 0));
SDL_SetColorKey(Surf_Font.get(), SDL_TRUE, SDL_MapRGB(Surf_Font->format, 0, 0, 0));
chiffre = string_.begin();
pixel_count_loop = false;
continue;
Expand Down
2 changes: 1 addition & 1 deletion CIO/CMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bool CMenu::render()
// if we need a new surface
if(!surface)
{
surface = makeSdlSurface(SDL_SWSURFACE, global::s2->getRes().x, global::s2->getRes().y, 32);
surface = makeRGBSurface(global::s2->getRes().x, global::s2->getRes().y);
if(!surface)
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions CIO/CPicture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ bool CPicture::render()
// if we need a new surface
if(!Surf_Picture)
{
Surf_Picture = makeSdlSurface(SDL_SWSURFACE, w, h, 32);
Surf_Picture = makeRGBSurface(w, h);
if(!Surf_Picture)
return false;
SDL_SetColorKey(Surf_Picture.get(), SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Picture->format, 0, 0, 0));
SDL_SetColorKey(Surf_Picture.get(), SDL_TRUE, SDL_MapRGB(Surf_Picture->format, 0, 0, 0));
}

CSurface::Draw(Surf_Picture, global::bmpArray[picture_].surface, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion CIO/CSelectBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ bool CSelectBox::render()
// if we need a new surface
if(!Surf_SelectBox)
{
if((Surf_SelectBox = makeSdlSurface(SDL_SWSURFACE, w_, h_, 32)) == nullptr)
if((Surf_SelectBox = makeRGBSurface(w_, h_)) == nullptr)
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion CIO/CTextfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ bool CTextfield::render()
// if we need a new surface
if(!Surf_Text)
{
Surf_Text = makeSdlSurface(SDL_SWSURFACE, w, h, 32);
Surf_Text = makeRGBSurface(w, h);
if(!Surf_Text)
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion CIO/CWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ bool CWindow::render()
// if we need a new surface
if(!surface)
{
if(!(surface = makeSdlSurface(SDL_SWSURFACE, w_, h_, 32)))
if(!(surface = makeRGBSurface(w_, h_)))
return false;
}

Expand Down
Loading

0 comments on commit 2d7562e

Please sign in to comment.