diff --git a/CGame_Render.cpp b/CGame_Render.cpp index 17425ec..580a062 100644 --- a/CGame_Render.cpp +++ b/CGame_Render.cpp @@ -35,7 +35,8 @@ void CGame::SetAppIcon() 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 != ((Surf_Display->flags & SDL_FULLSCREEN) != 0) + || useOpenGL != CSurface::useOpenGL) { ReCreateWindow(); } @@ -50,8 +51,10 @@ void CGame::Render() surfLoadScreen->h - 1); if(useOpenGL) + { + SDL_BlitSurface(Surf_Display.get(), nullptr, Surf_DisplayGL.get(), nullptr); SDL_GL_SwapBuffers(); - else + } else SDL_Flip(Surf_Display.get()); return; } diff --git a/CIO/CButton.cpp b/CIO/CButton.cpp index 6f2a373..3372c68 100644 --- a/CIO/CButton.cpp +++ b/CIO/CButton.cpp @@ -20,16 +20,9 @@ CButton::CButton(void callback(int), int clickedParam, Sint16 x, Sint16 y, Uint1 this->clickedParam = clickedParam; motionEntryParam = -1; motionLeaveParam = -1; - Surf_Button = nullptr; - needSurface = true; needRender = true; } -CButton::~CButton() -{ - SDL_FreeSurface(Surf_Button); -} - void CButton::setButtonPicture(int picture) { this->button_picture = picture; @@ -150,13 +143,10 @@ bool CButton::render() return true; needRender = false; // if we need a new surface - if(needSurface) + if(!Surf_Button) { - SDL_FreeSurface(Surf_Button); - Surf_Button = nullptr; - if((Surf_Button = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0, 0, 0, 0)) == nullptr) + if((Surf_Button = makeSdlSurface(SDL_SWSURFACE, w, h, 32)) == nullptr) return false; - needSurface = false; } // at first completly fill the background (not the fastest way, but simplier) diff --git a/CIO/CButton.h b/CIO/CButton.h index 75a5916..597e5a8 100644 --- a/CIO/CButton.h +++ b/CIO/CButton.h @@ -1,16 +1,15 @@ #ifndef _CBUTTON_H #define _CBUTTON_H -#include "../defines.h" -#include "SDL.h" +#include "defines.h" +#include "SdlSurface.h" class CButton { friend class CDebug; private: - SDL_Surface* Surf_Button; - bool needSurface; + SdlSurface Surf_Button; bool needRender; Sint16 x_; Sint16 y_; @@ -30,10 +29,8 @@ class CButton int motionLeaveParam; public: - // Constructor - Destructor CButton(void callback(int), int clickedParam, Sint16 x = 0, Sint16 y = 0, Uint16 w = 20, Uint16 h = 20, int color = BUTTON_GREY, const char* text = nullptr, int button_picture = -1); - ~CButton(); // Access int getX() { return x_; }; int getY() { return y_; }; @@ -49,7 +46,7 @@ class CButton SDL_Surface* getSurface() { render(); - return Surf_Button; + return Surf_Button.get(); }; void setColor(int color); void setTextColor(int color) diff --git a/CIO/CControlContainer.h b/CIO/CControlContainer.h index c81c160..9fefbd3 100644 --- a/CIO/CControlContainer.h +++ b/CIO/CControlContainer.h @@ -1,7 +1,7 @@ #ifndef _CCONTROLCONTAINER_H #define _CCONTROLCONTAINER_H -#include "../defines.h" +#include "defines.h" #include #include diff --git a/CIO/CFile.cpp b/CIO/CFile.cpp index 179c158..68e9392 100644 --- a/CIO/CFile.cpp +++ b/CIO/CFile.cpp @@ -435,7 +435,7 @@ 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, 0, 0, 0, 0))) + if(!(bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8))) return false; SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, colors.data(), 0, colors.size()); @@ -504,7 +504,7 @@ bool CFile::open_lbm(const std::string& filename) SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0)); bmpArray++; - if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, (bmpArray - 1)->w, (bmpArray - 1)->h, 32, 0, 0, 0, 0))) + if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, (bmpArray - 1)->w, (bmpArray - 1)->h, 32))) { SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0)); CSurface::Draw(bmpArray->surface, (bmpArray - 1)->surface, 0, 0); @@ -1054,7 +1054,7 @@ 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, 0, 0, 0, 0)) == nullptr) + if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == 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)); @@ -1224,7 +1224,7 @@ 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, 0, 0, 0, 0)) == nullptr) + if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == nullptr) return false; SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size()); @@ -1363,7 +1363,7 @@ 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, 0, 0, 0, 0)) == nullptr) + if((shadowArray->surface = makeSdlSurface(SDL_SWSURFACE, shadowArray->w, shadowArray->h, 8)) == nullptr) return false; SDL_SetPalette(shadowArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size()); // SDL_SetAlpha(shadowArray->surface, SDL_SRCALPHA, 128); @@ -1472,7 +1472,7 @@ 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, 0, 0, 0, 0)) == nullptr) + if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == nullptr) return false; SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size()); diff --git a/CIO/CFile.h b/CIO/CFile.h index 8ad3a15..79c60ff 100644 --- a/CIO/CFile.h +++ b/CIO/CFile.h @@ -3,7 +3,7 @@ #ifndef _CFILE_H #define _CFILE_H -#include "../defines.h" +#include "defines.h" #include #include diff --git a/CIO/CFont.cpp b/CIO/CFont.cpp index 91c57e7..b511024 100644 --- a/CIO/CFont.cpp +++ b/CIO/CFont.cpp @@ -3,8 +3,7 @@ #include "../globals.h" #include -CFont::CFont(std::string text, unsigned x, unsigned y, unsigned fontsize, unsigned color) - : Surf_Font(nullptr), x_(x), y_(y), string_(std::move(text)) +CFont::CFont(std::string text, unsigned x, unsigned y, unsigned fontsize, unsigned color) : x_(x), y_(y), string_(std::move(text)) { // only three sizes are available (in pixels) if(fontsize != 9 && fontsize != 11 && fontsize != 14) @@ -14,38 +13,42 @@ CFont::CFont(std::string text, unsigned x, unsigned y, unsigned fontsize, unsign initialColor_ = this->color_ = color; callback = nullptr; clickedParam = 0; - // create surface and write text to it - writeText(); } -CFont::~CFont() +void CFont::setPos(Position pos) { - SDL_FreeSurface(Surf_Font); + if(pos != Position(x_, y_)) + { + x_ = pos.x; + y_ = pos.y; + Surf_Font.reset(); + } } void CFont::setFontsize(unsigned fontsize) { if(fontsize != 9 && fontsize != 11 && fontsize != 14) - this->fontsize_ = 9; - else - this->fontsize_ = fontsize; - writeText(); + fontsize = 9; + if(fontsize != fontsize_) + { + fontsize_ = fontsize; + Surf_Font.reset(); + } } void CFont::setColor(unsigned color) { + if(color != color_) + Surf_Font.reset(); initialColor_ = color_ = color; - writeText(); } void CFont::setText(std::string text) { if(text == string_) return; - SDL_FreeSurface(Surf_Font); - Surf_Font = nullptr; + Surf_Font.reset(); this->string_ = std::move(text); - writeText(); } void CFont::setMouseData(SDL_MouseButtonEvent button) @@ -72,6 +75,13 @@ void CFont::setMouseData(SDL_MouseButtonEvent button) } } +SDL_Surface* CFont::getSurface() +{ + if(!Surf_Font) + writeText(); + return Surf_Font.get(); +} + namespace { unsigned getIndexForChar(uint8_t c) { @@ -201,10 +211,10 @@ unsigned getCharWidth(uint8_t c, unsigned fontsize, unsigned color) } } // namespace -bool CFont::writeText() +void CFont::writeText() { if(string_.empty()) - return true; + return; // data for counting pixels to create the surface unsigned pixel_ctr_w = 0; unsigned pixel_ctr_w_tmp = 0; @@ -244,11 +254,10 @@ bool CFont::writeText() pixel_ctr_w = pixel_ctr_w_tmp; w = pixel_ctr_w; h = pixel_ctr_h; - if(Surf_Font) - SDL_FreeSurface(Surf_Font); - if(!(Surf_Font = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0, 0, 0, 0))) - return false; - SDL_SetColorKey(Surf_Font, SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Font->format, 0, 0, 0)); + Surf_Font = makeSdlSurface(SDL_SWSURFACE, w, h, 32); + if(!Surf_Font) + return; + SDL_SetColorKey(Surf_Font.get(), SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Font->format, 0, 0, 0)); chiffre = string_.begin(); pixel_count_loop = false; continue; @@ -286,7 +295,6 @@ bool CFont::writeText() // go to next chiffre ++chiffre; } - return true; } bool CFont::writeText(SDL_Surface* Surf_Dest, const std::string& string, unsigned x, unsigned y, unsigned fontsize, unsigned color, diff --git a/CIO/CFont.h b/CIO/CFont.h index 7cc493b..b0b6063 100644 --- a/CIO/CFont.h +++ b/CIO/CFont.h @@ -1,9 +1,10 @@ #ifndef _CFONT_H #define _CFONT_H -#include "../defines.h" +#include "defines.h" #include #include +#include #include class CFont @@ -11,7 +12,7 @@ class CFont friend class CDebug; private: - SDL_Surface* Surf_Font; + SdlSurface Surf_Font; Sint16 x_; Sint16 y_; Uint16 w; @@ -22,15 +23,14 @@ class CFont std::function callback; int clickedParam; + void writeText(); + public: - // Constructor - Destructor CFont(std::string text, unsigned x = 0, unsigned y = 0, unsigned fontsize = 9, unsigned color = FONT_YELLOW); - ~CFont(); // Access int getX() { return x_; }; int getY() { return y_; }; - void setX(int x) { this->x_ = x; }; - void setY(int y) { this->y_ = y; }; + void setPos(Position pos); unsigned getW() { return w; }; unsigned getH() { return fontsize_; }; void setFontsize(unsigned fontsize); @@ -48,10 +48,9 @@ class CFont clickedParam = 0; } void setMouseData(SDL_MouseButtonEvent button); - SDL_Surface* getSurface() { return Surf_Font; }; + SDL_Surface* getSurface(); // Methods // fontsize can be 9, 11 or 14 (otherwise it will be set to 9) ---- '\n' is possible - bool writeText(); // this function can be used as CFont::writeText to write text directly to a surface without creating an object static bool writeText(SDL_Surface* Surf_Dest, const std::string& string, unsigned x = 0, unsigned y = 0, unsigned fontsize = 9, unsigned color = FONT_YELLOW, FontAlign align = FontAlign::Left); diff --git a/CIO/CPicture.cpp b/CIO/CPicture.cpp index c26e0ee..5e2ac80 100644 --- a/CIO/CPicture.cpp +++ b/CIO/CPicture.cpp @@ -18,16 +18,9 @@ CPicture::CPicture(void callback(int), int clickedParam, Uint16 x, Uint16 y, int this->clickedParam = clickedParam; motionEntryParam = -1; motionLeaveParam = -1; - Surf_Picture = nullptr; - needSurface = true; needRender = true; } -CPicture::~CPicture() -{ - SDL_FreeSurface(Surf_Picture); -} - void CPicture::setMouseData(const SDL_MouseMotionEvent& motion) { // cursor is on the picture @@ -77,14 +70,12 @@ bool CPicture::render() return true; needRender = false; // if we need a new surface - if(needSurface) + if(!Surf_Picture) { - SDL_FreeSurface(Surf_Picture); - Surf_Picture = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0, 0, 0, 0); + Surf_Picture = makeSdlSurface(SDL_SWSURFACE, w, h, 32); if(!Surf_Picture) return false; - SDL_SetColorKey(Surf_Picture, SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Picture->format, 0, 0, 0)); - needSurface = false; + SDL_SetColorKey(Surf_Picture.get(), SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Picture->format, 0, 0, 0)); } CSurface::Draw(Surf_Picture, global::bmpArray[picture_].surface, 0, 0); diff --git a/CIO/CPicture.h b/CIO/CPicture.h index 6b5215e..f650790 100644 --- a/CIO/CPicture.h +++ b/CIO/CPicture.h @@ -1,15 +1,14 @@ #ifndef _CPICTURE_H #define _CPICTURE_H -#include +#include "SdlSurface.h" class CPicture { friend class CDebug; private: - SDL_Surface* Surf_Picture; - bool needSurface; + SdlSurface Surf_Picture; bool needRender; Uint16 x; Uint16 y; @@ -24,9 +23,7 @@ class CPicture int motionLeaveParam; public: - // Constructor - Destructor CPicture(void callback(int), int clickedParam, Uint16 x = 0, Uint16 y = 0, int picture = -1); - ~CPicture(); // Access int getX() { return x; }; int getY() { return y; }; @@ -40,7 +37,7 @@ class CPicture SDL_Surface* getSurface() { render(); - return Surf_Picture; + return Surf_Picture.get(); }; void setMotionParams(int entry, int leave) { diff --git a/CIO/CSelectBox.cpp b/CIO/CSelectBox.cpp index 363f609..4817ba8 100644 --- a/CIO/CSelectBox.cpp +++ b/CIO/CSelectBox.cpp @@ -15,8 +15,6 @@ CSelectBox::CSelectBox(Sint16 x, Sint16 y, Uint16 w, Uint16 h, int fontsize, int this->text_color = text_color; setColor(bg_color); - Surf_SelectBox = nullptr; - needSurface = true; needRender = true; rendered = false; // button position is relative to the selectbox @@ -25,11 +23,6 @@ CSelectBox::CSelectBox(Sint16 x, Sint16 y, Uint16 w, Uint16 h, int fontsize, int std::make_unique(nullptr, 0, w - 1 - 20, h - 1 - 20, 20, 20, BUTTON_GREY, nullptr, PICTURE_SMALL_ARROW_DOWN); } -CSelectBox::~CSelectBox() -{ - SDL_FreeSurface(Surf_SelectBox); -} - void CSelectBox::setOption(const std::string& string, std::function callback, int param) { // explanation: row_height = row_separator + fontsize @@ -155,7 +148,7 @@ void CSelectBox::setMouseData(SDL_MouseButtonEvent button) { for(auto& entry : Entries) { - entry->setY(entry->getY() + 10); + entry->setPos(Position(entry->getX(), entry->getY() + 10)); } } } @@ -170,7 +163,7 @@ void CSelectBox::setMouseData(SDL_MouseButtonEvent button) { for(auto& entry : Entries) { - entry->setY(entry->getY() - 10); + entry->setPos(Position(entry->getX(), entry->getY() - 10)); } } } @@ -218,13 +211,10 @@ bool CSelectBox::render() return true; needRender = false; // if we need a new surface - if(needSurface) + if(!Surf_SelectBox) { - SDL_FreeSurface(Surf_SelectBox); - Surf_SelectBox = nullptr; - if((Surf_SelectBox = SDL_CreateRGBSurface(SDL_SWSURFACE, w_, h_, 32, 0, 0, 0, 0)) == nullptr) + if((Surf_SelectBox = makeSdlSurface(SDL_SWSURFACE, w_, h_, 32)) == nullptr) return false; - needSurface = false; } // draw the pictures for background and foreground or, if not set, fill with black color @@ -272,7 +262,7 @@ bool CSelectBox::render() Surf_SelectBox->h - pos_y); } } else - SDL_FillRect(Surf_SelectBox, nullptr, SDL_MapRGB(Surf_SelectBox->format, 0, 0, 0)); + SDL_FillRect(Surf_SelectBox.get(), nullptr, SDL_MapRGB(Surf_SelectBox->format, 0, 0, 0)); for(auto& entry : Entries) { diff --git a/CIO/CSelectBox.h b/CIO/CSelectBox.h index 783de80..04daccf 100644 --- a/CIO/CSelectBox.h +++ b/CIO/CSelectBox.h @@ -1,8 +1,8 @@ #ifndef _CSELECTBOX_H #define _CSELECTBOX_H -#include "../defines.h" -#include +#include "defines.h" +#include "SdlSurface.h" #include #include #include @@ -15,9 +15,8 @@ class CSelectBox friend class CDebug; private: - SDL_Surface* Surf_SelectBox; + SdlSurface Surf_SelectBox; std::vector> Entries; - bool needSurface; bool needRender; Sint16 x_; Sint16 y_; @@ -37,7 +36,6 @@ class CSelectBox // Constructor - Destructor CSelectBox(Sint16 x = 0, Sint16 y = 0, Uint16 w = 100, Uint16 h = 100, int fontsize = 14, int text_color = FONT_YELLOW, int bg_color = -1); - ~CSelectBox(); // Access; int getX() { return x_; } int getY() { return y_; } @@ -52,7 +50,7 @@ class CSelectBox SDL_Surface* getSurface() { render(); - return Surf_SelectBox; + return Surf_SelectBox.get(); } void setColor(int color); void setTextColor(int color) diff --git a/CIO/CTextfield.cpp b/CIO/CTextfield.cpp index a71023c..935d87c 100644 --- a/CIO/CTextfield.cpp +++ b/CIO/CTextfield.cpp @@ -21,18 +21,10 @@ CTextfield::CTextfield(Sint16 x, Sint16 y, Uint16 cols, Uint16 rows, int fontsiz // allocate memory for the text: chiffres (cols) + '\n' for each line * rows + blinking chiffre + '\0' text_.resize((this->cols + 1) * this->rows + 2); - Surf_Text = nullptr; - needSurface = true; needRender = true; rendered = false; this->button_style = button_style; - textObj = new CFont("", x, y, fontsize, text_color); -} - -CTextfield::~CTextfield() -{ - SDL_FreeSurface(Surf_Text); - delete textObj; + textObj = std::make_unique("", x, y, fontsize, text_color); } bool CTextfield::hasRendered() @@ -88,6 +80,25 @@ void CTextfield::setColor(int color) needRender = true; } +void CTextfield::setTextColor(int color) +{ + textColor_ = color; + textObj->setColor(color); + needRender = true; +} + +void CTextfield::setX(int x) +{ + this->x_ = x; + textObj->setPos(Position(x_, y_)); +} + +void CTextfield::setY(int y) +{ + this->y_ = y; + textObj->setPos(Position(x_, y_)); +} + void CTextfield::setText(const std::string& text) { char* txtPtr = &this->text_[0]; @@ -257,13 +268,11 @@ bool CTextfield::render() return true; needRender = false; // if we need a new surface - if(needSurface) + if(!Surf_Text) { - SDL_FreeSurface(Surf_Text); - Surf_Text = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0, 0, 0, 0); + Surf_Text = makeSdlSurface(SDL_SWSURFACE, w, h, 32); if(!Surf_Text) return false; - needSurface = false; } // draw the pictures for background and foreground or, if not set, fill with black color @@ -411,7 +420,7 @@ bool CTextfield::render() } } } else - SDL_FillRect(Surf_Text, nullptr, SDL_MapRGB(Surf_Text->format, 0, 0, 0)); + SDL_FillRect(Surf_Text.get(), nullptr, SDL_MapRGB(Surf_Text->format, 0, 0, 0)); char* txtPtr = &text_[0]; @@ -427,10 +436,8 @@ bool CTextfield::render() } // write text - // textObj->setText(text); - delete textObj; - textObj = nullptr; - textObj = new CFont(&text_[0], x_, y_, fontsize_, textColor_); + textObj->setText(text_.data()); + textObj = std::make_unique(&text_[0], x_, y_, fontsize_, textColor_); // delete blinking chiffre (otherwise it could be written between user input chiffres) if(blinking_chiffre && active) diff --git a/CIO/CTextfield.h b/CIO/CTextfield.h index dd29719..f00e6e0 100644 --- a/CIO/CTextfield.h +++ b/CIO/CTextfield.h @@ -1,7 +1,7 @@ #ifndef _CTEXTFIELD_H #define _CTEXTFIELD_H -#include "../defines.h" +#include "defines.h" #include #include @@ -12,9 +12,8 @@ class CTextfield friend class CDebug; private: - SDL_Surface* Surf_Text; - CFont* textObj; - bool needSurface; + SdlSurface Surf_Text; + std::unique_ptr textObj; bool needRender; Sint16 x_; Sint16 y_; @@ -38,7 +37,6 @@ class CTextfield // Constructor - Destructor CTextfield(Sint16 x = 0, Sint16 y = 0, Uint16 cols = 10, Uint16 rows = 1, int fontsize = 14, int text_color = FONT_YELLOW, int bg_color = -1, bool button_style = false); - ~CTextfield(); // Access int getX() { return x_; }; int getY() { return y_; }; @@ -46,8 +44,8 @@ class CTextfield int getH() { return h; }; int getCols() { return cols; }; int getRows() { return rows; }; - void setX(int x) { this->x_ = x; } - void setY(int y) { this->y_ = y; } + void setX(int x); + void setY(int y); void setText(const std::string& text); void setActive() { active = true; } void setInactive() { active = false; } @@ -59,14 +57,10 @@ class CTextfield SDL_Surface* getSurface() { render(); - return Surf_Text; + return Surf_Text.get(); } void setColor(int color); - void setTextColor(int color) - { - textColor_ = color; - needRender = true; - } + void setTextColor(int color); std::string getText() const { return &text_.front(); } }; diff --git a/CMakeLists.txt b/CMakeLists.txt index 97c67f5..3a261d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ ENDIF() add_executable(s25edit ${MAIN_SOURCES} ${CIO_SOURCES} ${icon_RC}) target_link_libraries(s25edit PRIVATE SGE rttrConfig s25Common gamedata endian::static nowide::static PUBLIC Boost::disable_autolinking) +target_include_directories(s25edit PRIVATE include) if(MINGW) target_link_libraries(s25edit PRIVATE -mconsole) diff --git a/CMap.cpp b/CMap.cpp index 1c6b2a3..995eb2a 100644 --- a/CMap.cpp +++ b/CMap.cpp @@ -77,8 +77,8 @@ void CMap::constructMap(const std::string& filename, int width, int height, MapT int border_texture) { map = nullptr; - Surf_Map = nullptr; - Surf_RightMenubar = nullptr; + Surf_Map.reset(); + Surf_RightMenubar.reset(); displayRect.left = 0; displayRect.top = 0; displayRect.setSize(global::s2->GameResolution); @@ -106,7 +106,7 @@ void CMap::constructMap(const std::string& filename, int width, int height, MapT } } - needSurface = true; + Surf_Map.reset(); active = true; VertexX_ = 10; VertexY_ = 10; @@ -213,12 +213,8 @@ void CMap::destructMap() unloadMapPics(); undoBuffer.clear(); redoBuffer.clear(); - // free the map surface - SDL_FreeSurface(Surf_Map); - Surf_Map = nullptr; - // free the surface of the right menubar - SDL_FreeSurface(Surf_RightMenubar); - Surf_RightMenubar = nullptr; + Surf_Map.reset(); + Surf_RightMenubar.reset(); // free vertex array Vertices.clear(); // free map structure memory @@ -1104,19 +1100,18 @@ void CMap::render() if(displayRect.getSize() != global::s2->GameResolution) { displayRect.setSize(global::s2->GameResolution); - needSurface = true; + Surf_Map.reset(); } // if we need a new surface - if(needSurface) + if(!Surf_Map) { - SDL_FreeSurface(Surf_Map); - Surf_Map = SDL_CreateRGBSurface(SDL_SWSURFACE, displayRect.getSize().x, displayRect.getSize().y, BitsPerPixel, 0, 0, 0, 0); - if(Surf_Map == nullptr) + Surf_Map = makeSdlSurface(SDL_SWSURFACE, displayRect.getSize().x, displayRect.getSize().y, BitsPerPixel); + if(!Surf_Map) return; if(BitsPerPixel == 8) - SDL_SetPalette(Surf_Map, SDL_LOGPAL, global::palArray[PAL_xBBM].colors.data(), 0, global::palArray[PAL_xBBM].colors.size()); - needSurface = false; + SDL_SetPalette(Surf_Map.get(), SDL_LOGPAL, global::palArray[PAL_xBBM].colors.data(), 0, + global::palArray[PAL_xBBM].colors.size()); } // else // clear the surface before drawing new (in normal case not needed) @@ -1127,7 +1122,7 @@ void CMap::render() modifyVertex(); if(!map->vertex.empty()) - CSurface::DrawTriangleField(Surf_Map, displayRect, *map); + CSurface::DrawTriangleField(Surf_Map.get(), displayRect, *map); // draw pictures to cursor position int symbol_index, symbol_index2 = -1; @@ -1255,13 +1250,11 @@ void CMap::render() if(!Surf_RightMenubar) { // we permute width and height, cause we want to rotate the menubar 90 degrees - if((Surf_RightMenubar = - SDL_CreateRGBSurface(SDL_SWSURFACE, global::bmpArray[MENUBAR].h, global::bmpArray[MENUBAR].w, 8, 0, 0, 0, 0)) - != nullptr) + if((Surf_RightMenubar = makeSdlSurface(SDL_SWSURFACE, global::bmpArray[MENUBAR].h, global::bmpArray[MENUBAR].w, 8)) != nullptr) { - SDL_SetPalette(Surf_RightMenubar, SDL_LOGPAL, global::palArray[PAL_RESOURCE].colors.data(), 0, + SDL_SetPalette(Surf_RightMenubar.get(), SDL_LOGPAL, global::palArray[PAL_RESOURCE].colors.data(), 0, global::palArray[PAL_RESOURCE].colors.size()); - SDL_SetColorKey(Surf_RightMenubar, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(Surf_RightMenubar->format, 0, 0, 0)); + SDL_SetColorKey(Surf_RightMenubar.get(), SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(Surf_RightMenubar->format, 0, 0, 0)); CSurface::Draw(Surf_RightMenubar, global::bmpArray[MENUBAR].surface, 0, 0, 270); } } diff --git a/CMap.h b/CMap.h index a65f9d6..6f2285f 100644 --- a/CMap.h +++ b/CMap.h @@ -25,12 +25,11 @@ class CMap private: std::string filename_; - SDL_Surface* Surf_Map; - SDL_Surface* Surf_RightMenubar; + SdlSurface Surf_Map; + SdlSurface Surf_RightMenubar; bobMAP* map; DisplayRectangle displayRect; bool active; - bool needSurface; int VertexX_, VertexY_; bool RenderBuildHelp; bool RenderBorders; @@ -111,7 +110,7 @@ class CMap void setBitsPerPixel(int bbp) { BitsPerPixel = bbp; - needSurface = true; + Surf_Map.reset(); } void setMode(int mode) { this->mode = mode; } int getMode() { return mode; } @@ -123,7 +122,7 @@ class CMap SDL_Surface* getSurface() { render(); - return Surf_Map; + return Surf_Map.get(); } DisplayRectangle getDisplayRect() { return displayRect; } void setDisplayRect(const DisplayRectangle& displayRect) { this->displayRect = displayRect; } diff --git a/CSurface.cpp b/CSurface.cpp index 22c3348..9f78a31 100644 --- a/CSurface.cpp +++ b/CSurface.cpp @@ -107,9 +107,9 @@ bool CSurface::Draw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, return true; } -bool CSurface::Draw(SDL_Surface* Surf_Dest, SdlSurface& Surf_Src, int X, int Y, int angle) +bool CSurface::Draw(SdlSurface& Surf_Dest, SdlSurface& Surf_Src, int X, int Y, int angle) { - return Draw(Surf_Dest, Surf_Src.get(), X, Y, angle); + return Draw(Surf_Dest.get(), Surf_Src.get(), X, Y, angle); } bool CSurface::Draw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H) diff --git a/CSurface.h b/CSurface.h index 74f58c9..2686d86 100644 --- a/CSurface.h +++ b/CSurface.h @@ -19,12 +19,17 @@ class CSurface static bool Draw(SdlSurface& Surf_Dest, SDL_Surface* Surf_Src, int X = 0, int Y = 0); // blits from source on destination to position X,Y and rotates (angle --> degrees --> 90, 180, 270) static bool Draw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int angle); - static bool Draw(SDL_Surface* Surf_Dest, SdlSurface& Surf_Src, int X, int Y, int angle); + static bool Draw(SdlSurface& Surf_Dest, SdlSurface& Surf_Src, int X, int Y, int angle); // blits rectangle (X2,Y2,W,H) from source on destination to position X,Y static bool Draw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H); static bool Draw(SDL_Surface* Surf_Dest, SdlSurface& Surf_Src, int X, int Y, int X2, int Y2, int W, int H); + static bool Draw(SdlSurface& Surf_Dest, SdlSurface& Surf_Src, int X, int Y, int X2, int Y2, int W, int H) + { + return Draw(Surf_Dest.get(), Surf_Src.get(), X, Y, X2, Y2, W, H); + } static void DrawPixel_Color(SDL_Surface* screen, int x, int y, Uint32 color); static void DrawPixel_RGB(SDL_Surface* screen, int x, int y, Uint8 R, Uint8 G, Uint8 B); + static void DrawPixel_RGB(SdlSurface& screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) { DrawPixel_RGB(screen.get(), x, y, R, G, B); } static void DrawPixel_RGBA(SDL_Surface* screen, int x, int y, Uint8 R, Uint8 G, Uint8 B, Uint8 A); static Uint32 GetPixel(SDL_Surface* surface, int x, int y); static void DrawTriangleField(SDL_Surface* display, const DisplayRectangle& displayRect, const bobMAP& myMap); diff --git a/SGE/sge_rotation.cpp b/SGE/sge_rotation.cpp index 3ebc3ac..d3f3f14 100644 --- a/SGE/sge_rotation.cpp +++ b/SGE/sge_rotation.cpp @@ -560,9 +560,8 @@ SDL_Surface* sge_transform_surface(SDL_Surface* src, Uint32 bcol, float angle, f Sint16 qx = -xmin; Sint16 qy = -ymin; - SDL_Surface* dest; - dest = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, - src->format->Amask); + SDL_Surface* dest = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, + src->format->Bmask, src->format->Amask); if(!dest) return nullptr; diff --git a/SdlSurface.h b/include/SdlSurface.h similarity index 72% rename from SdlSurface.h rename to include/SdlSurface.h index ab4718b..2ada05d 100644 --- a/SdlSurface.h +++ b/include/SdlSurface.h @@ -10,10 +10,9 @@ struct SdlSurfaceDeleter }; using SdlSurface = std::unique_ptr; -inline auto makeSdlSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask = 0, Uint32 Gmask = 0, Uint32 Bmask = 0, - Uint32 Amask = 0) +inline auto makeSdlSurface(Uint32 flags, int width, int height, int depth, bool withAlpha = false) { - return SdlSurface(SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask)); + return SdlSurface(SDL_CreateRGBSurface(flags, width, height, depth, 0xFF000000, 0xFF0000, 0xFF00, withAlpha ? 0xFF:0)); } #endif // SdlSurface_h__ diff --git a/defines.h b/include/defines.h similarity index 100% rename from defines.h rename to include/defines.h